Page 177 - The Definitive Guide to Building Java Robots
P. 177
Preston_5564C05.fm Page 158 Tuesday, September 20, 2005 5:13 AM
158 CHAPTER 5 ■ SPEECH
// stop capturing audio data
public void stop() {
microphone.stopRecording();
}
public String listen(){
// gets recognition results from recognizer
Result result = recognizer.recognize();
String ruleName = "";
if (result != null) {
// gets best and final with no filler words
String resultText = result.getBestFinalResultNoFiller();
// display text
System.out.println("I heard --> " + resultText);
RuleParse rParse = null;
String [] rules = ruleGrammar.listRuleNames();
for (int i=0; i < rules.length;i++) {
try {
// test rule name and execute
rParse = ruleGrammar.parse(resultText,rules[i]);
// set rulename
ruleName = rParse.getRuleName().getRuleName();
} catch (Exception e) {
// do nothing
}
}
}
// return rulename
return ruleName;
}
// test class
public static void main(String[] args) throws Exception {
// this is the configuration file
URL url = SphinxSR.class.getResource("notepad.config.xml");
SphinxSR sr = new SphinxSR(url);
System.out.println("Loading...");
sr.open();
sr.start();
String rule = "";
System.out.println("Listening...");
while (true) {
rule = sr.listen();
if (rule.equals("notepad")) {
Runtime.getRuntime().exec("cmd /c notepad.exe");
}