Example #1
0
 def test_sentence_creation_one_word(self):
     sentence = Sentence("One")
     self.assertIsNotNone(sentence)
     self.assertEqual(1, sentence.num_words())
     with self.assertRaises(Exception):
         sentence.sentence.word(1)
     self.assertEqual("One", sentence.text())
Example #2
0
 def test_question_create_from_sentence(self):
     sentence = Sentence("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)
Example #3
0
 def test_sentence_creation_two_words_diff_split_char(self):
     sentence = Sentence("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())
Example #4
0
 def test_split_into_words(self):
     sentence = Sentence("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())
Example #5
0
    def test_combine_answers(self):
        question = Question()
        sentence1 = Sentence("Hi")
        sentence1._response = "Hello"
        question._sentences.append(sentence1)
        sentence2 = Sentence("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")
Example #6
0
 def test_words_from_current_pos(self):
     sentence = Sentence("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())
Example #7
0
    def match_sentence(self, bot, clientid, pattern_sentence, topic_pattern,
                       that_pattern):

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

        if logging.getLogger().isEnabledFor(logging.DEBUG):
            logging.debug(
                "AIML Parser matching sentence [%s], topic=[%s], that=[%s] ",
                pattern_sentence.text(), topic_pattern, that_pattern)

        sentence = Sentence()
        sentence.append_sentence(pattern_sentence)
        sentence.append_word('__TOPIC__')
        sentence.append_sentence(topic_sentence)
        sentence.append_word('__THAT__')
        sentence.append_sentence(that_sentence)
        if logging.getLogger().isEnabledFor(logging.DEBUG):
            logging.debug("Matching [%s]", sentence.words_from_current_pos(0))

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

        template = self.pattern_parser._root_node.match(
            bot, clientid, context, sentence)

        if template is not None:
            context._template_node = template

            context.list_matches()

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

            return context

        return None
Example #8
0
    def match_sentence(self, sentence, topic="*", that="*"):

        return self.matcher.match_sentence(PatternMatcherBaseClass.bot,
                                           PatternMatcherBaseClass.clientid,
                                           Sentence(sentence), topic, that)
Example #9
0
    def match_sentence(self, bot, clientid, pattern_sentence, topic_pattern, that_pattern):

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

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

        sentence = Sentence()
        sentence.append_sentence(pattern_sentence)
        sentence.append_word('__TOPIC__')
        sentence.append_sentence(topic_sentence)
        sentence.append_word('__THAT__')
        sentence.append_sentence(that_sentence)
        logging.debug("Matching [%s]"%sentence.words_from_current_pos(0))

        context = MatchContext()

        template = self.pattern_parser._root_node.match(bot, clientid, context, sentence)

        if template is not None:
            context._template_node = template

            context.list_matches()

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

            return context

        return None
Example #10
0
 def test_sentence_creation_one_word(self):
     sentence = Sentence("One")
     self.assertIsNotNone(sentence)
     self.assertEqual(1, sentence.num_words())
Example #11
0
 def test_sentence_creation_spaces(self):
     sentence = Sentence(" ")
     self.assertIsNotNone(sentence)
     self.assertEqual(0, sentence.num_words())
     with self.assertRaises(Exception):
         sentence.sentence.word(0)
Example #12
0
    def test_simple_sequence(self):

        graph = PatternGraph()

        pattern_element = ET.fromstring("<pattern>A B C</pattern>")
        topic_element =  ET.fromstring("<topic>*</topic>")
        that_element = ET.fromstring("<that>*</that>")
        template_node = TemplateNode ()

        graph.add_pattern_to_graph(pattern_element, topic_element, that_element, template_node)

        matcher = PatternMatcher(graph)

        pattern_stars = []
        topic_stars = []
        that_stars = []
        result = matcher.match(PatternMatcherTests.bot, PatternMatcherTests.clientid,
                               Sentence("A B"), pattern_stars,
                               Sentence("*"), topic_stars,
                               Sentence("*"), that_stars)
        self.assertIsNone(result)
        self.assertEqual(len(pattern_stars), 0)
        self.assertEqual(len(topic_stars), 0)
        self.assertEqual(len(that_stars), 0)

        pattern_stars = []
        topic_stars = []
        that_stars = []
        result = matcher.match(PatternMatcherTests.bot, PatternMatcherTests.clientid,
                               Sentence("A B C D"), pattern_stars,
                               Sentence("*"), topic_stars,
                               Sentence("*"), that_stars)
        self.assertIsNone(result)
        self.assertEqual(len(pattern_stars), 0)
        self.assertEqual(len(topic_stars), 0)
        self.assertEqual(len(that_stars), 0)

        pattern_stars = []
        topic_stars = []
        that_stars = []
        result = matcher.match(PatternMatcherTests.bot, PatternMatcherTests.clientid,
                               Sentence("A B C"), pattern_stars,
                               Sentence("*"), topic_stars,
                               Sentence("*"), that_stars)
        self.assertIsNotNone(result)
        self.assertIsInstance(result, PatternTemplateNode)
        self.assertEqual(len(pattern_stars), 0)
        self.assertEqual(len(topic_stars), 0)
        self.assertEqual(len(that_stars), 0)

        pattern_stars = []
        topic_stars = []
        that_stars = []
        result = matcher.match(PatternMatcherTests.bot, PatternMatcherTests.clientid,
                               Sentence("A B C"), pattern_stars,
                               Sentence("X X"), topic_stars,
                               Sentence("*"), that_stars)
        self.assertIsNotNone(result)
        self.assertIsInstance(result, PatternTemplateNode)
        self.assertEqual(len(pattern_stars), 0)
        self.assertEqual(len(topic_stars), 1)
        self.assertEqual(topic_stars[0], "X X")
        self.assertEqual(len(that_stars), 0)

        pattern_stars = []
        topic_stars = []
        that_stars = []
        result = matcher.match(PatternMatcherTests.bot, PatternMatcherTests.clientid,
                               Sentence("A B C"), pattern_stars,
                               Sentence("*"), topic_stars,
                               Sentence("Y Y"), that_stars)
        self.assertIsNotNone(result)
        self.assertIsInstance(result, PatternTemplateNode)
        self.assertEqual(len(pattern_stars), 0)
        self.assertEqual(len(topic_stars), 0)
        self.assertEqual(len(that_stars), 1)
        self.assertEqual(that_stars[0], "Y Y")

        pattern_stars = []
        topic_stars = []
        that_stars = []
        result = matcher.match(PatternMatcherTests.bot, PatternMatcherTests.clientid,
                               Sentence("A B C"), pattern_stars,
                               Sentence("X X"), topic_stars,
                               Sentence("Y Y"), that_stars)
        self.assertIsNotNone(result)
        self.assertIsInstance(result, PatternTemplateNode)
        self.assertEqual(len(pattern_stars), 0)
        self.assertEqual(len(topic_stars), 1)
        self.assertEqual(topic_stars[0], "X X")
        self.assertEqual(len(that_stars), 1)
        self.assertEqual(that_stars[0], "Y Y")
Example #13
0
 def test_sentence_creation_two_words_diff_split_char(self):
     sentence = Sentence("One,Two", ",")
     self.assertIsNotNone(sentence)
     self.assertEqual(2, sentence.num_words())
Example #14
0
 def test_sentence_creation_two_words(self):
     sentence = Sentence("One Two")
     self.assertIsNotNone(sentence)
     self.assertEqual(2, sentence.num_words())
Example #15
0
 def test_sentence_creation_spaces(self):
     sentence = Sentence(" ")
     self.assertIsNotNone(sentence)
     self.assertEqual(0, sentence.num_words())
     with self.assertRaises(Exception):
         sentence.sentence.word(0)
Example #16
0
    def match_sentence(self, bot, clientid, pattern_sentence, topic_pattern,
                       that_pattern):

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

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

        sentence = Sentence()
        sentence.append_sentence(pattern_sentence)
        sentence.append_word('__TOPIC__')
        sentence.append_sentence(topic_sentence)
        sentence.append_word('__THAT__')
        sentence.append_sentence(that_sentence)
        logging.debug("Matching [%s]" % sentence.words_from_current_pos(0))

        context = MatchContext()

        template = self.pattern_parser._root_node.match(
            bot, clientid, context, sentence)

        if template is not None:
            context._template_node = template

            context.list_matches()

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

            return context

        return None
Example #17
0
 def test_split_into_words(self):
     sentence = Sentence("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))