gpt4 book ai didi

java - 如何检测 CMU Sphinx 中的词汇表单词

转载 作者:行者123 更新时间:2023-12-01 11:41:49 24 4
gpt4 key购买 nike

我在使用 Java 版 Sphinx 语音识别库时遇到问题。我用它来获取输入并处理它。在语法文件中,我是这样写的:

#JSGF V1.0;

grammar hello;

public <sentence> = (play | pause | next | previous);

我的语法很简单,只包含4个词:“播放”、“暂停”、“下一个”、“上一个”。我已经使用Sphinx成功检测到它们。但我希望我的应用程序在我说出一些不属于语法的单词时显示类似“无法识别的单词”的消息。目前,例如,如果我对着麦克风说话,不属于这样的语法:“stop”,它仍然会显示它检测到它是最接近的结果的单词。

我的代码是这样的:

public class SphinxDemo {

static int i = 1;
static String resultText;

public static void main(String[] args) {
try {
URL url;
if (args.length > 0) {
url = new File(args[0]).toURI().toURL();
} else {
url = SphinxDemo.class.getResource("helloworld.config.xml");
}

System.out.println("Loading...");

ConfigurationManager cm = new ConfigurationManager(url);

Recognizer recognizer = (Recognizer) cm.lookup("recognizer");
Microphone microphone = (Microphone) cm.lookup("microphone");

/* allocate the resource necessary for the recognizer */
recognizer.allocate();

/* the microphone will keep recording until the program exits */

if (microphone.startRecording()) {
System.out
.println("Say: play|pause|previous|next");

while (true) {

System.out
.println("Start speaking. Press Ctrl-C to quit.\n");

Result result = recognizer.recognize();
if (result != null) {

System.out.println("Enter your choise" + "\n");
resultText = result.getBestFinalResultNoFiller();
System.out.println("You said: " + resultText + "\n");
}

if(!(resultText.equalsIgnoreCase("play") || resultText.equalsIgnoreCase("previous") || resultText.equalsIgnoreCase("pause")||resultText.equalsIgnoreCase("next"))){
System.out.println("Unrecognized word\n");
}

}
} else {
System.out.println("Cannot start microphone.");
recognizer.deallocate();
System.exit(1);
}

} catch (IOException e) {
System.err.println("Problem when loading SphinxDemo: " + e);
e.printStackTrace();
} catch (PropertyException e) {
System.err.println("Problem configuring SphinxDemo: " + e);
e.printStackTrace();
} catch (InstantiationException e) {
System.err.println("Problem creating SphinxDemo: " + e);
e.printStackTrace();
}

}
}

我尝试添加类似的内容来检测无法识别的单词,但它不起作用:

  if(!(resultText.equalsIgnoreCase("play") || resultText.equalsIgnoreCase("previous") || resultText.equalsIgnoreCase("pause")||resultText.equalsIgnoreCase("next"))){
System.out.println("Unrecognized word\n");
}

最佳答案

如果你使用最新的cmusphinx,它将返回<unk>当单词不在语法中时。

关于java - 如何检测 CMU Sphinx 中的词汇表单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29467853/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com