SpeechRecognitionEngine.EmulateRecognizeAsync 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
使用文本代替异步语音识别的音频来模拟输入到语音识别器。
重载
EmulateRecognizeAsync(String) |
使用文本代替异步语音识别的音频来模拟输入到语音识别器。 |
EmulateRecognizeAsync(RecognizedWordUnit[], CompareOptions) |
使用 RecognizedWordUnit 对象的数组代替异步语音识别的音频来模拟特定字输入到语音识别器,并指定识别器如何处理单词和加载的语音识别语法间的 Unicode 比较。 |
EmulateRecognizeAsync(String, CompareOptions) |
使用文本代替异步语音识别的音频来模拟短语输入到语音识别器,并指定识别器如何处理短语和加载的语音识别语法间的 Unicode 比较。 |
注解
这些方法绕过系统音频输入,并将文本作为 String 对象或对象数组 RecognizedWordUnit 提供给识别器。 在测试或调试应用程序或语法时,这非常有用。 例如,可以使用仿真来确定单词是否在语法中,以及识别单词时返回的语义。 使用 方法在 SetInputToNull 仿真操作期间禁用语音识别引擎的音频输入。
语音识别器引发 SpeechDetected、、 SpeechHypothesizedSpeechRecognitionRejected和 SpeechRecognized 事件,就好像未模拟识别操作一样。 当识别器完成异步识别操作时,它将引发 EmulateRecognizeCompleted 事件。 识别器忽略新行和多余的空格,并将标点视为文本输入。
备注
RecognitionResult语音识别器为响应模拟输入null
而生成的 对象的 Audio 属性值为 。
若要模拟同步识别,请使用 EmulateRecognize 方法。
EmulateRecognizeAsync(String)
- Source:
- SpeechRecognitionEngine.cs
- Source:
- SpeechRecognitionEngine.cs
- Source:
- SpeechRecognitionEngine.cs
使用文本代替异步语音识别的音频来模拟输入到语音识别器。
public:
void EmulateRecognizeAsync(System::String ^ inputText);
public void EmulateRecognizeAsync (string inputText);
member this.EmulateRecognizeAsync : string -> unit
Public Sub EmulateRecognizeAsync (inputText As String)
参数
- inputText
- String
标识操作的输入。
例外
识别器没有加载的语音识别语法,或者识别器不是完全标识的异步操作。
inputText
为 null
。
inputText
为空字符串 ("")。
示例
下面的代码示例是控制台应用程序的一部分,该应用程序演示异步模拟输入、关联的识别结果以及语音识别器引发的关联事件。 该示例生成以下输出。
TestRecognizeAsync("Smith")...
SpeechDetected event raised.
SpeechRecognized event raised.
Grammar = Smith; Text = Smith
EmulateRecognizeCompleted event raised.
Grammar = Smith; Text = Smith
Done.
TestRecognizeAsync("Jones")...
SpeechDetected event raised.
SpeechRecognized event raised.
Grammar = Jones; Text = Jones
EmulateRecognizeCompleted event raised.
Grammar = Jones; Text = Jones
Done.
TestRecognizeAsync("Mister")...
SpeechDetected event raised.
SpeechHypothesized event raised.
Grammar = Smith; Text = mister
SpeechRecognitionRejected event raised.
Grammar = <not available>; Text =
EmulateRecognizeCompleted event raised.
No recognition result available.
Done.
TestRecognizeAsync("Mister Smith")...
SpeechDetected event raised.
SpeechRecognized event raised.
Grammar = Smith; Text = mister Smith
EmulateRecognizeCompleted event raised.
Grammar = Smith; Text = mister Smith
Done.
press any key to exit...
using System;
using System.Globalization;
using System.Speech.Recognition;
using System.Threading;
namespace SreEmulateRecognizeAsync
{
class Program
{
// Indicate when an asynchronous operation is finished.
static bool completed;
static void Main(string[] args)
{
using (SpeechRecognitionEngine recognizer =
new SpeechRecognitionEngine(new CultureInfo("en-US")))
{
// Load grammars.
recognizer.LoadGrammar(CreateNameGrammar("Smith"));
recognizer.LoadGrammar(CreateNameGrammar("Jones"));
// Configure the audio input.
recognizer.SetInputToNull();
// Add event handlers for the events raised by the
// EmulateRecognizeAsync 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);
recognizer.EmulateRecognizeCompleted +=
new EventHandler<EmulateRecognizeCompletedEventArgs>(
EmulateRecognizeCompletedHandler);
// Start four asynchronous emulated recognition operations.
TestRecognizeAsync(recognizer, "Smith");
TestRecognizeAsync(recognizer, "Jones");
TestRecognizeAsync(recognizer, "Mister");
TestRecognizeAsync(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 asynchronous
// recognition.
private static void TestRecognizeAsync(
SpeechRecognitionEngine recognizer, string input)
{
completed = false;
Console.WriteLine("TestRecognizeAsync(\"{0}\")...", input);
recognizer.EmulateRecognizeAsync(input);
// Wait for the operation to complete.
while (!completed)
{
Thread.Sleep(333);
}
Console.WriteLine(" Done.");
Console.WriteLine();
}
static void SpeechDetectedHandler(
object sender, SpeechDetectedEventArgs e)
{
Console.WriteLine(" SpeechDetected event raised.");
}
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.");
}
}
// Handle events.
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.");
}
}
static void EmulateRecognizeCompletedHandler(
object sender, EmulateRecognizeCompletedEventArgs e)
{
Console.WriteLine(" EmulateRecognizeCompleted event raised.");
if (e.Error != null)
{
Console.WriteLine(" {0} exception encountered: {1}:",
e.Error.GetType().Name, e.Error.Message);
}
else if (e.Cancelled)
{
Console.WriteLine(" Operation cancelled.");
}
else 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.");
}
completed = true;
}
}
}
注解
语音识别器引发 SpeechDetected、、 SpeechHypothesizedSpeechRecognitionRejected和 SpeechRecognized 事件,就好像未模拟识别操作一样。 当识别器完成异步识别操作时,它将引发 EmulateRecognizeCompleted 事件。
Vista 和 Windows 7 随附的识别器在向输入短语应用语法规则时忽略大小写和字符宽度。 有关此比较类型的详细信息,请参阅 CompareOptions 枚举值 OrdinalIgnoreCase 和 IgnoreWidth。 识别器还会忽略新行和额外的空格,并将标点符号视为文本输入。
此方法将存储在任务中,它返回该方法的同步对应项可能引发的所有非使用异常。 如果异常存储在返回的任务中,则在等待任务时将引发该异常。 使用异常(如 ArgumentException)仍会同步引发。 有关存储的异常,请参阅 引发的 EmulateRecognize(String)异常。
另请参阅
- EmulateRecognize(RecognizedWordUnit[], CompareOptions)
- RecognizeAsync()
- SpeechDetected
- SpeechHypothesized
- SpeechRecognitionRejected
- SpeechRecognized
- EmulateRecognizeCompleted
适用于
EmulateRecognizeAsync(RecognizedWordUnit[], CompareOptions)
- Source:
- SpeechRecognitionEngine.cs
- Source:
- SpeechRecognitionEngine.cs
- Source:
- SpeechRecognitionEngine.cs
使用 RecognizedWordUnit 对象的数组代替异步语音识别的音频来模拟特定字输入到语音识别器,并指定识别器如何处理单词和加载的语音识别语法间的 Unicode 比较。
public:
void EmulateRecognizeAsync(cli::array <System::Speech::Recognition::RecognizedWordUnit ^> ^ wordUnits, System::Globalization::CompareOptions compareOptions);
public void EmulateRecognizeAsync (System.Speech.Recognition.RecognizedWordUnit[] wordUnits, System.Globalization.CompareOptions compareOptions);
member this.EmulateRecognizeAsync : System.Speech.Recognition.RecognizedWordUnit[] * System.Globalization.CompareOptions -> unit
Public Sub EmulateRecognizeAsync (wordUnits As RecognizedWordUnit(), compareOptions As CompareOptions)
参数
- wordUnits
- RecognizedWordUnit[]
词单元数组,包含识别操作输入。
- compareOptions
- CompareOptions
描述用于模拟识别操作的比较说明类型的枚举值的按位组合。
例外
识别器没有加载的语音识别语法,或者识别器不是完全标识的异步操作。
wordUnits
为 null
。
wordUnits
包含一个或多个 null
元素。
compareOptions
包含值 IgnoreNonSpace, IgnoreSymbols,或 StringSort 标志。
注解
语音识别器引发 SpeechDetected、、 SpeechHypothesizedSpeechRecognitionRejected和 SpeechRecognized 事件,就好像未模拟识别操作一样。 当识别器完成异步识别操作时,它将引发 EmulateRecognizeCompleted 事件。
识别器在将语法规则应用于输入短语时使用 compareOptions
。 如果 存在 或 IgnoreCase 值,则随 Vista 和 Windows 7 一起附带的OrdinalIgnoreCase识别器将忽略大小写。 识别器始终忽略字符宽度,从不忽略假名类型。 识别器还会忽略新行和额外的空格,并将标点符号视为文本输入。 有关字符宽度和假名类型的详细信息,请参阅 CompareOptions 枚举。
此方法将存储在任务中,它返回该方法的同步对应项可能引发的所有非使用异常。 如果异常存储在返回的任务中,则在等待任务时将引发该异常。 使用异常(如 ArgumentException)仍会同步引发。 有关存储的异常,请参阅 引发的 EmulateRecognize(RecognizedWordUnit[], CompareOptions)异常。
另请参阅
- EmulateRecognize(RecognizedWordUnit[], CompareOptions)
- RecognizeAsync()
- SpeechDetected
- SpeechHypothesized
- SpeechRecognitionRejected
- SpeechRecognized
- EmulateRecognizeCompleted
适用于
EmulateRecognizeAsync(String, CompareOptions)
- Source:
- SpeechRecognitionEngine.cs
- Source:
- SpeechRecognitionEngine.cs
- Source:
- SpeechRecognitionEngine.cs
使用文本代替异步语音识别的音频来模拟短语输入到语音识别器,并指定识别器如何处理短语和加载的语音识别语法间的 Unicode 比较。
public:
void EmulateRecognizeAsync(System::String ^ inputText, System::Globalization::CompareOptions compareOptions);
public void EmulateRecognizeAsync (string inputText, System.Globalization.CompareOptions compareOptions);
member this.EmulateRecognizeAsync : string * System.Globalization.CompareOptions -> unit
Public Sub EmulateRecognizeAsync (inputText As String, compareOptions As CompareOptions)
参数
- inputText
- String
标识操作的输入相。
- compareOptions
- CompareOptions
描述用于模拟识别操作的比较说明类型的枚举值的按位组合。
例外
识别器没有加载的语音识别语法,或者识别器不是完全标识的异步操作。
inputText
为 null
。
inputText
为空字符串 ("")。
compareOptions
包含值 IgnoreNonSpace, IgnoreSymbols,或 StringSort 标志。
注解
语音识别器引发 SpeechDetected、、 SpeechHypothesizedSpeechRecognitionRejected和 SpeechRecognized 事件,就好像未模拟识别操作一样。 当识别器完成异步识别操作时,它将引发 EmulateRecognizeCompleted 事件。
识别器在将语法规则应用于输入短语时使用 compareOptions
。 如果 存在 或 IgnoreCase 值,则随 Vista 和 Windows 7 一起附带的OrdinalIgnoreCase识别器将忽略大小写。 识别器始终忽略字符宽度,从不忽略假名类型。 识别器还会忽略新行和额外的空格,并将标点符号视为文本输入。 有关字符宽度和假名类型的详细信息,请参阅 CompareOptions 枚举。
此方法将存储在任务中,它返回该方法的同步对应项可能引发的所有非使用异常。 如果异常存储在返回的任务中,则在等待任务时将引发该异常。 使用异常(如 ArgumentException)仍会同步引发。 有关存储的异常,请参阅 引发的 EmulateRecognize(String, CompareOptions)异常。
另请参阅
- EmulateRecognize(RecognizedWordUnit[], CompareOptions)
- RecognizeAsync()
- SpeechDetected
- SpeechHypothesized
- SpeechRecognitionRejected
- SpeechRecognized
- EmulateRecognizeCompleted