如何检测CMU Sphinx中的词汇单词

时间:2015-04-06 08:31:46

标签: java speech-recognition voice-recognition speech-to-text cmusphinx

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

#JSGF V1.0;

grammar hello;

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

我的语法很简单,只包含4个单词:&#34; play&#34; ,&#34;暂停&#34; ,&#34; next&#34; ,&#34;之前&#34;。我已经使用Sphinx来成功检测它们。但我希望我的应用程序显示如下消息:&#34;无法识别的单词&#34;当我说一些不属于语法的词。目前,例如,如果我对麦克风说话不属于语法,如:&#34;停止&#34; ,它仍然显示它检测到它是最近的结果。

我的代码是这样的:

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");
 }

1 个答案:

答案 0 :(得分:1)

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