Go to the documentation of this file.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
00011 import java.util.ArrayList;
00012 import java.util.List;
00013
00014 public class ObjectAmountPostProcessor implements PostProcessor {
00015
00016 public void run( List<Instruction> instructions ) {
00017
00018 for ( int i = 0; i < instructions.size(); i++ )
00019 findObjectAmount( instructions.get( i ), instructions.get( i ) );
00020
00021 }
00022
00030 private void findObjectAmount( SemanticObject so, SemanticObject parent ) {
00031
00032
00033
00034
00035 if ( so instanceof Instruction ) {
00036 Instruction in = (Instruction) so;
00037
00038
00039 for ( int i = 0; i < in.getObjects().size(); i++ )
00040 findObjectAmount( in.getObjects().get( i ), in );
00041
00042
00043 for ( int i = 0; i < in.getPrepositions().size(); i++ )
00044 findObjectAmount( in.getPrepositions().get( i ), in );
00045 }
00046
00047
00048
00049
00050 else if ( so instanceof ObjectX ) {
00051 ObjectX o = (ObjectX) so;
00052
00053 for ( int i = 0; i < o.getName().size(); i++ ) {
00054
00055 Word n = o.getName().get( i );
00056
00057 for ( int j = 0; j < n.getCycConcepts().size(); j++ ) {
00058
00059 String concept = n.getCycConcepts().get( j );
00060
00061 List<ObjectX> newObjects = transformQuantifier( o, concept );
00062
00063 if ( ! newObjects.isEmpty() ) {
00064 if ( parent instanceof Instruction ) {
00065 ( (Instruction) parent ).getObjects().remove( o );
00066 ( (Instruction) parent ).getObjects().addAll( newObjects );
00067 }
00068 else if ( parent instanceof Preposition ) {
00069 ( (Preposition) parent ).getObjects().remove( o );
00070 ( (Preposition) parent ).getObjects().addAll( newObjects );
00071 }
00072
00073 findObjectAmount( parent, parent );
00074 break;
00075 }
00076 }
00077
00078 }
00079
00080
00081 for ( int i = 0; i < o.getPrepositions().size(); i++ ) {
00082 findObjectAmount( o.getPrepositions().get( i ), o );
00083 }
00084
00085 }
00086
00087
00088
00089
00090 else if ( so instanceof Preposition ) {
00091 Preposition pp = (Preposition) so;
00092
00093
00094 for ( int i = 0; i < pp.getObjects().size(); i++ )
00095 findObjectAmount( pp.getObjects().get( i ), pp );
00096 }
00097 }
00098
00099 private List<ObjectX> transformQuantifier( ObjectX object, String concept ) {
00100
00101 List<ObjectX> newObjects = new ArrayList<ObjectX>();
00102
00103 for ( int i = 0; i < object.getPrepositions().size(); i++ ) {
00104 Preposition pp = object.getPrepositions().get( i );
00105
00106 for ( int j = 0; j < pp.getPrepositions().size(); j++ ) {
00107 Word p = pp.getPrepositions().get( j );
00108
00109 if ( p.getLabel().equalsIgnoreCase( "of" ) ) {
00110
00111
00112
00113 Quantifier q = new Quantifier();
00114
00115 try {
00116 if ( OpenCyc.getInstance().isaInUnivVocMt( concept, "IntegerExtent" )
00117 || OpenCyc.getInstance().genlsInUniversalVocabularyMt( concept, "IntegerExtent" ) ) {
00118 if ( object.getName().size() > 0 ) {
00119 object.getName().get( 0 ).getCycConcepts().clear();
00120 object.getName().get( 0 ).getCycConcepts().add( concept );
00121 q.getAlternatives().add( object.getName().get( 0 ) );
00122 }
00123 }
00124 else if ( OpenCyc.getInstance().isaInUnivVocMt( concept, "UnitOfMeasure" )
00125 || OpenCyc.getInstance().isaInUnivVocMt( concept, "PhysicalAmountSlot" )
00126 || OpenCyc.getInstance().genlsInUniversalVocabularyMt( concept, "PhysicalAmountSlot" ) ) {
00127 if ( object.getName().size() > 0 ) {
00128 q.setMeasure( object.getName().get( 0 ) );
00129 object.getName().get( 0 ).getCycConcepts().clear();
00130 object.getName().get( 0 ).getCycConcepts().add( concept );
00131 }
00132 q.setAlternatives( object.getQuantifier().getAlternatives() );
00133 }
00134 else
00135 return newObjects;
00136
00137 for ( int k = 0; k < pp.getObjects().size(); k++ ) {
00138
00139 pp.getObjects().get( k ).setQuantifier( q );
00140 newObjects.add( pp.getObjects().get( k ) );
00141 }
00142
00143 return newObjects;
00144 }
00145 catch ( Exception e ) {
00146 System.out.println( e.getMessage() );
00147 }
00148 }
00149 }
00150 }
00151 return newObjects;
00152 }
00153
00154 }