Menu

Sphinx4-Grammar inside Result?

Help
Anonymous
2004-07-25
2012-09-22
  • Anonymous

    Anonymous - 2004-07-25

    Hi!

    I would like to use Sphinx4 to start different shell scripts according to what was said. It seems that after recognition Result contains only the String representation.

    It would be easier to parse the recognized text if Result Tokens contains also the grammar.

    That is, the helloworld example contains hello.gram. Now as far as I understood, the Result.getBestToken() return Token that contains Words (Hello Rita) but no <greet> node for grammar.

    It would be useful for more complex grammar. What would be the best way to get the grammar nodes? Parsing result once again and get a new parse tree?

    Thanks,
    Petteri

     
    • Willie Walker

      Willie Walker - 2004-07-25

      Hi:

      You can do this with a little work.  Assume you're working with a JSGF grammar named "dog.Rover" (i.e., the header of your JSGF grammar says "grammar dog.Rover;"), and you're accessing it from a class named "Rover."

      Set up your configuration file along the following lines to define "jsgfGrammar" as the name the configuration manager should use for the grammar:

          <!-- ******************************************************** -->
          <!-- The linguist  configuration                              -->
          <!-- ******************************************************** -->
         
          <component name="flatLinguist"
                      type="edu.cmu.sphinx.linguist.flat.FlatLinguist">
              <property name="logMath" value="logMath"/>
              <property name="grammar" value="jsgfGrammar"/>
              <property name="acousticModel" value="wsj"/>
              <property name="wordInsertionProbability"
                      value="${wordInsertionProbability}"/>
              <property name="languageWeight" value="${languageWeight}"/>
              <property name="unitManager" value="unitManager"/>
              <property name="addOutOfGrammarBranch" value="true"/>
              <property name="outOfGrammarProbability" value="1E-60"/>
              <property name="phoneInsertionProbability" value="1E-10"/>
              <property name="phoneLoopAcousticModel" value="wsj"/>
          </component>
             
          <!-- ******************************************************** -->
          <!-- The Grammar  configuration                               -->
          <!-- ******************************************************** -->
                 
          <component name="jsgfGrammar" type="edu.cmu.sphinx.jsapi.JSGFGrammar">
              <property name="dictionary" value="dictionary"/>
              <property name="grammarLocation"
                   value="resource:/dog.Rover!/"/>
              <property name="grammarName" value="dog.Rover"/>
          <property name="logMath" value="logMath"/>
          </component>

      Now...in your application (dog/Rover.java), you can configure your recognizer, tell it to recognize, and get the JSGF RuleParse as follows:

      ...

      import javax.speech.recognition.RuleGrammar;
      import javax.speech.recognition.RuleParse;

      import edu.cmu.sphinx.frontend.util.Microphone;
      import edu.cmu.sphinx.jsapi.JSGFGrammar;
      import edu.cmu.sphinx.recognizer.Recognizer;
      import edu.cmu.sphinx.result.Result;
      import edu.cmu.sphinx.util.props.ConfigurationManager;
      import edu.cmu.sphinx.util.props.PropertyException;

      ...

          public void recognize() throws Exception {
              URL url = Rover.class.getResource("rover.config.xml");

              speak("Please wait.");
              System.out.println("Allocating recognizer...");
             
              ConfigurationManager cm = new ConfigurationManager(url);
             
              Recognizer recognizer = (Recognizer) cm.lookup("recognizer");
              Microphone microphone = (Microphone) cm.lookup("microphone");
             
              recognizer.allocate();
             
              RuleGrammar grammar =
                  ((JSGFGrammar) cm.lookup("jsgfGrammar")).getRuleGrammar();
             
              microphone.startRecording();
              while (true) {
                  System.out.println("Speak now.");
                  Result result = recognizer.recognize();
                  if (result != null) {
                      String resultText =
                          result.getBestFinalResultNoFiller();
                      System.out.println("I heard: " + resultText);
                      try {
                          RuleParse p = grammar.parse(resultText, null);
                      } catch (Exception e) {
                          e.printStackTrace();
                      }
                  } else {
                      System.out.println("I can't hear what you said.");
                  }
              }
          }

      The JSAPI RuleParse should tell you all you want to know. Hopefully this code snippet is enough.

      Will

      PS - 2/3 of the Sun Labs speech team is travelling this week, so it may take a while for us to respond this week.

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.