如果说某件事,我目前正在尝试使我的 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);