使用Sphinx4进行关键字或关键短语识别

我正在尝试使我的java代码(使用eclipse)执行某些function,如果说某些事情。 我正在使用Sphinx4库,这是我目前拥有的:

我想要它做的是在它说:

IF (TRUE) someFunction(); 

如果我的演讲是Hello Computer,Hello Jarvis,Good Morning Computer或Good Morning Jarvis,那就是运行该function。 或者换句话说,如果语音与.gram文件中的“public ”代码行匹配,则运行该函数。 更具体的是,如果我的演讲符合该语法规则,则返回“问候”。 如果这没有意义,我很抱歉……

这是我的listener.java文件:

 package speechRecognition; import java.io.File; import javax.sound.sampled.AudioInputStream; import javax.sound.sampled.AudioSystem; import javax.sound.sampled.Clip; import edu.cmu.sphinx.frontend.util.Microphone; import edu.cmu.sphinx.recognizer.Recognizer; import edu.cmu.sphinx.result.Result; import edu.cmu.sphinx.util.props.ConfigurationManager; public class Listener { public void someFunction(){ System.out.println("Did Something"); } public static void main(String[] args) { ConfigurationManager cm; if (args.length > 0) { cm = new ConfigurationManager(args[0]); } else { cm = new ConfigurationManager(Listener.class.getResource("configurations.config.xml")); } Recognizer recognizer = (Recognizer) cm.lookup("recognizer"); recognizer.allocate(); Microphone microphone = (Microphone) cm.lookup("microphone"); if (!microphone.startRecording()) { System.out.println("Cannot start microphone."); recognizer.deallocate(); System.exit(1); } while (true) { Result result = recognizer.recognize(); if (result != null) { String resultText = result.getBestFinalResultNoFiller(); if (resultText != "" && resultText != null) { IF (TRUE) someFunction(); } } else { System.out.println("I can't hear what you said.\n"); } } } } 

这是我的dictionary.gram:

 #JSGF V1.0; grammar dictionary; public  = (Hello | Good Morning) (Jarvis | Computer); 

你可以做到这一点,但唯一的事情是你需要在sphinx4中启用OOG定位。 基本上采用任何sphinx4语法示例并根据配置文件中的此Wiki页面启用OOG:

  ....     ...  

之后,它会回报你字作为的结果,如果只是随机的字被记录,如果从你的语法词记录将返回一个关键短语。

您需要调整outOfGrammar概率以获得可​​靠的检测。 有关详细信息,请参阅

http://cmusphinx.sourceforge.net/wiki/sphinx4:rejectionhandling