【问题标题】:Error using sphinx4 jars without Maven在没有 Maven 的情况下使用 sphinx4 jar 时出错
【发布时间】:2015-06-01 11:32:47
【问题描述】:

我的 API Sphinx4 有问题,我不知道为什么它不起作用。

我尝试编写一个小类来捕捉用户的声音并将他的讲话写在一个文件上。

1) 我在 Eclispe 上创建了一个新的 java 项目。

2) 我创建了 TranscriberDemo 类。

3) 我创建了一个文件夹“文件”。

4) 我已复制文件夹“en-us”和文件夹上的文件“cmudict-en-us.dict”、“en-us.lm.dmp”、“10001-90210-01803.wav” “文件”。

5) 我不使用 maven,所以我只包含了 jar 文件“sphinx4-core-1.0-SNAPSHOT.jar”和“sphinx4-data-1.0-SNAPSHOT.jar”。

你可以在这里下载:

核心:https://1fichier.com/?f3y6vqupdr

数据:https://1fichier.com/?lpzz8jyerv

我知道有源代码

这里:https://github.com/erka/sphinx-java-api

或在这里:http://sourceforge.net/projects/cmusphinx/files/sphinx4

但我不使用 maven,所以无法编译它们。

我的班级:

import java.io.InputStream;

import edu.cmu.sphinx.api.Configuration;
import edu.cmu.sphinx.api.SpeechResult;
import edu.cmu.sphinx.api.StreamSpeechRecognizer;
import edu.cmu.sphinx.result.WordResult;


public class TranscriberDemo
{
    public static void main(String[] args) throws Exception
    {
        System.out.println("Loading models...");

        Configuration configuration = new Configuration();

        // Load model from the jar
        configuration.setAcousticModelPath("file:en-us");

        configuration.setDictionaryPath("file:cmudict-en-us.dict");
        configuration.setLanguageModelPath("file:en-us.lm.dmp");

        StreamSpeechRecognizer recognizer = new StreamSpeechRecognizer(configuration);
        InputStream stream = TranscriberDemo.class.getResourceAsStream("file:10001-90210-01803.wav");
        stream.skip(44);

        // Simple recognition with generic model
        recognizer.startRecognition(stream);
        SpeechResult result;
        while ((result = recognizer.getResult()) != null)
        {
            System.out.format("Hypothesis: %s\n", result.getHypothesis());

            System.out.println("List of recognized words and their times:");
            for (WordResult r : result.getWords())
            {
                System.out.println(r);
            }

            System.out.println("Best 3 hypothesis:");
            for (String s : result.getNbest(3))
                System.out.println(s);
        }
        recognizer.stopRecognition();
    }
}

我的日志:

Loading models...
Exception in thread "main" java.lang.NoClassDefFoundError: com/google/common/base/Function
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:191)
    at edu.cmu.sphinx.util.props.ConfigurationManager.getPropertySheet(ConfigurationManager.java:91)
    at edu.cmu.sphinx.util.props.ConfigurationManagerUtils.listAllsPropNames(ConfigurationManagerUtils.java:556)
    at edu.cmu.sphinx.util.props.ConfigurationManagerUtils.setProperty(ConfigurationManagerUtils.java:609)
    at edu.cmu.sphinx.api.Context.setLocalProperty(Context.java:198)
    at edu.cmu.sphinx.api.Context.setAcousticModel(Context.java:88)
    at edu.cmu.sphinx.api.Context.<init>(Context.java:61)
    at edu.cmu.sphinx.api.Context.<init>(Context.java:44)
    at edu.cmu.sphinx.api.AbstractSpeechRecognizer.<init>(AbstractSpeechRecognizer.java:37)
    at edu.cmu.sphinx.api.StreamSpeechRecognizer.<init>(StreamSpeechRecognizer.java:35)
    at TranscriberDemo.main(TranscriberDemo.java:27)
Caused by: java.lang.ClassNotFoundException: com.google.common.base.Function
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
    ... 12 more

感谢您的帮助 =)

【问题讨论】:

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


    【解决方案1】:

    您的代码和操作存在多个问题:

    3) 我创建了一个文件夹“文件”。

    不需要

    4) 我已复制文件夹“en-us”和文件夹上的文件“cmudict-en-us.dict”、“en-us.lm.dmp”、“10001-90210-01803.wav” “文件”。

    不需要,您已经将模型作为 sphinx4-data 包的一部分。

    5) 我不使用 maven,所以我只包含了 jar 文件“sphinx4-core-1.0-SNAPSHOT.jar”和“sphinx4-data-1.0-SNAPSHOT.jar”。

    这是非常错误的,因为您从未经授权的位置拿走了过时的罐子。教程http://oss.sonatype.org中列出了下载jar的正确位置

    https://oss.sonatype.org/service/local/repositories/snapshots/content/edu/cmu/sphinx/sphinx4-core/1.0-SNAPSHOT/sphinx4-core-1.0-20150223.210646-7.jar

    https://oss.sonatype.org/service/local/repositories/snapshots/content/edu/cmu/sphinx/sphinx4-data/1.0-SNAPSHOT/sphinx4-data-1.0-20150223.210601-7.jar

    您从某个随机网站获取了恶意 jar,其中可能包含病毒或 rootkit。

    这里:https://github.com/erka/sphinx-java-api

    这也是一个错误的链接。正确的链接是http://github.com/cmusphinx/sphinx4

        InputStream stream = TranscriberDemo.class.getResourceAsStream("file:10001-90210-01803.wav");
    

    在这里您使用 file: URL 方案,它指向不适当上下文中的文件。如果您想从文件中创建 InputStream,请执行以下操作:

     InputStream stream = new FileInputStream(new File("10001-90210-01803.wav"));
    

    线程“main”中的异常 java.lang.NoClassDefFoundError: com/google/common/base/Function

    这个错误是由于你从其他地方拿了一个罐子,它说你需要额外的依赖。当您看到 ClassDefFoundError 时,这意味着您需要在类路径中添加额外的 jar。使用官方 sphinx4,您应该不会看到此错误。

    【讨论】:

      【解决方案2】:

      解决了。

      其实这是一个愚蠢的错误......

      感谢@Nikolay 的回答。我已经接受了你的回答,但我在这里继续这个过程:

      1) 从https://oss.sonatype.org/#nexus-search;quick~sphinx4 下载 sphinx4-core 和 sphinx4-data jar。

      2) 将它们包含在您的项目中。

      3) 测试您的代码。

      import edu.cmu.sphinx.api.Configuration;
      import edu.cmu.sphinx.api.LiveSpeechRecognizer;
      import edu.cmu.sphinx.api.SpeechResult;
      
      public class SpeechToText
      {
          public static void main(String[] args) throws Exception
          {
              Configuration configuration = new Configuration();
              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.dmp");
      
              LiveSpeechRecognizer recognizer = new LiveSpeechRecognizer(configuration);
              recognizer.startRecognition(true);
      
              SpeechResult result;
              while ((result = recognizer.getResult()) != null)
              {
                  System.out.println(result.getHypothesis());
              }
              recognizer.stopRecognition();
          }
      }
      

      仅此而已!

      【讨论】:

      • 我需要文件夹结构以及在哪里添加依赖文件?
      • 不,你没有,模型已经包含在 sphinx4-data 中。如果搜索其他模型,可能需要使用 gradle 或 maven... 或者使用其他 sphinx4-data。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-24
      • 1970-01-01
      • 1970-01-01
      • 2021-04-16
      • 2013-12-14
      相关资源
      最近更新 更多