コード例 #1
0
    def resolve_element_evals(self, bot, clientid, element):

        new_element = ET.Element(element.tag)

        new_element.text = TextUtils.strip_whitespace(element.text)

        for child in element:
            tag_name = TextUtils.tag_from_text(child.tag)

            if tag_name == 'eval':
                eval_str = ET.tostring(child, 'utf-8').decode('ascii')
                eval_str = TextUtils.strip_whitespace(eval_str)
                str_val = "<template>%s</template>" % eval_str
                template = ET.fromstring(str_val)

                ast = bot.brain.aiml_parser.template_parser.parse_template_expression(template)
                resolved = ast.resolve(bot, clientid)

                new_element.text += " " + resolved
            else:
                new_element.append(child)

        new_element.text = new_element.text.upper ()

        if element.tail is not None:
            new_element.tail = TextUtils.strip_whitespace(element.tail)

        return new_element
コード例 #2
0
ファイル: learn.py プロジェクト: Doshmaku/program-y
    def resolve_element_evals(self, bot, clientid, element):

        new_element = ET.Element(element.tag)

        new_element.text = TextUtils.strip_whitespace(element.text)

        for child in element:
            if child.tag == 'eval':
                eval_str = ET.tostring(child, 'utf-8').decode('ascii')
                eval_str = TextUtils.strip_whitespace(eval_str)
                str_val = "<template>%s</template>" % eval_str
                template = ET.fromstring(str_val)

                ast = bot.brain.aiml_parser.template_parser.parse_template_expression(template)
                resolved = ast.resolve(bot, clientid)

                new_element.text += " " + resolved
            else:
                new_element.append(child)

        new_element.text = new_element.text.upper ()

        if element.tail is not None:
            new_element.tail = TextUtils.strip_whitespace(element.tail)

        return new_element
コード例 #3
0
 def node_from_element(element):
     if element.tag == 'set':
         if 'name' in element.attrib:
             return PatternSetNode(element.attrib['name'])
         else:
             return PatternSetNode(TextUtils.strip_whitespace(element.text))
     elif element.tag == 'bot':
         if 'name' in element.attrib:
             return PatternBotNode(element.attrib['name'])
         else:
             return PatternBotNode(TextUtils.strip_whitespace(element.text))
     else:
         raise ParserException("Invalid parser graph node <%s>" % element.tag, xml_element=element)
コード例 #4
0
 def test_strip_whitespace(self):
     self.assertEquals("", TextUtils.strip_whitespace(""))
     self.assertEquals("", TextUtils.strip_whitespace(" "))
     self.assertEquals("", TextUtils.strip_whitespace("\t"))
     self.assertEquals("", TextUtils.strip_whitespace("\n"))
     self.assertEquals("", TextUtils.strip_whitespace("\r"))
     self.assertEquals("", TextUtils.strip_whitespace("\r\t\n"))
     self.assertEquals("test", TextUtils.strip_whitespace("\r\t\ntest\r\t\n"))
     self.assertEquals("test test", TextUtils.strip_whitespace("\r\t\ntest test\r\t\n"))
     self.assertEquals("test test", TextUtils.strip_whitespace("\r\t\ntest\n\r\ttest\r\t\n"))
コード例 #5
0
ファイル: graph.py プロジェクト: Freiza/program-y
    def add_that_to_node(self, that_element, base_node, userid="*"):
        try:

            current_node = self._pattern_factory.new_node_class('that')(userid)
            current_node = base_node.add_that(current_node)

            head_text = self.get_text_from_element(that_element)
            if head_text is not None:
                current_node = self._parse_text(TextUtils.strip_whitespace(head_text), current_node)

            added_child = False
            for sub_element in that_element:
                new_node = self.node_from_element(sub_element)
                current_node = current_node.add_child(new_node)

                tail_text = self.get_tail_from_element(sub_element)
                if tail_text is not None:
                    current_node = self._parse_text(tail_text, current_node)
                added_child = True

            if head_text is None:
                if added_child is False:
                    raise ParserException("That node text is empty", xml_element=that_element)

            return current_node

        except ParserException as parser_excep:
            parser_excep.xml_element = that_element
            raise parser_excep
コード例 #6
0
ファイル: graph.py プロジェクト: rollingstone/program-y
    def add_that_to_node(self, that_element, base_node):
        current_node = PatternThatNode()
        current_node = base_node.add_that(current_node)

        head_text = self.get_text_from_element(that_element)
        if head_text is not None:
            current_node = self._parse_text(
                TextUtils.strip_whitespace(head_text), current_node)

        added_child = False
        for sub_element in that_element:
            new_node = PatternGraph.node_from_element(sub_element)
            current_node = current_node.add_child(new_node)

            tail_text = self.get_tail_from_element(sub_element)
            if tail_text is not None:
                current_node = self._parse_text(tail_text, current_node)
            added_child = True

        if head_text is None:
            if added_child is False:
                raise ParserException("That node text is empty",
                                      xml_element=that_element)

        return current_node
コード例 #7
0
    def add_that_to_node(self, that_element, base_node, userid="*"):
        try:

            current_node = self._pattern_factory.new_node_class('that')(userid)
            current_node = base_node.add_that(current_node)

            head_text = self.get_text_from_element(that_element)
            if head_text is not None:
                current_node = self._parse_text(
                    TextUtils.strip_whitespace(head_text), current_node)

            added_child = False
            for sub_element in that_element:
                new_node = self.node_from_element(sub_element)
                current_node = current_node.add_child(new_node)

                tail_text = self.get_tail_from_element(sub_element)
                if tail_text is not None:
                    current_node = self._parse_text(tail_text, current_node)
                added_child = True

            if head_text is None:
                if added_child is False:
                    raise ParserException("That node text is empty",
                                          xml_element=that_element)

            return current_node

        except ParserException as parser_excep:
            parser_excep.xml_element = that_element
            raise parser_excep
コード例 #8
0
 def get_tail_from_element(self, element):
     text = element.tail
     if text is not None:
         text = TextUtils.strip_whitespace(text)
         if text == "":
             return None
         return text
     return None
コード例 #9
0
ファイル: graph.py プロジェクト: Doshmaku/program-y
 def get_tail_from_element(self, element):
     text = element.tail
     if text is not None:
         text = TextUtils.strip_whitespace(text)
         if text == "":
             return None
         return text
     return None
コード例 #10
0
ファイル: graph.py プロジェクト: rollingstone/program-y
    def _parse_text(self, pattern_text, current_node):

        words = pattern_text.split(" ")
        for word in words:
            word = TextUtils.strip_whitespace(word)
            new_node = PatternGraph.node_from_text(word)
            current_node = current_node.add_child(new_node)

        return current_node
コード例 #11
0
ファイル: graph.py プロジェクト: dkamotsky/program-y
    def node_from_element(self, element):

        node_name = element.tag
        if self._pattern_factory.exists(node_name) is False:
            raise ParserException("Unknown node name [%s]" % node_name)

        node_instance = self._pattern_factory.new_node_class(node_name)
        if 'name' in element.attrib:
            return node_instance(element.attrib['name'])
        else:
            return node_instance(TextUtils.strip_whitespace(element.text))
コード例 #12
0
    def _parse_text(self, pattern_text, current_node):

        #words = pattern_text.split(" ")
        stripped = pattern_text.strip()
        words = stripped.split(" ")
        for word in words:
            if word != '': # Blank nodes add no value, ignore them
                word = TextUtils.strip_whitespace(word)
                new_node = PatternGraph.node_from_text(word)
                current_node = current_node.add_child(new_node)

        return current_node
コード例 #13
0
    def node_from_element(self, element, userid="*"):

        node_name = TextUtils.tag_from_text(element.tag)
        if self._pattern_factory.exists(node_name) is False:
            raise ParserException("Unknown node name [%s]" % node_name)

        text = None
        if element.text is not None:
            text = TextUtils.strip_whitespace(element.text)

        node_class_instance = self._pattern_factory.new_node_class(node_name)
        node_instance = node_class_instance(element.attrib, text, userid)

        return node_instance
コード例 #14
0
ファイル: graph.py プロジェクト: Freiza/program-y
    def node_from_element(self, element, userid="*"):

        node_name = TextUtils.tag_from_text(element.tag)
        if self._pattern_factory.exists(node_name) is False:
            raise ParserException("Unknown node name [%s]"%node_name)

        text = None
        if element.text is not None:
            text = TextUtils.strip_whitespace(element.text)

        node_class_instance = self._pattern_factory.new_node_class(node_name)
        node_instance = node_class_instance(element.attrib, text, userid)

        return node_instance
コード例 #15
0
    def _parse_text(self, pattern_text, current_node, userid="*"):

        stripped = pattern_text.strip()

        words = self._aiml_parser.brain.tokenizer.texts_to_words(stripped)

        for word in words:
            if word != '':  # Blank nodes add no value, ignore them
                word = TextUtils.strip_whitespace(word)

                new_node = self.node_from_text(word, userid=userid)

                current_node = current_node.add_child(new_node)

        return current_node
コード例 #16
0
ファイル: graph.py プロジェクト: Freiza/program-y
    def _parse_text(self, pattern_text, current_node, userid="*"):

        stripped = pattern_text.strip()

        words = self._aiml_parser.brain.tokenizer.texts_to_words(stripped)

        for word in words:
            if word != '': # Blank nodes add no value, ignore them
                word = TextUtils.strip_whitespace(word)

                new_node = self.node_from_text(word, userid=userid)

                current_node = current_node.add_child(new_node)

        return current_node
コード例 #17
0
ファイル: graph.py プロジェクト: tomliau33/program-y
    def _parse_text(self, pattern_text, current_node):

        stripped = pattern_text.strip()
        if self._aiml_parser is not None and \
                        self._aiml_parser.brain is not None and \
                        self._aiml_parser.brain.configuration.language.chinese is True:
            words = ChineseLanguage.split_unicode(stripped)
        else:
            words = stripped.split(" ")

        for word in words:
            if word != '':  # Blank nodes add no value, ignore them
                word = TextUtils.strip_whitespace(word)

                new_node = self.node_from_text(word)

                current_node = current_node.add_child(new_node)

        return current_node
コード例 #18
0
ファイル: graph.py プロジェクト: Doshmaku/program-y
    def add_that_to_node(self, that_element, base_node):
        current_node = PatternThatNode()
        current_node = base_node.add_that(current_node)

        head_text = self.get_text_from_element(that_element)
        if head_text is not None:
            current_node = self._parse_text(TextUtils.strip_whitespace(head_text), current_node)

        added_child = False
        for sub_element in that_element:
            new_node = PatternGraph.node_from_element(sub_element)
            current_node = current_node.add_child(new_node)

            tail_text = self.get_tail_from_element(sub_element)
            if tail_text is not None:
                current_node = self._parse_text(tail_text, current_node)
            added_child = True

        if head_text is None:
            if added_child is False:
                raise ParserException("That node text is empty", xml_element=that_element)

        return current_node
コード例 #19
0
ファイル: graph.py プロジェクト: vowtk0123/cotoba-agent-oss
    def _parse_text(self, pattern_text, current_node, userid="*"):

        stripped = pattern_text.strip()

        words = self._aiml_parser.brain.tokenizer.texts_to_words(stripped)

        is_priority = False
        for word in words:
            if word != '':  # Blank nodes add no value, ignore them
                word = TextUtils.strip_whitespace(word)

                if word == '$':
                    is_priority = True
                    continue

                if is_priority is True:
                    word = '$' + word
                    is_priority = False

                new_node = self.node_from_text(word, userid=userid)

                current_node = current_node.add_child(new_node)

        return current_node