Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion rivescript/brain.py
Original file line number Diff line number Diff line change
Expand Up @@ -441,13 +441,25 @@ def reply_regexp(self, user, regexp):
if array in self.master._array:
rep = r'(?:' + '|'.join(self.expand_array(array)) + ')'
regexp = re.sub(r'\@' + re.escape(array) + r'\b', rep, regexp)
if '[' in regexp and ']' in regexp:
regexp = regexp.replace(' [', '(\s|)(').replace('[', '(')
regexp = regexp.replace('] ', '|)(\s|)').replace(']', '|)')

regexp = regexp.replace(' * ', '( * | )')
if regexp[-2:] == ' *':
regexp = regexp.replace(regexp[-2:], '( *|)')
if regexp[:2] == '* ':
regexp = regexp.replace(regexp[:2], '(|* )')

regexp = self.asterisk_processing(regexp)

# Simple replacements.
regexp = regexp.replace('*', '(.+?)') # Convert * into (.+?)
regexp = regexp.replace('*', '(.*?)') # Convert * into (.*?)
regexp = regexp.replace('#', '(\d+?)') # Convert # into (\d+?)
regexp = regexp.replace('_', '(\w+?)') # Convert _ into (\w+?)
regexp = re.sub(RE.weight, '', regexp) # Remove {weight} tags, allow spaces before the bracket
regexp = regexp.replace('<zerowidthstar>', r'(.*?)')
regexp = regexp.replace('(.*?) (.*?)', '(.*?)(.*?)')

# Optionals.
optionals = re.findall(RE.optionals, regexp)
Expand Down Expand Up @@ -562,6 +574,7 @@ def process_tags(self, user, msg, reply, st=[], bst=[], depth=0, ignore_object_e
"""
stars = ['']
stars.extend(st)
stars = [star.strip() if star else star for star in stars]
botstars = ['']
botstars.extend(bst)
if len(stars) == 1:
Expand Down Expand Up @@ -851,3 +864,12 @@ def default_history(self):
"input": ["undefined"] * 9,
"reply": ["undefined"] * 9,
}

def asterisk_processing(self, regexp):
for i in range(len(regexp)):
if regexp[i] == '*':
if (((regexp[i] == regexp[-1]) and regexp[-2].isalpha()) or
(regexp[i] == regexp[0] and regexp[1].isalpha()) or
(regexp[i+1].isalpha() or regexp[i-1].isalpha())):
regexp = regexp[:i] + '(\w{0,})' + regexp[i+1:]
return regexp
28 changes: 28 additions & 0 deletions tests/test_replies.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ def test_previous(self):
! sub who's = who is
! sub it's = it is
! sub didn't = did not
! array hi = hi|[hello] hi|welcome

+ (@hi)
- Hi Hi!

+ knock knock
- Who's there?
Expand All @@ -28,6 +32,8 @@ def test_previous(self):
+ *
- I don't know.
""")
self.reply("welcome", "Hi Hi!")
self.reply("Hello, Hi", "Hi Hi!")
self.reply("knock knock", "Who's there?")
self.reply("Canoe", "Canoe who?")
self.reply("Canoe help me with my homework?", "Haha! Canoe help me with my homework!")
Expand Down Expand Up @@ -111,9 +117,31 @@ def test_embedded_tags(self):

+ html test
- <set name=<b>Name</b>>This has some non-RS <em>tags</em> in it.

+ i am from rus*
- Are you from Russia?

+ i am from *lia
* <star> == brasi => Are you from Brasilia?
- Are you from big city?

+ hello my * *friend
- I love you, my <star>!

+ *
- Random reply
""")
self.reply("What is my name?", "Your name is undefined, right?")
self.reply("My name is Alice", "OK.")
self.reply("My name is Bob.", "I thought your name was Alice?")
self.reply("What is my name?", "Your name is Bob, right?")
self.reply("HTML Test", "This has some non-RS <em>tags</em> in it.")
self.reply("I am from Russia", "Are you from Russia?")
self.reply("I am from RussiaFederation", "Are you from Russia?")
self.reply("I am from Russia Federation", "Random reply")
self.reply("Hello my best friend", "I love you, my best!")
self.reply("Hello my best best friend", "I love you, my best best!")
self.reply("Hello my best little bestfriend", "I love you, my best little!")
self.reply("Hello my friend", "I love you, my !")
self.reply("I am from Brasilia", "Are you from Brasilia?")
self.reply("I am from Sicilia", "Are you from big city?")