Go to the documentation of this file.00001 package instruction.factory;
00002
00003 import instruction.converter.Instruction2CycLConverter;
00004 import instruction.disambiguator.Disambiguator;
00005 import instruction.exceptions.InstructionException;
00006 import instruction.postprocessor.InstructionPostProcessor;
00007 import instruction.semanticObjects.Instruction;
00008 import instruction.semanticObjects.SemanticObject;
00009 import instruction.semanticObjects.Word;
00010 import instruction.syntaxparser.Parser;
00011 import instruction.syntaxparser.SyntaxTree;
00012 import instruction.transformation.Transformations;
00013 import instruction.wordnet.WordNetRDF2;
00014
00015 import java.io.File;
00016 import java.net.URISyntaxException;
00017 import java.util.ArrayList;
00018 import java.util.Iterator;
00019 import java.util.List;
00020
00021 public class InstructionFactory {
00022
00023 Parser parser = null;
00024 Disambiguator disambiguator = new Disambiguator();
00025
00026 public InstructionFactory() {
00027
00028 }
00029
00030 public Disambiguator getDisambiguator() {
00031
00032 return disambiguator;
00033 }
00034
00040 public InstructionFactory(Parser p) {
00041
00042 parser = p;
00043 }
00044
00045 public void makeHowTo(String path) throws URISyntaxException {
00046
00047 Parser parser = new Parser();
00048 InstructionFactory factory = new InstructionFactory(parser);
00049 InstructionPostProcessor postProc = new InstructionPostProcessor();
00050
00051 File howtoFile = new File(path);
00052 if (howtoFile.exists()) {
00053
00054 parser.setFileToParse(howtoFile.getPath());
00055
00056 ArrayList<Instruction> howto = new ArrayList<Instruction>();
00057
00058 for (Iterator<String> iter = parser.iterator(); iter.hasNext();) {
00059 String sentence = iter.next();
00060
00061
00062 for (int k = 0; k <= Parser.PRE_PROCESSOR_LOWER_CASE; k += Parser.PRE_PROCESSOR_LOWER_CASE) {
00063 parser.usePreProcessor(Parser.PRE_PROCESSOR_QUOTATION_MARKS
00064 | Parser.PRE_PROCESSOR_USE_CIPHERS_ONLY | k);
00065
00066 SyntaxTree tree = parser.parse(sentence);
00067
00068 try {
00069 List<Instruction> instructions = factory
00070 .makeInstructions(tree);
00071
00072 howto.addAll(instructions);
00073
00074 if (!instructions.isEmpty())
00075 break;
00076 } catch (Exception e) {
00077 e.printStackTrace();
00078 continue;
00079 }
00080 }
00081 }
00082
00083 for (int i = 0; i < howto.size(); i++)
00084 System.out.println(howto.get(i));
00085
00086 try {
00087 postProc.run(howto);
00088 } catch (Exception e2) {
00089 e2.printStackTrace();
00090 }
00091
00092 for (int i = 0; i < howto.size(); i++)
00093 System.out.println(howto.get(i));
00094
00095 try {
00096 disambiguator.disambiguateInstructions(howto);
00097 } catch (Exception e1) {
00098 e1.printStackTrace();
00099 }
00100
00101 for (int i = 0; i < howto.size(); i++)
00102 System.out.println(howto.get(i));
00103
00104 String title = "Then " + howtoFile.getName().replaceAll("_", " ");
00105 SyntaxTree t = parser.parse(title);
00106 t.printTree();
00107 try {
00108 List<Instruction> titles = factory.makeInstructions(t);
00109
00110 postProc.run(titles);
00111 disambiguator.disambiguateInstructions(titles);
00112
00113 if (titles.size() == 0 || titles.size() > 2)
00114 throw new InstructionException(
00115 "Title of HowTo could not be resolved.");
00116 System.out.println(titles.get(0));
00117 Instruction2CycLConverter converter = new Instruction2CycLConverter();
00118 converter.convertHowTo(howto, titles.get(0));
00119 } catch (Exception e) {
00120 System.out.println(e.getMessage());
00121 e.printStackTrace();
00122 }
00123
00124 } else
00125 System.out.println("File " + path + " not found.");
00126
00127 }
00128
00129 public List<Instruction> makeInstructions(SyntaxTree tree) throws Exception {
00130
00131 List<SemanticObject> semObjects = transformTree(tree);
00132 List<Instruction> inst = new ArrayList<Instruction>();
00133 for (int i = 0; i < semObjects.size(); i++) {
00134 if (semObjects.get(i) instanceof Instruction) {
00135 inst.add((Instruction) semObjects.get(i));
00136 }
00137 }
00138 return inst;
00139 }
00140
00141 private List<SemanticObject> transformTree(SyntaxTree tree)
00142 throws Exception {
00143
00144 List<SemanticObject> objList = new ArrayList<SemanticObject>();
00145
00146 if (tree.getChildren().isEmpty()) {
00147 Word word = mapSyntaxTreeNodeToWord(tree);
00148 word.setSynSetIDs(WordNetRDF2.getSynsets(word.getLabel(), WordNetRDF2
00149 .convertPOS(word.getType())));
00150 objList.add(word);
00151 } else {
00152 for (int i = 0; i < tree.getChildren().size(); i++) {
00153 List<SemanticObject> obj = transformTree(tree.getChildren()
00154 .get(i));
00155 for (int j = 0; j < obj.size(); j++)
00156 objList.add(obj.get(j));
00157 }
00158 }
00159
00160 return Transformations.get(tree.getElement().getType())
00161 .doModelTransformation(objList);
00162 }
00163
00164 private Word mapSyntaxTreeNodeToWord(SyntaxTree tree) {
00165
00166 if (tree == null || tree.getElement() == null
00167 || tree.getElement().getName().isEmpty())
00168 return null;
00169
00170 Word word = new Word();
00171
00172 word.setLabel(tree.getElement().getName());
00173
00174 String type = tree.getElement().getType();
00175
00176 if (type.matches("NN.*"))
00177 word.setType(Word.TYPE_NOUN);
00178
00179 else if (type.matches("VBG"))
00180 word.setType(Word.TYPE_GERUND);
00181
00182 else if (type.matches("VBN"))
00183 word.setType(Word.TYPE_PAST_PARTICIPLE);
00184
00185 else if (type.matches("VB.*"))
00186 word.setType(Word.TYPE_VERB_INFINITIVE);
00187
00188 else if (type.equals("RB"))
00189 word.setType(Word.TYPE_ADV);
00190
00191 else if (type.matches("JJ.*"))
00192 word.setType(Word.TYPE_ADJ);
00193
00194 else if (type.matches("RP"))
00195 word.setType(Word.TYPE_PARTICLE);
00196
00197 else if (type.matches("CD"))
00198 word.setType(Word.TYPE_NUMBER);
00199
00200 else if (type.matches(".*PRP.*|WDT"))
00201 word.setType(Word.TYPE_PRONOUN);
00202
00203 else if (type.matches("IN|TO|WRB"))
00204 word.setType(Word.TYPE_PREPOSITION);
00205
00206 else if (type.matches("CC"))
00207 word.setType(Word.TYPE_CONJ);
00208
00209 else if (type.matches("DT"))
00210 word.setType(Word.TYPE_ARTICLE);
00211
00212 else if (type.equals(",") || type.equals("``") || type.equals("''")
00213 || type.equals("."))
00214 word.setType(Word.TYPE_PUNCTUATION);
00215
00216 return word;
00217 }
00218
00219 }