本文整理汇总了Python中chatterbot.conversation.Statement.text方法的典型用法代码示例。如果您正苦于以下问题:Python Statement.text方法的具体用法?Python Statement.text怎么用?Python Statement.text使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类chatterbot.conversation.Statement
的用法示例。
在下文中一共展示了Statement.text方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: handle_matches
# 需要导入模块: from chatterbot.conversation import Statement [as 别名]
# 或者: from chatterbot.conversation.Statement import text [as 别名]
def handle_matches(self, match):
"""
Returns a response statement from a matched input statement.
:param match: It is a valid matched pattern from the input statement
:type: `_sre.SRE_Match`
"""
response = Statement(text='')
try:
from_parsed = match.group("from")
target_parsed = match.group("target")
n_statement = match.group("number")
if n_statement == 'a' or n_statement == 'an':
n_statement = '1.0'
n = mathparse.parse(n_statement, self.language)
ureg = UnitRegistry()
from_parsed, target_parsed = self.get_valid_units(ureg, from_parsed, target_parsed)
if from_parsed is None or target_parsed is None:
raise
from_value = ureg.Quantity(float(n), from_parsed)
target_value = from_value.to(target_parsed)
response.confidence = 1.0
response.text = str(target_value.magnitude)
except Exception:
response.confidence = 0.0
finally:
return response
示例2: process
# 需要导入模块: from chatterbot.conversation import Statement [as 别名]
# 或者: from chatterbot.conversation.Statement import text [as 别名]
def process(self, statement):
"""
Takes a statement string.
Returns the simplified statement string
with the mathematical terms solved.
"""
input_text = statement.text
# Use the result cached by the process method if it exists
if input_text in self.cache:
cached_result = self.cache[input_text]
self.cache = {}
return cached_result
# Getting the mathematical terms within the input statement
expression = str(self.simplify_chunks(self.normalize(input_text)))
response = Statement(text=expression)
try:
response.text += '= ' + str(
eval(expression, {f: self.functions[f] for f in self.functions})
)
# Replace '**' with '^' for evaluated exponents
response.text = response.text.replace('**', '^')
# The confidence is 1 if the expression could be evaluated
response.confidence = 1
except:
response.confidence = 0
return response