Alexander Cloud
Alexander Cloud

Reputation: 11

How to Create Speech Recognition Console Calculator in C#?

I have created a simple C# Console Calculator and i want to add Speech Recognition which will recognize the human voice.

Example: if I want to add two numbers I will tell the program to add two numbers through my voice then it will do the calculation. But i don't know how to do it.here is the code

class Program
{
    static void Main(string[] args)
    {
        int num1;
        int num2;
        string operators;
        float ans;

        Console.Write(" first integer: ");
        num1 = Convert.ToInt32(Console.ReadLine());
        Console.Write("enter a operator (+, -, /, *): ");
        operand = Console.ReadLine();
        Console.Write("enter the second integer: ");
        num2 = Convert.ToInt32(Console.ReadLine());
        switch (operators)
        {
            case "-":
                answer = num1 - num2;
                break;

            case "+":
                answer = num1 + num2;
                break;

            case "/":
                answer = num1 / num2;
                break;

            case "*":
                answer = num1 * num2;
                break;

            default:
                answer = 0;
                break;
        }

        Console.WriteLine(num1.ToString() + " " + operators 
                                 + " " + num2.ToString() + " = " + ans.ToString());
        Console.ReadLine();
    }
}

Upvotes: 1

Views: 1707

Answers (1)

Mike Perrenoud
Mike Perrenoud

Reputation: 67898

  1. You're going to need to include the System.Speech reference.
  2. When the application is started up you'll need to issue the code to hookup the speech recognition. Note that it's possible the Text property will already contain the expression you're expecting - something like 1 * 2 if you spoke One times two.

Code for Speech Recognition

SpeechRecognizer rec = new SpeechRecognizer();
rec.SpeechRecognized += rec_SpeechRecognized;
void rec_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
    // parse the grammar here and perform your operations
    // write the result to the console
}

Upvotes: 1

Related Questions