Advertisement
meer11

Lecture_3_Practical_STT_code_block_AI

Oct 16th, 2019
279
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.97 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.Speech.Synthesis;
  11. using System.Speech.Recognition;
  12. using System.Diagnostics;
  13. //The System.Diagnostics namespace provides classes that allow you to interact with system processes, event logs
  14. namespace STT123
  15. {
  16.     public partial class Form1 : Form
  17.     {
  18.         SpeechSynthesizer speechsynth = new SpeechSynthesizer();
  19.         SpeechRecognitionEngine receng = new SpeechRecognitionEngine();
  20.         Choices choice = new Choices();
  21.         public Form1()
  22.         {
  23.             InitializeComponent();
  24.         }
  25.  
  26.         private void Form1_Load(object sender, EventArgs e)
  27.         {
  28.  
  29.         }
  30.  
  31.         private void button1_Click(object sender, EventArgs e)
  32.         {
  33.             button1.Enabled = false;
  34.             button2.Enabled = true;
  35.             choice.Add(new string[] { "hello", "how are you", "what is the current time", "open blog", "thank you", "close" });
  36.             Grammar gr = new Grammar(new GrammarBuilder(choice));
  37.             try
  38.             {
  39.                 receng.RequestRecognizerUpdate();
  40.                 receng.LoadGrammar(gr);
  41.                 receng.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(receng_SpeechRecognized);
  42.                 receng.SetInputToDefaultAudioDevice();
  43.                 receng.RecognizeAsync(RecognizeMode.Multiple);
  44.             }
  45.             catch (Exception ex)// handle exception
  46.             {
  47.                 MessageBox.Show(ex.Message, "Error");
  48.  
  49.             }
  50.         }
  51.  
  52.         private void receng_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
  53.         {
  54.             switch (e.Result.Text.ToString())
  55.             {
  56.                 case "hello":
  57.                     speechsynth.SpeakAsync("hai user");
  58.                     break;
  59.                 case "how are you":
  60.                     speechsynth.SpeakAsync("iam fine. what about you");
  61.                     break;
  62.                 case "what is the current time":
  63.                     speechsynth.SpeakAsync("right now it is " + DateTime.Now.ToLongTimeString());
  64.                     break;
  65.                 case "thank you":
  66.                     speechsynth.SpeakAsync("well. same to you");
  67.                     break;
  68.                 case "open blog":
  69.                     Process.Start("chrome", "http://Schoology.com");
  70.                     break;
  71.                 case "close":
  72.                     speechsynth.Speak("see you later. bye bye");
  73.                     Application.Exit();
  74.                     break;
  75.  
  76.             }
  77.             list1.Items.Add(e.Result.Text.ToString());
  78.         }
  79.  
  80.         private void button2_Click(object sender, EventArgs e)
  81.         {
  82.             receng.RecognizeAsyncStop();
  83.             button1.Enabled = true;
  84.             button2.Enabled = false;
  85.         }
  86.     }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement