Last active
March 6, 2016 21:14
-
-
Save alexcmd/4dca2d9917e63ea51784 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
// https://www.youtube.com/watch?v=9aHJ-FAzQaE | |
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("Говорилка включена"); | |
speechOn = true; | |
} | |
if (txt.IndexOf("молчи") >= 0) | |
{ | |
Console.WriteLine("Говорилка выключена"); | |
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