【发布时间】:2015-04-10 07:45:25
【问题描述】:
例如,目前我对如何说出一个句子/命令有很多不同的变体。如果我想要明天的日期,我可以说“明天的日期是几号?”因为我在我的应用程序中将它作为语法记录下来。但是,我不能说“明天是几号?”或“明天的日期是什么时候?”因为我的语法中没有它们。有没有办法检查口头命令是否包含某些“关键字”,如“明天”和“日期”。这会有所帮助,因为您可以说任何话,只要识别引擎听到关键字,它就会执行命令。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Speech.Recognition;
namespace Test
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
SpeechRecognitionEngine recoEngine = new SpeechRecognitionEngine();
Choices generalCommands = new Choices();
generalCommands.Add(new string[] { "what is tomorrow's date", "whats tomorrows date", "what will tomorrows date be" });
GrammarBuilder gBuilder = new GrammarBuilder();
gBuilder.Append(generalCommands);
Grammar grammar = new Grammar(gBuilder);
recoEngine.LoadGrammar(grammar);
recoEngine.SetInputToDefaultAudioDevice();
recoEngine.RecognizeAsync(RecognizeMode.Multiple);
recoEngine.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(SpeechRecognized);
}
private void SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
string speech = e.Result.Text;
switch (speech)
{
case "what is tomorrow's date":
case "whats tomorrows date":
case "what will tomorrows date be":
MessageBox.Show(DateTime.Now.AddDays(1).ToString());
break;
}
}
}
}
如您所见,我有 3 种不同的方式来询问明天的日期。我想让它检查语音中的关键字,然后执行命令。我已经尝试过这样的事情
if (speech.Contains("tomorrow's") && speech.Contains("date")
{
//...
}
为此,我确实改变了我的语法,但仍然没有用。我希望我已经说得够清楚了,我将不胜感激任何答案或 cmets。
【问题讨论】:
标签: c# speech-recognition