【问题标题】:Is there a way to check if the spoken command contains certain 'keywords' in windows speech recognition?有没有办法检查口语命令是否包含 Windows 语音识别中的某些“关键字”?
【发布时间】: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


    【解决方案1】:

    也许,您可以设置一个 if 语句,而不是对所说的内容使用 switch 语句:

    if (speech.Contains("tomorrow") && speech.Contains("date"))
    {
        MessageBox.Show(DateTime.Now.AddDays(1).ToString());
    }
    

    这将提供更大的灵活性,例如“明天的日期是什么时候?”或“给我明天的约会。”

    我意识到这真的太晚了,你可能已经继续前进了,但我把这个听听未来可能会好奇的人。

    【讨论】:

      【解决方案2】:

      您可以使用正则表达式。 而不是开关,做:

      Regex.Match(speech, @"\btomorrow(')*s\b\s+\bdate\b", RegexOptions.IgnoreCase)
      

      这将匹配您拥有的三种情况,以及以下情况,例如:

      • “明天”
      • “明天的日期”
      • “明天日期”
      • “明天的日期”

      不匹配:

      • “明天约会”
      • “明天”

      【讨论】:

        【解决方案3】:

        但是,我不能说“明天是几号?”或“明天的日期是什么时候?”因为我的语法中没有它们。

        要回答您的问题,可以在语法中插入占位符,请参阅AppendWildcard 上的文档。但是,为了获得最佳准确性,建议在语法中包含所有可能的选择,这样您将能够准确地处理用户输入并了解如何处理不同的用户请求。您通常可以花 10 分钟时间编写所有可能的变体,并充分了解各种可能性。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多