Exemplo n.º 1
0
    def test_check_child_is_wildcard_hash(self):

        wildcard = MockPatternWildCardNode("*")
        self.assertIsNotNone(wildcard)
        wildcard._0ormore_hash = PatternZeroOrMoreWildCardNode('#')
        wildcard._0ormore_hash._template = PatternTemplateNode(TemplateNode())

        context = MatchContext(max_search_depth=100,
                               max_search_timeout=-1,
                               tokenizer=self._client_context.brain.tokenizer)
        sentence = Sentence(self._client_context.brain.tokenizer,
                            "TEST SENTENCE")
        match = wildcard.check_child_is_wildcard("", self._client_context,
                                                 context, sentence, 1,
                                                 Match.WORD, 0)
        self.assertIsNotNone(match)

        context = MatchContext(max_search_depth=100,
                               max_search_timeout=-1,
                               tokenizer=self._client_context.brain.tokenizer)
        sentence = Sentence(self._client_context.brain.tokenizer, "TEST")
        match = wildcard.check_child_is_wildcard("", self._client_context,
                                                 context, sentence, 0,
                                                 Match.WORD, 0)
        self.assertIsNotNone(match)
Exemplo n.º 2
0
    def test_equals_attribs(self):
        self._client_context.brain._sets_collection.add_set(
            "TEST1", {
                "VALUE1": [["VALUE1"]],
                "VALUE2": [["VALUE2"]],
                "VALUE3": [["VALUE3"]],
                "VALUE4": [["VALUE4"]]
            }, "teststore")

        node1 = PatternSetNode({"name": "test1"}, "")
        node2 = PatternSetNode({"name": "test1"}, "", userid="testid")
        node3 = PatternSetNode({"name": "test1"}, "", userid="testid2")

        match1 = node1.equals(
            self._client_context,
            Sentence(self._client_context.brain.tokenizer, 'VALUE1'), 0)
        self.assertIsNotNone(match1)
        self.assertTrue(match1.matched)

        match2 = node2.equals(
            self._client_context,
            Sentence(self._client_context.brain.tokenizer, 'VALUE1'), 0)
        self.assertIsNotNone(match2)
        self.assertTrue(match2.matched)

        match3 = node3.equals(
            self._client_context,
            Sentence(self._client_context.brain.tokenizer, 'VALUE1'), 0)
        self.assertIsNotNone(match3)
        self.assertFalse(match3.matched)
Exemplo n.º 3
0
 def test_sentence_creation_one_word(self):
     sentence = Sentence(self._bot.brain.tokenizer, "One")
     self.assertIsNotNone(sentence)
     self.assertEqual(1, sentence.num_words())
     with self.assertRaises(Exception):
         sentence.sentence.word(1)
     self.assertEqual("One", sentence.text())
Exemplo n.º 4
0
 def test_sentence_creation_one_word(self):
     sentence = Sentence(self._bot.brain.tokenizer, "One")
     self.assertIsNotNone(sentence)
     self.assertEqual(1, sentence.num_words())
     with self.assertRaises(Exception):
         sentence.sentence.word(1)
     self.assertEqual("One", sentence.text())
Exemplo n.º 5
0
    def test_equals_attribs(self):
        loader = SetLoader()

        self._client_context.brain._sets_collection._sets["TEST1"] = loader.load_from_text("""
                    VALUE1
                    VALUE2
                    VALUE3
                    VALUE4
                """)

        node1 = PatternSetNode({"name": "test1"}, "")
        node2 = PatternSetNode({"name": "test1"}, "", userid="testid")
        node3 = PatternSetNode({"name": "test1"}, "", userid="testid2")

        match1 = node1.equals(self._client_context, Sentence(self._client_context.brain.tokenizer, 'VALUE1'), 0)
        self.assertIsNotNone(match1)
        self.assertTrue(match1.matched)

        match2 = node2.equals(self._client_context, Sentence(self._client_context.brain.tokenizer, 'VALUE1'), 0)
        self.assertIsNotNone(match2)
        self.assertTrue(match2.matched)

        match3 = node3.equals(self._client_context, Sentence(self._client_context.brain.tokenizer, 'VALUE1'), 0)
        self.assertIsNotNone(match3)
        self.assertFalse(match3.matched)
Exemplo n.º 6
0
 def test_question_create_from_sentence(self):
     sentence = Sentence(self._bot.brain.tokenizer, "One Two Three")
     question = Question.create_from_sentence(sentence)
     self.assertIsNotNone(question)
     self.assertEqual(1, len(question.sentences))
     self.assertEqual(sentence.text(), question.sentence(0).text())
     with self.assertRaises(Exception):
         question.sentence(1)
Exemplo n.º 7
0
 def test_question_create_from_sentence(self):
     sentence = Sentence(self._bot.brain.tokenizer, "One Two Three")
     question = Question.create_from_sentence(sentence)
     self.assertIsNotNone(question)
     self.assertEqual(1, len(question.sentences))
     self.assertEqual(sentence.text(), question.sentence(0).text())
     with self.assertRaises(Exception):
         question.sentence(1)
Exemplo n.º 8
0
 def test_split_into_words(self):
     sentence = Sentence(self._bot.brain.tokenizer, "HELLO")
     self.assertIsNotNone(sentence)
     self.assertEqual(1, sentence.num_words())
     self.assertEqual("HELLO", sentence.word(0))
     self.assertEqual("HELLO", sentence.words_from_current_pos(0))
     with self.assertRaises(Exception):
         sentence.sentence.word(1)
     self.assertEqual("HELLO", sentence.text())
Exemplo n.º 9
0
 def test_sentence_creation_two_words_diff_split_char(self):
     tokenizer = Tokenizer(",")
     sentence = Sentence(tokenizer, "One,Two",)
     self.assertIsNotNone(sentence)
     self.assertEqual(2, sentence.num_words())
     self.assertEqual("One", sentence.word(0))
     self.assertEqual("Two", sentence.word(1))
     with self.assertRaises(Exception):
         sentence.sentence.word(2)
     self.assertEqual("One,Two", sentence.text())
Exemplo n.º 10
0
 def test_sentence_creation_two_words_diff_split_char(self):
     tokenizer = Tokenizer(",")
     sentence = Sentence(tokenizer, "One,Two",)
     self.assertIsNotNone(sentence)
     self.assertEqual(2, sentence.num_words())
     self.assertEqual("One", sentence.word(0))
     self.assertEqual("Two", sentence.word(1))
     with self.assertRaises(Exception):
         sentence.sentence.word(2)
     self.assertEqual("One,Two", sentence.text())
Exemplo n.º 11
0
    def test_equals_userid(self):
        word1 = PatternPriorityWordNode("word")
        word2 = PatternPriorityWordNode("word", userid="testid")
        word3 = PatternPriorityWordNode("word", userid="testid2")

        match1 = word1.equals(self._client_context, Sentence(self._client_context.brain.tokenizer, 'word'), 0)
        self.assertIsNotNone(match1)
        self.assertTrue(match1.matched)

        match2 = word2.equals(self._client_context, Sentence(self._client_context.brain.tokenizer, 'word'), 0)
        self.assertIsNotNone(match2)
        self.assertTrue(match2.matched)

        match3 = word3.equals(self._client_context, Sentence(self._client_context.brain.tokenizer, 'word'), 0)
        self.assertIsNotNone(match3)
        self.assertFalse(match3.matched)
Exemplo n.º 12
0
    def test_arrow(self):
        node = PatternZeroOrMoreWildCardNode("^")

        self.assertFalse(node.is_root())
        self.assertFalse(node.is_priority())
        self.assertTrue(node.is_zero_or_more())
        self.assertFalse(node.is_one_or_more())
        self.assertFalse(node.is_set())
        self.assertFalse(node.is_bot())
        self.assertFalse(node.is_template())
        self.assertFalse(node.is_that())
        self.assertFalse(node.is_topic())
        self.assertTrue(node.is_wildcard())

        self.assertIsNotNone(node.children)
        self.assertFalse(node.has_children())

        sentence = Sentence(self._client_context.brain.tokenizer, "*")

        self.assertEqual(node.wildcard, "^")
        self.assertTrue(node.equivalent(PatternZeroOrMoreWildCardNode("^")))
        result = node.equals(self._client_context, sentence, 0)
        self.assertFalse(result.matched)
        self.assertEqual(node.to_string(), "ZEROORMORE [P(0)^(0)#(0)C(0)_(0)*(0)To(0)Th(0)Te(0)] wildcard=[^]")
        self.assertEqual('<zerormore wildcard="^">\n</zerormore>\n', node.to_xml(self._client_context))

        self.assertFalse(node.equivalent(PatternWordNode("test")))
Exemplo n.º 13
0
    def test_equals_pattern(self):
        node1 = PatternRegexNode({}, "^LEGION$")
        node2 = PatternRegexNode({}, "^LEGION$", userid="testid")
        node3 = PatternRegexNode({}, "^LEGION$", userid="testid2")

        match1 = node1.equals(self._client_context, Sentence(self._client_context.brain.tokenizer, 'LEGION'), 0)
        self.assertIsNotNone(match1)
        self.assertTrue(match1.matched)

        match2 = node2.equals(self._client_context, Sentence(self._client_context.brain.tokenizer, 'LEGION'), 0)
        self.assertIsNotNone(match2)
        self.assertTrue(match2.matched)

        match3 = node3.equals(self._client_context, Sentence(self._client_context.brain.tokenizer, 'LEGION'), 0)
        self.assertIsNotNone(match3)
        self.assertFalse(match3.matched)
Exemplo n.º 14
0
    def test_init(self):
        node = PatternPriorityWordNode("test1")
        self.assertIsNotNone(node)

        self.assertFalse(node.is_root())
        self.assertTrue(node.is_priority())
        self.assertFalse(node.is_wildcard())
        self.assertFalse(node.is_zero_or_more())
        self.assertFalse(node.is_one_or_more())
        self.assertFalse(node.is_set())
        self.assertFalse(node.is_bot())
        self.assertFalse(node.is_template())
        self.assertFalse(node.is_that())
        self.assertFalse(node.is_topic())
        self.assertFalse(node.is_wildcard())

        self.assertIsNotNone(node.children)
        self.assertFalse(node.has_children())

        sentence = Sentence(self._client_context.brain.tokenizer, "test1 test2")
        self.assertTrue(node.equivalent(PatternPriorityWordNode("test1")))
        result = node.equals(self._client_context, sentence, 0)
        self.assertTrue(result.matched)
        result = node.equals(self._client_context, sentence, 1)
        self.assertFalse(result.matched)
        self.assertEqual(node.to_string(), "PWORD [*] [P(0)^(0)#(0)C(0)_(0)*(0)To(0)Th(0)Te(0)] word=[test1]")
        self.assertEqual('<priority word="test1"></priority>\n', node.to_xml(self._client_context))

        node.add_child(PatternWordNode("test2"))
        self.assertEqual(len(node.children), 1)
        self.assertEqual(node.to_string(), "PWORD [*] [P(0)^(0)#(0)C(1)_(0)*(0)To(0)Th(0)Te(0)] word=[test1]")
        self.assertEqual('<priority word="test1"><word word="test2"></word>\n</priority>\n', node.to_xml(self._client_context))
Exemplo n.º 15
0
    def test_underline(self):
        node = PatternOneOrMoreWildCardNode("_")
        self.assertIsNotNone(node)

        self.assertFalse(node.is_root())
        self.assertFalse(node.is_priority())
        self.assertFalse(node.is_zero_or_more())
        self.assertTrue(node.is_one_or_more())
        self.assertFalse(node.is_set())
        self.assertFalse(node.is_bot())
        self.assertFalse(node.is_template())
        self.assertFalse(node.is_that())
        self.assertFalse(node.is_topic())
        self.assertTrue(node.is_wildcard())

        self.assertIsNotNone(node.children)
        self.assertFalse(node.has_children())

        self.assertEqual(node.wildcard, "_")

        sentence = Sentence(self._bot.brain.tokenizer, "*")

        self.assertTrue(node.equivalent(PatternOneOrMoreWildCardNode("_")))
        result = node.equals(self._bot, "testid", sentence, 0)
        self.assertFalse(result.matched)
        self.assertEqual(node.to_string(), "ONEORMORE [P(0)^(0)#(0)C(0)_(0)*(0)To(0)Th(0)Te(0)] wildcard=[_]")
        self.assertEqual('<oneormore wildcard="_">\n</oneormore>\n', node.to_xml(self._bot, self._clientid))

        self.assertFalse(node.equivalent(PatternWordNode("test")))
Exemplo n.º 16
0
    def setUp(self):

        self._client = TemplateGraphClient()
        self._client_context = self._client.create_client_context("testid")

        self._graph = self._client_context.bot.brain.aiml_parser.template_parser

        self.test_sentence = Sentence(self._client_context.brain.tokenizer,
                                      "test sentence")

        test_node = PatternOneOrMoreWildCardNode("*")

        self.test_sentence._matched_context = MatchContext(
            max_search_depth=100,
            max_search_timeout=-1,
            tokenizer=self._client_context.brain.tokenizer)
        self.test_sentence._matched_context._matched_nodes = [
            Match(Match.WORD, test_node, 'one'),
            Match(Match.WORD, test_node, 'two'),
            Match(Match.WORD, test_node, 'three'),
            Match(Match.WORD, test_node, 'four'),
            Match(Match.WORD, test_node, 'five'),
            Match(Match.WORD, test_node, 'six'),
            Match(Match.TOPIC, test_node, '*'),
            Match(Match.THAT, test_node, '*')
        ]

        conversation = self._client_context.bot.get_conversation(
            self._client_context)
        question = Question.create_from_sentence(self.test_sentence)
        conversation._questions.append(question)
Exemplo n.º 17
0
    def test_init(self):

        self._client_context.brain.properties.add_property("test1", "value1")

        node = PatternBotNode({}, "test1")
        self.assertIsNotNone(node)

        self.assertFalse(node.is_root())
        self.assertFalse(node.is_priority())
        self.assertFalse(node.is_wildcard())
        self.assertFalse(node.is_zero_or_more())
        self.assertFalse(node.is_one_or_more())
        self.assertFalse(node.is_set())
        self.assertTrue(node.is_bot())
        self.assertFalse(node.is_template())
        self.assertFalse(node.is_that())
        self.assertFalse(node.is_topic())
        self.assertFalse(node.is_wildcard())

        self.assertTrue(node.equivalent(PatternBotNode([], "test1")))
        self.assertFalse(node.equivalent(PatternBotNode([], "test2")))

        sentence = Sentence(self._client_context.brain.tokenizer, "value1 value2")

        result = node.equals(self._client_context, sentence, 0)
        self.assertTrue(result.matched)
        self.assertEqual(0, result.word_no)

        result = node.equals(self._client_context, sentence, 1)
        self.assertFalse(result.matched)
        self.assertEqual(1, result.word_no)

        self.assertEqual(node.to_string(), "BOT [*] [P(0)^(0)#(0)C(0)_(0)*(0)To(0)Th(0)Te(0)] property=[test1]")
        self.assertEqual('<bot property="test1">\n</bot>', node.to_xml(self._client_context))
Exemplo n.º 18
0
    def test_check_child_is_wildcard_no_wildcard_children(self):

        wildcard = MockPatternWildCardNode("*")
        self.assertIsNotNone(wildcard)

        context = MatchContext(max_search_depth=100, max_search_timeout=-1, tokenizer=self._client_context.brain.tokenizer)
        sentence = Sentence(self._client_context.brain.tokenizer, "TEST SENTENCE")
        match = wildcard.check_child_is_wildcard("", self._client_context, context, sentence, 0,  Match.WORD, 0)
        self.assertIsNone(match)
Exemplo n.º 19
0
 def get_exit_response(self, client_context):
     if self.exit_response_srai is not None:
         sentence = Sentence(client_context.brain.tokenizer, self.exit_response_srai)
         exit_response = client_context.brain.ask_question(client_context, sentence)
         if exit_response is None or not exit_response:
             exit_response = self.exit_response
         return exit_response
     else:
         return self.exit_response
Exemplo n.º 20
0
    def test_check_spelling_before_false(self):
        spelling_config = BotSpellingConfiguration()
        spelling_config._classname = "programytest.spelling.test_base.MockSpellingChecker"
        spelling_config._check_before = False

        storage_factory = None

        spell_checker = SpellingChecker.initiate_spellchecker(
            spelling_config, storage_factory)

        client = TestClient()
        client_context = client.create_client_context("user1")

        sentence = Sentence(client_context.brain.tokenizer, "Hello word")

        spell_checker.check_spelling_before(client_context, sentence)

        self.assertEqual(sentence.text(), "Hello word")
Exemplo n.º 21
0
    def test_bot_init_with_spellchecker(self):
        
        bot_config = BotConfiguration()
        bot_config.spelling._classname = "programy.spelling.norvig.NorvigSpellingChecker"
        bot_config.spelling._corpus = os.path.dirname(__file__) + os.sep + "test_corpus.txt"
        bot_config.spelling._check_before = True
        bot_config.spelling._check_and_retry = True
        bot = Bot(bot_config)
        self.assertIsNotNone(bot)

        test_sentence = Sentence(bot.brain.tokenizer, "locetion")
        bot.check_spelling_before(test_sentence)
        self.assertIsNotNone(test_sentence)
        self.assertEqual("LOCATION", test_sentence.text())

        test_sentence = Sentence(bot.brain.tokenizer, "locetion")
        response = bot.check_spelling_and_retry(self._client_context, test_sentence)
        self.assertIsNone(response)
Exemplo n.º 22
0
 def get_default_response(self, client_context):
     if self.default_response_srai is not None:
         sentence = Sentence(client_context.brain.tokenizer, self.default_response_srai)
         default_response = client_context.brain.ask_question(client_context, sentence)
         if default_response is None or not default_response:
             default_response = self.default_response
         return default_response
     else:
         return self.default_response
Exemplo n.º 23
0
    def test_equivalent_property(self):
        self._client_context.brain.properties.add_property("test1", "value1")

        bot1 = PatternBotNode({"property": "test1"}, None)
        bot2 = PatternBotNode({"property": "test1"}, None, userid="testid")
        bot3 = PatternBotNode({"property": "test1"}, None, userid="testid2")

        match1 = bot1.equals(self._client_context, Sentence(self._client_context.brain.tokenizer, 'value1'), 0)
        self.assertIsNotNone(match1)
        self.assertTrue(match1.matched)

        match2 = bot2.equals(self._client_context, Sentence(self._client_context.brain.tokenizer, 'value1'), 0)
        self.assertIsNotNone(match2)
        self.assertTrue(match2.matched)

        match3 = bot3.equals(self._client_context, Sentence(self._client_context.brain.tokenizer, 'value1'), 0)
        self.assertIsNotNone(match3)
        self.assertFalse(match3.matched)
Exemplo n.º 24
0
    def test_bot_init_with_spellchecker(self):
        
        bot_config = BotConfiguration()
        bot_config.spelling._classname = "programy.spelling.norvig.NorvigSpellingChecker"
        bot_config.spelling._corpus = os.path.dirname(__file__) + os.sep + "test_corpus.txt"
        bot_config.spelling._check_before = True
        bot_config.spelling._check_and_retry = True
        bot = Bot(bot_config)
        self.assertIsNotNone(bot)

        test_sentence = Sentence(bot.brain.tokenizer, "locetion")
        bot.check_spelling_before(test_sentence)
        self.assertIsNotNone(test_sentence)
        self.assertEqual("LOCATION", test_sentence.text())

        test_sentence = Sentence(bot.brain.tokenizer, "locetion")
        response = bot.check_spelling_and_retry(self._client_context, test_sentence)
        self.assertIsNone(response)
Exemplo n.º 25
0
 def get_initial_question(self, client_context):
     if self.initial_question_srai is not None:
         sentence = Sentence(client_context.brain.tokenizer, self.initial_question_srai)
         initial_question = client_context.brain.ask_question(client_context, sentence)
         if initial_question is None or not initial_question:
             initial_question = self.initial_question
         return initial_question
     else:
         return self.initial_question
Exemplo n.º 26
0
    def test_equals_template(self):
        self._client_context.brain.regex_templates.add_regex("LEGION", re.compile("^LEGION$", re.IGNORECASE))

        node1 = PatternRegexNode({"template": "LEGION"}, "")
        node2 = PatternRegexNode({"template": "LEGION"}, "", userid="testid")
        node3 = PatternRegexNode({"template": "LEGION"}, "", userid="testid2")

        match1 = node1.equals(self._client_context, Sentence(self._client_context.brain.tokenizer, 'LEGION'), 0)
        self.assertIsNotNone(match1)
        self.assertTrue(match1.matched)

        match2 = node2.equals(self._client_context, Sentence(self._client_context.brain.tokenizer, 'LEGION'), 0)
        self.assertIsNotNone(match2)
        self.assertTrue(match2.matched)

        match3 = node3.equals(self._client_context, Sentence(self._client_context.brain.tokenizer, 'LEGION'), 0)
        self.assertIsNotNone(match3)
        self.assertFalse(match3.matched)
Exemplo n.º 27
0
    def load_conversation(self, client_context, conversation):
        db_conversations = self._storage_engine.session.query(
            Conversation).filter(
                Conversation.clientid == client_context.client.id,
                Conversation.userid == client_context.userid)
        question = Question()
        conversation.questions.append(question)

        current_question = 0
        for conversation in db_conversations:

            if conversation.question != current_question:
                question = Question()
                conversation.questions.append(question)

            sentence = Sentence(client_context.bot.brain.tokenizer,
                                conversation.sentence)
            sentence.response = conversation.response
            question.sentences.append(sentence)
Exemplo n.º 28
0
    def test_combine_answers(self):
        question = Question()
        sentence1 = Sentence(self._bot.brain.tokenizer, "Hi")
        sentence1._response = "Hello"
        question._sentences.append(sentence1)
        sentence2 = Sentence(self._bot.brain.tokenizer, "Hi Again")
        question._sentences.append(sentence2)
        sentence2._response = "World"

        self.assertEqual(2, len(question._sentences))
        self.assertEqual(question._sentences[0]._response, "Hello")
        self.assertEqual(question._sentences[1]._response, "World")

        sentences = question.combine_sentences()
        self.assertEqual("Hi. Hi Again", sentences)

        combined = question.combine_answers()
        self.assertIsNotNone(combined)
        self.assertEqual(combined, "Hello. World")
Exemplo n.º 29
0
 def test_words_from_current_pos(self):
     sentence = Sentence(self._bot.brain.tokenizer, "One Two Three")
     self.assertIsNotNone(sentence)
     self.assertEqual("One Two Three", sentence.words_from_current_pos(0))
     self.assertEqual("Two Three", sentence.words_from_current_pos(1))
     self.assertEqual("Three", sentence.words_from_current_pos(2))
     with self.assertRaises(Exception):
         self.assertEqual("Three", sentence.words_from_current_pos(3))
     self.assertEqual("One Two Three", sentence.text())
Exemplo n.º 30
0
    def failed_authentication(self, bot, clientid):
        if logging.getLogger().isEnabledFor(logging.ERROR):
            logging.error("[%s] failed authentication!")
        if self.authentication.configuration.denied_srai is not None:
            match_context = self._aiml_parser.match_sentence(bot, clientid,
                                                             Sentence(bot.brain.tokenizer, self.authentication.configuration.denied_srai),
                                                             topic_pattern="*",
                                                             that_pattern="*")
            if match_context is not None:
                return self.resolve_matched_template(bot, clientid, match_context)

        return self.authentication.configuration.denied_text
Exemplo n.º 31
0
 def get_exit_response(self, clientid):
     if self.exit_response_srai is not None:
         sentence = Sentence(self.brain.tokenizer, self.exit_response_srai)
         exit_response = self.brain.ask_question(self,
                                                 clientid,
                                                 sentence,
                                                 srai=False)
         if exit_response is None or not exit_response:
             exit_response = self.exit_response
         return exit_response
     else:
         return self.exit_response
Exemplo n.º 32
0
    def test_multi_word_set(self):
        loader = SetLoader()

        self._client_context.brain._sets_collection._sets[
            "TEST1"] = loader.load_from_text("""
            Red
            Red Amber
            Red Brown
        """)

        node = PatternSetNode([], "test1")
        self.assertIsNotNone(node)

        self.assertFalse(node.is_root())
        self.assertFalse(node.is_priority())
        self.assertFalse(node.is_wildcard())
        self.assertFalse(node.is_zero_or_more())
        self.assertFalse(node.is_one_or_more())
        self.assertTrue(node.is_set())
        self.assertFalse(node.is_bot())
        self.assertFalse(node.is_template())
        self.assertFalse(node.is_that())
        self.assertFalse(node.is_topic())
        self.assertFalse(node.is_wildcard())

        self.assertTrue(node.equivalent(PatternSetNode([], "TEST1")))
        self.assertIsNotNone(node.children)
        self.assertFalse(node.has_children())

        sentence = Sentence(self._client_context.brain.tokenizer,
                            "RED Red BROWN red AMBER")

        result = node.equals(self._client_context, sentence, 0)
        self.assertTrue(result.matched)
        self.assertEquals(result.matched_phrase, "Red")
        self.assertEquals(result.word_no, 0)

        result = node.equals(self._client_context, sentence,
                             result.word_no + 1)
        self.assertTrue(result.matched)
        self.assertEquals(result.matched_phrase, "Red Brown")

        result = node.equals(self._client_context, sentence,
                             result.word_no + 1)
        self.assertTrue(result.matched)
        self.assertEquals(result.matched_phrase, "Red Amber")

        self.assertEqual(
            node.to_string(),
            "SET [P(0)^(0)#(0)C(0)_(0)*(0)To(0)Th(0)Te(0)] name=[TEST1]")
        self.assertEqual('<set name="TEST1">\n</set>',
                         node.to_xml(self._client_context))
Exemplo n.º 33
0
 def get_default_response(self, clientid):
     if self.default_response_srai is not None:
         sentence = Sentence(self.brain.tokenizer,
                             self.default_response_srai)
         default_response = self.brain.ask_question(self,
                                                    clientid,
                                                    sentence,
                                                    srai=False)
         if default_response is None or not default_response:
             default_response = self.default_response
         return default_response
     else:
         return self.default_response
Exemplo n.º 34
0
    def test_number(self):
        self._client_context.brain.dynamics.add_dynamic_set('number', "programy.dynamic.sets.numeric.IsNumeric", None)

        node = PatternSetNode([], "NUMBER")
        self.assertIsNotNone(node)

        sentence = Sentence(self._client_context.brain.tokenizer, "12 XY")

        result = node.equals(self._client_context, sentence, 0)
        self.assertTrue(result.matched)

        result = node.equals(self._client_context, sentence, result.word_no+1)
        self.assertFalse(result.matched)
Exemplo n.º 35
0
    def test_equals(self):
        node1 = PatternISetNode([], "test1, test2, test3")
        node2 = PatternISetNode([], "test1, test2, test3", userid="testid")
        node3 = PatternISetNode([], "test1, test2, test3", userid="testid2")

        match1 = node1.equals(
            self._client_context,
            Sentence(self._client_context.brain.tokenizer, 'test1'), 0)
        self.assertIsNotNone(match1)
        self.assertTrue(match1.matched)

        match2 = node2.equals(
            self._client_context,
            Sentence(self._client_context.brain.tokenizer, 'test1'), 0)
        self.assertIsNotNone(match2)
        self.assertTrue(match2.matched)

        match3 = node3.equals(
            self._client_context,
            Sentence(self._client_context.brain.tokenizer, 'test1'), 0)
        self.assertIsNotNone(match3)
        self.assertFalse(match3.matched)
Exemplo n.º 36
0
 def get_initial_question(self, clientid):
     if self.initial_question_srai is not None:
         sentence = Sentence(self.brain.tokenizer,
                             self.initial_question_srai)
         initial_question = self.brain.ask_question(self,
                                                    clientid,
                                                    sentence,
                                                    srai=False)
         if initial_question is None or not initial_question:
             initial_question = self.initial_question
         return initial_question
     else:
         return self.initial_question
Exemplo n.º 37
0
 def test_words_from_current_pos(self):
     sentence = Sentence(self._bot.brain.tokenizer, "One Two Three")
     self.assertIsNotNone(sentence)
     self.assertEqual("One Two Three", sentence.words_from_current_pos(0))
     self.assertEqual("Two Three", sentence.words_from_current_pos(1))
     self.assertEqual("Three", sentence.words_from_current_pos(2))
     with self.assertRaises(Exception):
         self.assertEqual("Three", sentence.words_from_current_pos(3))
     self.assertEqual("One Two Three", sentence.text())
Exemplo n.º 38
0
    def match_sentence(self, client_context, pattern_sentence, topic_pattern, that_pattern):

        topic_sentence = Sentence(client_context.brain.tokenizer, topic_pattern)
        that_sentence = Sentence(client_context.brain.tokenizer, that_pattern)

        YLogger.debug(client_context, "AIML Parser matching sentence [%s], topic=[%s], that=[%s] ",
                          pattern_sentence.text(), topic_pattern, that_pattern)

        sentence = Sentence(client_context.brain.tokenizer)
        sentence.append_sentence(pattern_sentence)
        sentence.append_word('__TOPIC__')
        sentence.append_sentence(topic_sentence)
        sentence.append_word('__THAT__')
        sentence.append_sentence(that_sentence)
        YLogger.debug(client_context, "Matching [%s]", sentence.words_from_current_pos(0))

        context = MatchContext(max_search_depth=client_context.bot.configuration.max_search_depth,
                               max_search_timeout=client_context.bot.configuration.max_search_timeout,
                               tokenizer=client_context.brain.tokenizer)

        template = self._pattern_parser._root_node.match(client_context, context, sentence)

        if template is not None:
            context._template_node = template

            context.list_matches(client_context)

            # Save the matched context for the associated sentence
            pattern_sentence.matched_context = context

            return context

        return None
Exemplo n.º 39
0
 def test_sentence_creation_spaces(self):
     sentence = Sentence(self._bot.brain.tokenizer, " ")
     self.assertIsNotNone(sentence)
     self.assertEqual(0, sentence.num_words())
     with self.assertRaises(Exception):
         sentence.sentence.word(0)