/// <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) { 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); return(result); }
/// <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); // Normalize the input AIMLLoader loader = new AIMLLoader(this); AimlStandard.Normalize.SplitIntoSentences splitter = new AimlStandard.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) { string template = this.Graphmaster.Evaluate(path, request, MatchState.UserInput, new StringBuilder()); result.Templates.Add(template); } // process the templates into appropriate output foreach (string template in result.Templates) { if (template.Length > 0) { try { XmlNode templateNode = AIMLTagHandler.GetNode(template); string outputSentence = this.ProcessNode(templateNode, request, result, request.User); if (outputSentence.Length > 0) { result.OutputSentences.Add(outputSentence); } } catch (Exception e) { if (this.WillCallHome) { this.PhoneHome(e.Message, request); } this.WriteToLog("WARNING! A mal-formed template was encountered when trying to process the input: " + request.RawInput); } } } // populate the Result object result.Duration = DateTime.Now - request.StartedOn; request.User.AddResult(result); return(result); }
/// <summary> /// Recursively evaluates the template nodes returned from the bot /// </summary> /// <param name="node">the node to evaluate</param> /// <param name="query">the query that produced this node</param> /// <param name="request">the request from the user</param> /// <param name="result">the result to be sent to the user</param> /// <param name="user">the user who originated the request</param> /// <returns>the output string</returns> private string ProcessNode(XmlNode node, SubQuery query, Request request, Result result, User user) { // check for timeout (to avoid infinite loops) if (request.StartedOn.AddMilliseconds(request.Bot.TimeOut) < DateTime.Now) { request.Bot.WriteToLog("WARNING! Request timeout. User: "******" raw input: \"" + request.RawInput + "\" processing template: \"" + query.Template + "\""); request.HasTimedOut = true; return(string.Empty); } // process the node string tagName = node.Name.ToLower(); if (tagName == "template") { StringBuilder templateResult = new StringBuilder(); if (node.HasChildNodes) { // recursively check foreach (XmlNode childNode in node.ChildNodes) { templateResult.Append(ProcessNode(childNode, query, request, result, user)); } } return(templateResult.ToString()); } else { AIMLTagHandler tagHandler = null; tagHandler = getBespokeTags(user, query, request, result, node); if (Equals(null, tagHandler)) { switch (tagName) { case "bot": tagHandler = new AIMLTagHandlers.Bot(this, user, query, request, result, node); break; case "condition": tagHandler = new AIMLTagHandlers.Condition(this, user, query, request, result, node); break; case "date": tagHandler = new AIMLTagHandlers.Date(this, user, query, request, result, node); break; case "formal": tagHandler = new AIMLTagHandlers.Formal(this, user, query, request, result, node); break; case "gender": tagHandler = new AIMLTagHandlers.Gender(this, user, query, request, result, node); break; case "get": tagHandler = new AIMLTagHandlers.Get(this, user, query, request, result, node); break; case "gossip": tagHandler = new AIMLTagHandlers.Gossip(this, user, query, request, result, node); break; case "id": tagHandler = new AIMLTagHandlers.Id(this, user, query, request, result, node); break; case "input": tagHandler = new AIMLTagHandlers.Input(this, user, query, request, result, node); break; case "javascript": tagHandler = new AIMLTagHandlers.Javascript(this, user, query, request, result, node); break; case "learn": tagHandler = new AIMLTagHandlers.Learn(this, user, query, request, result, node); break; case "lowercase": tagHandler = new AIMLTagHandlers.Lowercase(this, user, query, request, result, node); break; case "person": tagHandler = new AIMLTagHandlers.Person(this, user, query, request, result, node); break; case "person2": tagHandler = new AIMLTagHandlers.Person2(this, user, query, request, result, node); break; case "random": tagHandler = new AIMLTagHandlers.Random(this, user, query, request, result, node); break; case "sentence": tagHandler = new AIMLTagHandlers.Sentence(this, user, query, request, result, node); break; case "set": tagHandler = new AIMLTagHandlers.Set(this, user, query, request, result, node); break; case "size": tagHandler = new AIMLTagHandlers.Size(this, user, query, request, result, node); break; case "sr": tagHandler = new AIMLTagHandlers.Sr(this, user, query, request, result, node); break; case "srai": tagHandler = new AIMLTagHandlers.Srai(this, user, query, request, result, node); break; case "star": tagHandler = new AIMLTagHandlers.Star(this, user, query, request, result, node); break; case "system": tagHandler = new AIMLTagHandlers.System(this, user, query, request, result, node); break; case "that": tagHandler = new AIMLTagHandlers.That(this, user, query, request, result, node); break; case "thatstar": tagHandler = new AIMLTagHandlers.ThatStar(this, user, query, request, result, node); break; case "think": tagHandler = new AIMLTagHandlers.Think(this, user, query, request, result, node); break; case "topicstar": tagHandler = new AIMLTagHandlers.TopicStar(this, user, query, request, result, node); break; case "uppercase": tagHandler = new AIMLTagHandlers.Uppercase(this, user, query, request, result, node); break; case "version": tagHandler = new AIMLTagHandlers.Version(this, user, query, request, result, node); break; default: tagHandler = null; break; } } if (Equals(null, tagHandler)) { return(node.InnerText); } else { if (tagHandler.IsRecursive) { if (node.HasChildNodes) { // recursively check foreach (XmlNode childNode in node.ChildNodes) { if (childNode.NodeType != XmlNodeType.Text) { childNode.InnerXml = ProcessNode(childNode, query, request, result, user); } } } return(tagHandler.Transform()); } else { string resultNodeInnerXML = tagHandler.Transform(); XmlNode resultNode = AIMLTagHandler.GetNode("<node>" + resultNodeInnerXML + "</node>"); if (resultNode.HasChildNodes) { StringBuilder recursiveResult = new StringBuilder(); // recursively check foreach (XmlNode childNode in resultNode.ChildNodes) { recursiveResult.Append(ProcessNode(childNode, query, request, result, user)); } return(recursiveResult.ToString()); } else { return(resultNode.InnerXml); } } } } }
/// <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); if (_config.isAcceptingUserInput) { // Normalize the input ReniBot.AimlEngine.Normalize.SplitIntoSentences splitter = new ReniBot.AimlEngine.Normalize.SplitIntoSentences(_config); string[] rawSentences = splitter.Transform(request.RawInput); foreach (string sentence in rawSentences) { result.InputSentences.Add(sentence); string topic = _botUserService.GetTopic(request.UserId); string lastOutput = _userResultService.GetLastOutput(request.UserId); string path = _loader.GeneratePath(sentence, lastOutput, topic, true); result.NormalizedPaths.Add(path); } // grab the templates for the various sentences from the graphmaster foreach (string path in result.NormalizedPaths) { Utils.SubQuery query = new SubQuery(path); query.Template = _graphmaster.Evaluate(path, query, request, MatchState.UserInput, new StringBuilder(), _config.TimeOut); result.HasTimedOut = request.HasTimedOut; 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, new User(_predicateService, _userResultService, _requestService) { UserId = request.UserId }); if (outputSentence.Length > 0) { result.OutputSentences.Add(outputSentence); } } catch (Exception e) { if (_config.WillCallHome) { PhoneHome(e.Message, request); } _logger.LogWarning("A problem was encountered when trying to process the input: " + request.RawInput + " with the template: \"" + query.Template + "\""); } } } } else { result.OutputSentences.Add(_config.NotAcceptingUserInputMessage); } // populate the Result object result.Duration = DateTime.Now - request.StartedOn; _userResultService.Add(result.Duration.Milliseconds, result.HasTimedOut, result.RawOutput, result.RequestId, result.UserId); return(result); }
/// <summary> /// Recursively evaluates the template nodes returned from the bot /// </summary> /// <param name="node">the node to evaluate</param> /// <param name="query">the query that produced this node</param> /// <param name="request">the request from the user</param> /// <param name="result">the result to be sent to the user</param> /// <param name="user">the user who originated the request</param> /// <returns>the output string</returns> private string ProcessNode(XmlNode node, SubQuery query, Request request, Result result, User user) { // check for timeout (to avoid infinite loops) if (request.StartedOn.AddMilliseconds(_config.TimeOut) < DateTime.Now) { //_logger.LogWarning("Request timeout. User: "******" raw input: \"" + request.rawInput + "\" processing template: \""+query.Template+"\""); request.HasTimedOut = true; return(string.Empty); } // process the node string tagName = node.Name.ToLower(); if (tagName == "template") { StringBuilder templateResult = new StringBuilder(); if (node.HasChildNodes) { // recursively check foreach (XmlNode childNode in node.ChildNodes) { templateResult.Append(ProcessNode(childNode, query, request, result, user)); } } return(templateResult.ToString()); } else { AIMLTagHandler tagHandler = _tagFactory.CreateTagHandler(tagName, this, user, query, request, result, node); if (object.Equals(null, tagHandler)) { return(node.InnerText); } else { if (tagHandler.isRecursive) { if (node.HasChildNodes) { // recursively check foreach (XmlNode childNode in node.ChildNodes) { if (childNode.NodeType != XmlNodeType.Text) { childNode.InnerXml = ProcessNode(childNode, query, request, result, user); } } } return(tagHandler.Transform()); } else { string resultNodeInnerXML = tagHandler.Transform(); XmlNode resultNode = AIMLTagHandler.GetNode("<node>" + resultNodeInnerXML + "</node>"); if (resultNode.HasChildNodes) { StringBuilder recursiveResult = new StringBuilder(); // recursively check foreach (XmlNode childNode in resultNode.ChildNodes) { recursiveResult.Append(ProcessNode(childNode, query, request, result, user)); } return(recursiveResult.ToString()); } else { return(resultNode.InnerXml); } } } } }