Skip to content

Commit

Permalink
Correct unicode equality issue in python 2.7
Browse files Browse the repository at this point in the history
  • Loading branch information
gunthercox committed Aug 3, 2016
1 parent 4621393 commit dc38a42
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 4 deletions.
1 change: 1 addition & 0 deletions chatterbot/adapters/logic/base_match.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-
from .logic_adapter import LogicAdapter
from .mixins import TieBreaking

Expand Down
11 changes: 9 additions & 2 deletions chatterbot/adapters/logic/closest_match.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-
from .base_match import BaseMatchAdapter
from fuzzywuzzy import process

Expand Down Expand Up @@ -32,8 +33,14 @@ def get(self, input_statement):
text_of_all_statements.append(statement.text)

# Check if an exact match exists
if input_statement.text in text_of_all_statements:
return 1, input_statement

# Decode unicode strings in python 2.x
if sys.version < '3':
if input_statement.text.decode('utf-8') in text_of_all_statements:
return 1, input_statement
else:
if input_statement.text in text_of_all_statements:
return 1, input_statement

# Get the closest matching statement from the database
closest_match, confidence = process.extract(
Expand Down
3 changes: 3 additions & 0 deletions chatterbot/conversation/statement.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# -*- coding: utf-8 -*-


class Statement(object):
"""
A statement represents a single spoken entity, sentence or
Expand Down
1 change: 0 additions & 1 deletion chatterbot/corpus/corpus.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ def load_corpus(self, dotted_path):
"""
Return the data contained within a specified corpus.
"""

corpus_path = self.get_file_path(dotted_path)

corpora = []
Expand Down
13 changes: 12 additions & 1 deletion tests/conversation_tests/test_statements.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-
from unittest import TestCase
from chatterbot.conversation import Statement, Response

Expand All @@ -7,7 +8,7 @@ class StatementTests(TestCase):
def setUp(self):
self.statement = Statement("A test statement.")

def test_equality(self):
def test_list_equality(self):
"""
It should be possible to check if a statement
exists in the list of statements that another
Expand All @@ -17,6 +18,16 @@ def test_equality(self):
self.assertEqual(len(self.statement.in_response_to), 1)
self.assertIn(Response("Yo"), self.statement.in_response_to)

def test_list_equality_unicode(self):
"""
Test that it is possible to check if a statement
is in a list of other statements when the
statements text is unicode.
"""
statements = [Statement("Hello"), Statement("我很好太感谢")]
statement = Statement("我很好太感谢")
self.assertIn(statement, statements)

def test_update_response_list_new(self):
self.statement.add_response(Response("Hello"))
self.assertTrue(len(self.statement.in_response_to), 1)
Expand Down

0 comments on commit dc38a42

Please sign in to comment.