Skip to content

Instantly share code, notes, and snippets.

@alexcmd
Created April 1, 2016 20:30
Show Gist options
  • Save alexcmd/1be71739d1fa532f8bc27a676da40645 to your computer and use it in GitHub Desktop.
Save alexcmd/1be71739d1fa532f8bc27a676da40645 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Speech.Recognition;
using Microsoft.Speech.Synthesis;
namespace ConsoleApplication5
{
class Program
{
static SpeechSynthesizer ss = new SpeechSynthesizer();
static ReadOnlyCollection<RecognizerInfo> isr = SpeechRecognitionEngine.InstalledRecognizers();
static SpeechRecognitionEngine sre = new SpeechRecognitionEngine(CultureInfo.GetCultureInfo("ru-RU"));
static bool done = false;
static bool speechOn = true;
static void Main(string[] args)
{
ss.SetOutputToDefaultAudioDevice();
ss.Speak("Привет, человек.");
sre.SetInputToDefaultAudioDevice();
sre.SpeechRecognized += sre_SpeechRecognized;
Choices ch_StartStopCommands = new Choices();
ch_StartStopCommands.Add("говори");
ch_StartStopCommands.Add("молчи");
ch_StartStopCommands.Add("стоп");
GrammarBuilder gb_StartStop = new GrammarBuilder();
gb_StartStop.Append(ch_StartStopCommands);
Grammar g_StartStop = new Grammar(gb_StartStop);
Choices ch_Numbers = new Choices();
ch_Numbers.Add("1");
ch_Numbers.Add("2");
ch_Numbers.Add("3");
ch_Numbers.Add("4");
ch_Numbers.Add("6");
ch_Numbers.Add("7");
ch_Numbers.Add("8");
ch_Numbers.Add("9");
ch_Numbers.Add("10");
GrammarBuilder gb_WhatIsXplusY = new GrammarBuilder();
gb_WhatIsXplusY.Append("Сколько будет ");
gb_WhatIsXplusY.Append(ch_Numbers);
gb_WhatIsXplusY.Append(" плюс ");
gb_WhatIsXplusY.Append(ch_Numbers);
Grammar g_WhatIsXplusY = new Grammar(gb_WhatIsXplusY);
sre.LoadGrammarAsync(g_StartStop);
sre.LoadGrammarAsync(g_WhatIsXplusY);
sre.RecognizeAsync(RecognizeMode.Multiple);
while (done == false) {; }
Console.WriteLine("\nHit to close shell\n");
Console.ReadLine();
}
static void sre_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
string txt = e.Result.Text;
double confidence = e.Result.Confidence;
Console.WriteLine("\nRecognized: " + txt);
if (confidence < 0.50)
{
foreach (var alt in e.Result.Alternates)
{
Console.WriteLine($"{alt.Text}\t{alt.Confidence}");
}
return;
}
if (txt.IndexOf("говори") >= 0)
{
Console.WriteLine("Говорилка включена");
ss.Speak("Голосовые функции включены");
speechOn = true;
}
if (txt.IndexOf("молчи") >= 0)
{
Console.WriteLine("Говорилка выключена");
ss.Speak("Голосовые функции выключены");
speechOn = false;
}
if (speechOn == false) return;
if (txt.IndexOf("стоп") >= 0)
{
((SpeechRecognitionEngine)sender).RecognizeAsyncCancel();
done = true;
ss.Speak("До свидания!");
}
if (txt.IndexOf("Сколько") >= 0 && txt.IndexOf("плюс") >= 0) // what is 2 plus 3
{
string[] words = txt.Split(' '); // or use e.Result.Words
int num1 = int.Parse(words[2]);
int num2 = int.Parse(words[4]);
int sum = num1 + num2;
Console.WriteLine("Говорилка: " + words[2] + " + " + words[4] + " = " + sum);
ss.SpeakAsync(words[2] + " плюс " + words[4] + " будет " + sum);
}
} // sre_SpeechRecognized
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment