如何在中文Windows 7上使用SAPI5.4识别英语句子? [英] How to recognize English sentences with SAPI5.4 on an Chinese Windows 7?

查看:93
本文介绍了如何在中文Windows 7上使用SAPI5.4识别英语句子?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我使用语法来识别用Choice对象构造的英语句子,我使用中文Windows 7语音识别工作正常。但SpeechRecognitionEngine的对象只能出现SpeechDetectedEventArgs并且不会出现LoadGrammarCompletedEventArgs或者当使用SrgsDocement的对象构造语法的对象时识别完成的事件。目标是我对项目的影响。

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



还有一个名为commongreetingGrammar.grxml的文件构造了SrgsDocement的对象。


And there is the file named commongreetingGrammar.grxml that constructs the object of SrgsDocement .

<?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>

推荐答案

.step =A1; < / tag > < / item >
<! - B1 - >
< item > 我很好。我出城了。我刚回来< / item >
< item > 保持忙。 < / item >
< item > 不太糟糕了,你呢? < / item >
< / one-of >
< / rule >
< / grammar >
.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>


一般情况下,没有办法识别语言。这个问题根本不适合。例如,无论出于何种原因,句子都可以用两种语言书写。如果两种语言都使用拉丁文并且某些语言的拼写相同(但可能含义不同;这种情况很常见),你怎么能把它们整理出来?



但是,特殊情况可能非常简单。例如,如果。你有一些先前存在的知识,即一些文字由用英语或中文写成的句子组成,将它们整理出来不仅是简单的,而且非常简单。这两种语言使用非重叠的Unicode子集。您可以在此处找到不同Unicode脚本的代码点范围:

http://www.unicode。 org / charts / [ ^ ]。



使用此信息,您可以针对某些代码点范围检查每个字符,例如:拉丁语,一般标点符号,数字和 CJK表意文字 http://en.wikipedia.org/wiki/CJK [ ^ ])。



使用此信息,您将能够将混合文本分成不同语言的句子。



-SA
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


谢谢你你的回复。

我担心我没有清楚地表达我的问题。

我尝试使用SpeechRecognitionEngine类识别英语句子,这是o的一部分f SAPI5.4,在中文Windows7上安装了用于Windows的Microsoft语音识别器8.0(简体中文 - PRC)。使用由Choice对象构造的语法对象,加载语法的SpeechRecognitionEngine对象可以识别一些简单的英语句子,例如,你好吗,是,退出。代码碎片如下。



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.

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>



但是,使用由.grxml文件构造的SrgsDocement对象构造的Grammar对象,加载了SpeechRecognitionEngine对象语法无法识别一些简单的英语句子,只能检测到音频输入。代码片段如下所示。


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.

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>





以下是名为commongreetingGrammar.grxml的grxml文件。





And following is the grxml file named commongreetingGrammar.grxml.

<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>


这篇关于如何在中文Windows 7上使用SAPI5.4识别英语句子?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆