$search
00001 package instruction.postprocessor; 00002 00003 import instruction.opencyc.OpenCyc; 00004 import instruction.semanticObjects.Instruction; 00005 import instruction.semanticObjects.ObjectX; 00006 import instruction.semanticObjects.Preposition; 00007 import instruction.semanticObjects.Quantifier; 00008 import instruction.semanticObjects.SemanticObject; 00009 import instruction.semanticObjects.Word; 00010 import java.io.IOException; 00011 import java.net.UnknownHostException; 00012 import java.util.List; 00013 00014 import org.opencyc.api.CycApiException; 00015 00016 public class TimeConstraintPostProcessor implements PostProcessor { 00017 00018 public void run( List<Instruction> instructions ) { 00019 00020 for ( int i = 0; i < instructions.size(); i++ ) { 00021 findTimeConstraint( instructions.get( i ), instructions.get( i ), instructions.get( i ) ); 00022 } 00023 } 00024 00025 private void findTimeConstraint( SemanticObject so, Instruction parentInstruction, SemanticObject parentSO ) { 00026 00027 if ( so instanceof Instruction ) { 00028 Instruction inst = (Instruction) so; 00029 00030 List<ObjectX> objects = inst.getObjects(); 00031 for ( int i = 0; i < objects.size(); i++ ) 00032 findTimeConstraint( objects.get( i ), parentInstruction, parentSO ); 00033 00034 List<Preposition> pp = inst.getPrepositions(); 00035 for ( int i = 0; i < pp.size(); i++ ) 00036 findTimeConstraint( pp.get( i ), parentInstruction, parentSO ); 00037 00038 } 00039 00040 else if ( so instanceof ObjectX ) { 00041 ObjectX o = (ObjectX) so; 00042 00043 List<Preposition> pp = o.getPrepositions(); 00044 for ( int i = 0; i < pp.size(); i++ ) 00045 findTimeConstraint( pp.get( i ), parentInstruction, o ); 00046 } 00047 00048 else if ( so instanceof Preposition ) { 00049 Preposition pp = (Preposition) so; 00050 00051 // Explore the Prepositions 00052 for ( int j = 0; j < pp.getPrepositions().size(); j++ ) { 00053 00054 if ( pp.getPrepositions().get( j ).getLabel().equalsIgnoreCase( "for" ) ) { 00055 00056 // System.out.println( "found prep \"for\"" ); 00057 for ( int k = 0; k < pp.getObjects().size(); k++ ) { 00058 00059 Word time = null; 00060 try { 00061 time = getTimeMeasure( pp.getObjects().get( k ) ); 00062 } 00063 catch ( Exception e ) { 00064 System.out.println(e.getMessage()); 00065 } 00066 00067 00068 if ( time != null ) { 00069 00070 // Build the time constraint 00071 Quantifier q = new Quantifier(); 00072 q.setMeasure( time ); 00073 q.setAlternatives( pp.getObjects().get( k ).getQuantifier().getAlternatives() ); 00074 00075 if ( q.getAlternatives().size() == 0 ) 00076 q.getAlternatives().add( new Word( Word.TYPE_NUMBER, "1" ) ); 00077 00078 parentInstruction.setTimeConstraint( q ); 00079 00080 // Remove the preposition identified as time constraint from the 00081 // parent Semantic Object 00082 if ( parentSO instanceof Instruction ) 00083 ( (Instruction) parentSO ).getPrepositions().remove( pp ); 00084 else if ( parentSO instanceof ObjectX ) 00085 ( (ObjectX) parentSO ).getPrepositions().remove( pp ); 00086 } 00087 } 00088 } 00089 } 00090 } 00091 } 00092 00103 private Word getTimeMeasure( ObjectX o ) throws UnknownHostException, CycApiException, IOException { 00104 00105 for ( int i = 0; i < o.getName().size(); i++ ) { 00106 for ( int j = 0; j < o.getName().get( i ).getCycConcepts().size(); j++ ) { 00107 String c = o.getName().get( i ).getCycConcepts().get( j ); 00108 if ( OpenCyc.getInstance().isaInUnivVocMt( c, "UnitOfTime" ) ) 00109 return o.getName().get( i ); 00110 } 00111 } 00112 return null; 00113 } 00114 00115 }