编译器错误-c#中的语音识别
本文关键字:语音识别 -c# 错误 编译器 | 更新日期: 2023-09-27 17:58:23
我试图用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;
}
}
}
这是什么?:
public class Grammar;
public partial class Form1 : Form
从类的结构来看,您似乎正在构建一个Form
。那么Grammar
是从哪里来的呢?完全删除第一个类声明至少可以修复语法错误:
public partial class Form1 : Form
必须在VisualStudio中添加:dataAI文本框、startBtn按钮、BtnClear按钮、stopBtn按钮和textBox1文本框+Speech。如果你有任何问题,请联系我!
我的工作代码:
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)
{
}
}
}