Go to the documentation of this file.00001 package instruction.importer;
00002
00003 import instruction.converter.Instruction2CycLConverter;
00004 import instruction.disambiguator.Disambiguator;
00005 import instruction.exceptions.InstructionException;
00006 import instruction.exporter.PlanExporter;
00007 import instruction.factory.InstructionFactory;
00008 import instruction.opencyc.OpenCyc20;
00009 import instruction.postprocessor.InstructionPostProcessor;
00010 import instruction.semanticObjects.Instruction;
00011 import instruction.syntaxparser.Parser;
00012 import instruction.syntaxparser.SyntaxTree;
00013 import instruction.wordnet.WordNetRDF2;
00014 import instruction.wrapper.IHowtoWebsiteWrapper;
00015
00016 import java.io.IOException;
00017 import java.net.UnknownHostException;
00018 import java.util.ArrayList;
00019 import java.util.Iterator;
00020 import java.util.List;
00021
00022 import org.opencyc.api.CycApiException;
00023
00024 public class PlanImporter {
00025
00026
00027 Parser parser = null;
00028 Disambiguator disambiguator = null;
00029 InstructionFactory factory = null;
00030 InstructionPostProcessor postProc = null;
00031 PlanExporter exporter = null;
00032 Instruction2CycLConverter converter = null;
00033
00034
00035 private List<SyntaxTree> trees = null;
00036 private List<Instruction> instructionList = null;
00037 private Instruction instTitle = null;
00038 private SyntaxTree parsedTitle = null;
00039 private IHowtoWebsiteWrapper wrapper = null;
00040 private String rplPlan = null;
00041 private String planName = null;
00042 private List<String> nlInst = null;
00043
00044
00045 private List<InstructionProgressListener> progressListeners = new ArrayList<InstructionProgressListener>();
00046
00054 public void initialize() throws Exception {
00055
00056 notifyProgressListeners( 0, "Initializing Parser..." );
00057 if ( parser == null )
00058 parser = new Parser();
00059 notifyProgressListeners( 20, "Done.\n" );
00060
00061 notifyProgressListeners( 20, "Initializing Disambiguator..." );
00062 if ( disambiguator == null )
00063 disambiguator = new Disambiguator();
00064 notifyProgressListeners( 40, "Done.\n" );
00065
00066 notifyProgressListeners( 40, "Initializing InstructionFactory..." );
00067 if ( factory == null )
00068 factory = new InstructionFactory( parser );
00069 notifyProgressListeners( 60, "Done.\n" );
00070
00071 notifyProgressListeners( 60, "Initializing Post-Processor..." );
00072 if ( postProc == null )
00073 postProc = new InstructionPostProcessor();
00074 notifyProgressListeners( 80, "Done.\n" );
00075
00076 notifyProgressListeners( -1, "Initializing Cyc Ontology..." );
00077 OpenCyc20.getInstance();
00078 notifyProgressListeners( 85, "Done.\n" );
00079
00080 notifyProgressListeners( -1, "Initializing WordNet 2.0..." );
00081 new WordNetRDF2();
00082 notifyProgressListeners( 85, "Done.\n" );
00083
00084 notifyProgressListeners( -1, "Initializing Instruction2CycL-Converter..." );
00085 if ( converter == null )
00086 converter = new Instruction2CycLConverter();
00087 notifyProgressListeners( 90, "Done.\n" );
00088
00089 notifyProgressListeners( -1, "Initializing RPL Plan Exporter..." );
00090 if ( exporter == null )
00091 exporter = PlanExporter.getInstance();
00092 notifyProgressListeners( 100, "Done.\n" );
00093 }
00094
00095 public void setWrapper( IHowtoWebsiteWrapper wrp ) {
00096
00097 wrapper = wrp;
00098 }
00099
00107 public void addProgressListener( InstructionProgressListener listener ) {
00108
00109 progressListeners.add( listener );
00110 }
00111
00112
00119 public void removeProgressListener( InstructionProgressListener listener ) {
00120
00121 progressListeners.remove( listener );
00122 }
00123
00130 private void notifyProgressListeners( int percent, String msg ) {
00131
00132 for ( Iterator<InstructionProgressListener> i = progressListeners.iterator(); i.hasNext(); ) {
00133 i.next().progressNotification( percent, msg );
00134 }
00135 }
00136
00142 public Disambiguator getDisambiguator() {
00143
00144 return disambiguator;
00145 }
00146
00152 public void importHowto( IHowtoWebsiteWrapper wrapper ) {
00153
00154 String howtoTitle = wrapper.getHowtoTitle();
00155 List<String> nlInst = wrapper.getInstructions();
00156
00157 ArrayList<Instruction> howto = new ArrayList<Instruction>();
00158
00159 for ( Iterator<String> iter = nlInst.iterator(); iter.hasNext(); ) {
00160 String sentence = iter.next();
00161
00162 for ( int k = 0; k <= Parser.PRE_PROCESSOR_LOWER_CASE; k += Parser.PRE_PROCESSOR_LOWER_CASE ) {
00163 parser.usePreProcessor( Parser.PRE_PROCESSOR_QUOTATION_MARKS | Parser.PRE_PROCESSOR_USE_CIPHERS_ONLY
00164 | k );
00165
00166 SyntaxTree tree = parser.parse( sentence );
00167 trees.add( tree );
00168
00169 try {
00170 List<Instruction> instructions = factory.makeInstructions( tree );
00171
00172 howto.addAll( instructions );
00173
00174 for(Instruction instr : instructions) {
00175 instr.setNLSentence(sentence);
00176 }
00177
00178 if ( ! instructions.isEmpty() )
00179 break;
00180 }
00181 catch ( Exception e ) {
00182 e.printStackTrace();
00183 continue;
00184 }
00185 }
00186 }
00187 notifyProgressListeners( 30, "Parsed." );
00188 notifyProgressListeners( 50, "Instructions recognized." );
00189
00190 for ( int i = 0; i < howto.size(); i++ )
00191 System.out.println( howto.get( i ) );
00192
00193 try {
00194 postProc.run( howto );
00195 }
00196 catch ( Exception e2 ) {
00197 e2.printStackTrace();
00198 }
00199
00200 for ( int i = 0; i < howto.size(); i++ )
00201 System.out.println( howto.get( i ) );
00202
00203 try {
00204 disambiguator.disambiguateInstructions( howto );
00205 instructionList = howto;
00206 }
00207 catch ( Exception e1 ) {
00208 e1.printStackTrace();
00209 }
00210 notifyProgressListeners( 70, "Instructions disambiguated." );
00211
00212 for ( int i = 0; i < howto.size(); i++ )
00213 System.out.println( howto.get( i ) );
00214
00215
00216 String title = "Then " + howtoTitle;
00217 SyntaxTree t = parser.parse( title );
00218 t.printTree();
00219 try {
00220 List<Instruction> titles = factory.makeInstructions( t );
00221
00222 postProc.run( titles );
00223 disambiguator.disambiguateInstructions( titles );
00224
00225 if ( titles.size() == 0 || titles.size() > 2 )
00226 throw new InstructionException( "Title of HowTo could not be resolved." );
00227 System.out.println( titles.get( 0 ) );
00228 converter.convertHowTo( howto, titles.get( 0 ) );
00229 notifyProgressListeners( 90, "Title resolved." );
00230 }
00231 catch ( Exception e ) {
00232 System.out.println( e.getMessage() );
00233 e.printStackTrace();
00234 }
00235
00236 }
00237
00238 public void parseInstructions() throws InstructionException {
00239
00240 if ( wrapper == null )
00241 throw new InstructionException( "Howto undefined." );
00242
00243 String howtoTitle = wrapper.getHowtoTitle();
00244 nlInst = wrapper.getInstructions();
00245
00246 trees = new ArrayList<SyntaxTree>();
00247
00248 parser.usePreProcessor( Parser.PRE_PROCESSOR_QUOTATION_MARKS | Parser.PRE_PROCESSOR_USE_CIPHERS_ONLY );
00249
00250 notifyProgressListeners( 0, "Parsing Instructions...\n" );
00251
00252
00253 int counter = 1;
00254 for ( Iterator<String> iter = nlInst.iterator(); iter.hasNext(); ) {
00255 notifyProgressListeners( (int) Math.round(100 * (double) counter / (double) nlInst.size() ), String.format(
00256 "%d of %d...", counter, nlInst.size() ) );
00257 String sentence = iter.next();
00258 SyntaxTree tree = parser.parse( sentence );
00259 trees.add( tree );
00260 notifyProgressListeners( -1, "Done.\n" );
00261 counter++;
00262 }
00263
00264 notifyProgressListeners( -1, "Parsing title of Howto..." );
00265 parsedTitle = parser.parse( "Then " + howtoTitle );
00266 notifyProgressListeners( 100, "Done.\n" );
00267 }
00268
00269 public void recognizeAndDisambiguateInstructions() throws Exception {
00270
00271 if ( disambiguator == null || postProc == null )
00272 throw new InstructionException( "PlanImporter is not initialized." );
00273
00274 if ( trees == null || trees.isEmpty() )
00275 throw new InstructionException( "There are no parsed instructions" );
00276
00277 instructionList = new ArrayList<Instruction>();
00278
00279 notifyProgressListeners( 0, "Recognizing Instructions..." );
00280 int sentNum = 0;
00281 for ( Iterator<SyntaxTree> i = trees.iterator(); i.hasNext(); ) {
00282
00283 List<Instruction> instructions = factory.makeInstructions( i.next() );
00284
00285
00286 instructionList.addAll( instructions );
00287 for (Instruction in: instructions) {
00288 System.out.println(in);
00289 in.setNLSentence(nlInst.get(sentNum));
00290 }
00291 sentNum++;
00292 }
00293 notifyProgressListeners( 40, "Done.\n" );
00294
00295 try {
00296 notifyProgressListeners( 50, "Running Post-Processor..." );
00297 postProc.run( instructionList );
00298
00299 notifyProgressListeners( 70, "Disambiguating Instructions..." );
00300 disambiguator.disambiguateInstructions( instructionList );
00301 notifyProgressListeners( 100, "Done.\n" );
00302 for (Iterator i = instructionList.iterator(); i.hasNext(); )
00303 System.out.println(i.next());
00304 }
00305
00306 catch ( Exception e2 ) {
00307 e2.printStackTrace();
00308 }
00309 }
00310
00311 public void convert2CycAssertions() throws CycApiException, UnknownHostException, IOException,
00312 InstructionException {
00313
00314 if ( converter == null )
00315 throw new InstructionException( "PlanImporter not initialized." );
00316
00317 if ( instructionList == null )
00318 throw new InstructionException( "No instructions found to convert." );
00319
00320 notifyProgressListeners( 50, "Importing into Cyc Knowledge Base..." );
00321 converter.convertHowTo( instructionList, instTitle );
00322 notifyProgressListeners( 100, "Done.\n" );
00323
00324 planName = converter.getPlanName();
00325 }
00326
00327 public void generateRPLPlan() throws InstructionException {
00328
00329 if ( exporter == null )
00330 throw new InstructionException( "PlanImporter not initialized." );
00331
00332 notifyProgressListeners( 50, "Generating RPL Plan..." );
00333 rplPlan = exporter.exportPlanToRPL( planName );
00334 notifyProgressListeners( 100, "Done.\n" );
00335 }
00336
00337 public List<SyntaxTree> getSyntaxTrees() {
00338
00339 return trees;
00340 }
00341
00342 public List<Instruction> getInstructions() {
00343
00344 return instructionList;
00345 }
00346
00347 public SyntaxTree getParsedTitle() {
00348
00349 return parsedTitle;
00350 }
00351
00352 public Instruction getTitle() {
00353
00354 return instTitle;
00355 }
00356
00357 public List<String> getAssertions() {
00358
00359 if (converter == null)
00360 return new ArrayList<String>();
00361
00362 return converter.getAssertions();
00363 }
00364
00365 public String getRPLPlan() {
00366
00367 return rplPlan;
00368 }
00369
00370 public IHowtoWebsiteWrapper getWrapper() {
00371
00372 return wrapper;
00373 }
00374
00375 public void setAddMappingListener(AddCycMappingListener listener) {
00376 postProc.setAddMappingListener(listener);
00377 }
00378 }