user2593282
user2593282

Reputation: 21

Sphinx4 Demo Applications Do Not Work

I am doing a project in Java that requires me to use speech recognition software. Basically, I am programming a "social robot", a robot that you can interact with like a person. I am using the "Zeno R25". With that being said, speech recognition is a key point when programming the robot. I am programming the robot using Java.

Now I tried to download the Sphinx4 demo programs from https://github.com/cmusphinx/sphinx4, but those did not work. There were a total of four of them, and all four failed to work, apparently from some problem dealing with Configuration objects. For example, I got the following error from DialogDemo.java:

--- exec-maven-plugin:1.2.1:exec (default-cli) @ sphinx4-samples ---
Exception in thread "main" java.lang.NoClassDefFoundError: edu/cmu/sphinx/api/Configuration
    at edu.cmu.sphinx.demo.dialog.DialogDemo.main(DialogDemo.java:133)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at edu.cmu.sphinx.demo.DemoRunner.main(DemoRunner.java:44)
Caused by: java.lang.ClassNotFoundException: edu.cmu.sphinx.api.Configuration
    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)
    ... 6 more

when I tried to run it. And yes, I launched the code off the DemoRunner class, as I was supposed to. The code for DialogDemo.java is as follows, and I marked the rogue line of code triggering the error with arrows:

/*
 * Copyright 2013 Carnegie Mellon University.
 * Portions Copyright 2004 Sun Microsystems, Inc.
 * Portions Copyright 2004 Mitsubishi Electric Research Laboratories.
 * All Rights Reserved.  Use is subject to license terms.
 *
 * See the file "license.terms" for information on usage and
 * redistribution of this file, and for a DISCLAIMER OF ALL
 * WARRANTIES.
 */

package edu.cmu.sphinx.demo.dialog;

import java.util.HashMap;
import java.util.Map;

import edu.cmu.sphinx.api.Configuration;
import edu.cmu.sphinx.api.LiveSpeechRecognizer;


public class DialogDemo {

    private static final String ACOUSTIC_MODEL =
        "resource:/edu/cmu/sphinx/models/en-us/en-us";
    private static final String DICTIONARY_PATH =
        "resource:/edu/cmu/sphinx/models/en-us/cmudict-en-us.dict";
    private static final String GRAMMAR_PATH =
        "resource:/edu/cmu/sphinx/demo/dialog/";          
    private static final String LANGUAGE_MODEL =
        "resource:/edu/cmu/sphinx/demo/dialog/weather.lm";

    private static final Map<String, Integer> DIGITS =
        new HashMap<String, Integer>();

    static {
        DIGITS.put("oh", 0);
        DIGITS.put("zero", 0);
        DIGITS.put("one", 1);
        DIGITS.put("two", 2);
        DIGITS.put("three", 3);
        DIGITS.put("four", 4);
        DIGITS.put("five", 5);
        DIGITS.put("six", 6);
        DIGITS.put("seven", 7);
        DIGITS.put("eight", 8);
        DIGITS.put("nine", 9);
    }

    private static double parseNumber(String[] tokens) {
        StringBuilder sb = new StringBuilder();

        for (int i = 1; i < tokens.length; ++i) {
            if (tokens[i].equals("point"))
                sb.append(".");
            else
                sb.append(DIGITS.get(tokens[i]));
        }

        return Double.parseDouble(sb.toString());
    }
    private static void recognizeDigits(LiveSpeechRecognizer recognizer) {
        System.out.println("Digits recognition (using GrXML)");
        System.out.println("--------------------------------");
        System.out.println("Example: one two three");
        System.out.println("Say \"101\" to exit");
        System.out.println("--------------------------------");

        recognizer.startRecognition(true);
        while (true) {
            String utterance = recognizer.getResult().getHypothesis();
            if (utterance.equals("one zero one")
                || utterance.equals("one oh one"))
                break;
            else
                System.out.println(utterance);
        }
        recognizer.stopRecognition();
    }

    private static void recognizerBankAccount(LiveSpeechRecognizer recognizer) {
        System.out.println("This is bank account voice menu");
        System.out.println("-------------------------------");
        System.out.println("Example: balance");
        System.out.println("Example: withdraw zero point five");
        System.out.println("Example: deposit one two three");
        System.out.println("Example: back");
        System.out.println("-------------------------------");

        double savings = .0;
        recognizer.startRecognition(true);

        while (true) {
            String utterance = recognizer.getResult().getHypothesis();
            if (utterance.endsWith("back")) {
                break;
            } else if (utterance.startsWith("deposit")) {
                double deposit = parseNumber(utterance.split("\\s"));
                savings += deposit;
                System.out.format("Deposited: $%.2f\n", deposit);
            } else if (utterance.startsWith("withdraw")) {
                double withdraw = parseNumber(utterance.split("\\s"));
                savings -= withdraw;
                System.out.format("Withdrawn: $%.2f\n", withdraw);
            } else if (!utterance.endsWith("balance")) {
                System.out.println("Unrecognized command: " + utterance);
            }

            System.out.format("Your savings: $%.2f\n", savings);
        }

        recognizer.stopRecognition();
    }

    private static void recognizeWeather(LiveSpeechRecognizer recognizer) {
        System.out.println("Try some forecast. End with \"the end\"");
        System.out.println("-------------------------------------");
        System.out.println("Example: mostly dry some fog patches tonight");
        System.out.println("Example: sunny spells on wednesday");
        System.out.println("-------------------------------------");

        recognizer.startRecognition(true);
        while (true) {
            String utterance = recognizer.getResult().getHypothesis();
            if (utterance.equals("the end"))
                break;
            else
                System.out.println(utterance);
        }
        recognizer.stopRecognition();
    }

    public static void main(String[] args) throws Exception {
-->     Configuration configuration = new Configuration();   <--
        configuration.setAcousticModelPath(ACOUSTIC_MODEL);
        configuration.setDictionaryPath(DICTIONARY_PATH);
        configuration.setGrammarPath(GRAMMAR_PATH);
        configuration.setUseGrammar(true);

        configuration.setGrammarName("dialog");
        LiveSpeechRecognizer jsgfRecognizer =
            new LiveSpeechRecognizer(configuration);

        configuration.setGrammarName("digits.grxml");
        LiveSpeechRecognizer grxmlRecognizer =
            new LiveSpeechRecognizer(configuration);

        configuration.setUseGrammar(false);
        configuration.setLanguageModelPath(LANGUAGE_MODEL);
        LiveSpeechRecognizer lmRecognizer =
            new LiveSpeechRecognizer(configuration);

        jsgfRecognizer.startRecognition(true);
        while (true) {
            System.out.println("Choose menu item:");
            System.out.println("Example: go to the bank account");
            System.out.println("Example: exit the program");
            System.out.println("Example: weather forecast");
            System.out.println("Example: digits\n");

            String utterance = jsgfRecognizer.getResult().getHypothesis();

            if (utterance.startsWith("exit"))
                break;

            if (utterance.equals("digits")) {
                jsgfRecognizer.stopRecognition();
                recognizeDigits(grxmlRecognizer);
                jsgfRecognizer.startRecognition(true);
            }

            if (utterance.equals("bank account")) {
                jsgfRecognizer.stopRecognition();
                recognizerBankAccount(jsgfRecognizer);
                jsgfRecognizer.startRecognition(true);
            }

            if (utterance.endsWith("weather forecast")) {
                jsgfRecognizer.stopRecognition();
                recognizeWeather(lmRecognizer);
                jsgfRecognizer.startRecognition(true);
            }
        }

        jsgfRecognizer.stopRecognition();
    }
}

It is a similar situation with the other three demos. Any help toward solving this problem would be greatly appreciated.

Upvotes: 1

Views: 1201

Answers (1)

Nikolay Shmyrev
Nikolay Shmyrev

Reputation: 25220

You need to include sphinx4 jars into classpath. It might be a setting in your IDE if you use it or you can add them in command line if you are using command line to start your program.

Sphinx4 tutorial says where to download JARs

To learn more about class loading and classpath read

https://en.wikipedia.org/wiki/Classpath_(Java)

Upvotes: 1

Related Questions