Exemplo n.º 1
0
        //*************************************************/
        // CONSTRUCTOR
        //*************************************************/
        #region Constructor

        public MathChatBotHelper()
        {
            NLPHelper = new NLPHelper();
            SimpleCalculatorHelper = new SimpleCalculatorHelper();
            Messages = new ObservableCollection <MessageObject>();

            GetCommands();
        }
Exemplo n.º 2
0
        /// <summary>
        /// Analyze a text message from the user
        /// </summary>
        /// <param name="text"></param>
        /// <returns>A message result</returns>
        private MessageResult AnalyzeText(string text)
        {
            try
            {
                var tempText = text.ToLower();

                // Get sentences that has been written
                var sentences = NLPHelper.GetSentences(tempText);

                // Does not support more than one sentence
                if (sentences.Count > 1)
                {
                    return(new MessageResult(Properties.Resources.i_cannot_process_more_than_one_sentence));
                }

                // Tagging on the given words
                List <TaggedWord> wordList = null;

                // Function for tagging
                Action <bool> tagging = (useSavedSentences) =>
                {
                    // Insert a question mark at the end of a text
                    if (!tempText.EndsWith("?"))
                    {
                        tempText += "?";
                    }
                    // Tag the words in the text
                    wordList = NLPHelper.Tag(tempText, useSavedSentences);
                };

                PerformanceTester.StartMET("Use calculator");
                // If text has any numbers use the calculator
                string output;
                if (SimpleCalculatorHelper.CheckInput(ref tempText, out output))
                {
                    // If text is not math command
                    if (output == null)
                    {
                        var input = tempText;

                        tagging(false);

                        if (wordList.Any(x => x.IsWHWord || x.IsVerb))
                        {
                            // Skip the WH-words and verbs in the beginning when using calculator
                            var context = wordList
                                          .SkipWhile(x => x.IsWHWord || x.IsVerb)
                                          .Where(x => x.POSStringIdentifier != ".")
                                          .ToList();
                            // Get text
                            var joined = context.ListToString();
                            // If sentence starts with a WH word insert an equal sign
                            if (wordList.FirstOrDefault().IsWHWord)
                            {
                                input = joined.Insert(0, "=");
                            }
                        }

                        // Use calculator
                        output = SimpleCalculatorHelper.UseCalculator(input);
                        PerformanceTester.StopMET("Use calculator");
                    }

                    // If output is not null write the calculator result to the user
                    if (output != null)
                    {
                        return(new MessageResult()
                        {
                            Messages = new MessageObject[] { new MessageObject(output) }
                        });
                    }
                }
                PerformanceTester.StopMET("Use calculator");

                tagging(true);

                PerformanceTester.StartMET("Analyze word list");
                // Analyze the input
                var analyzeResult = AnalyzeWordList(wordList);
                PerformanceTester.StopMET("Analyze word list");

                // If success
                if (analyzeResult.IsSuccess)
                {
                    // Analyze the search strings
                    return(AnalyzeSearchStrings(analyzeResult.SearchStrings));
                }
                // If error
                else
                {
                    return(new MessageResult(analyzeResult.ErrorMessage));
                }
            }
            catch (Exception mes)
            {
                return(new MessageResult(mes.Message));
            }
        }