From 3eba80cd03e0647e76f689f4bd249cf42c1fd704 Mon Sep 17 00:00:00 2001 From: Noah Petherbridge Date: Tue, 9 Aug 2016 11:07:08 -0700 Subject: [PATCH] Fix a regression supporting Unicode in Python 2 --- rivescript/brain.py | 1 + rivescript/interactive.py | 5 ++++- rivescript/parser.py | 1 + rivescript/sorting.py | 1 + rivescript/utils.py | 1 + tests/test_unicode.py | 8 ++++++++ 6 files changed, 16 insertions(+), 1 deletion(-) diff --git a/rivescript/brain.py b/rivescript/brain.py index 7f8b35e..e994958 100644 --- a/rivescript/brain.py +++ b/rivescript/brain.py @@ -5,6 +5,7 @@ # # https://www.rivescript.com/ +from __future__ import unicode_literals from .regexp import RE from .exceptions import ( RiveScriptError, RepliesNotSortedError, NoDefaultRandomTopicError, diff --git a/rivescript/interactive.py b/rivescript/interactive.py index 38437b8..8810a9f 100644 --- a/rivescript/interactive.py +++ b/rivescript/interactive.py @@ -11,7 +11,7 @@ import json import re from six.moves import input -from six import text_type +from six import text_type, PY2 from rivescript import RiveScript @@ -245,6 +245,9 @@ def interactive_mode(): while True: msg = input("You> ") + if PY2: + # For Python 2 only: cast the message to Unicode. + msg = msg.decode("utf-8") # Commands if msg == '/help': diff --git a/rivescript/parser.py b/rivescript/parser.py index 2a3bba4..43d0978 100644 --- a/rivescript/parser.py +++ b/rivescript/parser.py @@ -5,6 +5,7 @@ # # https://www.rivescript.com/ +from __future__ import unicode_literals from .regexp import RE import re diff --git a/rivescript/sorting.py b/rivescript/sorting.py index 388f536..23fa05d 100644 --- a/rivescript/sorting.py +++ b/rivescript/sorting.py @@ -5,6 +5,7 @@ # # https://www.rivescript.com/ +from __future__ import unicode_literals from .regexp import RE from . import utils import re diff --git a/rivescript/utils.py b/rivescript/utils.py index 2c7311a..805e16f 100644 --- a/rivescript/utils.py +++ b/rivescript/utils.py @@ -5,6 +5,7 @@ # # https://www.rivescript.com/ +from __future__ import unicode_literals from .regexp import RE import re import string diff --git a/tests/test_unicode.py b/tests/test_unicode.py index f900c5c..c89c87c 100644 --- a/tests/test_unicode.py +++ b/tests/test_unicode.py @@ -20,6 +20,12 @@ def test_unicode(self): + ブラッキー - エーフィ + + my favorite game is * + - >When did you first start playing ? + + + what is my favorite game + - Wasn't it ? + // Make sure %Previous continues working in UTF-8 mode. + knock knock - Who's there? @@ -51,6 +57,8 @@ def test_unicode(self): self.reply("äh", "What's the matter?") self.reply("ブラッキー", "エーフィ") + self.reply("My favorite game is Pokémon", "When did you first start playing Pokémon?") + self.reply("What is my favorite game?", "Wasn't it Pokémon?") self.reply("knock knock", "Who's there?") self.reply("Orange", "Orange who?") self.reply("banana", "Haha! Banana!")