Example #1
0
 public override string ProcessChange()
 {
     while (true)
     {
         if (Template.Name.ToLower() == "gender")
         {
             var text = Template.InnerText;
             if (text.Length > 0)
             {
                 // non atomic version of the node
                 return text.Substitute(ChatBot.Genders);
             }
             // atomic version of the node
             var starNode = GetNode("<star/>");
             var recursiveStar = new Star(Query, Request, starNode);
             Template.InnerText = recursiveStar.ProcessChange();
             if (!string.IsNullOrEmpty(Template.InnerText))
             {
                 continue;
             }
             return string.Empty;
         }
         return string.Empty;
     }
 }
Example #2
0
File: Sr.cs Project: rlebowitz/ai
        public override string ProcessChange()
        {
            if (Template.Name.ToLower() == "sr")
            {
                XmlNode starNode = GetNode("<star/>");
                Star recursiveStar = new Star(Query, Request, starNode);
                string starContent = recursiveStar.ProcessChange();

                XmlNode sraiNode = GetNode("<srai>" + starContent + "</srai>");
                Srai sraiHandler = new Srai(ChatBot, User, Request, sraiNode);
                return sraiHandler.ProcessChange();
            }
            return string.Empty;
        }
Example #3
0
        public override string ProcessChange()
        {
            while (true)
            {
                if (Template.Name.ToLower() != "sentence") return string.Empty;
                if (Template.InnerText.Length > 0)
                {
                    var result = new StringBuilder();
                    var letters = Template.InnerText.Trim().ToCharArray();
                    var doChange = true;
                    foreach (var t in letters)
                    {
                        var letterAsString = Convert.ToString(t);
                        if (ChatBot.Splitters.Contains(letterAsString))
                        {
                            doChange = true;
                        }

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

                        if (lowercaseLetter.IsMatch(letterAsString))
                        {
                            if (doChange)
                            {
                                result.Append(letterAsString.ToUpper(CultureInfo.CurrentCulture));
                                doChange = false;
                            }
                            else
                            {
                                result.Append(letterAsString.ToLower(CultureInfo.CurrentCulture));
                            }
                        }
                        else
                        {
                            result.Append(letterAsString);
                        }
                    }
                    return result.ToString();
                }
                // atomic version of the node
                var starNode = GetNode("<star/>");
                var recursiveStar = new Star(Query, Request, starNode);
                Template.InnerText = recursiveStar.ProcessChange();
                if (!string.IsNullOrEmpty(Template.InnerText)) continue;
                return string.Empty;
            }
        }
Example #4
0
 public void TestExpectedInputIndexSecond()
 {
     var testNode = StaticHelpers.GetNode("<star index=\"2\"/>");
     _tagHandler = new Star(_query, _request, testNode);
     Assert.AreEqual("first star", _tagHandler.ProcessChange());
 }
Example #5
0
 public void TestExpectedInput()
 {
     var testNode = StaticHelpers.GetNode("<star/>");
     _tagHandler = new Star(_query, _request, testNode);
     Assert.AreEqual("second star", _tagHandler.ProcessChange());
 }
Example #6
0
 public void TestBadInputTagName()
 {
     var testNode = StaticHelpers.GetNode("<stor index=\"1\"/>");
     _tagHandler = new Star(_query, _request, testNode);
     Assert.AreEqual("", _tagHandler.ProcessChange());
 }
Example #7
0
 /// <summary>
 ///     Recursively evaluates the template nodes returned from the ChatBot
 /// </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="user">the user who originated the request</param>
 /// <returns>the output string</returns>
 private string ProcessNode(XmlNode node, SubQuery query, Request request, User user)
 {
     // process the node
     var tagName = node.Name.ToLower();
     if (tagName == "template")
     {
         var templateResult = new StringBuilder();
         if (node.HasChildNodes)
         {
             // recursively check
             foreach (XmlNode childNode in node.ChildNodes)
             {
                 templateResult.Append(ProcessNode(childNode, query, request, user));
             }
         }
         return templateResult.ToString();
     }
     AIMLTagHandler tagHandler = null;
     switch (tagName)
     {
         case "bot":
             tagHandler = new Bot(node);
             break;
         case "condition":
             tagHandler = new Condition(user, node);
             break;
         case "date":
             tagHandler = new Date(node);
             break;
         case "formal":
             tagHandler = new Formal(node);
             break;
         case "gender":
             tagHandler = new AIMLTagHandlers.Gender(query, request, node);
             break;
         case "get":
             tagHandler = new Get(user, node);
             break;
         case "gossip":
             tagHandler = new Gossip(user, node);
             break;
         case "id":
             tagHandler = new Id(user, node);
             break;
         case "input":
             tagHandler = new Input(user, request, node);
             break;
         case "javascript":
             tagHandler = new Javascript(node);
             break;
         case "learn":
             tagHandler = new Learn(node);
             break;
         case "lowercase":
             tagHandler = new Lowercase(node);
             break;
         case "person":
             tagHandler = new Person(query, request, node);
             break;
         case "person2":
             tagHandler = new Person2(query, request, node);
             break;
         case "random":
             tagHandler = new Random(node);
             break;
         case "sentence":
             tagHandler = new Sentence(query, request, node);
             break;
         case "set":
             tagHandler = new Set(user, node);
             break;
         case "size":
             tagHandler = new Size(node);
             break;
         case "sr":
             tagHandler = new Sr(this, user, query, request, node);
             break;
         case "srai":
             tagHandler = new Srai(this, user, request, node);
             break;
         case "star":
             tagHandler = new Star(query, request, node);
             break;
         case "system":
             tagHandler = new SystemTag(node);
             break;
         case "that":
             tagHandler = new That(user, request, node);
             break;
         case "thatstar":
             tagHandler = new ThatStar(query, request, node);
             break;
         case "think":
             tagHandler = new Think(node);
             break;
         case "topicstar":
             tagHandler = new Topicstar(query, request, node);
             break;
         case "uppercase":
             tagHandler = new Uppercase(node);
             break;
         case "version":
             tagHandler = new Version(node);
             break;
         default:
             Log.ErrorFormat("Unknown AIML tag: {0}", tagName);
             break;
     }
     if (Equals(null, tagHandler))
     {
         return node.InnerText;
     }
     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, user);
                 }
             }
         }
         return tagHandler.ProcessChange();
     }
     var resultNodeInnerXML = tagHandler.ProcessChange();
     var resultNode = AIMLTagHandler.GetNode("<node>" + resultNodeInnerXML + "</node>");
     if (resultNode.HasChildNodes)
     {
         var recursiveResult = new StringBuilder();
         // recursively check
         foreach (XmlNode childNode in resultNode.ChildNodes)
         {
             recursiveResult.Append(ProcessNode(childNode, query, request, user));
         }
         return recursiveResult.ToString();
     }
     return resultNode.InnerXml;
 }