SpeechRecognitionEngine.EmulateRecognize 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
使用文本代替同步语音识别的音频来模拟输入到语音识别器。
重载
EmulateRecognize(String) |
使用文本代替同步语音识别的音频来模拟输入短语到语音识别器。 |
EmulateRecognize(RecognizedWordUnit[], CompareOptions) |
使用文本代替同步语音识别的音频来模拟特定字输入到语音识别器,并指定识别器如何处理单词和加载的语音识别语法间的 Unicode 比较。 |
EmulateRecognize(String, CompareOptions) |
使用文本代替异步语音识别的音频来模拟短语输入到语音识别器,并指定识别器如何处理短语和加载的语音识别语法间的 Unicode 比较。 |
注解
这些方法绕过系统音频输入,并将文本作为 String 对象或对象数组 RecognizedWordUnit 提供给识别器。 在测试或调试应用程序或语法时,这非常有用。 例如,可以使用仿真来确定单词是否在语法中,以及识别单词时返回的语义。 使用 方法在 SetInputToNull 仿真操作期间禁用语音识别引擎的音频输入。
语音识别器引发 SpeechDetected、、 SpeechHypothesizedSpeechRecognitionRejected和 SpeechRecognized 事件,就好像未模拟识别操作一样。 识别器忽略新行和多余的空格,并将标点视为文本输入。
注意
RecognitionResult语音识别器为响应模拟输入null
而生成的 对象的 Audio 属性值为 。
若要模拟异步识别,请使用 EmulateRecognizeAsync 方法。
EmulateRecognize(String)
- Source:
- SpeechRecognitionEngine.cs
- Source:
- SpeechRecognitionEngine.cs
- Source:
- SpeechRecognitionEngine.cs
使用文本代替同步语音识别的音频来模拟输入短语到语音识别器。
public:
System::Speech::Recognition::RecognitionResult ^ EmulateRecognize(System::String ^ inputText);
public System.Speech.Recognition.RecognitionResult EmulateRecognize (string inputText);
member this.EmulateRecognize : string -> System.Speech.Recognition.RecognitionResult
Public Function EmulateRecognize (inputText As String) As RecognitionResult
参数
- inputText
- String
标识操作的输入。
返回
输入的标识结果或 null
,如果操作不成功或识别器未启用。
例外
识别器没有加载的语音识别语法。
inputText
为 null
。
inputText
为空字符串 ("")。
示例
下面的代码示例是控制台应用程序的一部分,该应用程序演示了模拟输入、关联的识别结果以及语音识别器引发的关联事件。 该示例生成以下输出。
TestRecognize("Smith")...
SpeechDetected event raised.
SpeechRecognized event raised.
Grammar = Smith; Text = Smith
...Recognition result text = Smith
TestRecognize("Jones")...
SpeechDetected event raised.
SpeechRecognized event raised.
Grammar = Jones; Text = Jones
...Recognition result text = Jones
TestRecognize("Mister")...
SpeechDetected event raised.
SpeechHypothesized event raised.
Grammar = Smith; Text = mister
SpeechRecognitionRejected event raised.
Grammar = <not available>; Text =
...No recognition result.
TestRecognize("Mister Smith")...
SpeechDetected event raised.
SpeechRecognized event raised.
Grammar = Smith; Text = mister Smith
...Recognition result text = mister Smith
press any key to exit...
using System;
using System.Globalization;
using System.Speech.Recognition;
namespace Sre_EmulateRecognize
{
class Program
{
static void Main(string[] args)
{
// Create an in-process speech recognizer for the en-US locale.
using (SpeechRecognitionEngine recognizer =
new SpeechRecognitionEngine(new CultureInfo("en-US")))
{
// Load grammars.
recognizer.LoadGrammar(CreateNameGrammar("Smith"));
recognizer.LoadGrammar(CreateNameGrammar("Jones"));
// Disable audio input to the recognizer.
recognizer.SetInputToNull();
// Add handlers for events raised by the EmulateRecognize method.
recognizer.SpeechDetected +=
new EventHandler<SpeechDetectedEventArgs>(
SpeechDetectedHandler);
recognizer.SpeechHypothesized +=
new EventHandler<SpeechHypothesizedEventArgs>(
SpeechHypothesizedHandler);
recognizer.SpeechRecognitionRejected +=
new EventHandler<SpeechRecognitionRejectedEventArgs>(
SpeechRecognitionRejectedHandler);
recognizer.SpeechRecognized +=
new EventHandler<SpeechRecognizedEventArgs>(
SpeechRecognizedHandler);
// Start four synchronous emulated recognition operations.
TestRecognize(recognizer, "Smith");
TestRecognize(recognizer, "Jones");
TestRecognize(recognizer, "Mister");
TestRecognize(recognizer, "Mister Smith");
}
Console.WriteLine("press any key to exit...");
Console.ReadKey(true);
}
// Create a simple name grammar.
// Set the grammar name to the surname.
private static Grammar CreateNameGrammar(string surname)
{
GrammarBuilder builder = new GrammarBuilder("mister", 0, 1);
builder.Append(surname);
Grammar nameGrammar = new Grammar(builder);
nameGrammar.Name = surname;
return nameGrammar;
}
// Send emulated input to the recognizer for synchronous recognition.
private static void TestRecognize(
SpeechRecognitionEngine recognizer, string input)
{
Console.WriteLine("TestRecognize(\"{0}\")...", input);
RecognitionResult result =
recognizer.EmulateRecognize(input,CompareOptions.IgnoreCase);
if (result != null)
{
Console.WriteLine("...Recognition result text = {0}",
result.Text ?? "<null>");
}
else
{
Console.WriteLine("...No recognition result.");
}
Console.WriteLine();
}
static void SpeechDetectedHandler(
object sender, SpeechDetectedEventArgs e)
{
Console.WriteLine(" SpeechDetected event raised.");
}
// Handle events.
static void SpeechHypothesizedHandler(
object sender, SpeechHypothesizedEventArgs e)
{
Console.WriteLine(" SpeechHypothesized event raised.");
if (e.Result != null)
{
Console.WriteLine(" Grammar = {0}; Text = {1}",
e.Result.Grammar.Name ?? "<none>", e.Result.Text);
}
else
{
Console.WriteLine(" No recognition result available.");
}
}
static void SpeechRecognitionRejectedHandler(
object sender, SpeechRecognitionRejectedEventArgs e)
{
Console.WriteLine(" SpeechRecognitionRejected event raised.");
if (e.Result != null)
{
string grammarName;
if (e.Result.Grammar != null)
{
grammarName = e.Result.Grammar.Name ?? "<none>";
}
else
{
grammarName = "<not available>";
}
Console.WriteLine(" Grammar = {0}; Text = {1}",
grammarName, e.Result.Text);
}
else
{
Console.WriteLine(" No recognition result available.");
}
}
static void SpeechRecognizedHandler(
object sender, SpeechRecognizedEventArgs e)
{
Console.WriteLine(" SpeechRecognized event raised.");
if (e.Result != null)
{
Console.WriteLine(" Grammar = {0}; Text = {1}",
e.Result.Grammar.Name ?? "<none>", e.Result.Text);
}
else
{
Console.WriteLine(" No recognition result available.");
}
}
}
}
注解
语音识别器引发 SpeechDetected、、 SpeechHypothesizedSpeechRecognitionRejected和 SpeechRecognized 事件,就好像未模拟识别操作一样。
Vista 和 Windows 7 随附的识别器在向输入短语应用语法规则时忽略大小写和字符宽度。 有关此比较类型的详细信息,请参阅 CompareOptions 枚举值 OrdinalIgnoreCase 和 IgnoreWidth。 识别器还会忽略新行和额外的空格,并将标点符号视为文本输入。
另请参阅
- EmulateRecognizeAsync(String)
- Recognize()
- SpeechDetected
- SpeechHypothesized
- SpeechRecognitionRejected
- SpeechRecognized
适用于
EmulateRecognize(RecognizedWordUnit[], CompareOptions)
- Source:
- SpeechRecognitionEngine.cs
- Source:
- SpeechRecognitionEngine.cs
- Source:
- SpeechRecognitionEngine.cs
使用文本代替同步语音识别的音频来模拟特定字输入到语音识别器,并指定识别器如何处理单词和加载的语音识别语法间的 Unicode 比较。
public:
System::Speech::Recognition::RecognitionResult ^ EmulateRecognize(cli::array <System::Speech::Recognition::RecognizedWordUnit ^> ^ wordUnits, System::Globalization::CompareOptions compareOptions);
public System.Speech.Recognition.RecognitionResult EmulateRecognize (System.Speech.Recognition.RecognizedWordUnit[] wordUnits, System.Globalization.CompareOptions compareOptions);
member this.EmulateRecognize : System.Speech.Recognition.RecognizedWordUnit[] * System.Globalization.CompareOptions -> System.Speech.Recognition.RecognitionResult
Public Function EmulateRecognize (wordUnits As RecognizedWordUnit(), compareOptions As CompareOptions) As RecognitionResult
参数
- wordUnits
- RecognizedWordUnit[]
词单元数组,包含识别操作输入。
- compareOptions
- CompareOptions
描述用于模拟识别操作的比较说明类型的枚举值的按位组合。
返回
输入的标识结果或 null
,如果操作不成功或识别器未启用。
例外
识别器没有加载的语音识别语法。
wordUnits
为 null
。
wordUnits
包含一个或多个 null
元素。
compareOptions
包含值 IgnoreNonSpace, IgnoreSymbols,或 StringSort 标志。
注解
语音识别器引发 SpeechDetected、、 SpeechHypothesizedSpeechRecognitionRejected和 SpeechRecognized 事件,就好像未模拟识别操作一样。
识别器在将语法规则应用于输入短语时使用 compareOptions
。 如果 存在 或 IgnoreCase 值,则随 Vista 和 Windows 7 一起附带的OrdinalIgnoreCase识别器将忽略大小写。 识别器始终忽略字符宽度,并且从不忽略假名类型。 识别器还会忽略新行和额外的空格,并将标点视为文本输入。 有关字符宽度和假名类型的详细信息,请参阅 CompareOptions 枚举。
另请参阅
- EmulateRecognizeAsync(String)
- Recognize()
- SpeechDetected
- SpeechHypothesized
- SpeechRecognitionRejected
- SpeechRecognized
适用于
EmulateRecognize(String, CompareOptions)
- Source:
- SpeechRecognitionEngine.cs
- Source:
- SpeechRecognitionEngine.cs
- Source:
- SpeechRecognitionEngine.cs
使用文本代替异步语音识别的音频来模拟短语输入到语音识别器,并指定识别器如何处理短语和加载的语音识别语法间的 Unicode 比较。
public:
System::Speech::Recognition::RecognitionResult ^ EmulateRecognize(System::String ^ inputText, System::Globalization::CompareOptions compareOptions);
public System.Speech.Recognition.RecognitionResult EmulateRecognize (string inputText, System.Globalization.CompareOptions compareOptions);
member this.EmulateRecognize : string * System.Globalization.CompareOptions -> System.Speech.Recognition.RecognitionResult
Public Function EmulateRecognize (inputText As String, compareOptions As CompareOptions) As RecognitionResult
参数
- inputText
- String
标识操作的输入相。
- compareOptions
- CompareOptions
描述用于模拟识别操作的比较说明类型的枚举值的按位组合。
返回
输入的标识结果或 null
,如果操作不成功或识别器未启用。
例外
识别器没有加载的语音识别语法。
inputText
为 null
。
inputText
为空字符串 ("")。
compareOptions
包含值 IgnoreNonSpace, IgnoreSymbols,或 StringSort 标志。
注解
语音识别器引发 SpeechDetected、、 SpeechHypothesizedSpeechRecognitionRejected和 SpeechRecognized 事件,就好像未模拟识别操作一样。
识别器在将语法规则应用于输入短语时使用 compareOptions
。 如果 存在 或 IgnoreCase 值,则随 Vista 和 Windows 7 一起附带的OrdinalIgnoreCase识别器将忽略大小写。 识别器始终忽略字符宽度,并且从不忽略假名类型。 识别器还会忽略新行和额外的空格,并将标点视为文本输入。 有关字符宽度和假名类型的详细信息,请参阅 CompareOptions 枚举。
另请参阅
- EmulateRecognizeAsync(String)
- Recognize()
- SpeechDetected
- SpeechHypothesized
- SpeechRecognitionRejected
- SpeechRecognized