Esempio n. 1
0
    def resolve_element_evals(self, client_context, 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 = client_context.brain.aiml_parser.template_parser.parse_template_expression(
                    template)
                resolved = ast.resolve(client_context)

                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
Esempio n. 2
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"))
Esempio n. 3
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
Esempio n. 4
0
 def get_text_from_element(self, element):
     text = element.text
     if text is not None:
         text = TextUtils.strip_whitespace(text)
         if text == "":
             return None
         return text
     return None
Esempio n. 5
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
Esempio n. 6
0
    def _parse_text(self, pattern_text, current_node, userid="*"):

        stripped = pattern_text.strip()

        words = self._aiml_parser.brain.nlp.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