-2

一直试图在 C# 中做一个简单的语音识别表单,但遇到了问题。对此很新,所以请帮忙!

面临2个错误:第一个错误是:“[预期”第二个错误是:无效令牌';' 在类、结构或接口成员声明中

下面是我的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Speech.Recognition;
using System.Text.RegularExpressions;
using System.Diagnostics;

namespace Speech
{
    public class Grammar;
    public partial class Form1 : Form

    {
        private SpeechRecognitionEngine recognitionEngine;

        public Form1()
        {
            InitializeComponent();
            recognitionEngine = new SpeechRecognitionEngine();
            recognitionEngine.SetInputToDefaultAudioDevice();
            recognitionEngine.SpeechRecognized += (s, args) =>
                {
                    string line = "";
                    foreach (RecognizedWordUnit word in args.Result.Words)
                    {
                        if (word.Confidence > 0.8f)
                            line += word.Text + " ";
                    }

                    string command = Regex.Replace(line, "Start", "").Trim();

                    switch (command)
                    {
                        case "Notepad":
                            Process.Start("notepad.exe");
                            break;
                        case "Calculator":
                            Process.Start("calc.exe");
                            break;
                        case "Paint":
                            Process.Start("mspaint.exe");
                            break;
                    }

                    txtOutput.Text += line;
                    txtOutput.Text += Environment.NewLine;
                };
            recognitionEngine.LoadGrammar(CreateGrammarObject());
        }

        private void button_start_Click(object sender, EventArgs e)
        {
            recognitionEngine.RecognizeAsync(RecognizeMode.Multiple);
        }

        private void button_stop_Click(object sender, EventArgs e)
        {
            recognitionEngine.RecognizeAsyncStop();
        }

        }

        private Grammar CreateGrammarObject()
        {
            Choices commandChoices = new Choices("Calculator", "Notepad", "Internet Explorer", "Paint");
            GrammarBuilder grammarBuilder = new GrammarBuilder("Start");
            grammarBuilder.Append(commandChoices);
            Grammar g = new Grammar(grammarBuilder);
            return g;
        }


    }
}
4

2 回答 2

2

这是什么?:

public class Grammar;
public partial class Form1 : Form

从类的结构来看,您正在构建一个Form. 那么是Grammar从哪里来的呢?完全删除第一类声明应该至少修复该语法错误:

public partial class Form1 : Form
于 2013-02-09T13:20:50.960 回答
0

您必须有:dataAI 文本框、startBtn 按钮、BtnClear 按钮、stopBtn 按钮和 textBox1 文本框 + 在 VisualStudio 中添加了语音。如果您有任何问题,请与我联系!

我的工作代码:

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;
using System.Speech.Synthesis;
using System.Speech.Recognition;
using System.Threading;
using System.Diagnostics;

namespace FreezeAI
{
    
    public partial class Form1 : Form
    {
        SpeechSynthesizer ss = new SpeechSynthesizer();
        PromptBuilder pb = new PromptBuilder();
        SpeechRecognitionEngine sre = new SpeechRecognitionEngine();
        Choices clist = new Choices();

        public Form1()
        {
            InitializeComponent();
        }

        private void dataAI_TextChanged(object sender, EventArgs e)
        {
            pb.ClearContent();
            pb.AppendText(dataAI.Text);
            ss.Speak(pb);
        }

        private void button1_Click(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

            if (textBox1.Text.Contains("hello"))
            {
                dataAI.Text = "Hi!";
            }
            else if (textBox1.Text.Contains("Hello"))
            {
                dataAI.Text = "Hi!";
            }
            else if (textBox1.Text.Contains("How are you?"))
            {
                dataAI.Text = "Good, and you?";
            }
            else if (textBox1.Text.Contains("how are you?"))
            {
                dataAI.Text = "Good, and you?";
            }
            else if (textBox1.Text.Contains("how are you"))
            {
                dataAI.Text = "Iam robot.. I dont have emotions.. :(";
            }
            else if (textBox1.Text.Contains("stop record"))
            {
                dataAI.Text = "Ok stopping..";
                sre.RecognizeAsyncStop();
                BtnStart.Enabled = true;
                BtnStop.Enabled = false;
            }
            else if (textBox1.Text.Contains("Stop record"))
            {
                dataAI.Text = "Ok stopping..";
                sre.RecognizeAsyncStop();
                BtnStart.Enabled = true;
                BtnStop.Enabled = false;
            }
            else if (textBox1.Text.Contains("Stop Record"))
            {
                dataAI.Text = "Ok stopping..";
                sre.RecognizeAsyncStop();
                BtnStart.Enabled = true;
                BtnStop.Enabled = false;
            }
            else if (textBox1.Text.Contains("stop Record"))
            {
                dataAI.Text = "Ok stopping..";
                sre.RecognizeAsyncStop();
                BtnStart.Enabled = true;
                BtnStop.Enabled = false;
            }
            else if (textBox1.Text.Contains("stop the record"))
            {
                dataAI.Text = "Ok stopping..";
                sre.RecognizeAsyncStop();
                BtnStart.Enabled = true;
                BtnStop.Enabled = false;
            }
            else if (textBox1.Text.Contains("Stop the record"))
            {
                dataAI.Text = "Ok stopping..";
                sre.RecognizeAsyncStop();
                BtnStart.Enabled = true;
                BtnStop.Enabled = false;
            }
            else if (textBox1.Text.Contains("Stop The record"))
            {
                dataAI.Text = "Ok stopping..";
                sre.RecognizeAsyncStop();
                BtnStart.Enabled = true;
                BtnStop.Enabled = false;
            }
            else if (textBox1.Text.Contains("Stop The Record"))
            {
                dataAI.Text = "Ok stopping..";
                sre.RecognizeAsyncStop();
                BtnStart.Enabled = true;
                BtnStop.Enabled = false;
            }
            else if (textBox1.Text.Contains("stop The Record"))
            {
                dataAI.Text = "Ok stopping..";
                sre.RecognizeAsyncStop();
                BtnStart.Enabled = true;
                BtnStop.Enabled = false;
            }
            else if (textBox1.Text.Contains("stop the Record"))
            {
                dataAI.Text = "Ok stopping..";
                sre.RecognizeAsyncStop();
                BtnStart.Enabled = true;
                BtnStop.Enabled = false;
            }
            else if (textBox1.Text.Contains("stop The record"))
            {
                dataAI.Text = "Ok stopping..";
                sre.RecognizeAsyncStop();
                BtnStart.Enabled = true;
                BtnStop.Enabled = false;
            }
            else if (textBox1.Text.Contains("stop The record"))
            {
                dataAI.Text = "Ok stopping..";
                sre.RecognizeAsyncStop();
                BtnStart.Enabled = true;
                BtnStop.Enabled = false;
            }
            else if (textBox1.Text.Contains("clear"))
            {
                dataAI.Text = "Ok cleared..";
                textBox1.Text = String.Empty;
            }

        }

        private void BtnStart_Click(object sender, EventArgs e)
        {
            BtnStart.Enabled = false;
            BtnStop.Enabled = true;
            Grammar gr = new DictationGrammar();
            dataAI.Text = "Ok starting..";
            try
            {
                sre.RequestRecognizerUpdate();
                sre.LoadGrammar(gr);
                sre.SpeechRecognized += sre_SpeechRecognized;
                sre.SetInputToDefaultAudioDevice();
                sre.RecognizeAsync(RecognizeMode.Multiple);

            }
            catch (Exception ex) { MessageBox.Show(ex.Message, "Error"); }
        }

        private void sre_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
        {
            textBox1.Text = textBox1.Text + e.Result.Text.ToString() + Environment.NewLine;
        }

        private void BtnStop_Click(object sender, EventArgs e)
        {
            sre.RecognizeAsyncStop();
            BtnStart.Enabled = true;
            BtnStop.Enabled = false;
            dataAI.Text = "Ok stopping..";
        }

        private void BtnClear_Click(object sender, EventArgs e)
        {
            textBox1.Text = String.Empty;
            MessageBox.Show("Cleared!");
            dataAI.Text = "Ok cleared..";
        }


        private void Form1_Load(object sender, EventArgs e)
        {
            
        }

    }
}
于 2021-03-29T07:46:42.060 回答