我目前正在尝试使我的java代码(使用eclipse)执行某些功能(如果说了某件事)。我正在使用Sphinx4库,这是我目前拥有的:
我想做的是在它说的那行:
IF (TRUE) someFunction();
如果我的发言是Hello Computer,Hello Jarvis,Good Morning Computer或Good Morning Jarvis,则运行该功能。或者换句话说,如果语音与.gram文件中的“public ”行代码匹配,请运行该函数。更具体地说,如果我的讲话符合该语法规则,则返回“greet”。对不起,如果这没有道理...
这是我的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 <greet> = (Hello | Good Morning) (Jarvis | Computer);
最佳答案
您可以这样做,但是唯一的事情是您需要在sphinx4中启用OOG发现。基本上以任何sphinx4语法示例为例,并根据此wiki页面在配置文件中启用OOG:
<component name="flatLinguist"
type="edu.cmu.sphinx.linguist.flat.FlatLinguist">
....
<property name="addOutOfGrammarBranch" value="true"/>
<property name="outOfGrammarProbability" value="1E-20"/>
<property name="phoneInsertionProbability" value="1E-10"/>
<property name="phoneLoopAcousticModel" value="wsj"/>
...
</component>
之后,如果只记录了随机单词,它将返回
<unk>
单词,如果记录了语法中的单词,则将返回关键短语。您需要调整outOfGrammar概率以获得可靠的检测。有关更多详细信息,请参见
http://cmusphinx.sourceforge.net/wiki/sphinx4:rejectionhandling
关于java - 用Sphinx4发现关键字或关键词,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16994980/