예제 #1
0
        /// <summary>
        /// Given a request containing user input, produces a result from the bot
        /// </summary>
        /// <param name="request">the request from the user</param>
        /// <returns>the result to be output to the user</returns>
        public Result Chat(Request request)
        {
            Result result = new Result(request.user, this, request);

            if (isAcceptingUserInput)
            {
                // Normalize the input
                AIMLLoader loader = new AIMLLoader(this);
                Normalize.SplitIntoSentences splitter = new Normalize.SplitIntoSentences(this);
                string[] rawSentences = splitter.Transform(request.rawInput);
                foreach (string sentence in rawSentences)
                {
                    result.InputSentences.Add(sentence);
                    string path = loader.generatePath(sentence, request.user.getLastBotOutput(), request.user.Topic, true);
                    result.NormalizedPaths.Add(path);
                }

                // grab the templates for the various sentences from the graphmaster
                foreach (string path in result.NormalizedPaths)
                {
                    SubQuery query = new SubQuery(path);
                    query.Template = Graphmaster.evaluate(path, query, request, MatchState.UserInput, new StringBuilder());
                    result.SubQueries.Add(query);
                }

                // process the templates into appropriate output
                foreach (SubQuery query in result.SubQueries)
                {
                    if (query.Template.Length > 0)
                    {
                        try
                        {
                            XmlNode templateNode   = AIMLTagHandler.getNode(query.Template);
                            string  outputSentence = processNode(templateNode, query, request, result, request.user);
                            if (outputSentence.Length > 0)
                            {
                                result.OutputSentences.Add(outputSentence);
                            }
                        }
                        catch (Exception e)
                        {
                            if (WillCallHome)
                            {
                                phoneHome(e.Message, request);
                            }
                            writeToLog("WARNING! A problem was encountered when trying to process the input: " + request.rawInput + " with the template: \"" + query.Template + "\"");
                        }
                    }
                }
            }
            else
            {
                result.OutputSentences.Add(NotAcceptingUserInputMessage);
            }

            // populate the Result object
            result.Duration = DateTime.Now - request.StartedOn;
            request.user.addResult(result);
            writeToLog("Вопрос: " + result.RawInput);
            writeToLog("Ответ: " + result.RawOutput);

            return(result);
        }
예제 #2
0
 public void testSplitterAllMadeFromWhiteSpace()
 {
     this.mockSplitter = new SplitIntoSentences(this.mockBot, "     ");
     Assert.AreEqual(new string[0], this.mockSplitter.Transform());
 }
예제 #3
0
 public void testSplitterAllSentenceMadeOfSplitters()
 {
     this.mockSplitter = new SplitIntoSentences(this.mockBot, "!?.;");
     Assert.AreEqual(new string[0], this.mockSplitter.Transform());
 }
예제 #4
0
 public void testSplitterAllSentenceWithSplitterAtStart()
 {
     this.mockSplitter = new SplitIntoSentences(this.mockBot, ".This is a sentence without splitters");
     Assert.AreEqual("This is a sentence without splitters", (string)this.mockSplitter.Transform()[0]);
 }
예제 #5
0
 public void testSplitterAllNoRawInput()
 {
     this.mockSplitter = new SplitIntoSentences(this.mockBot, "");
     Assert.AreEqual(new string[0], this.mockSplitter.Transform());
 }
예제 #6
0
 public void testSplitterAllTokensPassedByMethod()
 {
     this.mockSplitter = new SplitIntoSentences(this.mockBot);
     Assert.AreEqual(this.goodResult, this.mockSplitter.Transform(rawInput));
 }