$search
00001 package instruction.postprocessor; 00002 00003 import instruction.configuration.ConfigurationManager; 00004 import instruction.exceptions.CycMappingException; 00005 import instruction.exceptions.WordNetException; 00006 import instruction.importer.AddCycMappingListener; 00007 import instruction.opencyc.OpenCyc; 00008 import instruction.opencyc.OpenCyc20; 00009 import instruction.semanticObjects.Action; 00010 import instruction.semanticObjects.Instruction; 00011 import instruction.semanticObjects.ObjectX; 00012 import instruction.semanticObjects.Preposition; 00013 import instruction.semanticObjects.Quantifier; 00014 import instruction.semanticObjects.SemanticObject; 00015 import instruction.semanticObjects.Word; 00016 import instruction.wordnet.WordNetRDF2; 00017 00018 import java.util.ArrayList; 00019 import java.util.Iterator; 00020 import java.util.List; 00021 00022 public class MeaningsPostProcessor implements PostProcessor { 00023 00024 AddCycMappingListener listener; 00025 00026 public void run( List<Instruction> instructions ) { 00027 00028 for ( int i = 0; i < instructions.size(); i++ ) { 00029 try { 00030 findMeanings( instructions.get( i ) ); 00031 } 00032 catch ( Exception e ) { 00033 //e.printStackTrace(); 00034 instructions.remove( i ); 00035 i -= 1; 00036 continue; 00037 } 00038 } 00039 } 00040 00050 public void getCycConcepts( Word w ) throws Exception { 00051 00052 ArrayList<String> synsetIDs = w.getSynSetIDs(); 00053 00054 w.getCycConcepts().clear(); 00055 00056 if ( synsetIDs.size() == 0 ) { 00057 // System.out.println( "Word " + w.getLabel() + " is unknown in WordNet" ); 00058 throw new WordNetException( w ); 00059 } 00060 00061 for ( Iterator<String> i = synsetIDs.iterator(); i.hasNext(); ) { 00062 String synset = i.next(); 00063 00064 ArrayList<String> concepts = OpenCyc.getInstance().getCycConceptFromWordNetID( synset ); 00065 for ( Iterator<String> j = concepts.iterator(); j.hasNext(); ) { 00066 String c = j.next(); 00067 if ( ! w.getCycConcepts().contains( c ) ) 00068 w.getCycConcepts().add( c ); 00069 } 00070 } 00071 00072 if ( w.getCycConcepts().size() == 0 ) { 00073 if(listener != null) { 00074 String[] mapping = listener.getCycMappingForWord(w); 00075 if (mapping != null) { 00076 OpenCyc20.getInstance().addMapping(mapping[0], mapping[1]); 00077 List<String> old = ConfigurationManager.getMappings().get(mapping[1]); 00078 if(old == null) 00079 old = new ArrayList<String>(); 00080 00081 old.add(mapping[1]); 00082 w.getCycConcepts().add(mapping[1]); 00083 00084 ConfigurationManager.getMappings().put(mapping[0], old); 00085 00086 ConfigurationManager.saveSettings(); 00087 } 00088 } 00089 else 00090 System.out.println( "Missing Cyc-Mapping for " + w.getLabel() ); 00091 00092 throw new CycMappingException(w); 00093 } 00094 } 00095 00103 public void findMeanings( SemanticObject so ) throws Exception { 00104 00105 if ( so == null ) 00106 return; 00107 00108 // ================================================================== 00109 // Action 00110 // ================================================================== 00111 if ( so instanceof Action ) { 00112 Word a = ( (Action) so ).getAction(); 00113 a.setSynSetIDs( WordNetRDF2.getSynsets( a.getLabel(), WordNetRDF2.convertPOS(a.getType()) ) ); 00114 getCycConcepts( a ); 00115 00116 for ( int i = 0; i < ( (Action) so ).getAdverb().size(); i++ ) { 00117 try { 00118 Word adv = ( (Action) so ).getAdverb().get( i ); 00119 adv.setSynSetIDs( WordNetRDF2.getSynsets( adv.getLabel(), WordNetRDF2.convertPOS(adv.getType()) ) ); 00120 getCycConcepts( adv ); 00121 } 00122 catch ( Exception e ) { 00123 } 00124 } 00125 } 00126 00127 // ================================================================== 00128 // Instruction 00129 // ================================================================== 00130 else if ( so instanceof Instruction ) { 00131 Instruction i = (Instruction) so; 00132 00133 // Action 00134 findMeanings( i.getAction() ); 00135 00136 // Objects 00137 List<ObjectX> objects = i.getObjects(); 00138 for ( int j = 0; j < objects.size(); j++ ) { 00139 try { 00140 findMeanings( objects.get( j ) ); 00141 } 00142 catch ( Exception e ) { 00143 objects.remove( j ); 00144 j -= 1; 00145 continue; 00146 } 00147 } 00148 00149 // Prepositions 00150 List<Preposition> pp = i.getPrepositions(); 00151 for ( int j = 0; j < pp.size(); j++ ) { 00152 try { 00153 findMeanings( pp.get( j ) ); 00154 } 00155 catch ( Exception e ) { 00156 pp.remove( j ); 00157 j -= 1; 00158 continue; 00159 } 00160 if ( pp.get( j ).getObjects().size() == 0 ) { 00161 pp.remove( j ); 00162 j -= 1; 00163 continue; 00164 } 00165 00166 } 00167 } 00168 00169 // ================================================================== 00170 // Object 00171 // ================================================================== 00172 else if ( so instanceof ObjectX ) { 00173 ObjectX o = (ObjectX) so; 00174 00175 // Object Name 00176 for ( int j = 0; j < o.getName().size(); j++ ) { 00177 Word w = o.getName().get( j ); 00178 w.setSynSetIDs( WordNetRDF2.getSynsets( w.getLabel(), WordNetRDF2.convertPOS(w.getType()) ) ); 00179 getCycConcepts( w ); 00180 } 00181 00182 // Adjectives 00183 for ( int j = 0; j < o.getAdjectives().size(); j++ ) { 00184 Word w = o.getAdjectives().get( j ); 00185 try { 00186 ArrayList<String> synset = WordNetRDF2.getSynsets( w.getLabel(), WordNetRDF2.convertPOS(w.getType()) ); 00187 if (synset.isEmpty()) { 00188 synset = WordNetRDF2.getSynsets( w.getLabel(), WordNetRDF2.convertPOS(Word.TYPE_PAST_PARTICIPLE) ); 00189 } 00190 w.setSynSetIDs( synset ); 00191 getCycConcepts( w ); 00192 } 00193 catch ( Exception e ) { 00194 o.getAdjectives().remove( j ); 00195 j -= 1; 00196 continue; 00197 } 00198 } 00199 00200 // Prepositions 00201 List<Preposition> pp = o.getPrepositions(); 00202 for ( int j = 0; j < pp.size(); j++ ) { 00203 try { 00204 findMeanings( pp.get( j ) ); 00205 } 00206 catch ( Exception e ) { 00207 pp.remove( j ); 00208 j -= 1; 00209 continue; 00210 } 00211 } 00212 00213 // Quantifier 00214 Quantifier q = o.getQuantifier(); 00215 try { 00216 findMeanings( q ); 00217 } 00218 catch ( Exception e ) { 00219 // o.setQuantifier( null ); 00220 } 00221 } 00222 00223 // ================================================================== 00224 // Prepositions 00225 // ================================================================== 00226 else if ( so instanceof Preposition ) { 00227 Preposition pp = (Preposition) so; 00228 00229 // Objects 00230 List<ObjectX> o = pp.getObjects(); 00231 for ( int j = 0; j < o.size(); j++ ) { 00232 try { 00233 findMeanings( o.get( j ) ); 00234 } 00235 catch ( Exception e ) { 00236 o.remove( j ); 00237 j -= 1; 00238 continue; 00239 } 00240 } 00241 } 00242 00243 // ================================================================== 00244 // Quantifier 00245 // ================================================================== 00246 else if ( so instanceof Quantifier ) { 00247 Word m = ( (Quantifier) so ).getMeasure(); 00248 if ( ! m.getLabel().isEmpty() ) { 00249 m.setSynSetIDs( WordNetRDF2.getSynsets( m.getLabel(), WordNetRDF2.convertPOS(m.getType())) ); 00250 getCycConcepts( m ); 00251 } 00252 } 00253 } 00254 00255 public void setAddMappingListener(AddCycMappingListener listener) { 00256 this.listener = listener; 00257 } 00258 }