Page 174 - The Definitive Guide to Building Java Robots
P. 174

Preston_5564C05.fm  Page 155  Tuesday, September 20, 2005  5:13 AM



                                                                                  CHAPTER 5  ■  SPEECH   155



                            The second line is the grammar location, which will be the class path of the project name:
                        com.scottpreston.javarobot.chapter5.
                            The third line is the name of the grammar file at the location specified by the previous line.
                            The fourth line is required because all scores and probabilities are maintained in this class.
                        See Example 5-13.

                        Example 5-13. Grammar Configuration of notepad.config.xml
                        <component name="jsgfGrammar" type="edu.cmu.sphinx.jsapi.JSGFGrammar">
                            <property name="dictionary" value="dictionary"/>
                            <property name="grammarLocation"     ➥
                        value="resource:/com.scottpreston.javarobot.chapter5.SphinxSR!/➥
                        com/scottpreston/javarobot/chapter5/"/>
                            <property name="grammarName" value="notepad"/>
                            <property name="logMath" value="logMath"/>
                        </component>
                            Now that I have both text files—notepad.gram and notepad.config.xml—I can create my
                        speech recognition class, SphinxSR. It has two fields: recognizer and microphone. Both have
                        parameters defined in the configuration file. I also get a copy of the grammar file. Later, I’ll use
                        the RuleGrammar to test what command I want to execute, because the rules are structured
                        per command. Once I get the GSGFGrammar from the ConfigurationManager, I allocate() the
                        GSFFGrammar object to create it. Then I set the private field ruleGrammar. At the end of the
                        constructor, I loop through all the rule names and rules for informational purposes so I know
                        what to speak.
                            The next methods—open() and close()—allocate() and deallocate() resources for speech
                        recognition. The method, start(), begins recording from the microphone, while stop() halts
                        recording from the microphone.
                            The listen() method calls recognize() on the recognizer. This method returns the recogni-
                        tion results. They can be either full or partial. As long as the result is not null, I can call a few
                        methods on it, but the only one I care about is the one that returns the best and final result with
                        no filler: getBestFinalResultNoFiller(). This returns a string of the spoken words. Next, to test
                        the ruleName, I get the ruleNames from the ruleGrammar object. Then I create a RuleParse
                        object to parse the resultText against the ruleName. If there’s a match, I return the ruleName.
                            In main(), after constructing SphinxSR with the URL path to the configuration file, I
                        open(), start(), and then listen() until I hear the commands/ruleNames I’m looking for. When I
                        hear the rule “notepad”, I execute notepad. When I hear the rule “exit”, I exit the program. See
                        Example 5-14.



                        ■Note  To get this to work and avoid an out-of-memory exception, you may be required to increase the
                        memory size for your JVM by adding the arguments –Xms 128m and –Xmx 128m. To do this in Eclipse, click
                        the Run menu, followed by Run, and then modify the VM arguments as shown in Figure 5-5.
   169   170   171   172   173   174   175   176   177   178   179