Пример #1
0
    def add_pattern_to_graph(self, pattern_element, topic_element, that_element, template_graph_root, learn=False):

        pattern_node = self.add_pattern_to_node(pattern_element)

        topic_node = self.add_topic_to_node(topic_element, pattern_node)

        that_node = self.add_that_to_node(that_element, topic_node)

        if that_node.has_template() is True:
            if learn is False:
                if pattern_element.text is not None:
                    raise DuplicateGrammarException("Dupicate grammar tree found [%s]"%(pattern_element.text.strip()))
                else:
                    raise DuplicateGrammarException("Dupicate grammar tree found for bot/set")
            else:
                if pattern_element.text is not None:
                    if logging.getLogger().isEnabledFor(logging.WARNING):
                        logging.warning("Dupicate grammar tree found [%s] in learn, replacing existing",
                                        pattern_element.text.strip())
                else:
                    if logging.getLogger().isEnabledFor(logging.WARNING):
                        logging.warning("Dupicate grammar tree found for bot/set in learn, replacing existing")

                self.add_template_to_node(template_graph_root, that_node)
        else:
            self.add_template_to_node(template_graph_root, that_node)
        return that_node
Пример #2
0
    def add_pattern_to_graph(self, pattern_element, topic_element, that_element, template_graph_root, learn=False, userid="*"):

        pattern_node = self.add_pattern_to_node(pattern_element, userid=userid)

        topic_node = self.add_topic_to_node(topic_element, pattern_node, userid=userid)

        that_node = self.add_that_to_node(that_element, topic_node, userid=userid)

        if that_node.has_template() is True:
            if learn is False:
                if type(pattern_node) is PatternNluNode:
                    excep_msg = "Duplicate grammar tree found [intent=%s, %s=%s]" % (pattern_node.intent, pattern_node.scoreName(), pattern_node.score)
                elif pattern_element.text is not None:
                    excep_msg = "Duplicate grammar tree found [%s]" % (pattern_element.text.strip())
                else:
                    excep_msg = "Duplicate grammar tree found for [TAG defined]"
                duplicate_excep = DuplicateGrammarException(excep_msg)
                duplicate_excep.xml_element = pattern_element
                raise duplicate_excep
            else:
                if pattern_element.text is not None:
                    YLogger.warning(self, "Duplicate grammar tree found [%s] in learn, replacing existing",
                                    pattern_element.text.strip())
                else:
                    YLogger.warning(self, "Duplicate grammar tree found for bot/set in learn, replacing existing")

                pattern_template_node = self.add_template_to_node(template_graph_root, that_node)
        else:
            pattern_template_node = self.add_template_to_node(template_graph_root, that_node)

        return pattern_template_node
Пример #3
0
    def add_pattern_to_graph(self, pattern_element, topic_element, that_element, template_graph_root, learn=False, userid="*"):

        pattern_node = self.add_pattern_to_node(pattern_element, userid=userid)

        topic_node = self.add_topic_to_node(topic_element, pattern_node, userid=userid)

        that_node = self.add_that_to_node(that_element, topic_node, userid=userid)

        if that_node.has_template() is True:
            if learn is False:
                if pattern_element.text is not None:
                    raise DuplicateGrammarException("Dupicate grammar tree found [%s]"%(pattern_element.text.strip()))
                else:
                    raise DuplicateGrammarException("Dupicate grammar tree found for bot/set")
            else:
                if pattern_element.text is not None:
                    YLogger.warning(self, "Duplicate grammar tree found [%s] in learn, replacing existing",
                                        pattern_element.text.strip())
                else:
                    YLogger.warning(self, "Duplicate grammar tree found for bot/set in learn, replacing existing")

                self.add_template_to_node(template_graph_root, that_node)
        else:
            self.add_template_to_node(template_graph_root, that_node)

        return that_node
Пример #4
0
 def test_duplicate_grammar_exception_via_sets(self):
     element = ET.fromstring("<template />")
     xml_exception = ET.ParseError()
     xml_exception.position = []
     xml_exception.position.append(1)
     xml_exception.position.append(2)
     exception = DuplicateGrammarException("message", filename="test.xml")
     exception.xml_exception = xml_exception
     self.assertEqual(xml_exception, exception.xml_exception)
     exception.xml_element = element
     self.assertEqual(element, exception.xml_element)
     self.assertEqual("message", exception.message)
     self.assertEqual("test.xml", exception.filename)
     self.assertEqual(xml_exception, exception.xml_exception)
     self.assertEqual(element, exception._xml_element)
     self.assertEqual("message in [test.xml] at [line(1), column(2)]", exception.format_message())
Пример #5
0
    def test_handle_aiml_error_no_expression(self):
        self.parser._errors = []
        duplicate = DuplicateGrammarException("test duplicate message")
        self.parser.handle_aiml_error(duplicate, "test.aiml", None)

        self.assertEquals(
            [['test duplicate message', 'test.aiml', None, None]],
            self.parser._errors)
Пример #6
0
    def test_handle_aiml_duplicate_with_line_numbers(self):
        aiml = MockElement("aiml")
        aiml._start_line_number = 99
        aiml._end_line_number = 999
        self.parser._duplicates = []
        duplicate = DuplicateGrammarException("test duplicate message")
        self.parser.handle_aiml_duplicate(duplicate, "test.aiml", aiml)

        self.assertEquals(
            [['test duplicate message', 'test.aiml', '99', '999']],
            self.parser._duplicates)
Пример #7
0
    def test_handle_aiml_duplicate_no_duplicates(self):
        aiml = ET.fromstring("""<?xml version="1.0" encoding="ISO-8859-1"?>
            <aiml version="1.01">
                <category>
                    <pattern>*</pattern>
                    <template>RESPONSE</template>
                </category>
            </aiml>
        """)

        duplicate = DuplicateGrammarException("test duplicate message")
        self.parser.handle_aiml_duplicate(duplicate, "test.aiml", aiml)

        self.assertEquals(None, self.parser._duplicates)
Пример #8
0
    def add_pattern_to_graph(self, pattern_element, topic_element,
                             that_element, template_graph_root):

        current_node = self.add_pattern_to_node(pattern_element)

        current_node = self.add_topic_to_node(topic_element, current_node)

        current_node = self.add_that_to_node(that_element, current_node)

        if current_node.has_template():
            raise DuplicateGrammarException(
                "Dupicate grammar tree found [%s]" % (pattern_element.text))
        else:
            self.add_template_to_node(template_graph_root, current_node)
Пример #9
0
 def test_duplicate_grammar_exception_filename_set(self):
     exception = DuplicateGrammarException("duplicate")
     exception.filename = "test.xml"
     self.assertEqual("test.xml", exception.filename)
     self.assertEqual("duplicate in [test.xml]", exception.format_message())
Пример #10
0
 def test_duplicate_grammar_exception_basic(self):
     exception = DuplicateGrammarException("duplicate")
     self.assertEqual("duplicate", exception.message)
     self.assertEqual("duplicate", exception.format_message())