Exemplo n.º 1
0
        private void Rec_Recognised(object sender, SpeechRecognizedEventArgs e)
        {
            string[] words = e.Result.Text.Split(' ');
            string   rslt = "<NOT SETED>", data = "<NOT SETED>";

            Console.WriteLine("CMD: " + words[0]);

            if (words.Length > 1)
            {
                rslt = FindInDictionary(words[1]);
                data = e.Result.Text.Replace(words[0] + " " + words[1] + " ", "");
            }

            statusBar.ChangeText($"{e.Result.Text} [{e.Result.Confidence}] [{e.Result.Grammar.Name}]");

            //for direct use of command
            XmlDocument doc = new XmlDocument();

#if debug
            doc.Load(@"D:\MY PROJECTS\8th sem project\Voice-Coding-EditChanges\res\MainResource.xml");
#else
            doc.Load(@"Resources\MainResource.xml");
#endif


            XmlNodeList nodeList4 = doc.GetElementsByTagName("Operator"); // obj for operator
            string[]    opt1      = new string[nodeList4.Count];
            int         n         = 0;
            //for operator recognization
            foreach (XmlNode node4 in nodeList4)
            {
                opt1[n] = node4.Attributes["name"]?.InnerText;
                n++;
            }
            for (int o = 0; o < opt1.Length; o++)
            {
                if (words[0] == opt1[o].ToString())
                {
                    sim.Keyboard.TextEntry($"{FindInDictionary(words[0])}" + " ");
                    break;
                }
            }

            //for alphabet
            XmlNodeList nodeList7 = doc.GetElementsByTagName("Alphabet");
            string[]    aph1      = new string[nodeList7.Count];
            int         c         = 0;
            foreach (XmlNode node7 in nodeList7)
            {
                aph1[c] = node7.Attributes["name"]?.InnerText;
                c++;
            }
            for (c = 0; c < aph1.Length; c++)
            {
                if (words[0] == aph1[c].ToString())
                {
                    sim.Keyboard.TextEntry($"{FindInDictionary(words[0])}" + " ");
                    break;
                }
            }

            //for number
            XmlNodeList nodeList8 = doc.GetElementsByTagName("Number");
            string[]    num1      = new string[nodeList8.Count];
            int         d         = 0;
            foreach (XmlNode node8 in nodeList8)
            {
                num1[d] = node8.Attributes["name"]?.InnerText;
                d++;
            }
            for (d = 0; d < num1.Length; d++)
            {
                if (words[0] == num1[d].ToString())
                {
                    sim.Keyboard.TextEntry($"{FindInDictionary(words[0])}" + " ");
                    break;
                }
            }



            switch (words[0])
            {
            //INCLUDE "file_name"  2
            case "include":
                /* if (words[1] == "iostream")
                 * {
                 *   sim.Keyboard.TextEntry(Encoding.UTF8.GetString(Encoding.UTF8.GetBytes($"#include < {words[1]} >\r")));
                 *   sim.Keyboard.TextEntry($"using namespace std;\r");
                 *   level++;
                 *   sim.Keyboard.TextEntry("int" + " " + "main" + "()\r{\r" + Tab(level) + "\r}\r");
                 *   sim.Keyboard.KeyPress(
                 *       new VirtualKeyCode[] {
                 *       VirtualKeyCode.UP,
                 *       VirtualKeyCode.UP
                 *       });
                 * }*/
                //else
                //{
                sim.Keyboard.TextEntry(Encoding.UTF8.GetString(Encoding.UTF8.GetBytes($"#include < {words[1]} >\r")));
                // }
                break;



            //for datatype
            // case "add":
            //   sim.Keyboard.TextEntry(rslt + " " + words[2] \r");
            //   break;


            //USING_NAMESPACE "name_of_namespace"  2
            case "using_namespace":
                sim.Keyboard.TextEntry($"using namespace {rslt};\r");
                break;

            //FUNCTION "data_type" "Function_name" 3
            case "function":
                level++;
                sim.Keyboard.TextEntry(rslt + " " + words[2] + "()\r{\r" + Tab(level) + "\r}\r");
                sim.Keyboard.KeyPress(
                    new VirtualKeyCode[] {
                    VirtualKeyCode.UP,
                    VirtualKeyCode.UP
                });
                break;

            case "add":
                if (words[1] == "for")
                {
                    sim.Keyboard.TextEntry($"for" + " " + "(" + ";" + ";" + " " + ")" + "\r{\r\r}\r");
                    sim.Keyboard.KeyPress(
                        new VirtualKeyCode[] {
                        VirtualKeyCode.UP,
                        VirtualKeyCode.UP,
                        VirtualKeyCode.UP,
                        VirtualKeyCode.UP,
                        VirtualKeyCode.RIGHT,
                        VirtualKeyCode.RIGHT,
                        VirtualKeyCode.RIGHT,
                        VirtualKeyCode.RIGHT,
                        VirtualKeyCode.RIGHT,
                    });
                }
                else if (words[1] == "if")
                {
                    sim.Keyboard.TextEntry("if" + " " + "(" + " " + ")" + "{\r\r}\r");
                    sim.Keyboard.TextEntry("else" + " " + "{\r\r}\r");
                    sim.Keyboard.KeyPress(
                        new VirtualKeyCode[] {
                        VirtualKeyCode.UP,
                        VirtualKeyCode.UP,
                        VirtualKeyCode.UP,
                        VirtualKeyCode.UP,
                        VirtualKeyCode.UP,
                        VirtualKeyCode.UP,
                        VirtualKeyCode.RIGHT,
                        VirtualKeyCode.RIGHT,
                        VirtualKeyCode.RIGHT,
                        VirtualKeyCode.RIGHT
                    });;
                }

                else
                {
                    XmlNodeList list = doc.GetElementsByTagName("Datatype");
                    foreach (XmlNode node in list)
                    {
                        if (node.Attributes["name"].Value == words[1])
                        {
                            sim.Keyboard.TextEntry($"{FindInDictionary(words[1])}" + " " + words[2] + "");
                            break;
                        }
                    }
                }
                break;

            //PRINT_LINE STRING/VAR "data_to_be_printed"  3
            case "print_line":
                if (words[1] == "string")
                {
                    sim.Keyboard.TextEntry($"cout<<\"{data}\"<<endl;\r");
                }
                else
                {
                    sim.Keyboard.TextEntry($"cout<<{data}<<endl;\r");
                }
                break;

            //PRINT STRING/VAR "data_to_be_printed" 3
            case "print":
                if (words[1] == "string")
                {
                    sim.Keyboard.TextEntry($"cout<<\"{data}\";\r");
                }
                else
                {
                    sim.Keyboard.TextEntry($"cout<<{data};\r");
                }
                break;

            case "sorry":
                sim.Keyboard.KeyDown(VirtualKeyCode.CONTROL);
                sim.Keyboard.KeyPress(VirtualKeyCode.VK_Z);
                sim.Keyboard.KeyUp(VirtualKeyCode.CONTROL);
                // sim.Keyboard.KeyPress(VirtualKeyCode.RIGHT);
                break;

            case "back":
                sim.Keyboard.KeyPress(VirtualKeyCode.BACK);
                break;



            case "erase":
                sim.Keyboard.KeyDown(VirtualKeyCode.CONTROL);
                sim.Keyboard.KeyPress(VirtualKeyCode.BACK);
                sim.Keyboard.KeyUp(VirtualKeyCode.CONTROL);
                break;

            case "clear":
                sim.Keyboard.KeyDown(VirtualKeyCode.CONTROL);
                sim.Keyboard.KeyPress(VirtualKeyCode.VK_A);
                sim.Keyboard.KeyUp(VirtualKeyCode.CONTROL);
                sim.Keyboard.KeyPress(VirtualKeyCode.BACK);
                break;

            case "left":
                sim.Keyboard.KeyPress(VirtualKeyCode.LEFT);
                break;

            case "right":
                sim.Keyboard.KeyPress(VirtualKeyCode.RIGHT);
                break;

            case "up":
                sim.Keyboard.KeyPress(VirtualKeyCode.UP);
                break;

            case "down":
                sim.Keyboard.KeyPress(VirtualKeyCode.DOWN);
                break;

            case "newline":
                sim.Keyboard.KeyPress(VirtualKeyCode.RETURN);
                break;

            case "over":
                sim.Keyboard.TextEntry(";");
                break;

            case "lessthan":
                sim.Keyboard.TextEntry("<");
                break;

            case "lessthanorequalto":
                sim.Keyboard.TextEntry("<=");
                break;


            case "tab":
                sim.Keyboard.KeyPress(VirtualKeyCode.TAB);
                break;

            case "space":
                sim.Keyboard.KeyPress(VirtualKeyCode.SPACE);
                break;

            case "backspace":
                sim.Keyboard.KeyPress(VirtualKeyCode.BACK);
                break;

            case "stop":
                rec.RecognizeAsyncCancel();
                recognizing = false;
                statusBar.ToggleColor(recognizing);
                break;

            case "exit":
                OnExitEvent(this, new RoutedEventArgs());
                break;
            }
        }