本文整理汇总了Python中programy.utils.text.text.TextUtils类的典型用法代码示例。如果您正苦于以下问题:Python TextUtils类的具体用法?Python TextUtils怎么用?Python TextUtils使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了TextUtils类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: resolve_element_evals
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
示例2: test_get_tabs
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")
示例3: node_from_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)
示例4: test_strip_all_whitespace
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"))
示例5: node_from_element
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
示例6: add_that_to_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
示例7: parse_expression
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)
示例8: parse_type3_condition
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)
示例9: _parse_node_with_attribs
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])
示例10: _parse_node_with_attribs
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))
示例11: parse_expression
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)
示例12: test_tag_and_namespace_from_text
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)
示例13: get_tail_from_element
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
示例14: parse_tag_expression
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)
示例15: parse_expression
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 !")