Skip to content

Commit

Permalink
Merge pull request aichaos#89 from aichaos/rsts
Browse files Browse the repository at this point in the history
Fixes for the RiveScript Test Suite (aichaos/rsts)
  • Loading branch information
kirsle authored Apr 30, 2017
2 parents 572e0e4 + e7401ef commit 0a6a0b9
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 9 deletions.
18 changes: 14 additions & 4 deletions rivescript/brain.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#
# https://www.rivescript.com/

from __future__ import unicode_literals
from __future__ import unicode_literals, division
from .regexp import RE
from .exceptions import (
RiveScriptError, RepliesNotSortedError, NoDefaultRandomTopicError,
Expand Down Expand Up @@ -438,7 +438,7 @@ def reply_regexp(self, user, regexp):
regexp = regexp.replace('*', '(.+?)') # Convert * into (.+?)
regexp = regexp.replace('#', '(\d+?)') # Convert # into (\d+?)
regexp = regexp.replace('_', '(\w+?)') # Convert _ into (\w+?)
regexp = re.sub(r'\s*\{weight=\d+\}', '', regexp) # Remove {weight} tags, allow spaces before the bracket
regexp = re.sub(RE.weight, '', regexp) # Remove {weight} tags, allow spaces before the bracket
regexp = regexp.replace('<zerowidthstar>', r'(.*?)')

# Optionals.
Expand All @@ -461,7 +461,7 @@ def reply_regexp(self, user, regexp):
'(?:' + pipes + r'|(?:\\s|\\b))', regexp)

# _ wildcards can't match numbers!
regexp = re.sub(RE.literal_w, r'[A-Za-z]', regexp)
regexp = re.sub(RE.literal_w, r'[^\\s\\d]', regexp)

# Filter in arrays.
arrays = re.findall(RE.array, regexp)
Expand Down Expand Up @@ -566,6 +566,16 @@ def process_tags(self, user, msg, reply, st=[], bst=[], depth=0, ignore_object_e
if len(botstars) == 1:
botstars.append("undefined")

matcher = re.findall(RE.reply_array, reply)
for match in matcher:
name = match
if name in self.master._array:
result = "{random}" + "|".join(self.master._array[name]) + "{/random}"
else:
result = "\x00@" + name + "\x00"
reply = reply.replace("(@"+name+")", result)
reply = re.sub(RE.ph_array, r'(@\1)', reply)

# Tag shortcuts.
reply = reply.replace('<person>', '{person}<star>{/person}')
reply = reply.replace('<@>', '{@<star>}')
Expand Down Expand Up @@ -696,7 +706,7 @@ def process_tags(self, user, msg, reply, st=[], bst=[], depth=0, ignore_object_e
elif tag == "mult":
new = orig * value
elif tag == "div":
new = orig / value
new = orig // value
self.master.set_uservar(user, var, new)
except:
insert = "[ERR: Math couldn't '{}' to value '{}']".format(tag, curv)
Expand Down
4 changes: 2 additions & 2 deletions rivescript/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
"""Exception classes for RiveScript."""

# Exportable constants.
RS_ERR_MATCH = "[ERR: No reply matched]"
RS_ERR_REPLY = "[ERR: No reply found]"
RS_ERR_MATCH = "[ERR: No Reply Matched]"
RS_ERR_REPLY = "[ERR: No Reply Found]"
RS_ERR_DEEP_RECURSION = "[ERR: Deep recursion detected]"
RS_ERR_OBJECT = "[ERR: Error when executing Python object]"
RS_ERR_OBJECT_HANDLER = "[ERR: No Object Handler]"
Expand Down
4 changes: 3 additions & 1 deletion rivescript/regexp.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@ class RE(object):
equals = re.compile('\s*=\s*')
ws = re.compile('\s+')
objend = re.compile('^\s*<\s*object')
weight = re.compile('\{weight=(\d+)\}')
weight = re.compile(r'\s*\{weight=(\d+)\}\s*')
inherit = re.compile('\{inherits=(\d+)\}')
wilds = re.compile('[\s\*\#\_]+')
nasties = re.compile('[^A-Za-z0-9 ]')
crlf = re.compile('<crlf>')
literal_w = re.compile(r'\\w')
array = re.compile(r'\@(.+?)\b')
reply_array = re.compile(r'\(@([A-Za-z0-9_]+)\)')
ph_array = re.compile(r'\x00@([A-Za-z0-9_]+)\x00')
def_syntax = re.compile(r'^.+(?:\s+.+|)\s*=\s*.+?$')
name_syntax = re.compile(r'[^a-z0-9_\-\s]')
obj_syntax = re.compile(r'[^A-Za-z0-9_\-\s]')
Expand Down
4 changes: 2 additions & 2 deletions tests/test_sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,9 @@ def test_lastmatch(self):
self.uservar('__lastmatch__', None) # Before any user input and reply, no match.
self.reply("helo", "hello") # For matched case
self.uservar('__lastmatch__','helo')
self.reply("helo you","[ERR: No reply matched]") # For not-matched case
self.reply("helo you","[ERR: No Reply Matched]") # For not-matched case
self.uservar('__lastmatch__', None)
self.reply("helo again","[ERR: No reply matched]") # For not-matched case again
self.reply("helo again","[ERR: No Reply Matched]") # For not-matched case again
self.uservar('__lastmatch__', None)
self.reply("helo", "hello") # For matched case again
self.uservar('__lastmatch__', 'helo')

0 comments on commit 0a6a0b9

Please sign in to comment.