From 33307aa76f2a57c07c3305a2b7f4eaea31e6407d Mon Sep 17 00:00:00 2001 From: Bobloy Date: Thu, 3 May 2018 11:35:35 -0400 Subject: [PATCH] no python2 nonsense --- chatter/chatterbot/comparisons.py | 11 +++-------- chatter/chatterbot/conversation.py | 7 ------- chatter/chatterbot/ext/sqlalchemy_app/types.py | 5 ----- .../chatterbot/input/variable_input_type_adapter.py | 6 ++---- chatter/chatterbot/preprocessors.py | 9 +-------- chatter/chatterbot/trainers.py | 8 ++++---- chatter/chatterbot/utils.py | 9 +-------- 7 files changed, 11 insertions(+), 44 deletions(-) diff --git a/chatter/chatterbot/comparisons.py b/chatter/chatterbot/comparisons.py index c500487..43cda14 100644 --- a/chatter/chatterbot/comparisons.py +++ b/chatter/chatterbot/comparisons.py @@ -58,19 +58,14 @@ class LevenshteinDistance(Comparator): :rtype: float """ - PYTHON = sys.version_info[0] - # Return 0 if either statement has a falsy text value if not statement.text or not other_statement.text: return 0 # Get the lowercase version of both strings - if PYTHON < 3: - statement_text = unicode(statement.text.lower()) # NOQA - other_statement_text = unicode(other_statement.text.lower()) # NOQA - else: - statement_text = str(statement.text.lower()) - other_statement_text = str(other_statement.text.lower()) + + statement_text = str(statement.text.lower()) + other_statement_text = str(other_statement.text.lower()) similarity = SequenceMatcher( None, diff --git a/chatter/chatterbot/conversation.py b/chatter/chatterbot/conversation.py index ea674aa..a798e17 100644 --- a/chatter/chatterbot/conversation.py +++ b/chatter/chatterbot/conversation.py @@ -33,13 +33,6 @@ class Statement(StatementMixin): 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', []) diff --git a/chatter/chatterbot/ext/sqlalchemy_app/types.py b/chatter/chatterbot/ext/sqlalchemy_app/types.py index b48f4f6..ee9b123 100644 --- a/chatter/chatterbot/ext/sqlalchemy_app/types.py +++ b/chatter/chatterbot/ext/sqlalchemy_app/types.py @@ -13,9 +13,4 @@ class UnicodeString(TypeDecorator): Coerce Python bytestrings to unicode before saving them to the database. """ - import sys - - if sys.version_info[0] < 3: - if isinstance(value, str): - value = value.decode('utf-8') return value diff --git a/chatter/chatterbot/input/variable_input_type_adapter.py b/chatter/chatterbot/input/variable_input_type_adapter.py index c0bd8cb..a93abe9 100644 --- a/chatter/chatterbot/input/variable_input_type_adapter.py +++ b/chatter/chatterbot/input/variable_input_type_adapter.py @@ -14,10 +14,8 @@ class VariableInputTypeAdapter(InputAdapter): def detect_type(self, statement): import sys - if sys.version_info[0] < 3: - string_types = basestring # NOQA - else: - string_types = str + + string_types = str if hasattr(statement, 'text'): return self.OBJECT diff --git a/chatter/chatterbot/preprocessors.py b/chatter/chatterbot/preprocessors.py index f7043b1..59c6456 100644 --- a/chatter/chatterbot/preprocessors.py +++ b/chatter/chatterbot/preprocessors.py @@ -30,11 +30,7 @@ def unescape_html(chatbot, statement): import sys # Replace HTML escape characters - if sys.version_info[0] < 3: - from HTMLParser import HTMLParser - html = HTMLParser() - else: - import html + import html statement.text = html.unescape(statement.text) @@ -49,9 +45,6 @@ def convert_to_ascii(chatbot, statement): import unicodedata import sys - # Normalize unicode characters - if sys.version_info[0] < 3: - statement.text = unicode(statement.text) # NOQA text = unicodedata.normalize('NFKD', statement.text) text = text.encode('ascii', 'ignore').decode('utf-8') diff --git a/chatter/chatterbot/trainers.py b/chatter/chatterbot/trainers.py index e48c436..2d1aa59 100644 --- a/chatter/chatterbot/trainers.py +++ b/chatter/chatterbot/trainers.py @@ -393,10 +393,10 @@ class UbuntuCorpusTrainer(Trainer): file_kwargs = {} - if sys.version_info[0] > 2: - # Specify the encoding in Python versions 3 and up - file_kwargs['encoding'] = 'utf-8' - # WARNING: This might fail to read a unicode corpus file in Python 2.x + + # Specify the encoding in Python versions 3 and up + file_kwargs['encoding'] = 'utf-8' + # WARNING: This might fail to read a unicode corpus file in Python 2.x for file in glob.iglob(extracted_corpus_path): self.logger.info('Training from: {}'.format(file)) diff --git a/chatter/chatterbot/utils.py b/chatter/chatterbot/utils.py index 684d7f7..33c35c1 100644 --- a/chatter/chatterbot/utils.py +++ b/chatter/chatterbot/utils.py @@ -77,15 +77,8 @@ def input_function(): """ import sys - if sys.version_info[0] < 3: - user_input = str(raw_input()) # NOQA - # Avoid problems using format strings with unicode characters - if user_input: - user_input = user_input.decode('utf-8') - - else: - user_input = input() # NOQA + user_input = input() # NOQA return user_input