本文整理汇总了Python中chatterbot.conversation.Statement类的典型用法代码示例。如果您正苦于以下问题:Python Statement类的具体用法?Python Statement怎么用?Python Statement使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Statement类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: process_input
def process_input(self, statement):
new_message = False
input_statement = self.context.get_last_input_statement()
response_statement = self.context.get_last_response_statement()
if input_statement:
last_message_id = input_statement.extra_data.get("hipchat_message_id", None)
if last_message_id:
self.recent_message_ids.add(last_message_id)
if response_statement:
last_message_id = response_statement.extra_data.get("hipchat_message_id", None)
if last_message_id:
self.recent_message_ids.add(last_message_id)
while not new_message:
data = self.get_most_recent_message(self.hipchat_room)
if data and data["id"] not in self.recent_message_ids:
self.recent_message_ids.add(data["id"])
new_message = True
else:
pass
sleep(3.5)
text = data["message"]
statement = Statement(text)
statement.add_extra_data("hipchat_message_id", data["id"])
return statement
示例2: get_statement
def get_statement(self):
from chatterbot.conversation import Statement as StatementObject
statement = StatementObject(self.text, extra_data=self.extra_data)
for response in self.in_response_to:
statement.add_response(response.get_response())
return statement
示例3: handle_matches
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
示例4: get_statements
def get_statements(self):
"""
Returns list of random statements from the API.
"""
from twitter import TwitterError
statements = []
# Generate a random word
random_word = self.random_word(self.random_seed_word, self.lang)
self.chatbot.logger.info('Requesting 50 random tweets containing the word {}'.format(random_word))
tweets = self.api.GetSearch(term=random_word, count=50, lang=self.lang)
for tweet in tweets:
statement = Statement(text=tweet.text)
if tweet.in_reply_to_status_id:
try:
status = self.api.GetStatus(tweet.in_reply_to_status_id)
statement.in_response_to = status.text
statements.append(statement)
except TwitterError as error:
self.chatbot.logger.warning(str(error))
self.chatbot.logger.info('Adding {} tweets with responses'.format(len(statements)))
return statements
示例5: process
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)))
# Returning important information
try:
expression += '= ' + str(
eval(expression, {f: getattr(numpy, f) for f in self.functions})
)
response = Statement(expression)
response.confidence = 1
# return a confidence of 1 if the expression could be evaluated
return 1, response
except:
response = Statement(expression)
response.confidence = 0
return 0, response
示例6: read_file
def read_file(files, queue, preprocessors, stemmer):
statements_from_file = []
for tsv_file in files:
with open(tsv_file, 'r', encoding='utf-8') as tsv:
reader = csv.reader(tsv, delimiter='\t')
previous_statement_text = None
previous_statement_search_text = ''
for row in reader:
if len(row) > 0:
statement = Statement(
text=row[3],
in_response_to=previous_statement_text,
conversation='training',
created_at=date_parser.parse(row[0]),
persona=row[1]
)
for preprocessor in preprocessors:
statement = preprocessor(statement)
statement.search_text = stemmer.get_bigram_pair_string(statement.text)
statement.search_in_response_to = previous_statement_search_text
previous_statement_text = statement.text
previous_statement_search_text = statement.search_text
statements_from_file.append(statement)
queue.put(tuple(statements_from_file))
示例7: process
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
示例8: process
def process(self, statement):
"""
Takes a statement string.
Returns the equation from the statement with the mathematical terms solved.
"""
from mathparse import mathparse
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 = mathparse.extract_expression(input_text, language=self.language)
response = Statement(text=expression)
try:
response.text += ' = ' + str(
mathparse.parse(expression, language=self.language)
)
# The confidence is 1 if the expression could be evaluated
response.confidence = 1
except mathparse.PostfixTokenEvaluationException:
response.confidence = 0
return response
示例9: process
def process(self, statement, additional_response_selection_parameters=None):
now = datetime.now()
time_features = self.time_question_features(statement.text.lower())
confidence = self.classifier.classify(time_features)
response = Statement(text='The current time is ' + now.strftime('%I:%M %p'))
response.confidence = confidence
return response
示例10: search
def search(self, input_statement, **additional_parameters):
"""
Search for close matches to the input. Confidence scores for
subsequent results will order of increasing value.
:param input_statement: A statement.
:type input_statement: chatterbot.conversation.Statement
:param **additional_parameters: Additional parameters to be passed
to the ``filter`` method of the storage adapter when searching.
:rtype: Generator yielding one closest matching statement at a time.
"""
self.chatbot.logger.info('Beginning search for close text match')
input_search_text = input_statement.search_text
if not input_statement.search_text:
self.chatbot.logger.warn(
'No value for search_text was available on the provided input'
)
input_search_text = self.chatbot.storage.tagger.get_bigram_pair_string(
input_statement.text
)
search_parameters = {
'search_text_contains': input_search_text,
'persona_not_startswith': 'bot:',
'page_size': self.search_page_size
}
if additional_parameters:
search_parameters.update(additional_parameters)
statement_list = self.chatbot.storage.filter(**search_parameters)
closest_match = Statement(text='')
closest_match.confidence = 0
self.chatbot.logger.info('Processing search results')
# Find the closest matching known statement
for statement in statement_list:
confidence = self.compare_statements(input_statement, statement)
if confidence > closest_match.confidence:
statement.confidence = confidence
closest_match = statement
self.chatbot.logger.info('Similar text found: {} {}'.format(
closest_match.text, confidence
))
yield closest_match
示例11: StatementIntegrationTestCase
class StatementIntegrationTestCase(TestCase):
"""
Test case to make sure that the Django Statement model
and ChatterBot Statement object have a common interface.
"""
def setUp(self):
super().setUp()
from datetime import datetime
from pytz import UTC
now = datetime(2020, 2, 15, 3, 14, 10, 0, UTC)
self.object = StatementObject(text='_', created_at=now)
self.model = StatementModel(text='_', created_at=now)
# Simulate both statements being saved
self.model.save()
self.object.id = self.model.id
def test_text(self):
self.assertTrue(hasattr(self.object, 'text'))
self.assertTrue(hasattr(self.model, 'text'))
def test_in_response_to(self):
self.assertTrue(hasattr(self.object, 'in_response_to'))
self.assertTrue(hasattr(self.model, 'in_response_to'))
def test_conversation(self):
self.assertTrue(hasattr(self.object, 'conversation'))
self.assertTrue(hasattr(self.model, 'conversation'))
def test_tags(self):
self.assertTrue(hasattr(self.object, 'tags'))
self.assertTrue(hasattr(self.model, 'tags'))
def test__str__(self):
self.assertTrue(hasattr(self.object, '__str__'))
self.assertTrue(hasattr(self.model, '__str__'))
self.assertEqual(str(self.object), str(self.model))
def test_add_tags(self):
self.object.add_tags('a', 'b')
self.model.add_tags('a', 'b')
self.assertIn('a', self.object.get_tags())
self.assertIn('a', self.model.get_tags())
def test_serialize(self):
object_data = self.object.serialize()
model_data = self.model.serialize()
self.assertEqual(object_data, model_data)
示例12: process
def process(self, statement):
from chatterbot.conversation import Statement
now = datetime.now()
time_features = self.time_question_features(statement.text.lower())
confidence = self.classifier.classify(time_features)
response = Statement(text='The current time is ' + now.strftime('%I:%M %p'))
response.confidence = confidence
return response
示例13: test_update_does_not_modify_existing_statement
def test_update_does_not_modify_existing_statement(self):
statement = Statement("New statement")
self.adapter.update(statement)
self.adapter.read_only = True
statement.add_response(Response("New response"))
self.adapter.update(statement)
statement_found = self.adapter.find("New statement")
self.assertEqual(statement_found.text, statement.text)
self.assertEqual(len(statement_found.in_response_to), 0)
示例14: test_getting_and_updating_statement
def test_getting_and_updating_statement(self):
statement = Statement("Hi")
self.adapter.update(statement)
statement.add_response(Response("Hello"))
statement.add_response(Response("Hello"))
self.adapter.update(statement)
response = self.adapter.find(statement.text)
self.assertEqual(len(response.in_response_to), 1)
self.assertEqual(response.in_response_to[0].occurrence, 2)
示例15: test_update_does_not_modify_existing_statement
def test_update_does_not_modify_existing_statement(self):
statement = Statement("New statement")
self.adapter.update(statement)
self.adapter.read_only = True
statement.update_occurrence_count()
self.adapter.update(statement)
statement_found = self.adapter.find("New statement")
self.assertEqual(statement_found.text, statement.text)
self.assertEqual(statement.occurrence, 2)
self.assertEqual(statement_found.occurrence, 1)