예제 #1
1
        public void TestEvaluateTimeOut()
        {
            _chatBot = new ChatBot();
            ConfigurationManager.AppSettings["timeoutMax"] = "10";
            _node = new Node();
            _request = new Request("Test 1", new User("1", _chatBot), _chatBot);

            var path = "Test 1 <that> that <topic> topic";
            var template = "<srai>TEST</srai>";

            _node = new Node();
            _node.AddCategory(path, template);

            var pathAlt = "Alt Test <that> that <topic> topic";
            var templateAlt = "<srai>TEST ALT</srai>";

            _node.AddCategory(pathAlt, templateAlt);
            _subQuery = new SubQuery(path);

            Thread.Sleep(20);

            var result =
                _node.Evaluate("Test 1 <that> that <topic> topic", _subQuery, _request, MatchState.UserInput,
                    new StringBuilder());
            Assert.AreEqual(string.Empty, result);
            Assert.AreEqual(true, _request.HasTimedOut);
        }
예제 #2
0
        public void TestAddCategoryWithGoodData()
        {
            var path = "Test 1 <that> * <topic> *";
            var template = "<srai>Test</srai>";
            _node = new Node();
            _node.AddCategory(path, template);

            Assert.AreEqual(1, _node.NumberOfChildNodes);
            Assert.AreEqual(string.Empty, _node.Template);
            Assert.AreEqual(string.Empty, _node.Word);
        }
예제 #3
0
        public void TestAddCategoryAsLeafNode()
        {
            var node = new Node();
            var path = "";
            var template = "<srai>TEST</srai>";
            node.AddCategory(path, template);
            node.Word = "*";

            Assert.AreEqual(0, node.NumberOfChildNodes);
            Assert.AreEqual(template, node.Template);
            Assert.AreEqual("*", node.Word);
        }
예제 #4
0
        /// <summary>
        ///     Adds a category to the node
        /// </summary>
        /// <param name="node">the node to which the category will be added</param>
        /// <param name="path">the path for the category</param>
        /// <param name="template">the template to find at the end of the path</param>
        public static void AddCategory(this Node node, string path, string template)
        {
            while (true)
            {
                if (template.Length == 0)
                {
                    var message = $"Category {path} has an empty template tag.";
                    throw new XmlException(message);
                }
                // check we're not at the leaf node
                if (path.Trim().Length == 0)
                {
                    node.Template = template;
                    return;
                }

                // otherwise, this sentence requires further child nodemappers in order to
                // be fully mapped within the GraphMaster structure.

                // split the input into its component words
                var words = path.Trim().Split(" ".ToCharArray());

                // get the first word (to form the key for the child nodemapper)
                var firstWord = words[0].ToUpper();

                // concatenate the rest of the sentence into a suffix (to act as the
                // path argument in the child nodemapper)
                // ToDo: should just join array minus first word.
                var newPath = path.Substring(firstWord.Length, path.Length - firstWord.Length).Trim();

                // o.k. check we don't already have a child with the key from this sentence
                // if we do then pass the handling of this sentence down the branch to the 
                // child nodemapper otherwise the child nodemapper doesn't yet exist, so create a new one
                if (node.Children.ContainsKey(firstWord))
                {
                    var childNode = node.Children[firstWord];
                    node = childNode;
                    path = newPath;
                    continue;
                }
                else
                {
                    var childNode = new Node {Word = firstWord};
                    childNode.AddCategory(newPath, template);
                    node.Children.Add(childNode.Word, childNode);
                }
                break;
            }
        }
예제 #5
0
        public void TestEvaluateWith_WildCardThat()
        {
            var path = "Test 1 <that> _ <topic> topic";
            var template = "<srai>TEST</srai>";
            var node = new Node();
            node.AddCategory(path, template);

            var pathAlt = "Alt Test <that> that <topic> topic";
            var templateAlt = "<srai>TEST ALT</srai>";
            node.AddCategory(pathAlt, templateAlt);

            var request = new Request("Test 1", _user);
            var searcher = new NodeSearcher();
            var result = searcher.Evaluate(node, "Test 1 <that> WILDCARD WORDS <topic> topic", MatchState.UserInput,
                new StringBuilder());

            Assert.AreEqual("<srai>TEST</srai>", result);
            Assert.AreEqual("WILDCARD WORDS", searcher.Query.ThatStar[0]);
        }
예제 #6
0
        public void testEvaluateWith_WildCardThat()
        {
            var path = "Test 1 <that> _ <topic> topic";
            var template = "<srai>TEST</srai>";
            _node = new Node();
            _node.AddCategory(path, template);

            var pathAlt = "Alt Test <that> that <topic> topic";
            var templateAlt = "<srai>TEST ALT</srai>";

            _node.AddCategory(pathAlt, templateAlt);
            _request = new Request("Test 1", new User("1", _chatBot), _chatBot);
            _subQuery = new SubQuery(path);
            var result = _node.Evaluate("Test 1 <that> WILDCARD WORDS <topic> topic", _subQuery, _request,
    MatchState.UserInput, new StringBuilder());

            Assert.AreEqual("<srai>TEST</srai>", result);
            Assert.AreEqual("WILDCARD WORDS", _subQuery.ThatStar[0]);
        }
예제 #7
0
        public void TestEvaluateWithWildcardsInDifferentPartsOfPath()
        {
            var path = "Test * 1 <that> Test * 1 <topic> Test * 1";
            var template = "<srai>TEST</srai>";

            _node = new Node();
            _node.AddCategory(path, template);

            var pathAlt = "Alt Test <that> that <topic> topic";
            var templateAlt = "<srai>TEST ALT</srai>";

            _node.AddCategory(pathAlt, templateAlt);

            _request = new Request("Test 1", new User("1", _chatBot), _chatBot);
            _subQuery = new SubQuery(path);

            Assert.AreEqual("<srai>TEST</srai>",
                _node.Evaluate(
                    "Test WILDCARD USER WORDS 1 <that> Test WILDCARD THAT WORDS 1 <topic> Test WILDCARD TOPIC WORDS 1",
                    _subQuery, _request, MatchState.UserInput, new StringBuilder()));
            Assert.AreEqual("WILDCARD USER WORDS", _subQuery.InputStar[0]);
            Assert.AreEqual("WILDCARD THAT WORDS", _subQuery.ThatStar[0]);
            Assert.AreEqual("WILDCARD TOPIC WORDS", _subQuery.TopicStar[0]);
        }
예제 #8
0
        public void TestEvaluateWithStarWildCardUserInputNotMatched()
        {
            var path = "Test * 1 <that> that <topic> topic>";
            var template = "<srai>TEST</srai>";
            _node = new Node();
            _node.AddCategory(path, template);

            var pathAlt = "Alt Test <that> that <topic> topic";
            var templateAlt = "<srai>TEST ALT</srai>";

            _node.AddCategory(pathAlt, templateAlt);
            _request = new Request("Test 1", new User("1", _chatBot), _chatBot);
            _subQuery = new SubQuery(path);
            Assert.AreEqual("<srai>TEST ALT</srai>",
                _node.Evaluate("Alt Test <that> that <topic> topic", _subQuery, _request,
                    MatchState.UserInput, new StringBuilder()));
        }
예제 #9
0
        public void TestEvaluateWithMultipleWildcardsSwitched()
        {
            var path = "Test * 1 _ <that> Test * 1 _ <topic> Test _ 1 *";
            var template = "<srai>TEST</srai>";

            _node = new Node();
            _node.AddCategory(path, template);

            var pathAlt = "Alt Test <that> that <topic> topic";
            var templateAlt = "<srai>TEST ALT</srai>";

            _node.AddCategory(pathAlt, templateAlt);

            _request = new Request("Test 1", new User("1", _chatBot), _chatBot);
            _subQuery = new SubQuery(path);

            Assert.AreEqual("<srai>TEST</srai>",
                _node.Evaluate(
                    "Test FIRST USER 1 SECOND USER <that> Test FIRST THAT 1 SECOND THAT <topic> Test FIRST TOPIC 1 SECOND TOPIC",
                    _subQuery, _request, MatchState.UserInput, new StringBuilder()));
            Assert.AreEqual(2, _subQuery.InputStar.Count);
            Assert.AreEqual("SECOND USER", _subQuery.InputStar[0]);
            Assert.AreEqual("FIRST USER", _subQuery.InputStar[1]);
            Assert.AreEqual(2, _subQuery.ThatStar.Count);
            Assert.AreEqual("SECOND THAT", _subQuery.ThatStar[0]);
            Assert.AreEqual("FIRST THAT", _subQuery.ThatStar[1]);
            Assert.AreEqual(2, _subQuery.TopicStar.Count);
            Assert.AreEqual("SECOND TOPIC", _subQuery.TopicStar[0]);
            Assert.AreEqual("FIRST TOPIC", _subQuery.TopicStar[1]);
        }
예제 #10
0
        public void TestEvaluateWithInternationalCharset()
        {
            var path = "中 文 <that> * <topic> *";
            var template = "中文 (Chinese)";

            _node = new Node();
            _node.AddCategory(path, template);

            var path2 = "日 本 語 <that> * <topic> *";
            var template2 = "日 本 語 (Japanese)";

            _node.AddCategory(path2, template2);

            var path3 = "Русский язык <that> * <topic> *";
            var template3 = "Русский язык (Russian)";

            _node.AddCategory(path3, template3);

            _request = new Request("中 文", new User("1", _chatBot), _chatBot);
            _subQuery = new SubQuery(path);
            Assert.AreEqual("中文 (Chinese)",
                _node.Evaluate("中 文 <that> * <topic> *", _subQuery, _request, MatchState.UserInput,
                    new StringBuilder()));

            _request = new Request("日 本 語", new User("1", _chatBot), _chatBot);
            _subQuery = new SubQuery(path);
            Assert.AreEqual("日 本 語 (Japanese)",
                _node.Evaluate("日 本 語 <that> * <topic> *", _subQuery, _request, MatchState.UserInput,
                    new StringBuilder()));

            _request = new Request("Русский язык", new User("1", _chatBot), _chatBot);
            _subQuery = new SubQuery(path);
            Assert.AreEqual("Русский язык (Russian)",
                _node.Evaluate("Русский язык <that> * <topic> *", _subQuery, _request,
                    MatchState.UserInput, new StringBuilder()));
        }
예제 #11
0
        public void TestEvaluateWithWildcardsInDifferentPartsOfPath()
        {
            var node = new Node();
            var path = "Test * 1 <that> Test * 1 <topic> Test * 1";
            var template = "<srai>TEST</srai>";
            node.AddCategory(path, template);

            var pathAlt = "Alt Test <that> that <topic> topic";
            var templateAlt = "<srai>TEST ALT</srai>";
            node.AddCategory(pathAlt, templateAlt);

            var request = new Request("Test 1", _user);
            var searcher = new NodeSearcher();
            var result = searcher.Evaluate(node,
                "Test WILDCARD USER WORDS 1 <that> Test WILDCARD THAT WORDS 1 <topic> Test WILDCARD TOPIC WORDS 1",
                MatchState.UserInput, new StringBuilder());
            Assert.AreEqual("<srai>TEST</srai>", result);
            Assert.AreEqual("WILDCARD USER WORDS", searcher.Query.InputStar[0]);
            Assert.AreEqual("WILDCARD THAT WORDS", searcher.Query.ThatStar[0]);
            Assert.AreEqual("WILDCARD TOPIC WORDS", searcher.Query.TopicStar[0]);
        }
예제 #12
0
        public void TestEvaluateWithStarWildCardUserInputNotMatched()
        {
            var node = new Node();

            var path = "Test * 1 <that> that <topic> topic>";
            var template = "<srai>TEST</srai>";
            node.AddCategory(path, template);

            var pathAlt = "Alt Test <that> that <topic> topic";
            var templateAlt = "<srai>TEST ALT</srai>";
            node.AddCategory(pathAlt, templateAlt);

            var request = new Request("Test 1", _user);
            var searcher = new NodeSearcher();
            var result = searcher.Evaluate(node, "Alt Test <that> that <topic> topic", MatchState.UserInput,
                new StringBuilder());
            Assert.AreEqual("<srai>TEST ALT</srai>", result);
        }
예제 #13
0
 public void TestAddCategoryWithEmptyTemplate()
 {
     var node = new Node();
     var path = "Test 1 <that> * <topic> *";
     node.AddCategory(path, string.Empty);
 }
예제 #14
0
        public void TestEvaluateWithMultipleWildcardsSwitched()
        {
            var node = new Node();
            var path = "Test * 1 _ <that> Test * 1 _ <topic> Test _ 1 *";
            var template = "<srai>TEST</srai>";
            node.AddCategory(path, template);

            var pathAlt = "Alt Test <that> that <topic> topic";
            var templateAlt = "<srai>TEST ALT</srai>";
            node.AddCategory(pathAlt, templateAlt);

            var request = new Request("Test 1", _user);
            var searcher = new NodeSearcher();
            var result = searcher.Evaluate(node,
                "Test FIRST USER 1 SECOND USER <that> Test FIRST THAT 1 SECOND THAT <topic> Test FIRST TOPIC 1 SECOND TOPIC",
                MatchState.UserInput, new StringBuilder());
            Assert.AreEqual("<srai>TEST</srai>", result);
            Assert.AreEqual(2, searcher.Query.InputStar.Count);
            Assert.AreEqual("SECOND USER", searcher.Query.InputStar[0]);
            Assert.AreEqual("FIRST USER", searcher.Query.InputStar[1]);
            Assert.AreEqual(2, searcher.Query.ThatStar.Count);
            Assert.AreEqual("SECOND THAT", searcher.Query.ThatStar[0]);
            Assert.AreEqual("FIRST THAT", searcher.Query.ThatStar[1]);
            Assert.AreEqual(2, searcher.Query.TopicStar.Count);
            Assert.AreEqual("SECOND TOPIC", searcher.Query.TopicStar[0]);
            Assert.AreEqual("FIRST TOPIC", searcher.Query.TopicStar[1]);
        }
예제 #15
0
        public void TestEvaluateWithInternationalCharset()
        {
            var node = new Node();

            var path = "中 文 <that> * <topic> *";
            var template = "中文 (Chinese)";
            node.AddCategory(path, template);

            var path2 = "日 本 語 <that> * <topic> *";
            var template2 = "日 本 語 (Japanese)";
            node.AddCategory(path2, template2);

            var path3 = "Русский язык <that> * <topic> *";
            var template3 = "Русский язык (Russian)";
            node.AddCategory(path3, template3);

            var request = new Request("中 文", _user);
            var searcher = new NodeSearcher();
            var result = searcher.Evaluate(node, "中 文 <that> * <topic> *", MatchState.UserInput, new StringBuilder());
            Assert.AreEqual("中文 (Chinese)", result);

            request = new Request("日 本 語", _user);
            searcher = new NodeSearcher();
            result = searcher.Evaluate(node, "日 本 語 <that> * <topic> *", MatchState.UserInput, new StringBuilder());
            Assert.AreEqual("日 本 語 (Japanese)", result);

            request = new Request("Русский язык", _user);
            searcher = new NodeSearcher();
            result = searcher.Evaluate(node, "Русский язык <that> * <topic> *", MatchState.UserInput,
                new StringBuilder());
            Assert.AreEqual("Русский язык (Russian)", result);
        }