def test_words_from_current_pos(self): sentence = Sentence(self._bot.brain.nlp.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())
def test_split_into_words(self): sentence = Sentence(self._bot.brain.nlp.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())
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