$search
00001 package instruction.transformation; 00002 00003 import instruction.semanticObjects.*; 00004 import java.util.ArrayList; 00005 import java.util.HashMap; 00006 import java.util.List; 00007 00015 public class Transformations { 00016 00017 private static HashMap<String, ModelTransformation> transformations = null; 00018 00019 private static ModelTransformation defaultTransformation; 00020 00024 private static void init() { 00025 00026 if ( transformations != null ) 00027 return; 00028 00032 transformations = new HashMap<String, ModelTransformation>(); 00033 00037 transformations.put( "NP", new ModelTransformation() { 00038 00039 public List<SemanticObject> doModelTransformation( List<SemanticObject> objects ) { 00040 00041 List<SemanticObject> newObjects = new ArrayList<SemanticObject>(); 00042 00043 ObjectX obj = null; 00044 Quantifier q = null; 00045 00046 for ( int i = 0; i < objects.size(); i++ ) { 00047 00048 SemanticObject o = objects.get( i ); 00049 00050 if ( o instanceof Word ) { 00051 Word w = (Word) o; 00052 00053 // Example Pattern: (NP (DT the)(NN pot)) 00054 if ( w.getType() == Word.TYPE_NOUN ) { 00055 00056 if ( obj == null ) { 00057 obj = new ObjectX(); 00058 newObjects.add( obj ); 00059 } 00060 00061 obj.addNameComponent( w ); 00062 } 00063 00064 // Example Pattern: (NP (JJ cold)(NN water)) 00065 else if ( w.getType() == Word.TYPE_ADJ ) { 00066 00067 if ( obj == null ) { 00068 obj = new ObjectX(); 00069 newObjects.add( obj ); 00070 } 00071 obj.addNameComponent( w ); 00072 } 00073 00074 // Example Pattern: (NP (DT a)(JJ boiled)(NN egg)) 00075 else if ( w.getType() == Word.TYPE_PAST_PARTICIPLE ) { 00076 00077 if ( obj == null ) { 00078 obj = new ObjectX(); 00079 newObjects.add( obj ); 00080 } 00081 obj.getAdjectives().add( w ); 00082 } 00083 00084 // Example Pattern: (NP (PRP it)) 00085 else if ( w.getType() == Word.TYPE_PRONOUN ) { 00086 if ( obj == null ) { 00087 obj = new ObjectX(); 00088 newObjects.add( obj ); 00089 } 00090 obj.addNameComponent( w ); 00091 } 00092 00093 // Example Pattern: (NP (CD 3)(NNS liters)) 00094 else if ( w.getType() == Word.TYPE_NUMBER ) { 00095 // if ( obj == null ) { 00096 // obj = new ObjectX(); 00097 // newObjects.add( obj ); 00098 // } 00099 // obj.getQuantifier().getAlternatives().add( w ); 00100 q = new Quantifier(); 00101 q.getAlternatives().add( w ); 00102 } 00103 00104 // Example Pattern: (NP (DT a)(NN cup)(CC and)(NN 00105 // saucer)) 00106 else if ( w.getType() == Word.TYPE_CONJ 00107 || ( w.getType() == Word.TYPE_PUNCTUATION && w.getLabel().equals( "," ) ) ) { 00108 if ( obj != null ) { 00109 obj = null; 00110 } 00111 } 00112 } 00113 00114 // Examle Pattern: (NP (NP (NN fish))(CC and)(NP (NNS 00115 // chips))) 00116 else if ( o instanceof ObjectX ) 00117 newObjects.add( o ); 00118 00119 // Example Pattern: (NP (DT the)(NN cup)(PP (IN on)(NP (DT 00120 // the)(NN table)))) 00121 else if ( o instanceof Preposition ) { 00122 for ( int j = 0; j < newObjects.size(); j++ ) { 00123 SemanticObject o2 = newObjects.get( j ); 00124 if ( o2 instanceof ObjectX ) 00125 ( (ObjectX) o2 ).addPreposition( (Preposition) o ); 00126 } 00127 } 00128 00129 // Example Pattern: (NP (QP (CD 3)(TO to)(CD 4))(NNS eggs)) 00130 else if ( o instanceof Quantifier ) { 00131 // if ( obj == null ) { 00132 // obj = new ObjectX(); 00133 // newObjects.add( obj ); 00134 // } 00135 // obj.getQuantifier().getAlternatives().addAll( ( (Quantifier) o 00136 // ).getAlternatives() ); 00137 q = (Quantifier) o; 00138 } 00139 00140 // Example: "...a pot that is large enough" 00141 else if ( o instanceof Description ) { 00142 for ( int j = 0; j < newObjects.size(); j++ ) { 00143 if ( newObjects.get( j ) instanceof ObjectX ) 00144 ( (ObjectX) newObjects.get( j ) ).addDescription( (Description) o ); 00145 } 00146 } 00147 00148 // Pass complete Instructions 00149 else if ( o instanceof Sentence ) 00150 newObjects.add( o ); 00151 00152 } 00153 00154 // add the quantifier to all objects 00155 for ( int i = 0; i < newObjects.size(); i++ ) { 00156 if ( newObjects.get( i ) instanceof ObjectX && q != null ) { 00157 ( (ObjectX) newObjects.get( i ) ).setQuantifier( q ); 00158 } 00159 } 00160 00161 return newObjects; 00162 } 00163 } ); 00164 00168 transformations.put( "VP", new ModelTransformation() { 00169 00170 public List<SemanticObject> doModelTransformation( List<SemanticObject> obj ) { 00171 00172 ArrayList<SemanticObject> newObj = new ArrayList<SemanticObject>(); 00173 00174 // Combine the upcoming SemanticObjects to a Description 00175 Sentence s = new Sentence(); 00176 00177 Action vp = new Action(); 00178 ArrayList<ObjectX> np = new ArrayList<ObjectX>(); 00179 ArrayList<Preposition> pp = new ArrayList<Preposition>(); 00180 ArrayList<Precondition> pr = new ArrayList<Precondition>(); 00181 ArrayList<Postcondition> po = new ArrayList<Postcondition>(); 00182 00183 // if an instruction has to be negated 00184 boolean negate = false; 00185 00186 for ( int i = 0; i < obj.size(); i++ ) { 00187 00188 SemanticObject o = obj.get( i ); 00189 00190 // Example Pattern: (VP (VB take)(NP (DT the)(NNS eggs))(CC 00191 // and)(NP (DT the)(NN milk))) 00192 if ( o instanceof ObjectX ) 00193 np.add( (ObjectX) o ); 00194 00195 else if ( o instanceof Word ) { 00196 Word word = (Word) o; 00197 00198 // Example Pattern: (VP (VB serve)(RB immediately)) 00199 if ( word.getType() == Word.TYPE_ADV ) { 00200 00201 // Example Pattern: (VP (VB do)(RB not)(VP (VB 00202 // stir))) (Negation) 00203 if ( word.getLabel().matches( "n't|not" ) ) 00204 negate = true; 00205 else 00206 vp.getAdverb().add( word ); 00207 } 00208 00209 // Example Pattern: (VP (VB take)(PRT (RP out))(NP ...) 00210 else if ( word.getType() == Word.TYPE_PARTICLE ) 00211 vp.getAction().setLabel( vp.getAction().getLabel() + " " + word.getLabel() ); 00212 00213 // Example Pattern: (VP (VB stir)) 00214 else if ( word.getType() == Word.TYPE_VERB_INFINITIVE ) 00215 vp.setAction( word ); 00216 } 00217 00218 // Gather ready objects to be added to the Description 00219 else if ( o instanceof Preposition ) 00220 pp.add( (Preposition) o ); 00221 00222 else if ( o instanceof Precondition ) 00223 pr.add( (Precondition) o ); 00224 00225 else if ( o instanceof Postcondition ) 00226 po.add( (Postcondition) o ); 00227 00228 else if ( o instanceof Sentence ) { 00229 ( (Instruction) o ).getAction().setDontDoIt( negate ); 00230 newObj.add( o ); 00231 } 00232 00233 // else if ( o instanceof Instruction ) 00234 // newObj.add( (Instruction) o ); 00235 } 00236 00237 // Add the gathered SemanticObjects to the Description 00238 s.setAction( vp ); 00239 s.setObjects( np ); 00240 s.setPreconditions( pr ); 00241 s.setPostconditions( po ); 00242 s.setPrepositions( pp ); 00243 00244 // Only let a Description pass if an Action is specified 00245 // and it's not an Action like "do" (for negations) 00246 if ( ! s.getAction().getAction().getLabel().isEmpty() 00247 && ! s.getAction().getAction().getLabel().equalsIgnoreCase( "do" ) ) 00248 newObj.add( s ); 00249 return newObj; 00250 } 00251 00252 } ); 00253 00257 transformations.put( "SBAR", new ModelTransformation() { 00258 public List<SemanticObject> doModelTransformation( List<SemanticObject> objects ) { 00259 00260 List<SemanticObject> newObjects = new ArrayList<SemanticObject>(); 00261 00262 int prepositionType = 0; 00263 00264 for ( int i = 0; i < objects.size(); i++ ) { 00265 SemanticObject o = objects.get( i ); 00266 00267 // make out the type of the SBAR sub sentence 00268 if ( o instanceof Word ) { 00269 Word w = (Word) o; 00270 00271 // Precondition 00272 if ( w.getType() == Word.TYPE_PREPOSITION && w.getLabel().toLowerCase().matches( "when|if|after" ) ) 00273 prepositionType = 1; 00274 00275 // Postcondition 00276 else if ( ( w.getType() == Word.TYPE_ADV || w.getType() == Word.TYPE_PREPOSITION ) 00277 && w.getLabel().toLowerCase().matches( "so|till|until" ) ) 00278 prepositionType = 2; 00279 00280 // Relative clause 00281 else if ( ( w.getType() == Word.TYPE_PRONOUN ) 00282 && w.getLabel().toLowerCase().matches( "that|which" ) ) 00283 prepositionType = 3; 00284 00285 } 00286 00287 // SBAR starts with "if", "when", "after" or something like 00288 // that 00289 // => Precondition 00290 else if ( o instanceof Sentence && prepositionType == 1 ) { 00291 Precondition cond = new Precondition(); 00292 cond.setCondition( (Sentence) o ); 00293 newObjects.add( cond ); 00294 } 00295 00296 // SBAR starts with "so that" or something like that 00297 // => Postcondition 00298 else if ( o instanceof Sentence && prepositionType == 2 ) { 00299 Postcondition cond = new Postcondition(); 00300 cond.setCondition( (Sentence) o ); 00301 newObjects.add( cond ); 00302 } 00303 00304 // SBAR starts with "which" or "that" or something like that 00305 // => Relative clause 00306 else if ( o instanceof Sentence && prepositionType == 3 ) { 00307 Sentence s = (Sentence) o; 00308 Description d = new Description(); 00309 00310 d.setAction( s.getAction() ); 00311 d.setObjects( s.getObjects() ); 00312 d.setPostconditions( s.getPostconditions() ); 00313 d.setPreconditions( s.getPreconditions() ); 00314 d.setPrepositions( d.getPrepositions() ); 00315 d.setActors( s.getActors() ); 00316 00317 newObjects.add( d ); 00318 } 00319 00320 // Re-Transform Instructions into Sentences, because Instructions 00321 // mustn't occour in SBARS; add Pre- and Postconditions 00322 else if ( o instanceof Instruction ) { 00323 Sentence s = new Sentence(); 00324 s.setAction( ( (Instruction) o ).getAction() ); 00325 s.setObjects( ( (Instruction) o ).getObjects() ); 00326 s.setPostconditions( ( (Instruction) o ).getPostconditions() ); 00327 s.setPreconditions( ( (Instruction) o ).getPreconditions() ); 00328 s.setPrepositions( ( (Instruction) o ).getPrepositions() ); 00329 00330 if ( prepositionType == 1 ) { 00331 Precondition cond = new Precondition(); 00332 cond.setCondition( s ); 00333 newObjects.add( cond ); 00334 } 00335 00336 else if ( prepositionType == 2 ) { 00337 Postcondition cond = new Postcondition(); 00338 cond.setCondition( s ); 00339 newObjects.add( cond ); 00340 } 00341 else if ( prepositionType == 3 ) { 00342 Instruction in = (Instruction) o; 00343 Description d = new Description(); 00344 d.setAction( in.getAction() ); 00345 d.setObjects( in.getObjects() ); 00346 d.setPostconditions( in.getPostconditions() ); 00347 d.setPreconditions( in.getPreconditions() ); 00348 d.setPrepositions( in.getPrepositions() ); 00349 newObjects.add( d ); 00350 } 00351 } 00352 } 00353 00354 return newObjects; 00355 } 00356 } ); 00357 00361 transformations.put( "PP", new ModelTransformation() { 00362 public List<SemanticObject> doModelTransformation( List<SemanticObject> objects ) { 00363 00364 List<SemanticObject> newObjects = new ArrayList<SemanticObject>(); 00365 Preposition p = null; 00366 00367 for ( int i = 0; i < objects.size(); i++ ) { 00368 SemanticObject o = objects.get( i ); 00369 00370 if ( o instanceof ObjectX ) { 00371 if ( p == null ) 00372 p = new Preposition(); 00373 p.addObject( (ObjectX) o ); 00374 } 00375 else if ( o instanceof Word ) { 00376 Word w = (Word) o; 00377 if ( w.getType() == Word.TYPE_PREPOSITION ) { 00378 if ( p == null ) 00379 p = new Preposition(); 00380 00381 p.addPreposition( w ); 00382 } 00383 } 00384 else if ( o instanceof Preposition ) 00385 newObjects.add( o ); 00386 } 00387 newObjects.add( p ); 00388 return newObjects; 00389 } 00390 } ); 00391 00395 transformations.put( "S", new ModelTransformation() { 00396 public List<SemanticObject> doModelTransformation( List<SemanticObject> objects ) { 00397 00398 List<SemanticObject> newObjects = new ArrayList<SemanticObject>(); 00399 00400 List<ObjectX> np = new ArrayList<ObjectX>(); 00401 List<Preposition> pp = new ArrayList<Preposition>(); 00402 List<Precondition> pr = new ArrayList<Precondition>(); 00403 List<Postcondition> po = new ArrayList<Postcondition>(); 00404 List<Word> adv = new ArrayList<Word>(); 00405 00406 int makeAllAlternative = Instruction.OPTIONAL_FALSE; 00407 00408 for ( int i = 0; i < objects.size(); i++ ) { 00409 00410 SemanticObject o = objects.get( i ); 00411 00412 // Example Pattern: (VP (VB take)(NP (DT the)(NNS eggs))(CC 00413 // and)(NP (DT the)(NN milk))) 00414 if ( o instanceof ObjectX ) 00415 np.add( (ObjectX) o ); 00416 00417 else if ( o instanceof Preposition ) { 00418 pp.add( (Preposition) o ); 00419 } 00420 else if ( o instanceof Precondition ) { 00421 pr.add( (Precondition) o ); 00422 } 00423 else if ( o instanceof Postcondition ) { 00424 po.add( (Postcondition) o ); 00425 } 00426 else if ( o instanceof Sentence ) { 00427 00428 // If there have been already Nouns preceding the 00429 // verb, it's a Sentence, otherwise an Instruction 00430 if ( ! np.isEmpty() ) { 00431 for ( int k = 0; k < np.size(); k++ ) { 00432 ( (Sentence) o ).addActor( np.get( k ) ); 00433 np.remove( k ); 00434 } 00435 for ( int k = 0; k < pp.size(); k++ ) { 00436 ( (Sentence) o ).addPreposition( pp.get( k ) ); 00437 pp.remove( k ); 00438 } 00439 newObjects.add( o ); 00440 } 00441 else { 00442 // Transform the Sentence into an Instruction 00443 Sentence d = (Sentence) o; 00444 Instruction inst = new Instruction(); 00445 inst.setAction( d.getAction() ); 00446 inst.setObjects( d.getObjects() ); 00447 inst.setPreconditions( d.getPreconditions() ); 00448 inst.setPostconditions( d.getPostconditions() ); 00449 inst.setPrepositions( d.getPrepositions() ); 00450 00451 newObjects.add( inst ); 00452 } 00453 } 00454 00455 else if ( o instanceof Description ) { 00456 Description d = (Description) o; 00457 Instruction inst = new Instruction(); 00458 inst.setAction( d.getAction() ); 00459 inst.setObjects( d.getObjects() ); 00460 inst.setPreconditions( d.getPreconditions() ); 00461 inst.setPostconditions( d.getPostconditions() ); 00462 inst.setPrepositions( d.getPrepositions() ); 00463 00464 newObjects.add( inst ); 00465 } 00466 00467 else if ( o instanceof Instruction ) 00468 newObjects.add( o ); 00469 00470 else if ( o instanceof Word ) { 00471 Word w = (Word) o; 00472 if ( w.getType() == Word.TYPE_CONJ && w.getLabel().equalsIgnoreCase( "or" ) ) { 00473 if ( np.isEmpty() && pp.isEmpty() && pr.isEmpty() && po.isEmpty() ) 00474 makeAllAlternative = Instruction.OPTIONAL_GLOBAL; 00475 else 00476 makeAllAlternative = Instruction.OPTIONAL_LOCAL; 00477 } 00478 else if ( w.getType() == Word.TYPE_ADV ) 00479 adv.add( w ); 00480 } 00481 } 00482 00483 for ( int i = 0; i < newObjects.size(); i++ ) { 00484 if ( newObjects.get( i ) instanceof Instruction && ! pr.isEmpty() ) { 00485 for ( int j = 0; j < pr.size(); j++ ) 00486 ( (Instruction) newObjects.get( i ) ).getPreconditions().add( pr.get( j ) ); 00487 } 00488 if ( newObjects.get( i ) instanceof Instruction ) { 00489 Instruction in = (Instruction) newObjects.get( i ); 00490 in.setOptional( makeAllAlternative ); 00491 in.getAction().getAdverb().addAll( adv ); 00492 } 00493 } 00494 00495 return newObjects; 00496 } 00497 } ); 00498 00502 transformations.put( "QP", new ModelTransformation() { 00503 public List<SemanticObject> doModelTransformation( List<SemanticObject> objects ) { 00504 00505 List<SemanticObject> newObjects = new ArrayList<SemanticObject>(); 00506 Quantifier q = new Quantifier(); 00507 00508 // Example Pattern: (QP (CD 1)(CC or)(CD 2)) 00509 for ( int i = 0; i < objects.size(); i++ ) { 00510 SemanticObject o = objects.get( i ); 00511 if ( o instanceof Word ) { 00512 Word w = (Word) o; 00513 if ( w.getType() == Word.TYPE_NUMBER ) { 00514 q.getAlternatives().add( w ); 00515 } 00516 } 00517 } 00518 00519 newObjects.add( q ); 00520 00521 return newObjects; 00522 } 00523 } ); 00524 00528 transformations.put( "ADVP", new ModelTransformation() { 00529 00530 public List<SemanticObject> doModelTransformation( List<SemanticObject> objects ) { 00531 00532 List<SemanticObject> newObjects = new ArrayList<SemanticObject>(); 00533 00534 // Check, if the ADVP contains a Preposition. 00535 // If so, it is likely that this ADVP is malformed and the Words 00536 // before the Preposition Object are Prepositions 00537 00538 boolean foundPP = false; 00539 00540 // Example Pattern: (ADVP (RB right)(JJ next)(PP (TO to)(NP 00541 // ...))) (malformed pattern) 00542 for ( int i = 0; i < objects.size(); i++ ) { 00543 SemanticObject o = objects.get( i ); 00544 if ( o instanceof Preposition ) { 00545 foundPP = true; 00546 Preposition p = (Preposition) o; 00547 for ( int j = i - 1; j >= 0; j-- ) { 00548 SemanticObject o2 = objects.get( j ); 00549 if ( o2 instanceof Word ) { 00550 p.getPrepositions().add( 0, (Word) o2 ); 00551 newObjects.remove( o2 ); 00552 } 00553 else 00554 break; 00555 } 00556 newObjects.add( p ); 00557 } 00558 else 00559 newObjects.add( o ); 00560 } 00561 00562 if ( ! foundPP ) 00563 return defaultTransformation.doModelTransformation( objects ); 00564 00565 return newObjects; 00566 } 00567 } ); 00568 00572 transformations.put( "PRN", new ModelTransformation() { 00573 00574 public List<SemanticObject> doModelTransformation( List<SemanticObject> objects ) { 00575 00576 List<SemanticObject> newObjects = new ArrayList<SemanticObject>(); 00577 00578 // For brackets: only let complete instructions pass 00579 for ( int i = 0; i < objects.size(); i++ ) { 00580 SemanticObject o = objects.get( i ); 00581 if ( o instanceof Instruction ) 00582 newObjects.add( o ); 00583 } 00584 00585 return newObjects; 00586 } 00587 } ); 00588 00593 transformations.put( "ROOT", new ModelTransformation() { 00594 00595 public List<SemanticObject> doModelTransformation( List<SemanticObject> objects ) { 00596 00597 List<SemanticObject> newObjects = new ArrayList<SemanticObject>(); 00598 00599 // For brackets: only let complete instructions pass 00600 for ( int i = 0; i < objects.size(); i++ ) { 00601 SemanticObject o = objects.get( i ); 00602 if ( o != null && o instanceof Instruction && ! ( o instanceof Description ) 00603 && ! ( o instanceof Sentence ) ) { 00604 //Instruction inst = (Instruction) o; 00605 // if (WordNet.wordExactlyExistsAs( 00606 // inst.getAction().getAction().getLabel(), Word.TYPE_VERB )) 00607 newObjects.add( o ); 00608 } 00609 } 00610 00611 return newObjects; 00612 } 00613 } ); 00614 00619 transformations.put( "ADJP", new ModelTransformation() { 00620 public List<SemanticObject> doModelTransformation( List<SemanticObject> objects ) { 00621 00622 List<SemanticObject> newObjects = new ArrayList<SemanticObject>(); 00623 00624 Quantifier q = null; 00625 00626 // For brackets: only let complete instructions pass 00627 for ( int i = 0; i < objects.size(); i++ ) { 00628 SemanticObject o = objects.get( i ); 00629 if ( o instanceof Word ) { 00630 Word w = (Word) o; 00631 if ( w.getType() == Word.TYPE_NUMBER ) { 00632 q = new Quantifier(); 00633 q.getAlternatives().add( w ); 00634 } 00635 else if ( q != null ) { 00636 q.setMeasure( w ); 00637 } 00638 } 00639 } 00640 00641 newObjects.add( q ); 00642 return newObjects; 00643 } 00644 } ); 00645 00650 transformations.put( "FRAG", get( "S" ) ); 00651 00655 defaultTransformation = new ModelTransformation() { 00656 00657 public List<SemanticObject> doModelTransformation( List<SemanticObject> objects ) { 00658 00659 return objects; 00660 } 00661 00662 }; 00663 } 00664 00672 public static ModelTransformation get( String key ) { 00673 00674 if ( transformations == null ) 00675 init(); 00676 ModelTransformation ret = transformations.get( key ); 00677 if ( ret == null ) 00678 return defaultTransformation; 00679 return ret; 00680 } 00681 00682 }