I have a code for speech recognition but it not work good
it has a problem in line 31
what is the solution ???

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;
import edu.cmu.sphinx.util.props.PropertyException;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;

public class HelloWorld {

       public static void main(String[] args) throws MalformedURLException, IOException, PropertyException, InstantiationException {
URL myURL = new URL("http://purple.com/");
               
              try {
             
//            if (args.length > 0)
//            {
//                 myURL = new File(args[0]).toURI().toURL();
//             } 
//else {
            //   myURL   = new URL("http:\\www.google.com");

//             }

             System.out.println("Loading...");

             ConfigurationManager cm = new ConfigurationManager(new URL("http:\\www.google.com"));


             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: (Command | Hello)");

                 while (true)
                 {
                     System.out.println
                         ("Start speaking. Press Ctrl-C to quit.\n");

                     /*
                      * This method will return when the end of speech
                      * is reached. Note that the endpointer will determine
                      * the end of speech.
                      */ 
                     Result result = recognizer.recognize();
                     
                     if (result != null)
                     {
                         String resultText = result.getBestFinalResultNoFiller();
                         System.out.println("You said: " + resultText + "\n");
                         if(resultText.equalsIgnoreCase("command"));
                         {
                                 Process p;
                                 p = Runtime.getRuntime().exec("cmd /c start");
                                 
                         }
                     }
                     else
                     {
                         System.out.println("I can't hear what you said.\n");
                     }
                 }
             }
             else
             {
                 System.out.println("Cannot start microphone.");
                 recognizer.deallocate();
                 System.exit(1);
             }
         } 
              catch (IOException e)
         {
             System.err.println("Problem when loading HelloWorld: " + e);
         } 
              catch (PropertyException e)
              {
             System.err.println("Problem configuring HelloWorld: " + e);
         }
              catch (InstantiationException e) {
             System.err.println("Problem creating HelloWorld: " + e);
         }
     } 
    }

Recommended Answers

All 18 Replies

I have a code for speech recognition but it not work good
it has a problem in line 31
what is the solution ???

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;
import edu.cmu.sphinx.util.props.PropertyException;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;

public class HelloWorld {

       public static void main(String[] args) throws MalformedURLException, IOException, PropertyException, InstantiationException {
URL myURL = new URL("http://purple.com/");
               
              try {
             
//            if (args.length > 0)
//            {
//                 myURL = new File(args[0]).toURI().toURL();
//             } 
//else {
            //   myURL   = new URL("http:\\www.google.com");

//             }

             System.out.println("Loading...");

             ConfigurationManager cm = new ConfigurationManager(new URL("http:\\www.google.com"));


             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: (Command | Hello)");

                 while (true)
                 {
                     System.out.println
                         ("Start speaking. Press Ctrl-C to quit.\n");

                     /*
                      * This method will return when the end of speech
                      * is reached. Note that the endpointer will determine
                      * the end of speech.
                      */ 
                     Result result = recognizer.recognize();
                     
                     if (result != null)
                     {
                         String resultText = result.getBestFinalResultNoFiller();
                         System.out.println("You said: " + resultText + "\n");
                         if(resultText.equalsIgnoreCase("command"));
                         {
                                 Process p;
                                 p = Runtime.getRuntime().exec("cmd /c start");
                                 
                         }
                     }
                     else
                     {
                         System.out.println("I can't hear what you said.\n");
                     }
                 }
             }
             else
             {
                 System.out.println("Cannot start microphone.");
                 recognizer.deallocate();
                 System.exit(1);
             }
         } 
              catch (IOException e)
         {
             System.err.println("Problem when loading HelloWorld: " + e);
         } 
              catch (PropertyException e)
              {
             System.err.println("Problem configuring HelloWorld: " + e);
         }
              catch (InstantiationException e) {
             System.err.println("Problem creating HelloWorld: " + e);
         }
     } 
    }

what is the problem? is it an error or what? if its an error please type out the error message as it will help to understand whats going wrong

"http:\\www.google.com"

In Java \ is a special character, used to start escape sequences in string literals. "\\" in a string literal is only a single \ character in the String itself.

this is the error


Exception in thread "main" java.lang.NullPointerException
at edu.cmu.sphinx.model.acoustic.WSJ_8gau_13dCep_16k_40mel_130Hz_6800Hz.ModelLoader.loadProperties(ModelLoader.java:372)
at edu.cmu.sphinx.model.acoustic.WSJ_8gau_13dCep_16k_40mel_130Hz_6800Hz.ModelLoader.getIsBinaryDefault(ModelLoader.java:386)
at edu.cmu.sphinx.model.acoustic.WSJ_8gau_13dCep_16k_40mel_130Hz_6800Hz.ModelLoader.newProperties(ModelLoader.java:346)
at edu.cmu.sphinx.util.props.ConfigurationManager.lookup(ConfigurationManager.java:214)
at edu.cmu.sphinx.util.props.ValidatingPropertySheet.getComponent(ValidatingPropertySheet.java:403)
at edu.cmu.sphinx.model.acoustic.WSJ_8gau_13dCep_16k_40mel_130Hz_6800Hz.Model.newProperties(Model.java:159)
at edu.cmu.sphinx.util.props.ConfigurationManager.lookup(ConfigurationManager.java:214)
at edu.cmu.sphinx.util.props.ValidatingPropertySheet.getComponent(ValidatingPropertySheet.java:403)
at edu.cmu.sphinx.linguist.flat.FlatLinguist.setupAcousticModel(FlatLinguist.java:299)
at edu.cmu.sphinx.linguist.flat.FlatLinguist.newProperties(FlatLinguist.java:246)
at edu.cmu.sphinx.util.props.ConfigurationManager.lookup(ConfigurationManager.java:214)
at edu.cmu.sphinx.util.props.ValidatingPropertySheet.getComponent(ValidatingPropertySheet.java:403)
at edu.cmu.sphinx.decoder.search.SimpleBreadthFirstSearchManager.newProperties(SimpleBreadthFirstSearchManager.java:180)
at edu.cmu.sphinx.util.props.ConfigurationManager.lookup(ConfigurationManager.java:214)
at edu.cmu.sphinx.util.props.ValidatingPropertySheet.getComponent(ValidatingPropertySheet.java:403)
at edu.cmu.sphinx.decoder.Decoder.newProperties(Decoder.java:71)
at edu.cmu.sphinx.util.props.ConfigurationManager.lookup(ConfigurationManager.java:214)
at edu.cmu.sphinx.util.props.ValidatingPropertySheet.getComponent(ValidatingPropertySheet.java:403)
at edu.cmu.sphinx.recognizer.Recognizer.newProperties(Recognizer.java:93)
at edu.cmu.sphinx.util.props.ConfigurationManager.lookup(ConfigurationManager.java:214)
at HelloWorld.main(HelloWorld.java:28)

Did you fix your \\ yet? That will cause an error in your
new ConfigurationManager(new URL("http:\\www.google.com"));

well, have you checked what JamesCherril told you?
I think that's exactly the problem you are having, based on this stacktrace.

t HelloWorld.main(HelloWorld.java:28)

Which is your line number in the code.
I guess it is this :

ConfigurationManager cm = new ConfigurationManager(new URL("http:\\www.google.com"));

You need to this correction JamesCherill said :

"http:\\www.google.com"

In Java \ is a special character, used to start escape sequences in string literals. "\\" in a string literal is only a single \ character in the String itself.

Use "http:\\\\www.google.com"

OK i do what you tell but i get the same error

Exception in thread "main" java.lang.IllegalArgumentException: protocol = http host = null
	at sun.net.spi.DefaultProxySelector.select(DefaultProxySelector.java:170)
	at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:922)
	at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:846)
	at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1296)
	at java.net.URL.openStream(URL.java:1035)
	at edu.cmu.sphinx.util.props.SaxLoader.load(SaxLoader.java:64)
	at edu.cmu.sphinx.util.props.ConfigurationManager.loader(ConfigurationManager.java:383)
	at edu.cmu.sphinx.util.props.ConfigurationManager.<init>(ConfigurationManager.java:115)
	at HelloWorld.main(HelloWorld.java:28)
Java Result: 1

it's not the same error.
can you show us your current code?

but i get the same error

If you can't see that

Exception in thread "main" java.lang.NullPointerException
at edu.cmu.sphinx.model.acoustic.WSJ_8gau_13dCep_16k_40mel_130Hz_6800Hz.ModelLoader.loadProperties(ModelLoader.java:372)

is NOT the same as

Exception in thread "main" java.lang.IllegalArgumentException: protocol = http host = null

then you may as well give up now.

You fixed the first error and created a completely new one. Maybe you should post the version of the code that gives the second error

there a new version of my project

//package demo.sphinx.helloworld;

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;
import edu.cmu.sphinx.util.props.PropertyException;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

public class HelloWorld {

       public static void main(String[] args) throws MalformedURLException, IOException, InstantiationException, PropertyException {
//        try {
//            URL url;
//           if (args.length > 0) {
//               url = new File(args[0]).toURI().toURL();
//            } else {
//                url = HelloWorld.class.getResource("helloworld.config.xml");
//            }

            System.out.println("Loading...");
            URL url=new URL("http:\\\\www.google.com");

            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: (Command | Hello)");

		while (true) {
		    System.out.println
			("Start speaking. Press Ctrl-C to quit.\n");

                    /*
                     * This method will return when the end of speech
                     * is reached. Note that the endpointer will determine
                     * the end of speech.
                     */ 
		    Result result = recognizer.recognize();
		    
		    if (result != null) {
			String resultText = result.getBestFinalResultNoFiller();
			System.out.println("You said: " + resultText + "\n");
			if(resultText.equalsIgnoreCase("command"));
			{
				Process p;
				p = Runtime.getRuntime().exec("cmd /c start");
				
			}
		    }
		    else
		    {
			System.out.println("I can't hear what you said.\n");
		    }
		}
	    } else {
		System.out.println("Cannot start microphone.");
		recognizer.deallocate();
		System.exit(1);
	    }
//        } catch (IOException e) {
//            System.err.println("Problem when loading HelloWorld: " + e);
//            e.printStackTrace();
//        } catch (PropertyException e) {
//            System.err.println("Problem configuring HelloWorld: " + e);
//            e.printStackTrace();
//        } catch (InstantiationException e) {
//            System.err.println("Problem creating HelloWorld: " + e);
//            e.printStackTrace();
//        }
    }
}

Are you assuming that http:\\www.google.com is the URL of a valid XML configuration file, or is that just some random URL that you knew existed?

OK I will explain it
this project about speech recognition it must load xml file which exist in the folder of src this is the original code

package demo.sphinx.helloworld;

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;
import edu.cmu.sphinx.util.props.PropertyException;
import java.io.File;
import java.io.IOException;
import java.net.URL;

public class HelloWorld {

       public static void main(String[] args) {
        try {
            URL url;
           if (args.length > 0) {
               url = new File(args[0]).toURI().toURL();
            } else {
                url = HelloWorld.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: (Command | Hello)");

		while (true) {
		    System.out.println
			("Start speaking. Press Ctrl-C to quit.\n");

                    /*
                     * This method will return when the end of speech
                     * is reached. Note that the endpointer will determine
                     * the end of speech.
                     */ 
		    Result result = recognizer.recognize();
		    
		    if (result != null) {
			String resultText = result.getBestFinalResultNoFiller();
			System.out.println("You said: " + resultText + "\n");
			if(resultText.equalsIgnoreCase("command"));
			{
				Process p;
				p = Runtime.getRuntime().exec("cmd /c start");
				
			}
		    }
		    else
		    {
			System.out.println("I can't hear what you said.\n");
		    }
		}
	    } else {
		System.out.println("Cannot start microphone.");
		recognizer.deallocate();
		System.exit(1);
	    }
        } catch (IOException e) {
            System.err.println("Problem when loading HelloWorld: " + e);
            e.printStackTrace();
        } catch (PropertyException e) {
            System.err.println("Problem configuring HelloWorld: " + e);
            e.printStackTrace();
        } catch (InstantiationException e) {
            System.err.println("Problem creating HelloWorld: " + e);
            e.printStackTrace();
        }
    }
}

Yes, I recognise and understand that code. You copied it from the Sphinx-4 Application Programmer's Guide.
So why in the name of all that's holy are you using http:\\www.google.com ?

I am sorry
someone said to me
i have to try a code first on any a valid url then try it on xml file

I'm sorry to bother

I'm sorry but you were given some bad advice there. If it's not the URL of a valid XML config file then, of course, you will get all kinds or errors. Try it with a valid file URL and I'm sure you will make good progress.
J

thank you for your helping
i do what you say
i put the right URL for xml file
but the program can recognize it
and this is the error

Exception in thread "main" java.lang.NullPointerException
	at edu.cmu.sphinx.model.acoustic.WSJ_8gau_13dCep_16k_40mel_130Hz_6800Hz.ModelLoader.loadProperties(ModelLoader.java:372)
	at edu.cmu.sphinx.model.acoustic.WSJ_8gau_13dCep_16k_40mel_130Hz_6800Hz.ModelLoader.getIsBinaryDefault(ModelLoader.java:386)
	at edu.cmu.sphinx.model.acoustic.WSJ_8gau_13dCep_16k_40mel_130Hz_6800Hz.ModelLoader.newProperties(ModelLoader.java:346)
	at edu.cmu.sphinx.util.props.ConfigurationManager.lookup(ConfigurationManager.java:214)
	at edu.cmu.sphinx.util.props.ValidatingPropertySheet.getComponent(ValidatingPropertySheet.java:403)
	at edu.cmu.sphinx.model.acoustic.WSJ_8gau_13dCep_16k_40mel_130Hz_6800Hz.Model.newProperties(Model.java:159)
	at edu.cmu.sphinx.util.props.ConfigurationManager.lookup(ConfigurationManager.java:214)
	at edu.cmu.sphinx.util.props.ValidatingPropertySheet.getComponent(ValidatingPropertySheet.java:403)
	at edu.cmu.sphinx.linguist.flat.FlatLinguist.setupAcousticModel(FlatLinguist.java:299)
	at edu.cmu.sphinx.linguist.flat.FlatLinguist.newProperties(FlatLinguist.java:246)
	at edu.cmu.sphinx.util.props.ConfigurationManager.lookup(ConfigurationManager.java:214)
	at edu.cmu.sphinx.util.props.ValidatingPropertySheet.getComponent(ValidatingPropertySheet.java:403)
	at edu.cmu.sphinx.decoder.search.SimpleBreadthFirstSearchManager.newProperties(SimpleBreadthFirstSearchManager.java:180)
	at edu.cmu.sphinx.util.props.ConfigurationManager.lookup(ConfigurationManager.java:214)
	at edu.cmu.sphinx.util.props.ValidatingPropertySheet.getComponent(ValidatingPropertySheet.java:403)
	at edu.cmu.sphinx.decoder.Decoder.newProperties(Decoder.java:71)
	at edu.cmu.sphinx.util.props.ConfigurationManager.lookup(ConfigurationManager.java:214)
	at edu.cmu.sphinx.util.props.ValidatingPropertySheet.getComponent(ValidatingPropertySheet.java:403)
	at edu.cmu.sphinx.recognizer.Recognizer.newProperties(Recognizer.java:93)
	at edu.cmu.sphinx.util.props.ConfigurationManager.lookup(ConfigurationManager.java:214)
	at HelloWorld.main(HelloWorld.java:34)
Java Result: 1

Unfortunately the sphinx package you are using was written by idiots who have never heard of error messages. However, looking at the class names and method names in that stack trace it does look like a problem with the configuration file - either it can't find it, or it found it but the contents are wrong.
I don't think I'm going to be able to help much more with this - you really need a sphinx expert.

thanks for your help

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.