MIRA(Artificial inteligience)

C:\Local\JASVA\JASVAscript\JASVAscript\bin\Debug

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Speech.Recognition;
using System.Speech.Synthesis;
using System.IO;
namespace JASVAscript
{
    public partial class Form1 : Form
    {
        SpeechRecognitionEngine speechRecognitionEngine = null;
        SpeechSynthesizer MIRA = new SpeechSynthesizer();
        String[] ArraySearchCommands;
        String[] ArrayKeywordSearch;
        public Form1()
        {
            InitializeComponent();
            try
            {
                speechRecognitionEngine = createSpeechEngine("en-US");   // 1.if I want other language change here
                speechRecognitionEngine.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(engine_SpeechRecognized);
                speechRecognitionEngine.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(WebSearch_SpeechRecognized); //generating methode
                LoadGrammerAndCommands();
                //8.use the system mic default option
                speechRecognitionEngine.SetInputToDefaultAudioDevice();
                //9.start listening
                speechRecognitionEngine.RecognizeAsync(RecognizeMode.Multiple); //12.RecognizeMode is enum ,I have to set this Multiple.
                ArraySearchCommands = File.ReadAllLines(Environment.CurrentDirectory + "//WebSearchCommands.txt");  //7.Ststem.IO makes me able to adject file.
                ArrayKeywordSearch = File.ReadAllLines(Environment.CurrentDirectory + "//WebKeyCommands.txt");
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void engine_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
        {
            string speech = e.Result.Text; //13.e.Result holdes all excuted regognizedText // it will recognize "Hello, jarvis " etc.
            switch (speech)
            {
                case "start reading":
                    CopyScreen.PerformClick();
                    StartBtn.PerformClick();
                    break;  //14. Must "break;" or it will messed up all, for each case we should break;
                case "read the result":
                case "read the results":
                case "whats the result":
                case "whats the results":
                //case "what is the result":   
                //    GetResult();
                //    break;
                //case "search":
                //    SearchBtn.PerformClick();
                //    break;
                //case "pause":
                //    PauseBtn.PerformClick();
                //    break;
                //case "resume":
                //    PauseBtn.PerformClick();
                //    break;
                //case "Stop":
                //    StopBtn.PerformClick();
                //    break;
                //case "close website search":
                //    CloseBtn.PerformClick();
                //    break;
                case "hide website reader":
                    FormBorderStyle = FormBorderStyle.None;
                    WindowState = FormWindowState.Minimized;
                    TopMost = false;
                    break;
            }
        }
        private void LoadGrammerAndCommands()
        {
            try
            {
                Choices texts = new Choices();
                string[] lines = File.ReadAllLines(Environment.CurrentDirectory + "//DefaultCommands.txt");
                //6. add the text to the known choices of speecheingine
                texts.Add(lines);  //10.this will add from lines to our choices in speechengine
                Grammar wordsList = new Grammar(new GrammarBuilder(texts));  //11.we will make grammer from text, whic is DefaultCommands.
                speechRecognitionEngine.LoadGrammar(wordsList);  //12. wordlist is our grammer name
                try
                {
                    Grammar websearchcommandgrammar = new Grammar(new GrammarBuilder(new Choices(ArraySearchCommands)));
                    speechRecognitionEngine.LoadGrammarAsync(websearchcommandgrammar);
                }
                catch(Exception ex)
                {
                    MIRA.SpeakAsync("I've detected an in valid entry in your web commands, possibly a blank line. web command will cease to work until it is fixed." + ex.Message);
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void WebSearch_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
        {
            string speech = e.Result.Text;  //2.e.Result.Text -> our executed grammer from speech engine
            int i = 0;
            try
            {
                foreach(string line in ArraySearchCommands)
                {
                    if(line == speech)
                    {
                        MIRA.SpeakAsync(ArrayKeywordSearch[i]);  //3.ArrayKeywordSearch is our string array
                        KeyWordsTxt.Text = ArrayKeywordSearch[i];
                        SearchBtn.PerformClick();
                    }
                    i += 1;
                }
            }
            //if we have the problem it will tell us the exact error
            catch(Exception ex)
            {
                i += 1;
                MIRA.SpeakAsync("Please check the " + speech + "web command on line " + i + ".It appears to be missing a proper response or keywords" + ex.Message);
            }
            //throw new NotImplementedException();
        }
        //5.if "preferredCulture" is in the config.Culture, then RecognizerInfor will get from the config // Getting language
        private SpeechRecognitionEngine createSpeechEngine(string preferredCulture)
        {
            foreach (RecognizerInfo config in SpeechRecognitionEngine.InstalledRecognizers())
            {
                if (config.Culture.ToString()== preferredCulture)
                {
                    speechRecognitionEngine = new SpeechRecognitionEngine(config);
                    break;
                }
            }
            if (speechRecognitionEngine == null)
            {
                MessageBox.Show("The desired culture is not installed danya!! the speech-engine will continue using"
                    + SpeechRecognitionEngine.InstalledRecognizers()[0].Culture.ToString() + "as the default culture danya!", "Culture"
                    + preferredCulture + "not found ㅠ.ㅠ");
                speechRecognitionEngine = new SpeechRecognitionEngine(SpeechRecognitionEngine.InstalledRecognizers()[0]);
            }
            return speechRecognitionEngine; //4.after done all these it will return speechRecognitionEngine.
        }
        private void CloseBtn_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}

댓글

이 블로그의 인기 게시물

About AActor!!! "UObject" has no member "BeginPlay"

UNREAL Android build information

Shader informations nice blog ~ ~