C# Voice Assistant не работает должным образом с массивами - PullRequest
1 голос
/ 01 апреля 2020
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Speech.Synthesis;
using System.Speech.Recognition;
using System.Diagnostics;

namespace Jerry
{
    public partial class Form1 : Form
    {

        SpeechSynthesizer s = new SpeechSynthesizer();


        Choices list = new Choices();
        public Form1()
        {
            SpeechRecognitionEngine rec = new SpeechRecognitionEngine();

            //Assistant
            list.Add(new String[] { "how are you", "what time is it", "what is today", "open youtube", "open chrome", "what's the weather like" });
            //Virtual friend
            list.Add(new String[] { "hello", "good morning", "good night", "do you like it", "tell me a joke", "thank you", "fine", "I'am sad", "give me an advice", "bye" });


            Grammar gr = new Grammar(new GrammarBuilder(list));

            try
            {
                rec.RequestRecognizerUpdate();
                rec.LoadGrammar(gr);
                rec.SpeechRecognized += rec_SpeachRecognized;
                rec.SetInputToDefaultAudioDevice();
                rec.RecognizeAsync(RecognizeMode.Multiple);
            }
            catch { return; }

            s.SelectVoiceByHints(VoiceGender.Female);

            InitializeComponent();
        }

        public void say(String h)
        {
            s.Speak(h);
            textBox2.AppendText("\n" + h);
        }

        **// Greetings
        string[] greetings = new String[3] { "Hello", "Hi", "Greetings" };
        public String greetings_action()
        {
            Random r = new Random();
            return greetings[r.Next(3)];
        }

_ _ _ _ _ _ _ _ _ _ _ _ _ _ 

        // Advice
        string[] advice = new String[3] { "Fight for your dream, and maybe one day, it will becoma reality ", "Don't, worry, and be happy", "When you are sad, think that there  are other people who are sadder than you" };
        public String advice_action()
        {
            Random r = new Random();
            return advice[r.Next(3)];
        }
        // How are you
        string[] how = new String[3] { "Great, and you", "mmmm, I am ok , I guess", "Better than never" };
        public String how_action()
        {
            Random r = new Random();
            return how[r.Next(3)];
        }

_ _ _ _ _ _ _ _ _ _ _ _ _



        private void rec_SpeachRecognized(object sender, SpeechRecognizedEventArgs e)
        {
            String r = e.Result.Text;

            if (r == "hello")
            {
                say(greetings_action());
            }


            if (r == "give me an advice, please") ;
            {
                say(advice_action());
            }

            if (r == "how are you")
            {
                say(how_action());
            }

            if (r == "fine")
            {
                say("Good, How can I help you?");
            }

            if (r == "I'am sad")
            {
                say("Why, how can I help you?");
            }

            if (r == "Give me an advice")
            {
                say("fight for your dream");

            }

            if (r == "what time is it")
            {
                say(DateTime.Now.ToString("h:mm tt"));
            }

            if (r == "what is today")
            {
                say(DateTime.Now.ToString("M/d/yyyy"));
            }

            if (r == "open chrome")
            {
                Process.Start("https://google.com");
            }

            if (r == "open youtube")
            {
                Process.Start("https://www.youtube.com/");
            }

            if (r == "what's the weather like")
            {
                Process.Start("https://www.google.com/search?q=weather&oq=we&aqs=chrome.0.69i59j69i57j0j69i61j69i60l4.1455j0j7&sourceid=chrome&ie=UTF-8");
            }

            if (r == "tell me a joke")
            {
                say(" What do you call a boomerang that doesn’t work, a stick.");
            }

            if (r == "good morning")
            {
                say("good morning");
            }

            if (r == "good night")
            {
                say("good night buddy");
                Close();
            }

            if (r == "bye")
            {
                say("see you soon");
                Close();
            }


            if (r == "thank you")
            {
                say("your welcome sweetie");
            }

            // if (r == "do you like it")
            //  {
            //    say("ohhh, yes");
            //}

            {
                textBox1.AppendText("\n" + r);
            }





        }
        private void Forml_Load(object sender, EventArgs e)
        {



        }


        private void Form1_Load(object sender, EventArgs e)
        {


        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }
    }
}

Код, который находится внутри подчеркивания, не работает должным образом, всякий раз, когда я запускаю программу, он говорит случайную строку из всех массивов, а не только ту, которую я запрашивал. Я пытаюсь решить эту проблему в течение нескольких дней ..................................... .................................................. .................................................. .................................................. .................................................. .................................................. ...........................

...