Search code examples
c#variablestext-to-speechspeechgarbage

How do I add variables to Speech Recognition?


I've done some coding on a virtual assistant, and I've hit a block. Here's the code:

 private void Form1_Load_1(object sender, System.EventArgs e)
    {
        SrgsDocument commands = new SrgsDocument();
        SrgsRule displayList = new SrgsRule("selectList");
        SrgsOneOf listTypes = new SrgsOneOf(new string[] { "hi " + name + ", my name is" , "hi " + name});
        displayList.Scope = SrgsRuleScope.Public;
        displayList.Elements.Add(listTypes);
        commands.Rules.Add(displayList);
        commands.Root = displayList;
        Grammar grammar = new Grammar(commands);
        recEngine.LoadGrammarAsync(grammar);
        recEngine.SetInputToDefaultAudioDevice();
        recEngine.RecognizeAsync(RecognizeMode.Multiple);
        recEngine.SpeechRecognized +=recEngine_SpeechRecognized;

    }                      
    SpeechSynthesizer synth = new SpeechSynthesizer();
    void recEngine_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
    {
        String message = e.Result.Text.ToLower();
        if (message.Contains("hi " + name + ", my name is"))
        {
            message = message.Replace("hi " + "name" + ", my name is", "");
            synth.Speak("Hello " + message + ", my name is " + name);
        }
        else if (message.Contains("hi " + name))
        {
            synth.Speak("Hello!");
        }
    }

What I'm trying to do, as the title suggests, is have the name variable be filled in from the speech recognition. I'm looking for a way to do this that involves user input, and not adding each possibility for name separately. Unfortunately, the SpeechRecognizer is only looking at the "My Name Is", and nothing else.

Somebody told me about the garbage class, but I can't figure out how to implement it.


Solution

  • What I ended up doing is adding in a dictation grammar and setting a timer. The timer is reset every time someone speaks. The dictation grammar goes into a textbox. When the timer = 0, use the code to check the textbox. Simple and done!