Esempio n. 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
Esempio n. 2
0
    def test_tag_and_namespace_from_text(self):
        tag, name = TextUtils.tag_and_namespace_from_text("")
        self.assertIsNotNone(tag)
        self.assertEqual("", tag)
        self.assertIsNone(name)

        tag, name = TextUtils.tag_and_namespace_from_text("text")
        self.assertIsNotNone(tag)
        self.assertEqual("text", tag)
        self.assertIsNone(name)

        tag, name = TextUtils.tag_and_namespace_from_text("{}")
        self.assertIsNotNone(tag)
        self.assertEqual("{}", tag)
        self.assertIsNone(name)

        tag, name = TextUtils.tag_and_namespace_from_text("{http://alicebot.org/2001/AIML}")
        self.assertIsNotNone(tag)
        self.assertEqual("{http://alicebot.org/2001/AIML}", tag)
        self.assertIsNone(name)

        tag, name = TextUtils.tag_and_namespace_from_text("{http://alicebot.org/2001/AIML}aiml")
        self.assertIsNotNone(tag)
        self.assertEqual("aiml", tag)
        self.assertIsNotNone(name)
        self.assertEqual("{http://alicebot.org/2001/AIML}", name)
Esempio n. 3
0
 def _get_directions_between_addresses_response(self, origin, destination,
                                                country, mode, units):
     origin = TextUtils.urlify(origin)
     destination = TextUtils.urlify(destination)
     url = GoogleMaps.DIRECTIONS.format(origin, destination, country, mode,
                                        units)
     return self._get_response_as_json(url)
Esempio n. 4
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:
            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
Esempio n. 5
0
 def _get_distance_between_addresses(self, origin, destination, country,
                                     mode, units):
     origin = TextUtils.urlify(origin)
     destination = TextUtils.urlify(destination)
     url = GoogleMaps.DISTANCE.format(origin, destination, country, mode,
                                      units)
     return self._get_response_as_json(url)
Esempio n. 6
0
 def test_replace_path_seperator(self):
     self.assertEquals("", TextUtils.replace_path_seperator(""))
     self.assertEquals(" ", TextUtils.replace_path_seperator(" "))
     self.assertEquals(os.sep, TextUtils.replace_path_seperator("/"))
     self.assertEquals(".." + os.sep,
                       TextUtils.replace_path_seperator("../"))
     self.assertEquals("\\",
                       TextUtils.replace_path_seperator("/", "/", "\\"))
Esempio n. 7
0
    def test_extract_namespace_and_tag(self):
        tag, namespace = TextUtils._extract_namespace_and_tag("{http://alicebot.org/2001/AIML}")
        self.assertEquals(None, namespace)
        self.assertEquals(None, tag)

        tag, namespace = TextUtils._extract_namespace_and_tag("{http://alicebot.org/2001/AIML}aiml")
        self.assertEquals("{http://alicebot.org/2001/AIML}", namespace)
        self.assertEquals("aiml", tag)
Esempio n. 8
0
    def test_get_tabs(self):
        tabs = TextUtils.get_tabs(0)
        self.assertEqual(tabs, "")

        tabs = TextUtils.get_tabs(1, tabs="\t")
        self.assertEqual(tabs, "\t")

        tabs = TextUtils.get_tabs(5, tabs="\t")
        self.assertEqual(tabs, "\t\t\t\t\t")
Esempio n. 9
0
    def test_get_tabs(self):
        tabs = TextUtils.get_tabs(0)
        self.assertEqual(tabs, "")

        tabs = TextUtils.get_tabs(1, tabs="\t")
        self.assertEqual(tabs, "\t")

        tabs = TextUtils.get_tabs(5, tabs="\t")
        self.assertEqual(tabs, "\t\t\t\t\t")
Esempio n. 10
0
    def node_from_element(self, element):

        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)

        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))
Esempio n. 11
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)
Esempio n. 12
0
 def test_strip_all_whitespace(self):
     self.assertEquals("", TextUtils.strip_all_whitespace(""))
     self.assertEquals("", TextUtils.strip_all_whitespace(" "))
     self.assertEquals("", TextUtils.strip_all_whitespace("\t"))
     self.assertEquals("", TextUtils.strip_all_whitespace("\n"))
     self.assertEquals("", TextUtils.strip_all_whitespace("\r"))
     self.assertEquals("", TextUtils.strip_all_whitespace("\r\t\n"))
     self.assertEquals("test", TextUtils.strip_all_whitespace("\r\t\ntest\r\t\n"))
     self.assertEquals("testtest", TextUtils.strip_all_whitespace("\r\t\ntest test\r\t\n"))
     self.assertEquals("testtest", TextUtils.strip_all_whitespace("\r\t\ntest\n\r\ttest\r\t\n"))
Esempio n. 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
Esempio n. 14
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. 15
0
    def _parse_node_with_attribs(self, graph, expression):

        self._name = TextUtils.tag_from_text(expression.tag)

        for attrib_name in expression.attrib:
            attrib_value = expression.attrib[attrib_name]
            if "<" in attrib_value and ">" in attrib_value:
                start = attrib_value.find("<")
                end = attrib_value.rfind(">")

                front = attrib_value[:start]
                middle = attrib_value[start:end + 1]
                back = attrib_value[end + 1:]

                root = TemplateNode()
                root.append(TemplateWordNode(front))

                xml = ET.fromstring(middle)
                xml_node = TemplateNode()
                graph.parse_tag_expression(xml, xml_node)
                root.append(xml_node)

                root.append(TemplateWordNode(back))

                self.set_attrib(attrib_name, root)

            else:
                self.set_attrib(attrib_name, TemplateWordNode(attrib_value))

        self.parse_text(graph, self.get_text_from_element(expression))

        for child in expression:
            graph.parse_tag_expression(child, self)
            self.parse_text(graph, self.get_tail_from_element(child))
Esempio n. 16
0
    def test_tag_and_namespace_from_text(self):
        tag, name = TextUtils.tag_and_namespace_from_text("")
        self.assertIsNotNone(tag)
        self.assertEquals("", tag)
        self.assertIsNone(name)

        tag, name = TextUtils.tag_and_namespace_from_text("text")
        self.assertIsNotNone(tag)
        self.assertEquals("text", tag)
        self.assertIsNone(name)

        tag, name = TextUtils.tag_and_namespace_from_text("{http://alicebot.org/2001/AIML}aiml ")
        self.assertIsNotNone(tag)
        self.assertEquals("aiml", tag)
        self.assertIsNotNone(name)
        self.assertEquals("{http://alicebot.org/2001/AIML}", name)
Esempio n. 17
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. 18
0
    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
Esempio n. 19
0
    def parse_expression(self, graph, expression):

        name_found = False

        if 'name' in expression.attrib:
            self.name = self.parse_attrib_value_as_word_node(
                graph, expression, 'name')
            name_found = True

        self.parse_text(graph, self.get_text_from_element(expression))

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

            if tag_name == 'name':
                self.name = self.parse_children_as_word_node(graph, child)
                name_found = True

            else:
                graph.parse_tag_expression(child, self)

            self.parse_text(graph, self.get_tail_from_element(child))

        if name_found is False:
            raise ParserException("Error, name not found in map",
                                  xml_element=expression)
Esempio n. 20
0
    def parse_expression(self, graph, expression):
        if 'text' in expression.attrib:
            self._text = graph.get_word_node(expression.attrib['text'])

        if 'url' in expression.attrib:
            self._url = graph.get_word_node(expression.attrib['url'])

        if 'postback' in expression.attrib:
            self._postback = graph.get_word_node(expression.attrib['postback'])

        head_text = self.get_text_from_element(expression)
        self.parse_text(graph, head_text)

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

            if tag_name == 'text':
                self._text = self.parse_children_as_word_node(graph, child)
            elif tag_name == 'url':
                self._url = self.parse_children_as_word_node(graph, child)
            elif tag_name == 'postback':
                self._postback = self.parse_children_as_word_node(graph, child)
            else:
                graph.parse_tag_expression(child, self)

            tail_text = self.get_tail_from_element(child)
            self.parse_text(graph, tail_text)
Esempio n. 21
0
    def parse_expression(self, graph, expression):

        if 'subj' in expression.attrib:
            self._subj = graph.get_word_node(expression.attrib['subj'])

        if 'pred' in expression.attrib:
            self._pred = graph.get_word_node(expression.attrib['pred'])

        if 'obj' in expression.attrib:
            self._obj = graph.get_word_node(expression.attrib['obj'])

        head_text = self.get_text_from_element(expression)
        self.parse_text(graph, head_text)

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

            if tag_name == 'subj':
                self._subj = self.parse_children_as_word_node(graph, child)

            elif tag_name == 'pred':
                self._pred = self.parse_children_as_word_node(graph, child)

            elif tag_name == 'obj':
                self._obj = self.parse_children_as_word_node(graph, child)

            else:
                raise ParserException("Invalid child item in triple")

            tail_text = self.get_tail_from_element(child)
            self.parse_text(graph, tail_text)
Esempio n. 22
0
    def parse_expression(self, graph, expression):

        if 'subj' in expression.attrib:
            self._subj = graph.get_word_node(expression.attrib['subj'])

        if 'pred' in expression.attrib:
            self._pred = graph.get_word_node(expression.attrib['pred'])

        if 'obj' in expression.attrib:
            self._obj = graph.get_word_node(expression.attrib['obj'])

        head_text = self.get_text_from_element(expression)
        self.parse_text(graph, head_text)

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

            if tag_name == 'subj':
                self._subj = self.parse_children_as_word_node(graph, child)
            elif tag_name == 'pred':
                self._pred = self.parse_children_as_word_node(graph, child)
            elif tag_name == 'obj':
                self._obj = self.parse_children_as_word_node(graph, child)
            else:
                graph.parse_tag_expression(child, self)

            tail_text = self.get_tail_from_element(child)
            self.parse_text(graph, tail_text)
Esempio n. 23
0
    def parse_expression(self, graph, expression):
        if 'image' in expression.attrib:
            self._image = graph.get_word_node(expression.attrib['image'])

        if 'title' in expression.attrib:
            self._title = graph.get_word_node(expression.attrib['title'])

        if 'subtitle' in expression.attrib:
            self._subtitle = graph.get_word_node(expression.attrib['subtitle'])

        head_text = self.get_text_from_element(expression)
        self.parse_text(graph, head_text)

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

            if tag_name == 'image':
                self._image = self.parse_children_as_word_node(graph, child)
            elif tag_name == 'title':
                self._title = self.parse_children_as_word_node(graph, child)
            elif tag_name == 'subtitle':
                self._subtitle = self.parse_children_as_word_node(graph, child)
            elif tag_name == 'button':
                button_class = graph.get_node_class_by_name("button")
                button = button_class()
                button.parse_expression(graph, child)
                self._buttons.append(button)
            else:
                graph.parse_tag_expression(child, self)

            tail_text = self.get_tail_from_element(child)
            self.parse_text(graph, tail_text)
Esempio n. 24
0
    def consume(self, bot, clientid, context, words, word_no, type, depth):

        if bot.configuration.tab_parse_output is True:
            tabs = TextUtils.get_tabs(depth)
        else:
            tabs = ""

        if context.search_depth_exceeded(depth) is True:
            #if depth > context.max_search_depth:
            if logging.getLogger().isEnabledFor(logging.ERROR):
                logging.error("%sMax search depth [%d]exceeded" %
                              (tabs, context.max_search_depth))
            return None

        if words.word(word_no) == PatternTopicNode.TOPIC:
            if logging.getLogger().isEnabledFor(logging.DEBUG):
                logging.debug("%sTopic matched %s" %
                              (tabs, words.word(word_no)))
            return super(PatternTopicNode,
                         self).consume(bot, clientid, context, words,
                                       word_no + 1, type, depth + 1)

        if logging.getLogger().isEnabledFor(logging.DEBUG):
            logging.debug("%sTopic NOT matched %s" %
                          (tabs, words.word(word_no)))
        return None
Esempio n. 25
0
    def _parse_node_with_attrib(self,
                                graph,
                                expression,
                                attrib_name,
                                default_value=None):

        attrib_found = False
        if attrib_name in expression.attrib:
            self.set_attrib(attrib_name,
                            TemplateWordNode(expression.attrib[attrib_name]))
            attrib_found = True

        self.parse_text(graph, self.get_text_from_element(expression))

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

            if tag_name == attrib_name:
                self.set_attrib(attrib_name,
                                self.parse_children_as_word_node(graph, child))
                attrib_found = True
            else:
                graph.parse_tag_expression(child, self)

            self.parse_text(graph, self.get_tail_from_element(child))

        if attrib_found is False:
            if default_value is not None:
                YLogger.debug(self, "Setting default value for attrib [%s]",
                              attrib_name)
                self.set_attrib(attrib_name, TemplateWordNode(default_value))
Esempio n. 26
0
    def _parse_node_with_attribs(self, graph, expression, attribs):

        attribs_found = []
        for attrib in attribs:
            attrib_name = attrib[0]
            if attrib_name in expression.attrib:
                self.set_attrib(attrib_name, expression.attrib[attrib_name])
                attribs_found.append(attrib_name)

        self.parse_text(graph, self.get_text_from_element(expression))

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

            for attrib in attribs:
                attrib_name = attrib[0]
                if tag_name == attrib_name:
                    self.set_attrib(attrib[0], self.get_text_from_element(child))
                else:
                    graph.parse_tag_expression(child, self)

            self.parse_text(graph, self.get_tail_from_element(child))

        for attrib in attribs:
            attrib_name = attrib[0]
            if attrib_name not in attribs_found:
                if attrib[1] is not None:
                    YLogger.debug(self, "Setting default value for attrib [%s]", attrib_name)
                    self.set_attrib(attrib_name, attrib[1])
Esempio n. 27
0
    def parse_type3_condition(self, graph, expression):
        for child in expression:
            tag_name = TextUtils.tag_from_text(child.tag)

            if tag_name in ['name', 'var', 'bot']:
                pass

            elif tag_name == 'li':
                list_item = TemplateConditionListItemNode()

                name, var_type = self.get_condition_name(child)
                list_item._name = name
                list_item._var_type = var_type

                list_item._value = self.get_condition_value(graph, child)

                self.children.append(list_item)

                list_item.parse_text(graph, self.get_text_from_element(child))

                for sub_pattern in child:
                    if sub_pattern.tag in ['name', 'var', 'bot', 'value']:
                        pass

                    elif sub_pattern.tag == 'loop':
                        list_item.loop = True

                    else:
                        graph.parse_tag_expression(sub_pattern, list_item)

                    tail_text = self.get_tail_from_element(sub_pattern)
                    list_item.parse_text(graph, tail_text)

            else:
                raise ParserException("Invalid element <%s> in condition element" % (tag_name), xml_element=expression)
Esempio n. 28
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. 29
0
    def parse_expression(self, graph, expression):

        name_found = False

        if 'name' in expression.attrib:
            name = self.parse_attrib_value_as_word_node(graph, expression, 'name')
            name_text = JapaneseLanguage.zenhan_normalize(name.children[0].word)
            map_name = name_text.upper()
            if graph.aiml_parser.brain.maps.storename(map_name) is None:
                if graph.aiml_parser.brain.dynamics.is_dynamic_map(map_name) is False:
                    raise ParserException("Map[%s] name not found" % map_name, xml_element=expression, nodename='map')
            self.name = name
            name_found = True

        self.parse_text(graph, self.get_text_from_element(expression))

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

            if tag_name == 'name':
                self.name = self.parse_children_as_word_node(graph, child)
                name_found = True

            else:
                graph.parse_tag_expression(child, self)

            self.parse_text(graph, self.get_tail_from_element(child))

        if name_found is False:
            raise ParserException("Name not found in map", xml_element=expression, nodename='map')
Esempio n. 30
0
    def parse_expression(self, graph, expression):
        if 'text' in expression.attrib:
            self._text = graph.get_word_node(expression.attrib['text'])

        if 'url' in expression.attrib:
            self._url = graph.get_word_node(expression.attrib['url'])

        head_text = self.get_text_from_element(expression)
        self.parse_text(graph, head_text)

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

            if tag_name == 'text':
                self._text = self.parse_children_as_word_node(graph, child)
            elif tag_name == 'url':
                self._url = self.parse_children_as_word_node(graph, child)
            else:
                raise ParserException("Invalid child nodes in link")

            tail_text = self.get_tail_from_element(child)
            self.parse_text(graph, tail_text)

        if self._text is None:
            raise ParserException("Missing text from link")
Esempio n. 31
0
    def parse_expression(self, graph, expression):

        subj = None
        pred = None
        obj = None

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

            if tag_name == 'subj':
                subj = self.parse_children_as_word_node(graph, child)
            elif tag_name == 'pred':
                pred = self.parse_children_as_word_node(graph, child)
            elif tag_name == 'obj':
                obj = self.parse_children_as_word_node(graph, child)
            else:
                if logging.getLogger().isEnabledFor(logging.WARNING):
                    logging.warning("Unknown tag name [%s] in select query",
                                    tag_name)

        if subj is None:
            raise ParserException("<subj> element missing from select query")

        if pred is None:
            raise ParserException("<pred> element missing from select query")

        if obj is None:
            raise ParserException("<obj> element missing from select query")

        self._query = RDFUniqueStatement(
            RDFQuery(subj, pred, obj, RDFQuery.QUERY))
Esempio n. 32
0
    def match_children(self, bot, clientid, children, child_type, words,
                       word_no, context, type, depth):

        if bot.configuration.tab_parse_output is True:
            tabs = TextUtils.get_tabs(depth)
        else:
            tabs = ""

        for child in children:

            result = child.equals(bot, clientid, words, word_no)
            if result.matched is True:
                word_no = result.word_no
                if logging.getLogger().isEnabledFor(logging.DEBUG):
                    logging.debug("%s%s matched %s" %
                                  (tabs, child_type, result.matched_phrase))

                match_node = Match(type, child, result.matched_phrase)

                context.add_match(match_node)

                match = child.consume(bot, clientid, context, words,
                                      word_no + 1, type, depth + 1)
                if match is not None:
                    if logging.getLogger().isEnabledFor(logging.DEBUG):
                        logging.debug("%sMatched %s child, success!" %
                                      (tabs, child_type))
                    return match, word_no
                else:
                    context.pop_match()

        return None, word_no
Esempio n. 33
0
    def _parse_node_with_attribs(self, graph, expression):

        self._name = TextUtils.tag_from_text(expression.tag)

        for attrib_name in expression.attrib:
            attrib_value = expression.attrib[attrib_name]
            if "<" in attrib_value and ">" in attrib_value:
                start = attrib_value.find("<")
                end = attrib_value.rfind(">")

                front = attrib_value[:start]
                middle = attrib_value[start:end+1]
                back = attrib_value[end+1:]

                root = TemplateNode()
                root.append(TemplateWordNode(front))

                xml = ET.fromstring(middle)
                xml_node = TemplateNode()
                graph.parse_tag_expression(xml, xml_node)
                root.append(xml_node)

                root.append(TemplateWordNode(back))

                self.set_attrib(attrib_name, root)

            else:
                self.set_attrib(attrib_name, TemplateWordNode(attrib_value))

        self.parse_text(graph, self.get_text_from_element(expression))

        for child in expression:
            graph.parse_tag_expression(child, self)
            self.parse_text(graph, self.get_tail_from_element(child))
Esempio n. 34
0
    def parse_expression(self, graph, expression):

        if 'name' in expression.attrib:
            self._slotName = self.parse_attrib_value_as_word_node(
                graph, expression, 'name')

        if 'item' in expression.attrib:
            self._itemName = self.parse_attrib_value_as_word_node(
                graph, expression, 'item')

        if 'index' in expression.attrib:
            self._index = self.parse_attrib_value_as_word_node(
                graph, expression, 'index')

        self.parse_text(graph, self.get_text_from_element(expression))

        for child in expression:
            tagName = TextUtils.tag_from_text(child.tag)

            if tagName == 'name':
                self._slotName = self.parse_children_as_word_node(graph, child)
            if tagName == 'item':
                self._itemName = self.parse_children_as_word_node(graph, child)
            if tagName == 'index':
                self._index = self.parse_children_as_word_node(graph, child)
            else:
                graph.parse_tag_expression(child, self)
                self.parse_text(graph, self.get_tail_from_element(child))

        if self._slotName is None or self._itemName is None:
            raise ParserException("Missing either slot or item",
                                  xml_element=expression,
                                  nodename='nluslot')
Esempio n. 35
0
    def parse_topic(self, topic_element, namespace):

        if 'name' in topic_element.attrib:
            name = topic_element.attrib['name']
            if name is None or not name:
                raise ParserException("Topic name empty or null", xml_element=topic_element)
            xml = "<topic>%s</topic>" % name
            YLogger.info(self, "Topic attrib converted to %s", xml)
            topic_pattern = ET.fromstring(xml)
        else:
            raise ParserException("Missing name attribute for topic", xml_element=topic_element)

        category_found = False
        num_category = 0
        for child in topic_element:
            tag_name, _ = TextUtils.tag_and_namespace_from_text(child.tag)
            if tag_name == 'category':
                self.parse_category(child, namespace, topic_pattern)
                category_found = True
                num_category += 1
            else:
                raise ParserException("Unknown child node of topic, %s" % child.tag, xml_element=topic_element)

        if category_found is False:
            raise ParserException("No categories in topic", xml_element=topic_element)

        return num_category
Esempio n. 36
0
    def _parse_node_with_attrib(self,
                                graph,
                                expression,
                                attrib_name,
                                default_value=None):

        attrib_found = True
        if attrib_name in expression.attrib:
            self.set_attrib(attrib_name, expression.attrib[attrib_name])

        self.parse_text(graph, self.get_text_from_element(expression))

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

            if tag_name == attrib_name:
                self.set_attrib(attrib_name, self.get_text_from_element(child))
            else:
                graph.parse_tag_expression(child, self)

            self.parse_text(graph, self.get_tail_from_element(child))

        if attrib_found is False:
            if logging.getLogger().isEnabledFor(logging.DEBUG):
                logging.debug("Setting default value for attrib [%s]",
                              attrib_name)
            self.set_attrib(attrib_name, default_value)
Esempio n. 37
0
    def _parse_node_with_attribs(self, graph, expression, attribs):

        attribs_found = []
        for attrib in attribs:
            attrib_name = attrib[0]
            if attrib_name in expression.attrib:
                self.set_attrib(attrib_name, expression.attrib[attrib_name])
                attribs_found.append(attrib_name)

        self.parse_text(graph, self.get_text_from_element(expression))

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

            for attrib in attribs:
                attrib_name = attrib[0]
                if tag_name == attrib_name:
                    self.set_attrib(attrib[0], self.get_text_from_element(child))
                else:
                    graph.parse_tag_expression(child, self)

            self.parse_text(graph, self.get_tail_from_element(child))

        for attrib in attribs:
            attrib_name = attrib[0]
            if attrib_name not in attribs_found:
                if attrib[1] is not None:
                    YLogger.debug(self, "Setting default value for attrib [%s]", attrib_name)
                    self.set_attrib(attrib_name, attrib[1])
Esempio n. 38
0
    def parse_type1_condition(self, graph, expression):
        self.parse_text(graph, self.get_text_from_element(expression))

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

            if tag_name == 'name':
                pass

            elif tag_name == 'var':
                pass

            elif tag_name == 'value':
                pass

            elif tag_name == 'li':
                raise ParserException(
                    "Error, li element not allowed as child of condition element",
                    xml_element=expression)

            elif tag_name == 'loop':
                raise ParserException(
                    "Error, this type of condition cannot have <loop /> element",
                    xml_element=expression)

            else:
                graph.parse_tag_expression(child, self)

            tail_text = self.get_tail_from_element(child)
            self.parse_text(graph, tail_text)
Esempio n. 39
0
    def parse_expression(self, graph, expression):
        if 'text' in expression.attrib:
            self._text = graph.get_word_node(expression.attrib['text'])

        if 'url' in expression.attrib:
            self._url = graph.get_word_node(expression.attrib['url'])

        if 'postback' in expression.attrib:
            self._postback = graph.get_word_node(expression.attrib['postback'])

        head_text = self.get_text_from_element(expression)
        self.parse_text(graph, head_text)

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

            if tag_name == 'text':
                self._text = self.parse_children_as_word_node(graph, child)
            elif tag_name == 'url':
                self._url = self.parse_children_as_word_node(graph, child)
            elif tag_name == 'postback':
                self._postback = self.parse_children_as_word_node(graph, child)
            else:
                graph.parse_tag_expression(child, self)

            tail_text = self.get_tail_from_element(child)
            self.parse_text(graph, tail_text)

        if self._text is None:
            raise ParserException("Missing text element",
                                  xml_element=expression,
                                  nodename='button')
Esempio n. 40
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
Esempio n. 41
0
 def parse_tag_expression(self, expression, branch):
     tag_name = TextUtils.tag_from_text(expression.tag)
     if self._template_factory.exists(tag_name):
         if tag_name == "condition":
             node_instance = self._template_factory.new_node_class(tag_name)()
         else:
             node_instance = self._template_factory.new_node_class(tag_name)()
         node_instance.parse_expression(self, expression)
         branch.children.append(node_instance)
     else:
         self.parse_unknown_as_xml_node(expression, branch)
Esempio n. 42
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
Esempio n. 43
0
    def parse_expression(self, graph, expression):

        if 'format' in expression.attrib:
            self.interval_format = graph.get_word_node(expression.attrib['format'])

        head_text = self.get_text_from_element(expression)
        self.parse_text(graph, head_text)

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

            if tag_name == 'format':
                self.interval_format = graph.get_word_node(self.get_text_from_element(child))

            elif tag_name == 'style':
                node = graph.get_base_node()
                node.parse_text(graph, self.get_text_from_element(child))
                for sub_child in child:
                    graph.parse_tag_expression(sub_child, node)
                    node.parse_text(graph, self.get_text_from_element(child))
                self.style = node

            elif tag_name == 'from':
                node = graph.get_base_node()
                node.parse_text(graph, self.get_text_from_element(child))
                for sub_child in child:
                    graph.parse_tag_expression(sub_child, node)
                    node.parse_text(graph, self.get_text_from_element(child))
                self.interval_from = node

            elif tag_name == 'to':
                node = graph.get_base_node()
                node.parse_text(graph, self.get_text_from_element(child))
                for sub_child in child:
                    graph.parse_tag_expression(sub_child, node)
                    node.parse_text(graph, self.get_text_from_element(child))
                self.interval_to = node
            else:
                graph.parse_tag_expression(child, self)

            tail_text = self.get_tail_from_element(child)
            self.parse_text(graph, tail_text)

        if self.interval_format is None:
            YLogger.warning(self, "Interval node, format missing, defaulting to 'c%%'!")
            self.interval_format = "%c"
        if self.style is None:
            YLogger.warning(self, "style node, format missing, defaulting to 'days'!")
            self.style = "days"
        if self.interval_from is None:
            YLogger.warning(self, "interval_from node, format missing !")
        if self.interval_to is None:
            YLogger.warning(self, "interval_to node, format missing !")
Esempio n. 44
0
 def parse_last_sentences_from_response(self, response):
     # TODO Issue here when the response is more than just a simple sentence
     # If the response contains punctuation such as "Hello. There" then THAT is none
     response = re.sub(r'<\s*br\s*/>\s*', ".", response)
     response = re.sub(r'<br></br>*', ".", response)
     sentences = response.split(".")
     sentences = [x for x in sentences if x]
     last_sentence = sentences[-1]
     that_pattern = TextUtils.strip_all_punctuation(last_sentence)
     that_pattern = that_pattern.strip()
     # TODO Added this to catch a failed sentence
     if that_pattern == "":
         that_pattern = '*'
     return that_pattern
Esempio n. 45
0
 def to_xml(self, client_context):
     xml = "<%s"%self._name
     for attrib_name in self._attribs:
         if isinstance(self._attribs[attrib_name], str):
             attrib_value = self._attribs[attrib_name]
         else:
             attrib_value = self._attribs[attrib_name].resolve(client_context)
         escaped = TextUtils.html_escape(attrib_value)
         xml += ' %s="%s"' % (attrib_name, escaped)
     xml += ">"
     child_xml = self.children_to_xml(client_context)
     xml += child_xml
     xml += "</%s>"%self._name
     return xml
Esempio n. 46
0
 def resolve_to_string(self, client_context):
     xml = "<%s" % self._name
     for attrib_name in self._attribs:
         if isinstance(self._attribs[attrib_name], str):
             attrib_value = self._attribs[attrib_name]
         else:
             attrib_value = self._attribs[attrib_name].resolve(client_context)
         escaped = TextUtils.html_escape(attrib_value)
         escaped = escaped.replace(" ", "")
         xml += ' %s="%s"' % (attrib_name, escaped)
     xml += ">"
     xml += self.resolve_children_to_string(client_context)
     xml += "</%s>" % self._name
     return xml
Esempio n. 47
0
    def consume(self, bot, clientid, context, words, word_no, type, depth):

        tabs = TextUtils.get_tabs(word_no)

        if depth > context.max_search_depth:
            logging.error("%sMax search depth [%d]exceeded" % (tabs, context.max_search_depth))
            return None

        if words.word(word_no) == PatternTopicNode.TOPIC:
            logging.debug("%sTopic matched %s" % (tabs, words.word(word_no)))
            return super(PatternTopicNode, self).consume(bot, clientid, context, words, word_no+1, type, depth+1)

        logging.debug("%sTopic NOT matched %s" % (tabs, words.word(word_no)))
        return None
Esempio n. 48
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
Esempio n. 49
0
    def parse_expression(self, graph, expression):
        head_text = self.get_text_from_element(expression)
        self.parse_text(graph, head_text)

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

            if tag_name == 'item':
                item = self.parse_children_as_word_node(graph, child)
                self._items.append(item)
            else:
                graph.parse_tag_expression(child, self)

            tail_text = self.get_tail_from_element(child)
            self.parse_text(graph, tail_text)
Esempio n. 50
0
    def parse_expression(self, graph, expression):
        li_found = False
        for child in expression:
            tag_name = TextUtils.tag_from_text(child.tag)

            if tag_name == 'li':
                li_found = True
                li_node = graph.get_base_node()
                self.children.append(li_node)
                li_node.parse_template_node(graph, child)
            else:
                raise ParserException("Unsupported random child tag: %s" % (tag_name), xml_element=expression)

        if li_found is False:
            raise ParserException("No li children of random element!", xml_element=expression)
Esempio n. 51
0
    def parse_expression(self, graph, expression):
        head_text = self.get_text_from_element(expression)
        self.parse_text(graph, head_text)

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

            if tag_name == 'card':
                card_class = graph.get_node_class_by_name("card")
                card = card_class()
                card.parse_expression(graph, child)
                self._cards.append(card)
            else:
                graph.parse_tag_expression(child, self)

            tail_text = self.get_tail_from_element(child)
            self.parse_text(graph, tail_text)
Esempio n. 52
0
    def parse_expression(self, graph, expression):
        if 'seconds' in expression.attrib:
            self._seconds = graph.get_word_node(expression.attrib['text'])

        head_text = self.get_text_from_element(expression)
        self.parse_text(graph, head_text)

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

            if tag_name == 'seconds':
                self._seconds = self.parse_children_as_word_node(graph, child)

            else:
                graph.parse_tag_expression(child, self)

            tail_text = self.get_tail_from_element(child)
            self.parse_text(graph, tail_text)
Esempio n. 53
0
    def parse_expression(self, graph, expression):

        if 'path' in expression.attrib:
            self.path = expression.attrib['path']

        head_text = self.get_text_from_element(expression)
        self.parse_text(graph, head_text)

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

            if tag_name == 'path':
                self.path = self.get_text_from_element(child)
            else:
                graph.parse_tag_expression(child, self)

            tail_text = self.get_tail_from_element(child)
            self.parse_text(graph, tail_text)

        if self.path is None:
            raise ParserException("EXTENSION node, path attribute missing !")
Esempio n. 54
0
    def _parse_node_with_attrib(self, graph, expression, attrib_name, default_value=None):

        attrib_found = True
        if attrib_name in expression.attrib:
            self.set_attrib(attrib_name, expression.attrib[attrib_name])

        self.parse_text(graph, self.get_text_from_element(expression))

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

            if tag_name == attrib_name:
                self.set_attrib(attrib_name, self.get_text_from_element(child))
            else:
                graph.parse_tag_expression(child, self)

            self.parse_text(graph, self.get_tail_from_element(child))

        if attrib_found is False:
            YLogger.debug(self, "Setting default value for attrib [%s]", attrib_name)
            self.set_attrib(attrib_name, default_value)
Esempio n. 55
0
    def parse_expression(self, graph, expression):

        name_found = False
        var_found = False

        if 'name' in expression.attrib:
            self.name = self.parse_attrib_value_as_word_node(graph, expression, 'name')
            self.local = False
            name_found = True

        if 'var' in expression.attrib:
            self.name = self.parse_attrib_value_as_word_node(graph, expression, 'var')
            self.local = True
            var_found = True

        self.parse_text(graph, self.get_text_from_element(expression))

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

            if tag_name == 'name':
                self.name = self.parse_children_as_word_node(graph, child)
                self.local = False
                name_found = True

            elif tag_name == 'var':
                self.name = self.parse_children_as_word_node(graph, child)
                self.local = True
                var_found = True

            else:
                graph.parse_tag_expression(child, self)

            self.parse_text(graph, self.get_tail_from_element(child))

        if name_found is True and var_found is True:
            raise ParserException("Set node has both name AND var values", xml_element=expression)

        if name_found is False and var_found is False:
            raise ParserException("Set node has both name AND var values", xml_element=expression)
Esempio n. 56
0
    def parse_type1_condition(self, graph, expression):
        self.parse_text(graph, self.get_text_from_element(expression))

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

            if tag_name in ['name', 'var', 'bot', 'value']:
                pass

            elif tag_name == 'li':
                raise ParserException("li element not allowed as child of condition element",
                                      xml_element=expression)

            elif tag_name == 'loop':
                raise ParserException("This type of condition cannot have <loop /> element",
                                      xml_element=expression)

            else:
                graph.parse_tag_expression(child, self)

            tail_text = self.get_tail_from_element(child)
            self.parse_text(graph, tail_text)
Esempio n. 57
0
    def ask_question(self, bot, clientid, sentence) -> str:

        conversation = bot.get_conversation(clientid)

        topic_pattern = conversation.predicate("topic")
        if topic_pattern is None:
            logging.info("No Topic pattern default to [*]")
            topic_pattern = "*"
        else:
            logging.info("Topic pattern = [%s]", topic_pattern)

        try:
            that_question = conversation.nth_question(2)
            that_sentence = that_question.current_sentence()

            # If the last response was valid, i.e not none and not empty string, then use
            # that as the that_pattern, otherwise we default to '*' as pattern
            if that_sentence.response is not None and that_sentence.response != '':
                that_pattern = TextUtils.strip_all_punctuation(that_sentence.response)
                logging.info("That pattern = [%s]", that_pattern)
            else:
                logging.info("That pattern, no response, default to [*]")
                that_pattern = "*"

        except Exception:
            logging.info("No That pattern default to [*]")
            that_pattern = "*"

        match_context =  self._aiml_parser.match_sentence(bot, clientid,
                                                        sentence,
                                                        topic_pattern=topic_pattern,
                                                        that_pattern=that_pattern)

        if match_context is not None:
            template_node = match_context.template_node()
            logging.debug("AIML Parser evaluating template [%s]", template_node.to_string())
            return template_node.template.resolve(bot, clientid)

        return None