语音识别 - 如果你好,或者你好

时间:2015-05-20 08:56:44

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

好的,基本上在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;
        }
    }
}

1 个答案:

答案 0 :(得分:0)

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

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