我目前正在编写一个语音识别程序,如果我说某个单词,我希望该程序可以执行某些操作。
例如,如果我说的是语法课 greetingg ,我希望语音合成器以“你好”回答。
我知道我可以使用e.Result.Text == "something" || e.Result.Text == "something else"
,但我想指定特定的语法或语法构建或选择列表。
我怎么能这样做?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Speech.Synthesis;
using System.Speech.Recognition;
using System.Threading;
using System.Xml.Linq;
using System.Xml;
using System.IO;
using System.Web;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
List<string> jokelist = new List<string>();
SpeechSynthesizer sfos = new SpeechSynthesizer();
PromptBuilder pBuilder = new PromptBuilder();
SpeechRecognitionEngine sRecognize = new SpeechRecognitionEngine(new System.Globalization.CultureInfo("en-US"));
internal static Choices slist2 = new Choices (new string[] { "hello", "hi", "howdy", "good morning", "hey", "what's up", "wazzup", "good day", "morning", "sup", "yo", "long time no see", "farewell", "take care", "later", "peace" });
internal static GrammarBuilder greetinggb = new GrammarBuilder(slist2);
internal static Grammar greetingg = new Grammar(greetinggb);
public void Form1_Load (object sender, EventArgs e)
{
jokelist.Add("Why was six afraid of seven? Because seven eight nine.");
jokelist.Add("I think you look good, ha-ha-ha-ha");
sRecognize.RequestRecognizerUpdate();
sRecognize.LoadGrammar(greetingg);
sRecognize.SpeechRecognized += sRecognize_SpeechRecognized;
sRecognize.SetInputToDefaultAudioDevice();
sRecognize.RecognizeAsync(RecognizeMode.Multiple);
}
Boolean listening = false;
public void sRecognize_SpeechRecognized (object sender, SpeechRecognizedEventArgs e)
{
SemanticValue semantics = e.Result.Semantics;
RecognitionResult result = e.Result;
if (listening)
{
if (from grammar greetingg)
{
sfos.Speak("hello");
listening = false;
}
else if (e.Result.Text == "tell me a joke")
{
sfos.Speak("Sure.");
sfos.Speak(jokelist[new Random().Next(jokelist.Count)]);
}
else if (e.Result.Confidence <= 0.79)
{
return;
}
else if (e.Result.Text == "sleep")
{
sfos.Speak("Okey then");
listening = false;
return;
}
}
else
{
if (e.Result.Text == "wake up")
{
sfos.Speak("yes master ?");
listening = true;
}
else
return;
}
richTextBox1.Text = richTextBox1.Text + " " + e.Result.Text.ToString();
}
}
}
答案 0 :(得分:2)
RecognitionResult类有一个属性Grammar,语音识别器用它来返回结果。您可以使用此属性来检查使用了哪个Grammar类。
当您查找类似的内容时,MSDN文档是很好的资源,它载有示例。
public void sRecognize_SpeechRecognized (object sender, SpeechRecognizedEventArgs e)
{
SemanticValue semantics = e.Result.Semantics;
RecognitionResult result = e.Result;
if (listening)
{
if (result.Grammar.Equals(greetingg))
{
sfos.Speak("hello");
}
else if (e.Result.Text == "tell me a joke")
{
chip.Speak("Sure.");
chip.Speak(jokelist[new Random().Next(jokelist.Count)]);
}
}
}
我看到的另一个问题是,当您开始收听时,您并未将listening
变量设置为true。
listening = true;
来电后添加sRecognize.RecognizeAsync(RecognizeMode.Multiple)
。