Vocabulary.java
Go to the documentation of this file.
00001 package com.riverlab.robotmanager.voice_recognition;
00002 
00003 import java.io.IOException;
00004 import java.io.StringReader;
00005 import java.util.ArrayList;
00006 import java.util.Arrays;
00007 import java.util.HashMap;
00008 import java.util.List;
00009 import java.util.Map;
00010 
00011 import javax.xml.parsers.DocumentBuilder;
00012 import javax.xml.parsers.DocumentBuilderFactory;
00013 import javax.xml.parsers.ParserConfigurationException;
00014 
00015 import org.w3c.dom.Document;
00016 import org.w3c.dom.Element;
00017 import org.w3c.dom.Node;
00018 import org.w3c.dom.NodeList;
00019 import org.xml.sax.InputSource;
00020 import org.xml.sax.SAXException;
00021 
00022 import android.util.Log;
00023 
00024 public class Vocabulary 
00025 {
00026         List<String> mDefaultSubVocabList;
00027         private HashMap<String, ArrayList<Phrase>> mSubVocabMap;
00028 
00029         public Vocabulary (String rawXML)
00030         {
00031                 Log.d("Vocabulary", "Creating vocabulary from XML");
00032                 //Create the builder factory
00033                 DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
00034                 DocumentBuilder builder = null;
00035 
00036                 //Create the builder from the builder factory
00037                 try {
00038                         builder = builderFactory.newDocumentBuilder();
00039                 } catch (ParserConfigurationException e) {
00040                         e.printStackTrace();  
00041                 }
00042 
00043                 //Use InputSource and StringReader to represent the rawXML String object
00044                 //as a file and parse it using the builder. 
00045                 Log.d("Vocabulary", "Reading document");
00046                 Document document = null;
00047                 try {
00048                         InputSource is = new InputSource(new StringReader(rawXML));
00049                         document = builder.parse(is);
00050                 } catch (SAXException e) {
00051                         e.printStackTrace();
00052                 } catch (IOException e) {
00053                         e.printStackTrace();
00054                 }
00055 
00056                 //Get the default sub-vocabs from the root vocab elemement's attributes
00057                 Element rootElement = document.getDocumentElement();
00058                 String defaultSubVocabs = rootElement.getAttribute("default");
00059                 Log.d("Vocabulary", "Root element: " + rootElement.getTagName());
00060                 String[] defaultSubVocabArray = defaultSubVocabs.split(",");
00061                 this.mDefaultSubVocabList = Arrays.asList(defaultSubVocabArray);
00062 
00063                 NodeList subVocabNodes = rootElement.getChildNodes();
00064                 mSubVocabMap = new HashMap<String, ArrayList<Phrase>>();
00065 
00066                 Log.d("Vocabulary", "Reading sub-vocabs");
00067                 for (int i = 0; i < subVocabNodes.getLength(); i++)
00068                 {
00069                         if (!(subVocabNodes.item(i).getNodeType() == Node.ELEMENT_NODE))
00070                         {
00071                                 continue;
00072                         }
00073 
00074                         Element subVocab = (Element) subVocabNodes.item(i);
00075                         String subVocabName = subVocab.getAttribute("name");
00076 
00077                         NodeList phraseNodes = subVocab.getChildNodes();
00078                         ArrayList<Phrase> phrases = new ArrayList<Phrase>();
00079                         for (int j = 0; j < phraseNodes.getLength(); j++)
00080                         {
00081                                 if (!(phraseNodes.item(j).getNodeType() == Node.ELEMENT_NODE))
00082                                 {
00083                                         continue;
00084                                 }
00085 
00086                                 Element phraseNode = (Element) phraseNodes.item(j);
00087 
00088                                 String tempName = phraseNode.getAttribute("text");
00089 
00090                                 NodeList modifiers = phraseNode.getChildNodes();
00091 
00092                                 if (modifiers.getLength() == 0)
00093                                 {
00094                                         phrases.add(new Phrase(tempName, new ArrayList<Modifier>()));
00095                                 }
00096                                 else
00097                                 {
00098                                         ArrayList<Modifier> tempModList = new ArrayList<Vocabulary.Modifier>();
00099                                         for (int k = 0; k < modifiers.getLength(); k++)
00100                                         {
00101                                                 if (!(modifiers.item(k).getNodeType() == Node.ELEMENT_NODE))
00102                                                 {
00103                                                         continue;
00104                                                 }
00105                                                 //Construct the remainder depth of this tree using 
00106                                                 //recursive modifier constructor
00107                                                 Element mod = (Element)modifiers.item(k);
00108                                                 tempModList.add(new Modifier(mod));
00109                                         }
00110                                         phrases.add(new Phrase(tempName, tempModList));
00111                                 }
00112                         }
00113                         mSubVocabMap.put(subVocabName, phrases);
00114                 }
00115         }
00116 
00117         public Phrase findPhrase(String phraseText)
00118         {
00119                 //Search sub-vocabularies for phrase
00120                 for (Map.Entry<String, ArrayList<Phrase>> subVocab : mSubVocabMap.entrySet())
00121                 {
00122                         ArrayList<Phrase> phrases = subVocab.getValue();
00123 
00124                         //Search through phrases in sub-vocabulary for phrase
00125                         for (Phrase phrase : phrases)
00126                         {
00127                                 if (phrase.text.equals(phraseText))
00128                                 {
00129                                         return phrase;
00130                                 }
00131                         }
00132                 }
00133                 //No such phrase
00134                 return null;
00135         }
00136 
00137         public Modifier findModifier(String phraseText, ArrayList<Modifier> modifiers)
00138         {
00139                 String modifierUses = "";
00140 
00141                 //Search sub-vocabularies for phrase
00142                 searchloop:
00143                         for (Map.Entry<String, ArrayList<Phrase>> subVocab : mSubVocabMap.entrySet())
00144                         {
00145                                 ArrayList<Phrase> phrases = subVocab.getValue();
00146 
00147                                 //Search through phrases in sub-vocabulary for phrase
00148                                 for (Phrase phrase : phrases)
00149                                 {
00150                                         if (phrase.text.equals(phraseText))
00151                                         {
00152                                                 modifierUses = subVocab.getKey();
00153                                                 break searchloop;
00154                                         }
00155                                 }
00156                         }                       
00157 
00158                 for (Modifier mod : modifiers)
00159                 {
00160                         if (mod.getUses() == modifierUses)
00161                         {
00162                                 return mod;
00163                         }
00164                 }
00165 
00166                 return null;
00167         }
00168 
00169         public NewPhrasesMessage getNextPhrases(ArrayList<String> previous)
00170         {
00171                 ArrayList<String> rtnList = new ArrayList<String>();
00172                 boolean isRequired = true;
00173                 boolean reset = false;
00174 
00175                 //If no phrases have been said, return the default list
00176                 if (previous.size() == 0)
00177                 {
00178                         for (String subVocabName : mDefaultSubVocabList)
00179                         {
00180                                 for (Phrase phrase : mSubVocabMap.get(subVocabName))
00181                                 {
00182                                         rtnList.add(phrase.getText());
00183                                 }
00184                         }
00185                 }
00186                 else //If a phrase has already been said
00187                 {
00188                         Phrase rootPhrase = findPhrase(previous.remove(0));
00189                         VocabMessage msg = rootPhrase.getNextSubVocabs(previous);
00190                         ArrayList<String> nextSubVocabs = msg.subVocabNames;
00191                         isRequired = msg.isRequired();
00192 
00193                         //If there is nothing left to say, return default list and reset
00194                         if (nextSubVocabs.size() == 0)
00195                         {
00196                                 for (String subVocabName : mDefaultSubVocabList)
00197                                 {
00198                                         for (Phrase phrase : mSubVocabMap.get(subVocabName))
00199                                         {
00200                                                 rtnList.add(phrase.getText());
00201                                         }
00202                                 }
00203                                 reset = true;
00204                         }
00205                         else //If there are still phrases left to say, get them from their subVocabs
00206                         {                       
00207                                 for (String subVocabName : nextSubVocabs)
00208                                 {
00209                                         try
00210                                         {
00211                                                 for (Phrase phrase : mSubVocabMap.get(subVocabName))
00212                                                 {
00213                                                         rtnList.add(phrase.getText());
00214                                                 }
00215                                         }
00216                                         catch (NullPointerException e)
00217                                         {
00218                                                 Log.w("Vocabulary", 
00219                                                                 "No subvocabulary \"" + subVocabName + "\" was found. Please check your vocabulary XML file.");
00220                                                 e.printStackTrace();
00221                                         }
00222                                 }
00223                         }
00224                 }
00225                 return new NewPhrasesMessage(rtnList, isRequired, reset);
00226         }
00227 
00228 
00229 
00230         public class Phrase
00231         {
00232                 private String text;
00233                 private boolean isRequired = true;
00234                 private ArrayList<Modifier> modifiers;
00235 
00236                 public Phrase(String text, ArrayList<Modifier> modifiers)
00237                 {
00238                         this.text = text;
00239                         this.modifiers = modifiers;
00240                 }
00241 
00242                 public String getText()
00243                 {
00244                         return text;
00245                 }
00246 
00247                 public boolean isRequired()
00248                 {
00249                         return isRequired;
00250                 }
00251 
00252                 public ArrayList<Modifier> getModifiers()
00253                 {
00254                         return modifiers;
00255                 }
00256 
00257                 public void setText(String text)
00258                 {
00259                         this.text = text;
00260                 }
00261 
00262                 public void setRequired(boolean required)
00263                 {
00264                         this.isRequired = required;
00265                 }
00266 
00267                 public void setModifiers(ArrayList<Modifier> modifiers)
00268                 {
00269                         this.modifiers = modifiers;
00270                 }
00271 
00272                 public VocabMessage getNextSubVocabs(ArrayList<String> previous)
00273                 {
00274                         ArrayList<String> rtnList = new ArrayList<String>();
00275 
00276                         if (previous.size() == 0)
00277                         {
00278                                 if (modifiers.size() == 0)
00279                                 {
00280                                         return new VocabMessage(rtnList, false);
00281                                 }
00282                                 else
00283                                 {
00284                                         boolean required = false;
00285                                         for (Modifier mod : modifiers)
00286                                         {
00287                                                 rtnList.add(mod.getUses());
00288                                                 required |= mod.isRequired();
00289                                         }
00290                                         return new VocabMessage(rtnList, required);
00291                                 }
00292                         }
00293                         else
00294                         {
00295                                 Modifier usedMod = findModifier(previous.remove(0), modifiers);
00296                                 if (usedMod == null)
00297                                 {
00298                                         Log.d("CommandInterpreter", "Error finding modifier, check for voice recognition bounce");
00299                                         return new VocabMessage(new ArrayList<String>(), false);
00300                                 }
00301                                 return usedMod.getNextSubVocabs(previous);
00302                         }
00303                 }
00304         }
00305 
00306         public class Modifier
00307         {
00308                 private String uses;
00309                 private boolean isRequired;
00310                 private ArrayList<Modifier> modifiers;
00311 
00312                 public Modifier(Element modifierNode)
00313                 {
00314                         this.uses = modifierNode.getAttribute("use");
00315                         this.isRequired = !(modifierNode.getAttribute("required").equals("false"));
00316 
00317                         Log.d("Vocabulary", "Constructing modifier using " + this.uses);
00318 
00319                         NodeList additionalMods = modifierNode.getChildNodes();
00320 
00321                         this.modifiers = new ArrayList<Vocabulary.Modifier>();
00322 
00323                         for (int i = 0; i < additionalMods.getLength(); i++)
00324                         {
00325                                 if (!(additionalMods.item(i).getNodeType() == Node.ELEMENT_NODE))
00326                                 {
00327                                         continue;
00328                                 }
00329 
00330                                 Element elm = (Element)additionalMods.item(i);
00331 
00332                                 this.modifiers.add(new Modifier(elm));
00333                         }
00334                 }
00335 
00336                 public String getUses()
00337                 {
00338                         return uses;
00339                 }
00340 
00341                 public boolean isRequired()
00342                 {
00343                         return isRequired;
00344                 }
00345 
00346                 public ArrayList<Modifier> getModifiers()
00347                 {
00348                         return modifiers;
00349                 }
00350 
00351                 public void setUses(String uses)
00352                 {
00353                         this.uses = uses;
00354                 }
00355 
00356                 public void setRequired(boolean required)
00357                 {
00358                         isRequired = required;
00359                 }
00360 
00361                 public void setModifiers(ArrayList<Modifier> modifiers)
00362                 {
00363                         this.modifiers = modifiers;
00364                 }
00365 
00366                 public VocabMessage getNextSubVocabs(ArrayList<String> previous)
00367                 {
00368                         ArrayList<String> rtnList = new ArrayList<String>();
00369 
00370                         if (previous.size() == 0)
00371                         {
00372                                 if (modifiers.size() == 0)
00373                                 {
00374                                         return new VocabMessage(rtnList, false);
00375                                 }
00376                                 else
00377                                 {
00378                                         boolean required = false;
00379                                         for (Modifier mod : modifiers)
00380                                         {
00381                                                 rtnList.add(mod.getUses());
00382                                                 required |= mod.isRequired();
00383                                         }
00384 
00385                                         return new VocabMessage(rtnList, required);
00386                                 }
00387                         }
00388                         else
00389                         {
00390                                 Modifier usedMod = findModifier(previous.remove(0), modifiers);
00391                                 return usedMod.getNextSubVocabs(previous);
00392                         }
00393                 }
00394         }
00395 
00396         public class VocabMessage
00397         {
00398                 private ArrayList<String> subVocabNames;
00399                 private boolean isRequired;
00400 
00401                 public VocabMessage(ArrayList<String> subVocabNames, boolean isRequired)
00402                 {
00403                         this.subVocabNames = subVocabNames;
00404                         this.isRequired = isRequired;
00405                 }
00406 
00407                 public ArrayList<String> getSubVocabNames() 
00408                 {
00409                         return subVocabNames;
00410                 }
00411 
00412                 public void setSubVocabNames(ArrayList<String> subVocabNames) 
00413                 {
00414                         this.subVocabNames = subVocabNames;
00415                 }
00416 
00417                 public boolean isRequired() 
00418                 {
00419                         return isRequired;
00420                 }
00421 
00422                 public void setRequired(boolean isRequired) 
00423                 {
00424                         this.isRequired = isRequired;
00425                 }
00426 
00427         }
00428 }


google_glass_driver
Author(s): Nicholas Otero
autogenerated on Fri Aug 28 2015 10:51:44