【发布时间】:2014-04-10 12:49:23
【问题描述】:
我正在尝试使用SpeechRecognitionEngine 构建语音识别程序。我想要实现的功能之一是我可以google 我所说的短语。我应该说:例如“谷歌+我的短语”。但问题是SpeechRecognitionEngine 只能识别我将在Grammar 中添加的单词,所以它无法识别我的短语。我该如何实现这个功能?
【问题讨论】:
标签: c# .net speech-recognition grammar
我正在尝试使用SpeechRecognitionEngine 构建语音识别程序。我想要实现的功能之一是我可以google 我所说的短语。我应该说:例如“谷歌+我的短语”。但问题是SpeechRecognitionEngine 只能识别我将在Grammar 中添加的单词,所以它无法识别我的短语。我该如何实现这个功能?
【问题讨论】:
标签: c# .net speech-recognition grammar
这是可能的,但更难实现。基本上,您将系统置于持续聆听/听写模式,并为每个识别的短语选择要做什么。
找到了您可能也想尝试的答案: C# SAPI - Recognizing phrases without pre-defined condition statements
这是一个简单的例子(这是.net 4.5,WinForms),希望对您有所帮助
using System;
using System.Windows.Forms;
using System.Speech.Recognition;
using System.Speech.Synthesis;
using System.Diagnostics;
using System.Text.RegularExpressions;
public partial class Main : Form
{
private SpeechRecognitionEngine listener;
private SpeechSynthesizer speaker;
public Main()
{
InitializeComponent();
}
private void Main_Load(object sender, EventArgs e)
{
speaker = new SpeechSynthesizer();
speaker.SelectVoice("Microsoft David Desktop");
GrammarBuilder builder = new GrammarBuilder();
builder.AppendDictation();
Grammar grammar = new Grammar(builder);
listener = new SpeechRecognitionEngine();
listener.LoadGrammar(grammar);
listener.SetInputToDefaultAudioDevice();
listener.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(listener_SpeechRecognized);
listener.RecognizeAsync(RecognizeMode.Multiple);
}
void listener_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
string commandName = e.Result.Text;
switch (commandName.ToLower())
{
case "exit":
speaker.Speak("talk to you later, bye");
Application.Exit();
break;
case "stop speaking":
case "stop talking":
case "be quiet":
case "silence":
speaker.SpeakAsyncCancelAll();
break;
case "hello":
speaker.SpeakAsync("hello, how are you doing?");
break;
case "i'm fine":
speaker.SpeakAsync("i am glad to hear that");
break;
case "thank you":
speaker.SpeakAsync("you're welcome");
break;
case "thanks":
speaker.SpeakAsync("no problem");
break;
case "what's the time":
case "what time is it":
case "time":
speaker.SpeakAsync(DateTime.Now.ToShortTimeString());
break;
case "what's the date":
case "what day is it":
case "what is today's date":
case "what is today":
case "today":
speaker.SpeakAsync(DateTime.Today.ToString("dddd, MMMM d, yyyy"));
break;
case "do read it":
Process.Start("http://www.reddit.com");
break;
case "do face book":
Process.Start("http://www.facebook.com");
break;
case "do search":
Process.Start("http://www.google.com");
break;
case "do videos":
Process.Start("http://www.youtube.com");
break;
default:
//handle non-normalized recognition
Match m = Regex.Match(commandName, "YOUR_PATTERN_HERE");
if (m.Success)
{
speaker.SpeakAsync("I found a match");
//example, probably should URL encode the value...
//Process.Start("http://www.google.com?q=" + m.Value);
}
break;
}
}
private void Main_FormClosing(object sender, FormClosingEventArgs e)
{
//be sure to clean up!
listener.UnloadAllGrammars();
listener.Dispose();
listener = null;
speaker.Dispose();
speaker = null;
grammar = null;
}
}
【讨论】:
这在语音技术的当前阶段是不可能的。你能做的最好的就是让语法足够大,这样你会觉得所有的单词都被识别了,但是准确性会大大降低。
【讨论】: