protected override string ProcessChange()
 {
     if (this.templateNode.Name.ToLower() == "person")
     {
         if (this.templateNode.InnerText.Length > 0)
         {
             // non atomic version of the node
             return(KarmaloopAIMLBot.Normalize.ApplySubstitutions.Substitute(this.bot, this.bot.PersonSubstitutions, this.templateNode.InnerText));
         }
         else
         {
             // atomic version of the node
             XmlNode starNode      = Utils.AIMLTagHandler.getNode("<star/>");
             star    recursiveStar = new star(this.bot, this.user, this.query, this.request, this.result, starNode);
             this.templateNode.InnerText = recursiveStar.Transform();
             if (this.templateNode.InnerText.Length > 0)
             {
                 return(this.ProcessChange());
             }
             else
             {
                 return(string.Empty);
             }
         }
     }
     return(string.Empty);
 }
        protected override string ProcessChange()
        {
            if (this.templateNode.Name.ToLower() == "sentence")
            {
                if (this.templateNode.InnerText.Length > 0)
                {
                    StringBuilder result   = new StringBuilder();
                    char[]        letters  = this.templateNode.InnerText.Trim().ToCharArray();
                    bool          doChange = true;
                    for (int i = 0; i < letters.Length; i++)
                    {
                        string letterAsString = Convert.ToString(letters[i]);
                        if (this.bot.Splitters.Contains(letterAsString))
                        {
                            doChange = true;
                        }

                        Regex lowercaseLetter = new Regex("[a-zA-Z]");

                        if (lowercaseLetter.IsMatch(letterAsString))
                        {
                            if (doChange)
                            {
                                result.Append(letterAsString.ToUpper(this.bot.Locale));
                                doChange = false;
                            }
                            else
                            {
                                result.Append(letterAsString.ToLower(this.bot.Locale));
                            }
                        }
                        else
                        {
                            result.Append(letterAsString);
                        }
                    }
                    return(result.ToString());
                }
                else
                {
                    // atomic version of the node
                    XmlNode starNode      = Utils.AIMLTagHandler.getNode("<star/>");
                    star    recursiveStar = new star(this.bot, this.user, this.query, this.request, this.result, starNode);
                    this.templateNode.InnerText = recursiveStar.Transform();
                    if (this.templateNode.InnerText.Length > 0)
                    {
                        return(this.ProcessChange());
                    }
                    else
                    {
                        return(string.Empty);
                    }
                }
            }

            return(string.Empty);
        }
        protected override string ProcessChange()
        {
            if (this.templateNode.Name.ToLower() == "sr")
            {
                XmlNode starNode      = Utils.AIMLTagHandler.getNode("<star/>");
                star    recursiveStar = new star(this.bot, this.user, this.query, this.request, this.result, starNode);
                string  starContent   = recursiveStar.Transform();

                XmlNode sraiNode    = KarmaloopAIMLBot.Utils.AIMLTagHandler.getNode("<srai>" + starContent + "</srai>");
                srai    sraiHandler = new srai(this.bot, this.user, this.query, this.request, this.result, sraiNode);
                return(sraiHandler.Transform());
            }
            return(string.Empty);
        }
示例#4
0
        /// <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, BotUser 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(this.processNode(childNode, query, request, result, user));
                    }
                }
                return(templateResult.ToString());
            }
            else
            {
                AIMLTagHandler tagHandler = null;
                tagHandler = this.getBespokeTags(user, query, request, result, node);
                if (object.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 (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 = this.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(this.processNode(childNode, query, request, result, user));
                            }
                            return(recursiveResult.ToString());
                        }
                        else
                        {
                            return(resultNode.InnerXml);
                        }
                    }
                }
            }
        }