Skip to content

Commit

Permalink
Try to decode at the statement level
Browse files Browse the repository at this point in the history
  • Loading branch information
gunthercox committed Dec 4, 2017
1 parent fde649b commit 48c5b60
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
8 changes: 8 additions & 0 deletions chatterbot/conversation/statement.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,21 @@ class Statement(StatementMixin):
"""

def __init__(self, text, **kwargs):
import sys

# Try not to allow non-string types to be passed to statements
try:
text = str(text)
except UnicodeEncodeError:
pass

# Prefer decoded utf8-strings in Python 2.7
if sys.version_info[0] < 3:
try:
text = text.decode('utf-8')
except UnicodeEncodeError:
pass

self.text = text
self.tags = kwargs.pop('tags', [])
self.in_response_to = kwargs.pop('in_response_to', [])
Expand Down
14 changes: 3 additions & 11 deletions chatterbot/input/input_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,14 @@ def process_input_statement(self, *args, **kwargs):

input_statement = self.process_input(*args, **kwargs)

input_statement_text = input_statement.text

if sys.version_info[0] < 3:
try:
input_statement_text = input_statement_text.decode('utf-8')
except UnicodeEncodeError:
pass

self.logger.info(u'Received input statement: {}'.format(input_statement_text))
self.logger.info(u'Received input statement: {}'.format(input_statement.text))

existing_statement = self.chatbot.storage.find(input_statement.text)

if existing_statement:
self.logger.info(u'"{}" is a known statement'.format(input_statement_text))
self.logger.info(u'"{}" is a known statement'.format(input_statement.text))
input_statement = existing_statement
else:
self.logger.info(u'"{}" is not a known statement'.format(input_statement_text))
self.logger.info(u'"{}" is not a known statement'.format(input_statement.text))

return input_statement

0 comments on commit 48c5b60

Please sign in to comment.