PreparePrepositionalPhrasesPostProcessor.java
Go to the documentation of this file.
00001 package instruction.postprocessor;
00002 
00003 import instruction.exceptions.InstructionException;
00004 import instruction.semanticObjects.Instruction;
00005 import instruction.semanticObjects.ObjectX;
00006 import instruction.semanticObjects.Preposition;
00007 import instruction.semanticObjects.SemanticObject;
00008 import instruction.semanticObjects.Word;
00009 import instruction.wordnet.PrepositionalMap;
00010 import instruction.wordnet.WordNetRDF2;
00011 
00012 import java.util.ArrayList;
00013 import java.util.List;
00014 
00015 public class PreparePrepositionalPhrasesPostProcessor implements PostProcessor {
00016 
00017         public void run(List<Instruction> instructions) {
00018 
00019                 for (int i = 0; i < instructions.size(); i++) {
00020                         try {
00021                                 correctPrepositionalPhrase(instructions.get(i));
00022                         } catch (Exception e) {
00023                                 System.out.println(e.getMessage());
00024                         }
00025 
00026                 }
00027         }
00028 
00036         private void correctPrepositionalPhrase(SemanticObject so) throws Exception {
00037 
00038                 // ==================================================================
00039                 // Instructions
00040                 // ==================================================================
00041                 if (so instanceof Instruction) {
00042                         Instruction in = (Instruction) so;
00043 
00044                         // Objects
00045                         for (int i = 0; i < in.getObjects().size(); i++)
00046                                 correctPrepositionalPhrase(in.getObjects().get(i));
00047 
00048                         // Prepositions
00049                         for (int i = 0; i < in.getPrepositions().size(); i++) {
00050                                 try {
00051                                         correctPrepositionalPhrase(in.getPrepositions().get(i));
00052                                 } catch (Exception e) {
00053                                         System.out.println(e.getMessage());
00054                                         in.getPrepositions().remove(i);
00055                                         i -= 1;
00056                                         continue;
00057                                 }
00058                         }
00059                 }
00060 
00061                 // ==================================================================
00062                 // Objects
00063                 // ==================================================================
00064                 else if (so instanceof ObjectX) {
00065                         ObjectX o = (ObjectX) so;
00066 
00067                         // Prepositions
00068                         for (int i = 0; i < o.getPrepositions().size(); i++) {
00069                                 try {
00070                                         correctPrepositionalPhrase(o.getPrepositions().get(i));
00071                                 } catch (Exception e) {
00072                                         System.out.println(e.getMessage());
00073                                         o.getPrepositions().remove(i);
00074                                         i -= 1;
00075                                         continue;
00076                                 }
00077                         }
00078 
00079                 }
00080 
00081                 // ==================================================================
00082                 // Prepositions
00083                 // ==================================================================
00084                 else if (so instanceof Preposition) {
00085                         Preposition pp = (Preposition) so;
00086 
00087                         List<Word> oldPP = pp.getPrepositions();
00088                         List<Word> newPP = getPrepositionalCompositions(oldPP);
00089                         pp.setPrepositions(newPP);
00090 
00091                         // Objects
00092                         for (int i = 0; i < pp.getObjects().size(); i++)
00093                                 correctPrepositionalPhrase(pp.getObjects().get(i));
00094                 }
00095         }
00096 
00104         private List<Word> getPrepositionalCompositions(List<Word> oldList)
00105                         throws Exception {
00106 
00107                 List<Word> newList = new ArrayList<Word>();
00108 
00109                 for (int i = 0; i < oldList.size(); i++) {
00110 
00111                         Word ppName = new Word(oldList.get(i).getType(), oldList.get(i)
00112                                         .getLabel());
00113                         Word ppNameTmp = new Word(ppName.getType(), ppName.getLabel());
00114                         int j = i + 1;
00115                         while (j <= oldList.size()) {
00116 
00117                                 if (PrepositionalMap.get(ppNameTmp.getLabel()) != null
00118                                                 || WordNetRDF2.wordExistsAs(ppNameTmp.getLabel(),
00119                                                                 WordNetRDF2.convertPOS(ppNameTmp.getType()))) {
00120 
00121                                         ppName.setLabel(ppNameTmp.getLabel());
00122                                         if (j < oldList.size())
00123                                                 ppNameTmp.setLabel(ppNameTmp.getLabel() + " "
00124                                                                 + oldList.get(j++).getLabel());
00125                                         else {
00126                                                 i = j - 1;
00127                                                 break;
00128                                         }
00129                                 } else {
00130                                         if (j > i + 1)
00131                                                 i = j - 2;
00132                                         else
00133                                                 throw new InstructionException("Preposition "
00134                                                                 + ppNameTmp.getLabel() + " is unknown");
00135                                         break;
00136                                 }
00137 
00138                         }
00139                         if (PrepositionalMap.get(ppNameTmp.getLabel()) != null)
00140                                 ppName.setType(Word.TYPE_PREPOSITION);
00141                         newList.add(ppName);
00142                 }
00143 
00144                 return newList;
00145         }
00146 
00147 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Friends


comp_ehow
Author(s): Moritz Tenorth, Daniel Nyga
autogenerated on Tue Apr 16 2013 00:18:03