Skip to content

Commit 43739d6

Browse files
committed
Merge pull request aichaos#62 from Dinh-Hung-Tu/whitespace_removal
fix format message with multiple whitespaces
2 parents 94df7bc + 8dcf162 commit 43739d6

2 files changed

Lines changed: 21 additions & 1 deletion

File tree

rivescript/brain.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,8 @@ def format_message(self, msg, botreply=False):
136136
else:
137137
# For everything else, strip all non-alphanumerics.
138138
msg = utils.strip_nasties(msg)
139-
139+
msg = msg.strip() # Strip leading and trailing white space
140+
msg = RE.ws.sub(" ",msg) # Replace the multiple whitespaces by single whitespace
140141
return msg
141142

142143
def _getreply(self, user, msg, context='normal', step=0, ignore_object_errors=True):

tests/test_format_message.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
from __future__ import unicode_literals, absolute_import
5+
6+
from .config import RiveScriptTestCase
7+
8+
class MessageFormatTests(RiveScriptTestCase):
9+
"""Test format message."""
10+
def test_format_message(self):
11+
self.new("""
12+
+ hello bot
13+
- hello human
14+
""")
15+
self.reply("hello bot", "hello human")
16+
self.reply("Hello Bot", "hello human")
17+
self.reply(" hello bot ", "hello human") # Strip leading and trailing whitespaces
18+
self.reply(" hello bot ", "hello human") # Replace the multiple whitespaces by single whitespace
19+
self.reply("hello bot!!!??? ", "hello human") # Strip nasties

0 commit comments

Comments
 (0)