示例#1
0
 private void TextBox_KeyDown(object sender, KeyEventArgs e)
 {
     StopCommandWaiting.Invoke(this, new EventArgs());
     if (e.Key == Key.Enter)
     {
         NLP(textBox.Text);
         textBox.Clear();
     }
 }
示例#2
0
        /// <summary>
        /// Neutral Language Processing
        /// </summary>
        private async void NLP(string sentence, bool speech = false)
        {
            if (sentence != null && sentence != "")
            {
                #region Pre-Processing
                if (IsDone)
                {
                    textBox1.Document.Blocks.Clear(); image.Source = null;
                }
                StopCommandWaiting.Invoke(this, new EventArgs());
                textBox1.AppendText(sentence + "\n"); this.textBox1.ScrollToEnd();

                var Received = lus.TextRequest(sentence);
                #endregion
                if (Received.Result.ActionIncomplete)
                {
                    #region ActionIncomplete
                    await HistoryWrite(Received.Result.Fulfillment.Speech, speech);

                    if (speech)
                    {
                        StartSpeak.RaiseEvent(new RoutedEventArgs(ButtonBase.ClickEvent));
                    }
                    return;
                }
                #endregion
                else
                {
                    #region ActionComplete
                    DeviceManager devman = new DeviceManager();
                    switch (Received.Result.Action)
                    {
                        #region WebSearch
                    case "web.search":
                        if (Received.Result.Parameters.ContainsKey("q"))
                        {
                            Process.Start(String.Concat("https://www.google.com/search?q=" + Received.Result.Parameters["q"]));
                        }
                        break;

                        #endregion
                        #region Smarthome
                    case "smarthome.appliances_on":
                        if (Received.Result.Parameters["all"].ToString() == "true")
                        {
                            string dev;
                            dev = Received.Result.Parameters["appliance_name"].ToString();
                            if (devman.IsTypeExist(dev))
                            {
                                HistoryWrite("I will turn on all " + dev, speech);
                                devman.SetAll(dev, 1);
                            }
                            else
                            {
                                HistoryWrite("I can't find any " + dev, speech);
                            }
                        }
                        else
                        {
                            string dev;
                            dev = Received.Result.Parameters["appliance_name"].ToString() + Received.Result.Parameters["number"];
                            if (devman.IsExist(dev))
                            {
                                HistoryWrite("I will turn on the " + dev, speech);
                                devman.Set(dev, 1);
                            }
                            else
                            {
                                HistoryWrite("I can't find the " + dev, speech);
                            }
                        }
                        break;

                    case "smarthome.appliances_off":

                        if (Received.Result.Parameters["all"].ToString() == "true")
                        {
                            string dev;
                            dev = Received.Result.Parameters["appliance_name"].ToString();
                            if (devman.IsTypeExist(dev))
                            {
                                HistoryWrite("I will turn off all " + dev, speech);
                                devman.SetAll(dev, 0);
                            }
                            else
                            {
                                HistoryWrite("I can't find any " + dev, speech);
                            }
                        }
                        else
                        {
                            string dev;
                            dev = Received.Result.Parameters["appliance_name"].ToString() + Received.Result.Parameters["number"];
                            if (devman.IsExist(dev))
                            {
                                HistoryWrite("I will turn off the " + dev, speech);
                                devman.Set(dev, 0);
                            }
                            else
                            {
                                HistoryWrite("I can't find the " + dev, speech);
                            }
                        }
                        break;

                        #endregion
                        #region Clock
                    case "clock.date":
                        if (Received.Result.Parameters["date"].ToString() != null)
                        {
                            HistoryWrite("It's " + Received.Result.Parameters["date"], speech);
                        }
                        else
                        {
                            HistoryWrite("It's " + DateTime.Now.ToShortDateString(), speech);
                        }
                        break;

                    case "clock.time":
                        HistoryWrite("It's " + DateTime.Now.ToShortTimeString(), speech);
                        break;

                        #endregion
                        #region Weather
                    case "weather.search":
                        var      WE        = new Weather();
                        DateTime Now       = DateTime.Now;
                        DateTime Converted = DateTime.Now;
                        string   location  = "autoip";
                        if (Received.Result.Parameters.ContainsKey("geo-city") && Received.Result.Parameters["geo-city"].ToString() != "")
                        {
                            location = Received.Result.Parameters["geo-city"].ToString();
                        }
                        else
                        {
                            location = "autoip";
                        }
                        string time = "present";
                        if (Received.Result.Parameters.ContainsKey("date"))
                        {
                            DateTime.TryParseExact(Received.Result.Parameters["date"].ToString(), "yyyy-MM-dd", CultureInfo.InvariantCulture, DateTimeStyles.None, out Converted);
                            if (Now < Converted)
                            {
                                time = "future";
                            }
                        }
                        if (time == "present")
                        {
                            var data = WE.GetCurrentWeather(location);
                            if (data == null)
                            {
                                HistoryWrite(Received.Result.Fulfillment.Speech);
                            }
                            else
                            {
                                if (location == "autoip")
                                {
                                    HistoryWrite("The current weather at current location is " + data["weather"].ToString().ToLower() + " with the temperature of " + data["temp_c"].ToString() + " C.", speech);
                                }
                                else
                                {
                                    HistoryWrite("The current weather in " + location + " is " + data["weather"].ToString().ToLower() + " with the temperature of " + data["temp_c"].ToString() + " C.", speech);
                                }
                                BitmapImage bi3 = new BitmapImage();
                                bi3.BeginInit();
                                bi3.UriSource = new Uri(data["icon_url"].ToString(), UriKind.Absolute);
                                bi3.EndInit();
                                image.Stretch = Stretch.Uniform;
                                image.Source  = bi3;
                            }
                        }
                        if (time == "future")
                        {
                            var data = WE.GetForecastWeather(Converted, location);
                            if (data == null)
                            {
                                HistoryWrite(Received.Result.Fulfillment.Speech);
                            }
                            else
                            {
                                if (location == "autoip")
                                {
                                    HistoryWrite("The weather at current location on " + data["date"].ToString() + " will be " + data["conditions"].ToString().ToLower() + " with the temperature of " + data["low_c"].ToString() + " C at lowest and " + data["high_c"] + " C at highest.", speech);
                                }
                                else
                                {
                                    HistoryWrite("The weather in " + location + " on " + data["date"].ToString() + " will be " + data["conditions"].ToString().ToLower() + " with the temperature of " + data["low_c"].ToString() + " C at lowest and " + data["high_c"] + " C at highest.", speech);
                                }
                                BitmapImage bi3 = new BitmapImage();
                                bi3.BeginInit();
                                bi3.UriSource = new Uri(data["icon_url"].ToString(), UriKind.Absolute);
                                bi3.EndInit();
                                image.Stretch = Stretch.Uniform;
                                image.Source  = bi3;
                            }
                        }
                        break;

                        #endregion
                        #region Easteregg
                    case "easteregg.singing":
                        int         Choice = Choices.Next() % 3;
                        var         path   = System.IO.Path.Combine("Singing", Convert.ToString(Choice) + ".wav");
                        SoundPlayer player = new SoundPlayer(path);
                        player.Play();
                        if (Choice == 1)
                        {
                            HistoryWrite("You are my sunshine, my only sunshine. You make me happy, when skies are gray.");
                        }

                        else if (Choice == 2)
                        {
                            HistoryWrite("Oh he floats through the air with the greatest of ease, this daring young man on the flying trapeze.");
                        }
                        else
                        {
                            HistoryWrite("Twinkle twinkle little star, how I wonder what you are!");
                        }
                        break;

                        #endregion
                        #region Reminder
                    case "reminder.add":
                        BitmapImage bir = new BitmapImage();
                        bir.BeginInit();
                        bir.UriSource = new Uri(@"\Assets\Reminder.png", UriKind.Relative);
                        bir.EndInit();
                        image.Stretch = Stretch.Uniform;
                        image.Source  = bir;
                        string summary = Received.Result.Parameters["summary"].ToString();
                        time = Received.Result.Parameters["time"].ToString();
                        DateTime converted = new DateTime();

                        if (time.Contains("/"))
                        {
                            /*if (!time.Contains("T"))
                             *  time = time.Substring(time.IndexOf('/') + 1);
                             * else */
                            time = time.Remove(time.IndexOf('/'));
                            time = time.Trim('/');
                        }
                        if (time.Contains("T"))
                        {
                            DateTime.TryParseExact(time, "yyyy-MM-dd\\THH:mm:ss\\Z", CultureInfo.InvariantCulture, DateTimeStyles.None, out converted);
                        }
                        else
                        {
                            if (time.Contains("-"))
                            {
                                DateTime.TryParseExact(time, "yyyy-MM-dd", CultureInfo.InvariantCulture, DateTimeStyles.None, out converted);
                            }
                            else
                            {
                                DateTime.TryParseExact(time, "HH:mm:ss", CultureInfo.InvariantCulture, DateTimeStyles.None, out converted);
                            }
                        }
                        while (converted < DateTime.Now)
                        {
                            converted = converted.AddDays(1);
                        }
                        summary       = summary.Trim();
                        item.reminder = summary;
                        item.datetime = converted;
                        item.Notified = false;
                        string cvt;
                        if (converted.Date > DateTime.Now.Date)
                        {
                            cvt = converted.ToString("dddd, MMMM dd, yyyy h:mm tt");
                        }
                        else
                        {
                            cvt = converted.ToShortTimeString();
                        }

                        await HistoryWrite("I'll remind you to " + summary + " on " + cvt + ". Ready to confirm?", speech);

                        if (speech)
                        {
                            Dispatcher.Invoke((Action)(() =>
                            {
                                StartSpeak_Click(this, new RoutedEventArgs());
                            }));
                        }
                        IsDone = false;
                        return;

                    case "reminder.confirm":
                        if (item != new Reminder())
                        {
                            ReminderManager.SaveReminder(item);
                            HistoryWrite(Received.Result.Fulfillment.Speech, speech);
                            item = new Reminder();
                        }
                        else
                        {
                            HistoryWrite("I'm not sure what you said.", speech);
                        }
                        IsDone = true;
                        break;

                    case "reminder.cancel":
                        item = new Reminder();
                        HistoryWrite(Received.Result.Fulfillment.Speech, speech);
                        break;

                        #endregion
                        #region Location
                    case "location.current":
                        string loc = Location.GetCurrentAddress();
                        if (loc != null)
                        {
                            HistoryWrite("We are at " + loc + " .", speech);
                        }
                        else
                        {
                            HistoryWrite(Received.Result.Fulfillment.Speech, speech);
                        }
                        break;

                        #endregion
                        #region History
                    case "history.event":
                        List <Dictionary <string, object> > result = null;
                        DateTime ChosenDate = new DateTime();
                        bool     IsToday    = true;
                        if (Received.Result.Parameters["date"].ToString() == "")
                        {
                            result = History.GetHistory(DateTime.Now);
                        }
                        else
                        {
                            DateTime.TryParseExact(Received.Result.Parameters["date"].ToString(), "yyyy-MM-dd", CultureInfo.InvariantCulture, DateTimeStyles.None, out ChosenDate);
                            result  = History.GetHistory(ChosenDate);
                            IsToday = false;
                        }
                        if (result == null)
                        {
                            HistoryWrite(Received.Result.Fulfillment.Speech, speech);
                        }
                        else
                        {
                            Choice = Choices.Next() % result.Count - 1;
                            var picked = result[Choice];
                            if (IsToday)
                            {
                                HistoryWrite("On this day in " + picked["year"].ToString() + ", " + AccentsRemover.RemoveAccents(picked["text"].ToString()), speech);
                            }
                            else
                            {
                                HistoryWrite("On " + ChosenDate.ToShortDateString() + " in " + picked["year"].ToString() + ", " + AccentsRemover.RemoveAccents(picked["text"].ToString()), speech);
                            }
                        }
                        break;

                        #endregion
                        #region Knowledge & Dictionary
                    case "dictionary.search":
                        string q          = Received.Result.Parameters["q"].ToString();
                        var    definition = Dictionary.Define(q);
                        if (definition == null)
                        {
                            HistoryWrite(Received.Result.Fulfillment.Speech, speech);
                        }
                        else
                        {
                            var First = definition[0];
                            HistoryWrite(q + " has a " + First.lexicalCategory + " meaning, " + First.entries[0].senses[0].definitions[0], speech, false);
                            HistoryWrite(q, false, true, true);
                            foreach (var entry in definition)
                            {
                                HistoryWrite(entry.lexicalCategory, false, true, true);
                                foreach (var def in entry.entries[0].senses)
                                {
                                    HistoryWrite("-" + def.definitions[0], false, true, true);
                                }
                            }
                        }
                        break;

                    case "knowledge.search":

                        var knowledgeresult = Knowledge.GetKnowledge(Received.Result.Parameters["questionword"].ToString() + " " + Received.Result.Parameters["q"].ToString());
                        if (knowledgeresult != null)
                        {
                            //BitmapImage bitmap = new BitmapImage(new Uri(@"Assets\simple.jpg", UriKind.Relative));
                            BitmapImage bitmap = new BitmapImage();
                            BitmapImage bik    = new BitmapImage();
                            try
                            {
                                bitmap = new BitmapImage(new Uri(knowledgeresult["simpleurl"], UriKind.Absolute));
                            }
                            catch { }
                            try
                            {
                                bik = new BitmapImage(new Uri(knowledgeresult["imageurl"], UriKind.Absolute));
                            }
                            catch { }
                            image.Source = bik;
                            Image simpleImage = new Image();
                            simpleImage.Source  = bitmap;
                            simpleImage.Stretch = Stretch.Uniform;
                            InlineUIContainer container = new InlineUIContainer(simpleImage);
                            Paragraph         paragraph = new Paragraph(container);
                            HistoryWrite(knowledgeresult["response"], speech);
                            textBox1.Document.Blocks.Add(paragraph);
                        }
                        else
                        {
                            Process.Start(String.Concat("https://www.google.com/search?q=" + Received.Result.Parameters["q"]));
                        }

                        break;

                        #endregion
                    default:
                        HistoryWrite(Received.Result.Fulfillment.Speech, speech);
                        break;
                    }
                    #endregion
                }
                StartCommandWaiting.Invoke(this, new EventArgs());
                IsDone = true;
            }
        }