Click here to Skip to main content
15,891,375 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
I am using an Chinese Windows 7 with speech recognition working fine if I use the grammar to recognize English sentences which is constructed with the object of Choice.But the object of SpeechRecognitionEngine only can arise SpeechDetectedEventArgs and doesn't arise LoadGrammarCompletedEventArgs or RecognizeCompletedEventArgs when the the object of Grammar is constructed with the object of SrgsDocement.There is my fragement of the project.
SpeechRecognitionEngine recognizer = new SpeechRecognitionEngine ( System.Globalization.CultureInfo.CreateSpecificCulture("zh-CN"));
               SrgsDocument srgsdoc = new SrgsDocument("./commongreetingGrammar.grxml");
               recognizer.MaxAlternates = 5;
               recognizer.LoadGrammarCompleted += new EventHandler<LoadGrammarCompletedEventArgs>(recognizer_LoadGrammarCompleted);
               recognizer.SpeechDetected += new EventHandler<SpeechDetectedEventArgs>(recognizer_SpeechDetected);
               recognizer.RecognizeCompleted += new EventHandler<RecognizeCompletedEventArgs>(recognizer_RecognizeCompleted);
               recognizer.LoadGrammar(new Grammar(srgsdoc));

               recognizer.SetInputToDefaultAudioDevice();
               recognizer.RecognizeAsync (RecognizeMode .Multiple);
           }
           catch (Exception ex)
           { Console.WriteLine(ex.Message); }
           Console.ReadKey();
       }

       static void recognizer_SpeechDetected(object sender, SpeechDetectedEventArgs e)
       {
           Console.WriteLine("Detect that someone is speeching");
       }

       static void recognizer_LoadGrammarCompleted(object sender, LoadGrammarCompletedEventArgs e)
       {
           if (e.Error == null)
               Console.WriteLine("complete to load grammar ");
           else
               Console.WriteLine("Fail to load grammar");
       }

       static void recognizer_RecognizeCompleted(object sender, RecognizeCompletedEventArgs e)
       {
           if (e.Result.Semantics["step"].Value.ToString() == "A1")
           {
               Console.WriteLine("A start to speak:{0}", e.Result.Text);
           }
       }

And there is the file named commongreetingGrammar.grxml that constructs the object of SrgsDocement .
XML
<?xml version="1.0" encoding="UTF-8"?>
<grammar version="1.0" xml:lang="zh-CN" mode="voice" root="commongreeting"
	xmlns="http://www.w3.org/2001/06/grammar" tag-format="semantics-ms/1.0">
	<rule id="commongreeting">
		<one-of>
			<!--A1-->
			<item>Hi,Jack,How's it going?<tag> $.step="A1";</tag></item>
			<!--B1-->
			<item>I’m fine. I’ve been out of town. I just got back. </item>
			<item>Keeping busy. </item>
			<item>Not too bad,and you? </item>		
		</one-of>
	</rule>
</grammar>
Posted
Updated 8-Mar-13 7:53am
v2

In general case, there is no a way of to recognize the language. The problem is simply ill-posed. For example, a sentence can be written in two languages, by whatever reason. If both languages use Latin script and some words of the languages have identical spelling (but may be different meaning; and this situation is very usual), how can you sort them out?

However, special cases can be quite easy. If, for example. you have preexisting knowledge that some text is composed of sentences written in ether English or Chinese, sorting them out will be not just simple, but very simple. These two languages use non-overlapping Unicode subsets. You can find out the ranges of code points for different Unicode scripts here:
http://www.unicode.org/charts/[^].

Using this information, you can check each character against some ranges of code points, such as: Latin, general punctuation, digits and CJK ideographs (http://en.wikipedia.org/wiki/CJK[^]).

Using this information, you will be able to split the mixed text into sentences written in different languages.

—SA
 
Share this answer
 
v2
Comments
Maciej Los 8-Mar-13 15:35pm    
Interesting hint, +5!
Sergey Alexandrovich Kryukov 8-Mar-13 15:44pm    
Thank you, Maciej.
—SA
rong_wang 9-Mar-13 23:12pm    
Thanks for Sergey Alexandrovich's reply.
I’m afraid that I didn’t present my problem clearly.Luckliy,I find the solution to the problem today and posted it.
Sergey Alexandrovich Kryukov 10-Mar-13 11:50am    
Certainly. Good luck.
—SA
Thank you for your reply.
I’m afraid that I didn’t present my problem clearly.
I try to recognize English sentences using the SpeechRecognitionEngine class ,which is part of SAPI5.4,on an Chinese Windows7 which has installed the Microsoft Speech Recognizer 8.0 for Windows (Chinese Simplified - PRC).Using the Grammar object constructed with the Choice object ,the SpeechRecognitionEngine object loaded the grammar can recognize some of simple English sentences,for example,”How are you ”,”yes”,”quit”.The fragments of code as followed.

C#
private static SpeechRecognizer  recognizer;
        static void Main(string[] args)
        {
            recognizer = new SpeechRecognizer();
            recognizer.LoadGrammarCompleted += new EventHandler<loadgrammarcompletedeventargs>(recognizer_LoadGrammarCompleted);
            recognizer.SpeechRecognized += new EventHandler<speechrecognizedeventargs>(recognizer_SpeechRecognized);
            recognizer.StateChanged += new EventHandler<statechangedeventargs>(recognizer_StateChanged);

            Choices yesChoices = new Choices(new string[] { "how are you", "yah", "yup" });
            SemanticResultValue yesValue = new SemanticResultValue(yesChoices, true);

            Choices noChoices = new Choices(new string[] { "no", "nope", "nah" });
            SemanticResultValue noValue = new SemanticResultValue(noChoices, false); ;

            SemanticResultKey yesnoKey = new SemanticResultKey("yesno", new Choices(new GrammarBuilder[] { yesValue, noValue }));
            Grammar yesnoGrammar = new Grammar(yesnoKey);
            yesnoGrammar.Name = "yesno";

            Grammar doneGrammar = new Grammar(new GrammarBuilder(new Choices(new string[] { "done", "eixt", "quit", "stop" })));
            doneGrammar.Name = "done";
            recognizer.LoadGrammarAsync(yesnoGrammar);
            recognizer.LoadGrammarAsync(doneGrammar);
            Console.ReadLine();
        }
    // Handle the StateChanged event.
        static void recognizer_StateChanged(object sender, StateChangedEventArgs e)
        {
            try
            {
                if (e.RecognizerState != RecognizerState.Stopped)
                {
                    recognizer.EmulateRecognizeAsync("Start Listening");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }
    // Handle the LoadGrammarCompleted event.
        static void recognizer_LoadGrammarCompleted(object sender, LoadGrammarCompletedEventArgs e)
        {
            try
            {
                string grammarName = e.Grammar.Name;
                bool grammarLoaded = e.Grammar.Loaded;
                if (e.Error != null)
                {
                    Console.WriteLine("load grammar for{0] failed with {1}", grammarName, e.Error.GetType().Name);

                }
                Console.WriteLine("Grammar {0} {1} loaded", grammarName, (grammarLoaded) ? "is" : "is'nt");
            }

            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }

        // Handle the SpeechRecognized event.
        static void recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
        {
            Console.WriteLine("Grammar({0}): {1}", e.Result.Grammar.Name, e.Result.Text);
 
            Console.WriteLine("Speech recognized: " + e.Result.Text);
            Console.WriteLine();
            Console.WriteLine("Semantic results:{0},alternates:{1}",e.Result.Text ,e.Result.Alternates.ToString());

            // The following code illustrates some of the information available
            // in the recognition result.
            Console.WriteLine("Grammar({0}), {1}: {2}",
              e.Result.Grammar.Name, e.Result.Audio.Duration, e.Result.Text);

            // Display the semantic values in the recognition result.
            foreach (KeyValuePair<string,> child in e.Result.Semantics)
            {
                Console.WriteLine(" {0} key: {1}",
                  child.Key, child.Value.Value ?? "null");
            }
            Console.WriteLine();

            // Display information about the words in the recognition result.
            foreach (RecognizedWordUnit word in e.Result.Words)
            {
                RecognizedAudio audio = e.Result.GetAudioForWordRange(word, word);
                Console.WriteLine(" {0,-10} {1,-10} {2,-10} {3} ({4})",
                  word.Text, word.LexicalForm, word.Pronunciation,
                  audio.Duration, word.DisplayAttributes);
            }

            // Display the recognition alternates for the result.
            foreach (RecognizedPhrase phrase in e.Result.Alternates)
            {
                Console.WriteLine(" alt({0}) {1}", phrase.Confidence, phrase.Text);
            }

        }
</statechangedeventargs></speechrecognizedeventargs></loadgrammarcompletedeventargs>

However, using the Grammar object constructed with the SrgsDocement object which is constructed with .grxml file,the SpeechRecognitionEngine object loaded the grammar can’t recognize some of simple English sentences and only can detect audioinput.The fragments of code as follewed.
C#
SpeechRecognitionEngine recognizer = new SpeechRecognitionEngine ( System.Globalization.CultureInfo.CreateSpecificCulture("zh-CN"));
                SrgsDocument srgsdoc = new SrgsDocument("./commongreetingGrammar.grxml");
                recognizer.MaxAlternates = 5;
                recognizer.LoadGrammarCompleted += new EventHandler<loadgrammarcompletedeventargs>(recognizer_LoadGrammarCompleted);
                recognizer.SpeechDetected += new EventHandler<speechdetectedeventargs>(recognizer_SpeechDetected);
                recognizer.RecognizeCompleted += new EventHandler<recognizecompletedeventargs>(recognizer_RecognizeCompleted);
                recognizer.LoadGrammar(new Grammar(srgsdoc));
               
                recognizer.SetInputToDefaultAudioDevice();
                recognizer.RecognizeAsync (RecognizeMode .Multiple);
            }
            catch (Exception ex)
            { Console.WriteLine(ex.Message); }
            Console.ReadKey();
        }
 
        static void recognizer_SpeechDetected(object sender, SpeechDetectedEventArgs e)
        {
            Console.WriteLine("Detect that someone is speeching");
        }
 
        static void recognizer_LoadGrammarCompleted(object sender, LoadGrammarCompletedEventArgs e)
        {
            if (e.Error == null)
                Console.WriteLine("complete to load grammar ");
            else
                Console.WriteLine("Fail to load grammar");
        }
 
        static void recognizer_RecognizeCompleted(object sender, RecognizeCompletedEventArgs e)
        {
            if (e.Result.Semantics["step"].Value.ToString() == "A1")
            {
                Console.WriteLine("A start to speak:{0}", e.Result.Text);
            }
        }</recognizecompletedeventargs></speechdetectedeventargs></loadgrammarcompletedeventargs>


And following is the grxml file named commongreetingGrammar.grxml.

XML
<grammar version="1.0" xml:lang="zh-CN" mode="voice" root="commongreeting">
	xmlns="http://www.w3.org/2001/06/grammar" tag-format="semantics-ms/1.0">
	<rule id="commongreeting">
		<one-of>
			<!--A1-->
			<item>Hi,Jack,How's it going?<tag> $.step="A1";</tag></item>
			<!--B1-->
			<item>I’m fine. I’ve been out of town. I just got back. </item>
			<item>Keeping busy. </item>
			<item>Not too bad,and you? </item>		
		</one-of>
	</rule>
</grammar>

Luckliy,I find the solution to the problem today.
The problem is that I didn’t install the English language pack and constructed the Grammar object wrongly,which cause the SpeechRecognitionEngine object to fail to recognize the English sentences.The details of the solution as followed.
1. Dowloading the Vistalizator and English language pack
We can visited the website: http://www.froggie.sk/ to download.
2. Install English language pack with Vistalizator
Run Vistalizator as administror,then click [Add Language] to select the English language pack,after loading Windows Language Pack,select Internal Installation mode to install language.If install language fail in Internal Installation mode,then choose install language in Express Installation mode.

3. Change the Settings of your computer
Modify the keyboard layout, location standards and formats: Control Panel - Clock, Language, and Regional.
Click this link, http://www.siqiboke.com/post/153.html you can get the detail how to install language pack.
4. The fragement of code as followed.
C#
static void Main(string[] args)
{
  //The SpeechRecognizer instance, recognizer, is declared outside this method.
  recognizer = new SpeechRecognizer();
  string grammarPath = @"C:\test\";
  
  FileStream fs = new FileStream(grammarPath + "AirportCodes.cfg", FileMode.Create);
  SrgsGrammarCompiler.Compile(grammarPath + "AirportCodes.grxml", (Stream)fs);
  fs.Close();
  Grammar gr = new Grammar(grammarPath + "AirportCodes.cfg", "flightBooker");
  
  gr.Name = "Flight Chooser";
  recognizer.LoadGrammarAsync(gr);

  // Configure recognizer input.                
  recognizer.SetInputToDefaultAudioDevice();

  // Attach a handler for the SpeechRecognized event.
  recognizer.SpeechRecognized +=new EventHandler<speechrecognizedeventargs>(recognizer_SpeechRecognized);

  // Start recognition.
  recognizer.RecognizeAsync(RecognizeMode.Multiple);
  Console.WriteLine("Starting asynchronous recognition...");

  // Keep the console window open.
  Console.ReadLine();
}
// Write to the console the text and the semantics from the recognition result.
static void recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
        {
            Console.WriteLine("Speech recognized: " + e.Result.Text);
            Console.WriteLine();
            Console.WriteLine("Semantic results:");
            Console.WriteLine("  The departure city is: " + e.Result.Semantics["LeavingFrom"].Value);
            Console.WriteLine("  The arrival city is: " + e.Result.Semantics["GoingTo"].Value);
        }</speechrecognizedeventargs>


The existing grxml file as followed.
XML
<grammar xml:lang="en-US" root="flightBooker">
tag-format="semantics/1.0" version="1.0" 
xmlns="http://www.w3.org/2001/06/grammar">

  <rule id="flightBooker" scope="public">
    <item> I want to fly from </item>
    <ruleref uri="#flightCities" />
    <tag> out.LeavingFrom=rules.latest(); </tag>
    <item> to </item>
    <ruleref uri="#flightCities" />
    <tag> out.GoingTo=rules.latest(); </tag>
  </rule>

  <rule id="flightCities" scope="private">
    <one-of>
      <item> Chicago <tag> out="ORD"; </tag></item>
      <item> Boston <tag> out="BOS"; </tag></item>
      <item> Miami <tag> out="MIA"; </tag></item>
      <item> Dallas <tag> out="DFW"; </tag></item>
    </one-of>
  </rule>

</grammar>

In this case ,I can create the grammar with grxml file conforming to Speech Recognition Grammar Specification 1.0 http://www.w3.org/TR/speech-grammar/,then I can use the Microsoft English Recognizer v8.0 to recognizer English sentences.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900