我已经在Google中下载了一些最常用的关键字和短语,并将它们放在与识别文本文件和响应文本文件分开的文本文件中。我分离了文件,以便每当我只说“搜索”一词时就可以使用它。默认情况下,搜索字词为false,因此它不会在每次识别文件中的单词时都在网上搜索。我已经在Stack Overflow中完成了大多数答案,但是没有一个对我有用。我尝试为搜索添加单独的表格。我还将路径文件放在“搜索过程”的if语句下,只有在我说搜索一词时才会触发该文件。
public partial class Form1 : Form
{
//user and jarvis texts
string[] grammarFile = (File.ReadAllLines(@"C:\Friday AI\user.txt.txt"));
string[] responseFile = (File.ReadAllLines(@"C:\Friday AI\jarvis.txt.txt"));
//speech synthesis
SpeechSynthesizer speechSynth = new SpeechSynthesizer();
//speech recognition
Choices grammarList = new Choices();
SpeechRecognitionEngine speechRecognition = new SpeechRecognitionEngine();
Boolean watchmovie = false;
Boolean wake = false;
public Boolean search = false;
public Form1()
{
//initialize grammarfile
grammarList.Add(grammarFile);
Grammar grammar = new Grammar(new GrammarBuilder(grammarList));
try
{
speechRecognition.RequestRecognizerUpdate();
speechRecognition.LoadGrammar(grammar);
speechRecognition.SpeechRecognized += rec_SpeechRecognized;
speechRecognition.SetInputToDefaultAudioDevice();
speechRecognition.RecognizeAsync(RecognizeMode.Multiple);
}
catch
{
return;
}
//custom speech setting
speechSynth.SelectVoiceByHints(VoiceGender.Male);
InitializeComponent();
}
private void rec_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
string result = e.Result.Text;
int resp = Array.IndexOf(grammarFile, result);
if (result.Contains("wake"))
{
wake = true;
label3.Text = "Status: Online";
}
if (result.Contains("sleep"))
{
wake = false;
label3.Text = "Status: Offline";
}
if (wake == true && search == false)
{
if (result.Contains("search"))
{
search = true;
say("what would you like to search");
label5.Text = "Search Status: Enabled";
}
if (responseFile[resp].IndexOf('*') == 0)
{
if (search == true)
{
if (result.Contains("search process"))
{
string[] globalsearch = (File.ReadAllLines(@"C:\Friday AI\search.txt.txt"));
Choices gsearch = new Choices();
gsearch.Add(globalsearch);
Grammar glosearch = new Grammar(new GrammarBuilder(gsearch));
try
{
speechRecognition.RequestRecognizerUpdate();
speechRecognition.LoadGrammar(glosearch);
speechRecognition.SpeechRecognized += rec_SpeechRec;
speechRecognition.SetInputToDefaultAudioDevice();
speechRecognition.RecognizeAsync(RecognizeMode.Multiple);
}
catch
{
return;
}
}
}
private void rec_SpeechRec(object sender, SpeechRecognizedEventArgs d)
{
string recognized = d.Result.Text;
Process.Start("https://www.google.com/search?q=" + recognized);
search = false;
}
该程序应该能够识别我所说的内容,并能够在网络上对其进行搜索。