3

I have a problem with the API Sphinx4 and I can't figure out why it doesn't work.

I try to write a little class for capture the voice of an user and write his speaking on a file.

1) I have create a new java project on Eclispe.

2) I have create the class TranscriberDemo.

3) I have create a folder "file".

4) I have copy the folder "en-us" and the files "cmudict-en-us.dict", "en-us.lm.dmp", "10001-90210-01803.wav" on the folder "file".

5) I don't use maven, so I have just include the jar files "sphinx4-core-1.0-SNAPSHOT.jar" and "sphinx4-data-1.0-SNAPSHOT.jar".

you can download them here:

core: https://1fichier.com/?f3y6vqupdr

data: https://1fichier.com/?lpzz8jyerv

I know that the source code is available

here: https://github.com/erka/sphinx-java-api

or here: http://sourceforge.net/projects/cmusphinx/files/sphinx4

But I don't use maven so I can't compile them.

My class:

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

My log:

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

Thanks for your help =)

tirz
  • 2,041
  • 1
  • 22
  • 37

2 Answers2

6

There are multiple issues with your code and your actions:

3) I have create a folder "file".

Not needed

4) I have copy the folder "en-us" and the files "cmudict-en-us.dict", "en-us.lm.dmp", "10001-90210-01803.wav" on the folder "file".

Not needed, you already have models as part of sphinx4-data package.

5) I don't use maven, so I have just include the jar files "sphinx4-core-1.0-SNAPSHOT.jar" and "sphinx4-data-1.0-SNAPSHOT.jar".

This is very wrong because you took outdated jars from unauthorized location. The right place to download jars is listed in tutorial http://oss.sonatype.org

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

You took malicious jars from some random website which might have a virus or rootkit in them.

here: https://github.com/erka/sphinx-java-api

This is a wrong link too. The correct link is http://github.com/cmusphinx/sphinx4

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

Here you use file: URL scheme which points to files in inappropriate context. If you want to create InputStream from file do like this:

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

Exception in thread "main" java.lang.NoClassDefFoundError: com/google/common/base/Function

This error is caused by the fact you took a jar from other place and it said you need additional dependencies. When you see ClassDefFoundError it means you need to add additional jar into your classpath. With official sphinx4 you should not see this error.

Nikolay Shmyrev
  • 24,897
  • 5
  • 43
  • 87
6

Solved.

In fact it was a silly mistake...

Thank you @Nikolay for your answer. I already accept your answer but I resume the process here:

1) Download the sphinx4-core and sphinx4-data jars from https://oss.sonatype.org/#nexus-search;quick~sphinx4.

2) Include them in your project.

3) Test your code.

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

And that is all!

tirz
  • 2,041
  • 1
  • 22
  • 37
  • I need folder structure and where to add dependencies file ? – JYoThI Jun 29 '17 at 06:02
  • 1
    No you don't, the model is already included in sphinx4-data. If you search for another model, you may need to use gradle or maven... Or use another sphinx4-data. – tirz Apr 14 '18 at 18:32