package ever.generic; import java.io.BufferedReader; import java.io.File; import java.io.FileOutputStream; import java.io.FileReader; import java.io.IOException; import java.io.ObjectOutputStream; import java.util.HashMap; import java.util.Scanner; import org.apache.commons.io.FileUtils; import ever.pipeline.AbstractGlobalInputAnalyzer; import ever.pipeline.Case; import ever.pipeline.Sentence; import ever.pipeline.SundanceConnector; import ever.pipeline.XmlHandler; /** * This Analyzer should be used as one of the first because a lot of other analyzer need the case frames * @author Pol Schumacher, Wirtschaftsinformatik, Institut fuer Informatik, Goethe Universitaet Frankfurt * */ public class CaseLoader extends AbstractGlobalInputAnalyzer { /** * Path to the case frame files produced by sundance */ private final String inputFileCF = "singlePrep.cases"; /** * Path to the case frame files produced by sundance */ private String domain = ""; /** * Path to the clause file produced by sundance */ private final String inputFileSeg = "singlePrep.sundance"; /** * The main method is just used for testing purpose * @param args */ public static void main(String args[]) { } /** * Runs Sundance and reads created Segmenter and case-frame files. Loads * case structure and creates id. * * @param pathToFile * Path to recipe/howto file. */ public Case loadCase(String pathToFile) { Case ret = new Case(); File in = new File(pathToFile); ret.addXmlString(loadStringFromFile(in)); if (in.getAbsolutePath().contains("&")) { String newName = in.getAbsolutePath().replace("'", ""); try { FileUtils.copyFile(in, new File(newName)); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if (in.getAbsolutePath().contains("'")) { String newName = in.getAbsolutePath().replace("'", ""); try { FileUtils.copyFile(in, new File(newName)); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } in.delete(); } XmlHandler.setInputFile(in); XmlHandler.copyPrepTextToSundanceInput(); // calls sundance SundanceConnector.run(); Scanner sc = null; Scanner clauseScanner = null; try { sc = new Scanner(new File(inputFileCF)); sc.useDelimiter("[*]{46}"); // Skip part with MIMEType sc.next(); // Read second output of sundance, the segmentation file clauseScanner = new Scanner(new File(inputFileSeg)); clauseScanner.useDelimiter("Original : "); } catch (IOException ex) { ex.printStackTrace(); } while (sc.hasNext()) { String caseFrame = sc.next(); String clause = clauseScanner.next(); ret.addSentence(new Sentence(caseFrame, clause)); } sc.close(); clauseScanner.close(); return ret; } private Case runSundance(Case c, String d) { // calls sundance SundanceConnector.run(); Scanner sc = null; Scanner clauseScanner = null; try { sc = new Scanner(new File(inputFileCF)); sc.useDelimiter("[*]{46}"); // Skip part with MIMEType sc.next(); // Read second output of sundance, the segmentation file clauseScanner = new Scanner(new File(inputFileSeg)); clauseScanner.useDelimiter("Original : "); } catch (IOException ex) { ex.printStackTrace(); } while (sc.hasNext()&clauseScanner.hasNext()) { String caseFrame = sc.next(); String clause = clauseScanner.next(); c.addSentence(new Sentence(caseFrame, clause)); } if(sc.hasNext()!=clauseScanner.hasNext()) System.out.println(c.getName()); clauseScanner.close(); sc.close(); return c; } private String loadStringFromFile(File input) { String out = ""; try { BufferedReader reader = new BufferedReader(new FileReader(input)); String line = reader.readLine(); while (line != null) { out += line; line = reader.readLine(); } } catch (IOException ex) { ex.printStackTrace(); return null; } return out; } @Override public Case analyze(Case c) { // TODO Auto-generated method stub return null; } @Override public HashMap analyze(HashMap c) { for (Integer key : c.keySet()) { Case local = c.get(key); XmlHandler.copyPrepTextToSundanceInput(local.getXmlString()); local=runSundance(local, domain); c.put(key, local); } try { FileOutputStream fileOut = new FileOutputStream("cases.ser"); ObjectOutputStream out = new ObjectOutputStream(fileOut); out.writeObject(c); out.close(); fileOut.close(); }catch(IOException i) { i.printStackTrace(); } return c; } @Override public void analyze() { // TODO Auto-generated method stub } @Override public boolean isLocal() { // TODO Auto-generated method stub return false; } public void setDomain(String s) { this.domain=s; } }