Skip to content

Commit 2a7752e

Browse files
committed
Fixes for the RiveScript Test Suite (aichaos/rsts)
1 parent 572e0e4 commit 2a7752e

3 files changed

Lines changed: 19 additions & 7 deletions

File tree

rivescript/brain.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#
66
# https://www.rivescript.com/
77

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

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

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

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

569+
matcher = re.findall(RE.reply_array, reply)
570+
for match in matcher:
571+
name = match
572+
if name in self.master._array:
573+
result = "{random}" + "|".join(self.master._array[name]) + "{/random}"
574+
else:
575+
result = "\x00@" + name + "\x00"
576+
reply = reply.replace("(@"+name+")", result)
577+
reply = re.sub(RE.ph_array, r'(@\1)', reply)
578+
569579
# Tag shortcuts.
570580
reply = reply.replace('<person>', '{person}<star>{/person}')
571581
reply = reply.replace('<@>', '{@<star>}')
@@ -696,7 +706,7 @@ def process_tags(self, user, msg, reply, st=[], bst=[], depth=0, ignore_object_e
696706
elif tag == "mult":
697707
new = orig * value
698708
elif tag == "div":
699-
new = orig / value
709+
new = orig // value
700710
self.master.set_uservar(user, var, new)
701711
except:
702712
insert = "[ERR: Math couldn't '{}' to value '{}']".format(tag, curv)

rivescript/exceptions.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
"""Exception classes for RiveScript."""
1111

1212
# Exportable constants.
13-
RS_ERR_MATCH = "[ERR: No reply matched]"
14-
RS_ERR_REPLY = "[ERR: No reply found]"
13+
RS_ERR_MATCH = "[ERR: No Reply Matched]"
14+
RS_ERR_REPLY = "[ERR: No Reply Found]"
1515
RS_ERR_DEEP_RECURSION = "[ERR: Deep recursion detected]"
1616
RS_ERR_OBJECT = "[ERR: Error when executing Python object]"
1717
RS_ERR_OBJECT_HANDLER = "[ERR: No Object Handler]"

rivescript/regexp.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,15 @@ class RE(object):
1515
equals = re.compile('\s*=\s*')
1616
ws = re.compile('\s+')
1717
objend = re.compile('^\s*<\s*object')
18-
weight = re.compile('\{weight=(\d+)\}')
18+
weight = re.compile(r'\s*\{weight=(\d+)\}\s*')
1919
inherit = re.compile('\{inherits=(\d+)\}')
2020
wilds = re.compile('[\s\*\#\_]+')
2121
nasties = re.compile('[^A-Za-z0-9 ]')
2222
crlf = re.compile('<crlf>')
2323
literal_w = re.compile(r'\\w')
2424
array = re.compile(r'\@(.+?)\b')
25+
reply_array = re.compile(r'\(@([A-Za-z0-9_]+)\)')
26+
ph_array = re.compile(r'\x00@([A-Za-z0-9_]+)\x00')
2527
def_syntax = re.compile(r'^.+(?:\s+.+|)\s*=\s*.+?$')
2628
name_syntax = re.compile(r'[^a-z0-9_\-\s]')
2729
obj_syntax = re.compile(r'[^A-Za-z0-9_\-\s]')

0 commit comments

Comments
 (0)