一直试图在 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;
}
}
}