语音识别 - 如果有人说你好或你好
Voice Recognition - IF hello OR hi is said
好吧,基本上在 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;
}
}
}
只需列出您可能的意见,然后查看其中是否包含所说的内容
string[] validInputs = new string[]{"Hello", "Hi"};
if(validInputs.Contains(said))
好吧,基本上在 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;
}
}
}
只需列出您可能的意见,然后查看其中是否包含所说的内容
string[] validInputs = new string[]{"Hello", "Hi"};
if(validInputs.Contains(said))