Reputation: 11
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
Reputation: 67898
System.Speech
reference.Text
property will already contain the expression you're expecting - something like 1 * 2
if you spoke One times two
.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