【问题标题】:Voice Recognition - IF hello OR hi is said语音识别 - 如果说你好或你好
【发布时间】:2015-05-20 17:01:27
【问题描述】:

好的,基本上在 C# 中,我希望某些代码在我说时运行,例如,你好或你好。我已经得到它,所以当我打招呼时它会运行代码,但我不知道如何制作它,所以如果我打招呼它也会运行代码。我不想为两者都做一个完整的 if 声明,因为我希望在未来做很多事情,(你好,嗨,嘿,你好吗,等等)。 到目前为止,这是我的代码:

using System;
using System.Windows.Forms;
using System.Speech.Recognition;


namespace Voice_Recognition
{
    public partial class Form1 : Form
    {
        SpeechRecognitionEngine recEngine = new SpeechRecognitionEngine();
        public Form1()
        {
            InitializeComponent();
        }
        private void btnEnable_Click(object sender, EventArgs e)
        {
            recEngine.RecognizeAsync(RecognizeMode.Multiple);
            btnDisable.Enabled = true;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Choices commands = new Choices();
            commands.Add(new string[] { "Hello", "Hey", "Hi" });
            GrammarBuilder gBuilder = new GrammarBuilder();
            gBuilder.Append(commands);
            Grammar grammar = new Grammar(gBuilder);
            gBuilder.Culture = new System.Globalization.CultureInfo("en-US");

            recEngine.LoadGrammarAsync(grammar);
            recEngine.SetInputToDefaultAudioDevice();
            recEngine.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(recEngine_SpeechRecognized);

        }
        void recEngine_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
        {
            string B = "\nBRIAN: ";
            string J = "\nYOU: ";
            string said = e.Result.Text;

            if (said == "Hello")
            {
                richTextBox1.Text += J + "Hello";
                string[] Hello = { "Hello.", "Hi.", "Hey.", "What's up?", "Howdy.", "Nice to see you.", "Hey, how are you going?", "Yo.", "Hiya", "Sup.", "Wassup?",
                        "What's crackin?", "How you goin'?", "What's new?", "How are you?",
                        "Hello Jake.", "Hi Jake.", "Hey Jake.", "What's up Jake?", "Howdy Jake.", "Nice to see you Jake", "Hey, how are you going Jake?", "Yo Jake.",
                        "Hiya Jake.", "Sup Jake?", "Wassup Jake?", "How you goin' Jake?", "What's new Jake?", "How are you Jake?"};
                richTextBox1.Text += B + Hello[new Random().Next(0, Hello.Length)];
            }            
        }

        private void btnDisable_Click(object sender, EventArgs e)
        {
            recEngine.RecognizeAsyncStop();
            btnDisable.Enabled = false;
        }
    }
}

【问题讨论】:

    标签: c# speech-recognition voice-recognition voice


    【解决方案1】:

    只需列出您的潜在输入,然后查看其中是否包含所说的内容

    string[] validInputs = new string[]{"Hello", "Hi"};
    if(validInputs.Contains(said))
    

    【讨论】:

    • 感谢您的回复,但是当我这样做时,“包含”下出现错误,提示“字符串 [] 不包含“包含”的定义
    • @Jakegeyer27 - 抱歉,您需要将Linq 包含在using System.Linq MSDN for contains
    • 顺便说一句,现在我也无法摆脱 'Choices commands = new Choices(); commands.Add(new string[] { "Hello", "Hey", "Hi" });'在我的 Form1_Load 中,即使我不需要它们
    • @Jakegeyer27 - 好吧,这些命令仍然被用作其他一些构造函数的参数,你可以用你刚刚用我的答案所做的输入列表替换其中的字符串参数,如果你希望(commands.Add(validInputs))。
    • 它说'名称'validInputs'在当前上下文中不存在',因为我必须将它放入recEngine_SpeechRecognized
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-24
    • 2018-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-19
    相关资源
    最近更新 更多