def test_node_with_star_with_none(self): root = TemplateNode() node = TemplateThatStarNode() root.append(node) conversation = Conversation(self._client_context) question = Question.create_from_text( self._client_context.brain.nlp.tokenizer, "Hello world") question.current_sentence()._response = "Hello matey" conversation.record_question(question) question = Question.create_from_text( self._client_context.brain.nlp.tokenizer, "How are you") question.current_sentence()._response = "Very well thanks" conversation.record_question(question) match = PatternOneOrMoreWildCardNode("*") context = MatchContext( max_search_depth=100, max_search_timeout=-1, tokenizer=self._client_context.brain.nlp.tokenizer) context.add_match(Match(Match.THAT, match, None)) question.current_sentence()._matched_context = context conversation.record_question(question) self._client_context.bot._conversations["testid"] = conversation self.assertEqual("", root.resolve(self._client_context))
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.nlp.tokenizer) sentence = Sentence(self._client_context.brain.nlp.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.nlp.tokenizer) sentence = Sentence(self._client_context.brain.nlp.tokenizer, "TEST") match = wildcard.check_child_is_wildcard("", self._client_context, context, sentence, 0, Match.WORD, 0) self.assertIsNotNone(match)
def test_time_functions(self): context = MatchContext(max_search_depth=100, max_search_timeout=-1, tokenizer=self._bot.brain.nlp.tokenizer) self.assertEqual(-1, context.max_search_timeout) self.assertFalse(context.search_time_exceeded()) context = MatchContext(max_search_depth=100, max_search_timeout=0, tokenizer=self._bot.brain.nlp.tokenizer) self.assertEqual(0, context.max_search_timeout) self.assertTrue(context.search_time_exceeded()) context = MatchContext(max_search_depth=100, max_search_timeout=60, tokenizer=self._bot.brain.nlp.tokenizer) time_now = datetime.datetime.now() prev_time = time_now - datetime.timedelta(seconds=-70) context._total_search_start = prev_time self.assertTrue(context.search_time_exceeded())
def setUp(self): self._client_context = ClientContext(TestClient(), "testid") self._client_context.bot = Bot(BotConfiguration()) self._client_context.brain = self._client_context.bot.brain self._graph = self._client_context.bot.brain.aiml_parser.template_parser self.test_sentence = Sentence(self._client_context.brain.nlp.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.nlp.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)
def match_sentence(self, client_context, pattern_sentence, topic_pattern, that_pattern): topic_sentence = Sentence(client_context.brain.nlp, topic_pattern) that_sentence = Sentence(client_context.brain.nlp, 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.nlp) 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.nlp.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 # YLogger.debug(context, "Matched sentence: [%s]", template.to_string()) return context return None
def test_match_context_depth(self): context = MatchContext(max_search_depth=100, max_search_timeout=60, tokenizer=self._bot.brain.nlp.tokenizer) self.assertEquals(100, context.max_search_depth) self.assertEquals(60, context.max_search_timeout) self.assertFalse(context.matched()) template = PatternTemplateNode(template=TemplateNode) context.set_template(template) self.assertEquals(template, context.template_node()) self.assertTrue(context.matched())
def test_resolve_no_defaults_inside_topic(self): root = TemplateNode() self.assertIsNotNone(root) self.assertIsNotNone(root.children) self.assertEqual(len(root.children), 0) node = TemplateTopicStarNode(index=1) self.assertIsNotNone(node) self.assertEqual(1, node.index) root.append(node) self.assertEqual(len(root.children), 1) conversation = Conversation(self._client_context) question = Question.create_from_text( self._client_context.brain.nlp.tokenizer, "Hello world") question.current_sentence()._response = "Hello matey" conversation.record_question(question) question = Question.create_from_text( self._client_context.brain.nlp.tokenizer, "How are you") question.current_sentence()._response = "Very well thanks" conversation.record_question(question) match = PatternOneOrMoreWildCardNode("*") context = MatchContext( max_search_depth=100, max_search_timeout=-1, tokenizer=self._client_context.brain.nlp.tokenizer) context.add_match(Match(Match.TOPIC, match, "Matched")) question.current_sentence()._matched_context = context conversation.record_question(question) self._client_context.bot._conversations["testid"] = conversation self.assertEqual("Matched", node.resolve(self._client_context))
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.nlp.tokenizer) sentence = Sentence(self._client_context.brain.nlp.tokenizer, "TEST SENTENCE") match = wildcard.check_child_is_wildcard("", self._client_context, context, sentence, 0, Match.WORD, 0) self.assertIsNone(match)
def test_invalid_topic_or_that(self): wildcard = MockPatternWildCardNode("*") self.assertIsNotNone(wildcard) context = MatchContext( max_search_depth=100, max_search_timeout=-1, tokenizer=self._client_context.brain.nlp.tokenizer) matches_added = 1 self.assertTrue( wildcard.invalid_topic_or_that("", self._client_context, PatternTopicNode.TOPIC, context, matches_added)) self.assertTrue( wildcard.invalid_topic_or_that("", self._client_context, PatternTopicNode.THAT, context, matches_added)) self.assertFalse( wildcard.invalid_topic_or_that("", self._client_context, "TEST", context, matches_added))
def test_match_context_star(self): word = PatternOneOrMoreWildCardNode("*") topic = PatternOneOrMoreWildCardNode("*") that = PatternOneOrMoreWildCardNode("*") context = MatchContext(max_search_depth=100, max_search_timeout=60, tokenizer=self._bot.brain.nlp.tokenizer) context.add_match(Match(Match.WORD, word, "Hello")) context.add_match(Match(Match.TOPIC, topic, "Hello Topic")) context.add_match(Match(Match.THAT, that, "Hello That")) self.assertEquals(3, len(context.matched_nodes)) self.assertEqual("Hello", context.star(1)) self.assertIsNone(context.star(2)) self.assertEqual("Hello Topic", context.topicstar(1)) self.assertIsNone(context.topicstar(2)) self.assertEqual("Hello That", context.thatstar(1)) self.assertIsNone(context.thatstar(2))
def test_match_context_pop_push(self): topic = PatternOneOrMoreWildCardNode("*") context = MatchContext(max_search_depth=100, max_search_timeout=60, tokenizer=self._bot.brain.nlp.tokenizer) context.add_match(Match(Match.TOPIC, topic, None)) self.assertEquals(1, len(context.matched_nodes)) context.add_match(Match(Match.TOPIC, topic, None)) self.assertEquals(2, len(context.matched_nodes)) context.add_match(Match(Match.TOPIC, topic, None)) self.assertEquals(3, len(context.matched_nodes)) context.pop_match() self.assertEquals(2, len(context.matched_nodes)) context.pop_match() self.assertEquals(1, len(context.matched_nodes)) context.pop_match() self.assertEquals(0, len(context.matched_nodes)) context.pop_match() self.assertEquals(0, len(context.matched_nodes))
def test_match_context_depth(self): context1 = MatchContext(max_search_depth=100, max_search_timeout=60, tokenizer=self._bot.brain.nlp.tokenizer) self.assertEquals(100, context1.max_search_depth) self.assertEquals(60, context1.max_search_time)