Skip to content

Commit

Permalink
Fix non-string type values from breaking comparison algorithms
Browse files Browse the repository at this point in the history
  • Loading branch information
gunthercox committed Mar 16, 2017
1 parent 949f478 commit 935a9bb
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
7 changes: 7 additions & 0 deletions chatterbot/conversation/statement.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ class Statement(object):
"""

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

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

self.text = text
self.in_response_to = kwargs.pop('in_response_to', [])

Expand Down
12 changes: 12 additions & 0 deletions tests/test_comparisons.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,18 @@ def test_levenshtein_distance_other_statement_false(self):

self.assertEqual(value, 0)

def test_levenshtein_distance_statement_integer(self):
"""
Test that an exception is not raised if a statement is initialized
with an integer value as its text attribute.
"""
statement = Statement(2)
other_statement = Statement('Hello')

value = comparisons.levenshtein_distance(statement, other_statement)

self.assertEqual(value, 0)

def test_exact_match_different_capitalization(self):
"""
Test that text capitalization is ignored.
Expand Down

0 comments on commit 935a9bb

Please sign in to comment.