diff --git a/chatterbot/conversation/statement.py b/chatterbot/conversation/statement.py index a8ec0cde4..851611ab2 100644 --- a/chatterbot/conversation/statement.py +++ b/chatterbot/conversation/statement.py @@ -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', []) diff --git a/tests/test_comparisons.py b/tests/test_comparisons.py index 83027b441..1b0133f99 100644 --- a/tests/test_comparisons.py +++ b/tests/test_comparisons.py @@ -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.