$search
00001 package instruction.disambiguator; 00002 00003 import java.io.IOException; 00004 import java.net.UnknownHostException; 00005 import java.util.ArrayList; 00006 import java.util.List; 00007 import org.opencyc.api.CycApiException; 00008 import instruction.exceptions.AmbiguityException; 00009 import instruction.exceptions.InstructionException; 00010 import instruction.opencyc.OpenCyc; 00011 import instruction.semanticObjects.Action; 00012 import instruction.semanticObjects.Instruction; 00013 import instruction.semanticObjects.ObjectX; 00014 import instruction.semanticObjects.Preposition; 00015 import instruction.semanticObjects.Quantifier; 00016 import instruction.semanticObjects.SemanticObject; 00017 import instruction.semanticObjects.Word; 00018 00019 public class Disambiguator { 00020 00021 public static int MODE_APPLY = 0; 00022 public static int MODE_TRAIN = 1; 00023 00024 private DisambiguatorKB kb; 00025 00026 private int runMode; 00027 00028 private IDisambiguationOracle oracle = null; 00029 00030 public Disambiguator () { 00031 00032 kb = new DisambiguatorKB(); 00033 } 00034 00035 public void setDisambiguationOracle(IDisambiguationOracle oracle) { 00036 this.oracle = oracle; 00037 } 00038 00039 public IDisambiguationOracle getDisambiguationOracle() { 00040 return oracle; 00041 } 00042 00043 public void setRunMode( int mode ) { 00044 00045 runMode = mode; 00046 if ( runMode == MODE_APPLY ) 00047 System.out.println( "Run Mode set to: APPLY-MODE" ); 00048 00049 else if ( runMode == MODE_TRAIN ) 00050 System.out.println( "Run Mode set to: TRAINING-MODE" ); 00051 } 00052 00053 public void save( String fileName ) throws IOException { 00054 00055 kb.saveAs( fileName ); 00056 } 00057 00058 public void load( String fileName ) throws Exception { 00059 00060 kb.load( fileName ); 00061 System.out.println( "Disambiguator KB snapshot loaded" ); 00062 } 00063 00064 public void disambiguateInstructions( List<Instruction> instructions ) throws AmbiguityException, 00065 InstructionException, UnknownHostException, CycApiException, IOException { 00066 00067 for ( int i = 0; i < instructions.size(); i++ ) { 00068 ArrayList<String> prepositions = new ArrayList<String>(); 00069 for ( int j = 0; j < instructions.get( i ).getPrepositions().size(); j++ ) { 00070 List<Preposition> pp = instructions.get( i ).getPrepositions(); 00071 for ( int k = 0; k < pp.get( j ).getPrepositions().size(); k++ ) { 00072 if ( pp.get( j ).getPrepositions().get( k ).getType() == Word.TYPE_PREPOSITION ) { 00073 prepositions.add( pp.get( j ).getPrepositions().get( k ).getLabel() ); 00074 if ( runMode == MODE_TRAIN ) 00075 kb.tellPreposition( pp.get( j ).getPrepositions().get( k ).getLabel().toLowerCase() ); 00076 } 00077 } 00078 } 00079 disambiguateInstruction( instructions.get( i ), instructions.get( i ).getAction().getAction() 00080 .getLabel(), prepositions, null ); 00081 } 00082 } 00083 00084 public void disambiguateInstruction( SemanticObject so, String action, List<String> prepositions, Instruction inst ) 00085 throws UnknownHostException, IOException, CycApiException, AmbiguityException, InstructionException { 00086 00087 // ================================================================== 00088 // Action 00089 // ================================================================== 00090 if ( so instanceof Action ) 00091 disambiguateAction( (Action) so, prepositions, inst ); 00092 00093 // ================================================================== 00094 // Instruction 00095 // ================================================================== 00096 else if ( so instanceof Instruction ) { 00097 Instruction i = (Instruction) so; 00098 00099 // Action 00100 disambiguateInstruction( i.getAction(), action, prepositions, i ); 00101 action = i.getAction().getAction().getCycConcepts().get( 0 ); 00102 00103 // Objects 00104 List<ObjectX> objects = i.getObjects(); 00105 for ( int j = 0; j < objects.size(); j++ ) 00106 disambiguateInstruction( objects.get( j ), action, prepositions, i ); 00107 00108 // Prepositions 00109 List<Preposition> pp = i.getPrepositions(); 00110 for ( int j = 0; j < pp.size(); j++ ) 00111 disambiguateInstruction( pp.get( j ), action, prepositions, i ); 00112 00113 // Time constraint 00114 Quantifier q = i.getTimeConstraint(); 00115 if ( q != null ) { 00116 for ( int j = 0; j < q.getAlternatives().size(); j++ ) { 00117 00118 } 00119 if ( ! q.getMeasure().getLabel().isEmpty() ) { 00120 Word m = q.getMeasure(); 00121 for ( int j = 0; j < m.getCycConcepts().size(); j++ ) { 00122 if ( OpenCyc.getInstance().isaInUnivVocMt( m.getCycConcepts().get( j ), "UnitOfTime" ) ) { 00123 String unitOfTime = m.getCycConcepts().get( j ); 00124 m.getCycConcepts().clear(); 00125 m.getCycConcepts().add( unitOfTime ); 00126 break; 00127 } 00128 } 00129 // Remove time constraint if disambiguation failed 00130 if ( m.getCycConcepts().size() == 0 || m.getCycConcepts().size() > 1 ) 00131 i.setTimeConstraint( null ); 00132 } 00133 } 00134 } 00135 00136 // ================================================================== 00137 // Object 00138 // ================================================================== 00139 else if ( so instanceof ObjectX ) { 00140 ObjectX o = (ObjectX) so; 00141 00142 try { 00143 disambiguateObject( o, action, inst ); 00144 } 00145 catch ( Exception e ) { 00146 System.out.println( "**********\n" + e.getMessage() ); 00147 } 00148 00149 List<Preposition> pp = o.getPrepositions(); 00150 for ( int j = 0; j < pp.size(); j++ ) 00151 disambiguateInstruction( pp.get( j ), action, prepositions, inst ); 00152 } 00153 00154 // ================================================================== 00155 // Preposition 00156 // ================================================================== 00157 else if ( so instanceof Preposition ) { 00158 Preposition pp = (Preposition) so; 00159 00160 List<ObjectX> o = pp.getObjects(); 00161 for ( int j = 0; j < o.size(); j++ ) 00162 disambiguateInstruction( o.get( j ), action, prepositions, inst ); 00163 } 00164 } 00165 00177 public void disambiguateAction( Action action, List<String> prepositions, Instruction inst ) throws AmbiguityException, 00178 InstructionException, UnknownHostException, CycApiException, IOException { 00179 00180 ArrayList<String> concepts = action.getAction().getCycConcepts(); 00181 00182 if ( runMode == MODE_APPLY ) { 00183 00184 List<String> knownActions = kb.askActions( prepositions ); 00185 00186 for ( int i = 0; i < knownActions.size(); i++ ) { 00187 int idx = concepts.indexOf( knownActions.get( i ) ); 00188 if ( idx < 0 ) 00189 continue; 00190 else { 00191 concepts.clear(); 00192 concepts.add( knownActions.get( i ) ); 00193 break; 00194 } 00195 } 00196 00197 String c = concepts.get( 0 ); 00198 concepts.clear(); 00199 concepts.add( c ); 00200 } 00201 else if ( runMode == MODE_TRAIN ) { 00202 00203 if (oracle == null) 00204 throw new AmbiguityException("No Disambiguation Oracle defined"); 00205 00206 String meaning = oracle.retrieveMeaningOfWord(action.getAction().getLabel(), concepts, inst); 00207 00208 if (meaning == null || ! concepts.contains(meaning)) 00209 meaning = concepts.get(0); 00210 00211 kb.tellAction( meaning, prepositions ); 00212 00213 } 00214 } 00215 00216 public void disambiguateObject( ObjectX o, String action, Instruction inst) throws InstructionException, AmbiguityException, 00217 UnknownHostException, CycApiException, IOException { 00218 00219 for ( int k = 0; k < o.getName().size(); k++ ) { 00220 00221 List<String> concepts = o.getName().get( k ).getCycConcepts(); 00222 00223 if ( runMode == MODE_APPLY ) { 00224 List<String> knownObjects = kb.askObjects( action ); 00225 00226 for ( int i = 0; i < knownObjects.size(); i++ ) { 00227 int idx = concepts.indexOf( knownObjects.get( i ) ); 00228 if ( idx < 0 ) 00229 continue; 00230 else { 00231 concepts.clear(); 00232 concepts.add( knownObjects.get( i ) ); 00233 break; 00234 } 00235 } 00236 00237 String c = concepts.get( 0 ); 00238 concepts.clear(); 00239 concepts.add( c ); 00240 } 00241 else if ( runMode == MODE_TRAIN ) { 00242 00243 if (oracle == null) 00244 throw new AmbiguityException("No Disambiguation Oracle defined"); 00245 00246 String meaning = oracle.retrieveMeaningOfWord(o.getName().get(k).getLabel(), concepts, inst); 00247 00248 if (meaning == null || ! concepts.contains(meaning)) 00249 meaning = concepts.get(0); 00250 00251 kb.tellObject( meaning, action ); 00252 00253 } 00254 00255 } 00256 } 00257 }