Skip to content

Commit

Permalink
Merge pull request #107 from Dinh-Hung-Tu/#101
Browse files Browse the repository at this point in the history
Fix #101
  • Loading branch information
kirsle committed Sep 21, 2017
2 parents 303d607 + 9a4a688 commit 8b67bda
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
16 changes: 8 additions & 8 deletions rivescript/brain.py
Expand Up @@ -434,6 +434,14 @@ def reply_regexp(self, user, regexp):
# to match the blank string too.
regexp = re.sub(RE.zero_star, r'<zerowidthstar>', regexp)

# Filter in arrays.
arrays = re.findall(RE.array, regexp)
for array in arrays:
rep = ''
if array in self.master._array:
rep = r'(?:' + '|'.join(self.expand_array(array)) + ')'
regexp = re.sub(r'\@' + re.escape(array) + r'\b', rep, regexp)

# Simple replacements.
regexp = regexp.replace('*', '(.+?)') # Convert * into (.+?)
regexp = regexp.replace('#', '(\d+?)') # Convert # into (\d+?)
Expand Down Expand Up @@ -463,14 +471,6 @@ def reply_regexp(self, user, regexp):
# _ wildcards can't match numbers!
regexp = re.sub(RE.literal_w, r'[^\\s\\d]', regexp)

# Filter in arrays.
arrays = re.findall(RE.array, regexp)
for array in arrays:
rep = ''
if array in self.master._array:
rep = r'(?:' + '|'.join(self.expand_array(array)) + ')'
regexp = re.sub(r'\@' + re.escape(array) + r'\b', rep, regexp)

# Filter in bot variables.
bvars = re.findall(RE.bot_tag, regexp)
for var in bvars:
Expand Down
9 changes: 9 additions & 0 deletions tests/test_triggers.py
Expand Up @@ -111,6 +111,15 @@ def test_trigger_arrays(self):
self.reply("I have a cyan car.", RS_ERR_MATCH)
self.reply("I like apples.", RS_ERR_MATCH)

def test_trigger_arrays_with_underscore(self):
self.new("""
! array colors_bright = white blue
+ what color is my (@colors_bright) *
- Your <star2> is <star1>.
""")
self.reply("What color is my white shirt?", "Your shirt is white.")

def test_nested_arrays(self):
self.new("""
! array primary = red green blue
Expand Down

0 comments on commit 8b67bda

Please sign in to comment.