我的目标是通过单击按钮让用户讲话,然后在单击按钮停止时,控制台将输出结果。 我尝试应用CMU sphinx官方网站编写的代码,如下所示:
LiveSpeechRecognizer recognizer = new LiveSpeechRecognizer(configuration);
// Start recognition process pruning previously cached data.
recognizer.startRecognition(true);
SpeechResult result = recognizer.getResult();
// Pause recognition process. It can be resumed then with startRecognition(false).
recognizer.stopRecognition();
这是我使用zk-maven项目实现的方式:
public class SphinxSpeechRecog extends SelectorComposer<Component> {
Configuration configuration = new Configuration();
private LiveSpeechRecognizer recognizer;
private SpeechResult result;
public SphinxSpeechRecog() {
// TODO Auto-generated constructor stub
}
@Listen("onClick=#speakbtn")
public void startSpeaking() throws IOException, InstantiationException {
//System.out.println("hi");
configuration.setAcousticModelPath("resource:/edu/cmu/sphinx/models/en-us/en-us");
configuration.setDictionaryPath("resource:/edu/cmu/sphinx/models/en-us/cmudict-en-us.dict");
configuration.setLanguageModelPath("resource:/edu/cmu/sphinx/models/en-us/en-us.lm.bin");
recognizer = new LiveSpeechRecognizer(configuration);
recognizer.startRecognition(true);
result = recognizer.getResult();
recognizer.stopRecognition();
}
@Listen("onClick=#stopspeakbtn")
public void stopSpeaking() {
System.out.print("result: "+result);
}
}
前端(.zul文件):
<?page title="sphinx speech recognition" contentType="text/html;charset=UTF-8"?>
<zk>
<window title="sphinx speech recognition" border="normal" apply="sphinx.SphinxSpeechRecog">
<button id="speakbtn" label="speak"/>
<button id="stopspeakbtn" label="stop speaking"/>
</window>
</zk>
java(eclipse)控制台输出一些奇怪的结果,例如(几分钟后):
结果:edu.cmu.sphinx.api.SpeechResult@3be41473
我该怎么做才能优化我的代码?以及如何获得我真正想看到的口语?
答案 0 :(得分:0)
for(WordResult word : result.getWords()) {
System.out.print(word.toString());
}
查看更多示例here。