有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

找不到java语音识别jar在其执行语法类上抛出错误

我通过Sphinx在JAVA中创建了一个语音识别项目的jar。我的代码执行得很完美,但当我通过“runnable jar file->;copy required libraries into sub folder”创建jar,并通过命令“java-jar{jar name}.jar”通过cmd执行它时,它会打开,但在选择speak按钮或调用sphinx方法后,它会给出错误edu。cmu。斯芬克斯jsapi。找不到JSGFGrammar类。 我没有办法解决这个问题。 我的语音到文本代码是:

package com.ongraph;
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 SpeechToTextOperation {
    ConfigurationManager cm;
    SpeechRecognizer speechRecognizer;
    Result result;
    Recognizer recognizer;
    Microphone microphone;
    private final static String STOP = "stop";
    private final static String XML_FILE = "helloworld.config.xml";

    public void voiceGet() throws InterruptedException {
        String resultString = null;
        int count_Check = 0;
        if (cm == null) {
            cm = new ConfigurationManager(getClass().getClassLoader().getResource(XML_FILE));
        }
        if (recognizer == null) {
            recognizer = (Recognizer) cm.lookup("recognizer");
            microphone = (Microphone) cm.lookup("microphone");
            microphone.clear();
        }
        recognizer.allocate();
        if (!(microphone.startRecording())) {
            System.out.println("Cannot start microphone.");
            recognizer.deallocate();
            System.exit(1);
        }
        instructions();
        //SpeechRecognizer.please_Speak.setVisible(true);
        while (true) {
            System.out
                    .println("Start speaking. Speak 'Stop' to Stop Recording.");
            if(count_Check == 0)
            {
                SpeechRecognizer.textArea.append("\n Start speaking...\n");
                count_Check++;
            }

            Result result = recognizer.recognize();
            resultString = result.getBestFinalResultNoFiller();
            if (resultString != null && !"".equals(resultString)
                    && !resultString.contains(STOP)) {
                SpeechRecognizer.textArea.append(resultString + "\n");
            } else {
                SpeechRecognizer.textArea
                        .append("'Application Stopped. Press 'Speak' again to restart'");
                recognizer.deallocate();
                microphone.stopRecording();
                break;
            }
        }
    }

    public void voiceStop() {
        microphone.clear();
        cm = null;
    }

    public void instructions() {
        // TODO Auto-generated method stub
        SpeechRecognizer.please_Speak.setVisible(true);

    }

}

cmd中的错误包括:

class not found !java.lang.ClassNotFoundException: edu.cmu.sphinx.jsapi.JSGFGrammar
Exception in thread "AWT-EventQueue-0" Property Exception component:'flatLinguist' property:'grammar' - mandatory property is not set!
edu.cmu.sphinx.util.props.InternalConfigurationException
        at edu.cmu.sphinx.util.props.PropertySheet.getComponent(PropertySheet.java:291)
        at edu.cmu.sphinx.linguist.flat.FlatLinguist.newProperties(FlatLinguist.java:246)
        at edu.cmu.sphinx.util.props.PropertySheet.getOwner(PropertySheet.java:460)
        at edu.cmu.sphinx.util.props.PropertySheet.getComponent(PropertySheet.java:279)
        at edu.cmu.sphinx.decoder.search.SimpleBreadthFirstSearchManager.newProperties(SimpleBreadthFirstSearchManager.java:179)
        at edu.cmu.sphinx.util.props.PropertySheet.getOwner(PropertySheet.java:460)
        at edu.cmu.sphinx.util.props.PropertySheet.getComponent(PropertySheet.java:279)
        at edu.cmu.sphinx.decoder.AbstractDecoder.newProperties(AbstractDecoder.java:65)
        at edu.cmu.sphinx.decoder.Decoder.newProperties(Decoder.java:37)
        at edu.cmu.sphinx.util.props.PropertySheet.getOwner(PropertySheet.java:460)
        at edu.cmu.sphinx.util.props.PropertySheet.getComponent(PropertySheet.java:279)
        at edu.cmu.sphinx.recognizer.Recognizer.newProperties(Recognizer.java:90)
        at edu.cmu.sphinx.util.props.PropertySheet.getOwner(PropertySheet.java:460)
        at edu.cmu.sphinx.util.props.ConfigurationManager.lookup(ConfigurationManager.java:161)
        at com.ongraph.SpeechToTextOperation.voiceGet(SpeechToTextOperation.java:24)
        at com.ongraph.SpeechRecognizer$1.actionPerformed(SpeechRecognizer.java:50)
        at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
        at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
        at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
        at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
        at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
        at java.awt.Component.processMouseEvent(Unknown Source)
        at javax.swing.JComponent.processMouseEvent(Unknown Source)
        at java.awt.Component.processEvent(Unknown Source)
        at java.awt.Container.processEvent(Unknown Source)
        at java.awt.Component.dispatchEventImpl(Unknown Source)
        at java.awt.Container.dispatchEventImpl(Unknown Source)
        at java.awt.Component.dispatchEvent(Unknown Source)
        at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
        at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
        at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
        at java.awt.Container.dispatchEventImpl(Unknown Source)
        at java.awt.Window.dispatchEventImpl(Unknown Source)
        at java.awt.Component.dispatchEvent(Unknown Source)
        at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
        at java.awt.EventQueue.access$400(Unknown Source)
        at java.awt.EventQueue$2.run(Unknown Source)
        at java.awt.EventQueue$2.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
        at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
        at java.awt.EventQueue$3.run(Unknown Source)
        at java.awt.EventQueue$3.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
        at java.awt.EventQueue.dispatchEvent(Unknown Source)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
        at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
        at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
        at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
        at java.awt.EventDispatchThread.run(Unknown Source)

共 (1) 个答案

  1. # 1 楼答案

    总的来说,最好使用最新版本5prealpha和http://cmusphinx.sourceforge.net/wiki/tutorialsphinx4中描述的API

    至于你的例外,它说找不到类。为了运行代码,需要将该类打包到jar中。包名也可能有所不同。最近edu。cmu。斯芬克斯jsapi包被重命名为edu。cmu。斯芬克斯jsgf。你可能有问题来更新它