本文整理汇总了Python中programy.utils.text.text.TextUtils.strip_whitespace方法的典型用法代码示例。如果您正苦于以下问题:Python TextUtils.strip_whitespace方法的具体用法?Python TextUtils.strip_whitespace怎么用?Python TextUtils.strip_whitespace使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类programy.utils.text.text.TextUtils
的用法示例。
在下文中一共展示了TextUtils.strip_whitespace方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: resolve_element_evals
# 需要导入模块: from programy.utils.text.text import TextUtils [as 别名]
# 或者: from programy.utils.text.text.TextUtils import strip_whitespace [as 别名]
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: node_from_element
# 需要导入模块: from programy.utils.text.text import TextUtils [as 别名]
# 或者: from programy.utils.text.text.TextUtils import strip_whitespace [as 别名]
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)
示例3: test_strip_whitespace
# 需要导入模块: from programy.utils.text.text import TextUtils [as 别名]
# 或者: from programy.utils.text.text.TextUtils import strip_whitespace [as 别名]
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"))
示例4: add_that_to_node
# 需要导入模块: from programy.utils.text.text import TextUtils [as 别名]
# 或者: from programy.utils.text.text.TextUtils import strip_whitespace [as 别名]
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
示例5: get_tail_from_element
# 需要导入模块: from programy.utils.text.text import TextUtils [as 别名]
# 或者: from programy.utils.text.text.TextUtils import strip_whitespace [as 别名]
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
示例6: _parse_text
# 需要导入模块: from programy.utils.text.text import TextUtils [as 别名]
# 或者: from programy.utils.text.text.TextUtils import strip_whitespace [as 别名]
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
示例7: node_from_element
# 需要导入模块: from programy.utils.text.text import TextUtils [as 别名]
# 或者: from programy.utils.text.text.TextUtils import strip_whitespace [as 别名]
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
示例8: _parse_text
# 需要导入模块: from programy.utils.text.text import TextUtils [as 别名]
# 或者: from programy.utils.text.text.TextUtils import strip_whitespace [as 别名]
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
示例9: add_that_to_node
# 需要导入模块: from programy.utils.text.text import TextUtils [as 别名]
# 或者: from programy.utils.text.text.TextUtils import strip_whitespace [as 别名]
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