0

I have the following code using the Speech Recognition library:

var listen = new SpeechRecognitionEngine(); var reader = new Choices(File.ReadLines(@"C:\words.txt") listen.LoadGrammar(new Grammar(new GrammarBuilder(reader))); listen.SpeechRecognized += listen_SpeechRecognized; listen.SpeechRecognitionRejected += listen_SpeechRecognitionRejected; listen.SetInputToDefaultAudioDevice(); listen.RecognizeAsync(RecognizeMode.Multiple); 

And I have an event listener like this...

static void listen_SpeechRecognized(object sender, SpeechRecognizedEventArgs e) { var talk = new SpeechSynthesizer(); if (e.Result.Text == "Search Stock Symbol") { talk.Speak("What symbol?"); //Do I have to create another event listener? //a Listener .. symbol = a.Result.Text //talk.Speak(GetQuote(symbol)) { } 

Would I have to create an event listener for every portion of the "conversation"? Is there a better way if that is the case?

Example Conversation:

  • Me: Search Stock Symbol
  • Computer: What Symbol?
  • Me: AAPL
  • Computer: Apple is trading at ....

1 Answer 1

3

Nope, just the one, then vary what you do depending on what text was received. In some code before:

 List<string> stockSymbols = new List<string>(); stockSymbols.Add("AAPL"); 

Then

 string lastSpeechInput; static void listen_SpeechRecognized(object sender, SpeechRecognizedEventArgs e) { var talk = new SpeechSynthesizer(); switch (e.Result.Text) { case "Search Stock Symbol": talk.Speak("What symbol?"); break; default: break; } if (stockSymbols.Contains(e.Result.Text) && lastSpeechInput == "Search Stock Symbol") { talk.Speak(getStockPrice(e.Result.Text); } lastSpeechInput = e.Result.Text; } 
Sign up to request clarification or add additional context in comments.

4 Comments

Would this work if "AAPL" is not in dictionary.txt? I've tried your code and it doesn't appear to work. This is the code I tried: pastebin.com/Jpkz8Xu9
Oh I see, fixed the lack of default in switch-case. Also note the made up function getStockPrice
Yea i know the function is made up. But the problem with this is that e.Result.Text will always be == "Search Stock Symbol". This will only work if "Search Stock Symbol" is in the list.
The question isn't how to iterate through a list and test for the presence of strings. That's just an example. I'm really only solving the question here, that's filler to show you what you do potentially do. What logic you do within that is up to you. The if statement will get fired if you say any word In the list (.Contains()) after saying search stock price.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.