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())
示例#2
0
    def match_sentence(self, client_context, pattern_sentence, topic_pattern, that_pattern):

        topic_sentence = Sentence(client_context, topic_pattern)
        that_sentence = Sentence(client_context, that_pattern)

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

        sentence = Sentence(client_context)
        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(client_context, 0))

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

        template = self._pattern_parser.root.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
示例#3
0
 def test_words_from_current_pos(self):
     sentence = Sentence(self._client_context, "One Two Three")
     self.assertIsNotNone(sentence)
     self.assertEqual(
         "One Two Three",
         sentence.words_from_current_pos(self._client_context, 0))
     self.assertEqual(
         "Two Three",
         sentence.words_from_current_pos(self._client_context, 1))
     self.assertEqual(
         "Three", sentence.words_from_current_pos(self._client_context, 2))
     with self.assertRaises(Exception):
         self.assertEqual(
             "Three",
             sentence.words_from_current_pos(self._client_context, 3))
     self.assertEqual("One Two Three", sentence.text(self._client_context))
 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))
     self.assertIsNone(sentence.word(1))
     self.assertEqual("HELLO", sentence.text())
示例#5
0
 def test_split_into_words(self):
     sentence = Sentence(self._client_context, "HELLO")
     self.assertIsNotNone(sentence)
     self.assertEqual(1, sentence.num_words())
     self.assertEqual("HELLO", sentence.word(0))
     self.assertEqual(
         "HELLO", sentence.words_from_current_pos(self._client_context, 0))
     with self.assertRaises(Exception):
         sentence.sentence.word(1)
     self.assertEqual("HELLO", sentence.text(self._client_context))
示例#6
0
    def match_sentence(self, client_context, pattern_sentence, topic_pattern,
                       that_pattern):

        topic_sentence = Sentence(client_context.brain.tokenizer,
                                  topic_pattern)
        if len(topic_sentence.words) == 0:
            topic_sentence.words.append('*')
        that_sentence = Sentence(client_context.brain.tokenizer, that_pattern)
        if len(that_sentence.words) == 0:
            that_sentence.words.append('*')

        if client_context.match_nlu is True:
            YLogger.debug(client_context,
                          "AIML Parser NLU matching topic=[%s], that=[%s] ",
                          topic_pattern, that_pattern)
        else:
            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,
            start_time=client_context.question_start_time)

        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