问题
I am making my own jarvis program and when I say "search for" + something I want to open Google and search for "something". Here my code...( I don't paste it all)
private void Form1_Load(object sender, EventArgs e)
{
_recognizer.SetInputToDefaultAudioDevice();
_recognizer.LoadGrammar(new Grammar(new GrammarBuilder(new Choices(File.ReadAllLines(@"C:\Users\Cpyros\Desktop\lefteris\Commands.txt")))));
_recognizer.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(_recognizer_SpeechRecognized);
_recognizer.RecognizeAsync(RecognizeMode.Multiple);
}
void _recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
int ranNum = rnd.Next(1, 10);
string speech = e.Result.Text;
switch (speech)
{
//GREETINGS
case "hello":
case "hello jarvis":
if (ranNum < 6) { JARVIS.Speak("Hello sir"); }
else if (ranNum > 5) { JARVIS.Speak("Hi"); }
break;
case "goodbye":
case "goodbye jarvis":
case "close":
case "close jarvis":
JARVIS.Speak("Until next time");
Close();
break;
case "jarvis":
if (ranNum < 5) { QEvent = ""; JARVIS.Speak("Yes sir"); }
else if (ranNum > 4) { QEvent = ""; JARVIS.Speak("Yes?"); }
break;
//WEBSITES
case "open facebook":
System.Diagnostics.Process.Start("http://www.facebook.com");
break;
case "open google":
Process.Start("https://www.google.gr/?gws_rd=cr");
JARVIS.Speak("Okay sir");
System.Windows.Forms.SendKeys.SendWait("^%.");
break;
here i want to add a case like "search for" + the thing i want to search...
any ideas?
回答1:
Google has a query string that you can use to go right to a user input search string. Take the following for example:
https://www.google.com/#q=test+and+such
(thanks to Matt R, I learned there is also https://www.google.com/search?q=test+and+such
)
You can then use a modifiction of your previous Google case statement:
default:
if (speech.Contains("search for")
{
Process.Start("https://www.google.com/#q=" + userInput);
...
You will have to make sure the userInput
is URL Encoded first by doing
string userInput = System.Web.HttpUtility.UrlEncode(input);
回答2:
As the switch argument text won't match a case statement if it has search items after 'search for', you could put this as your default
statement :
default:
if (speech.ToLower().Contains("search for")) // See if the string contains the 'search for' string.
{
string query = speech.Replace("search for", ""); // Remove the 'search for' text.
// Old code (does not make the text fully URL safe)
// query = query.Replace(' ', '+');
query = System.Web.HttpUtility.UrlEncode(query);
string url = "https://www.google.com.au/search?q=" + query;
System.Diagnostics.Process.Start(url);
}
break;
回答3:
This will not work because your "grammarbuild" does not recognize words that are not loaded into your "grammarbuild". (I think) I'm still trying this too.
回答4:
I have the same source code. you must add the search for command in the default commands.txt and make sure that you do not have anything in the custom commands.cs pertaining to the phrase
回答5:
As Nikki O said before, this will not work since you don't have a words in your GrammarBuilder. You can simple load multiple grammar or just do:
default:
if (speech.ToLower().Contains("search for")) // See if the string contains the 'search for' string.
{
var dictationGrammar= new DictationGrammar();
sre.LoadGrammarAsync(dictationGrammar);
string url = "https://www.google.com.au/search?q=" + dictationGrammar;
System.Diagnostics.Process.Start(url);
}
break;
This will load your dictation text that is not in your default grammar. I didn't test this code but this should work.
来源:https://stackoverflow.com/questions/18511875/google-search-via-speech-in-c-sharp