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
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
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)
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"))
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
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
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
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
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
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
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))
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
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
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
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
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
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
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
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