Skip to content

Commit 1c4cb29

Browse files
committed
Fix trigger sorting algorithm
1 parent 3ca5a83 commit 1c4cb29

3 files changed

Lines changed: 14 additions & 7 deletions

File tree

Changes

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ Revision history for the Python package RiveScript.
44
- New algorithm for handling variable tags (<get>, <set>, <add>, <sub>,
55
<mult>, <div>, <bot> and <env>) that allows for iterative nesting of
66
these tags (for example, <set copy=<get orig>> will work now).
7+
- Fix sorting algorithm, so triggers with matching word counts will be
8+
sorted by length descending.
9+
- stream() function can accept a multiline string instead of an array
710

811
1.06 Nov 25 2014
912
- Change package name from python-rivescript to simply rivescript.

rivescript/rivescript.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,11 @@ def load_file(self, filename):
164164
def stream(self, code):
165165
"""Stream in RiveScript source code dynamically.
166166
167-
`code` should be an array of lines of RiveScript code."""
167+
`code` can either be a string containing RiveScript code or an array
168+
of lines of RiveScript code."""
168169
self._say("Streaming code.")
170+
if type(code) in [str, unicode]:
171+
code = code.split("\n")
169172
self._parse("stream()", code)
170173

171174
def _parse(self, fname, code):
@@ -1144,8 +1147,10 @@ def _sort_trigger_set(self, triggers):
11441147
for ip in sorted(track.keys()):
11451148
self._say("ip=" + str(ip))
11461149
for kind in ['atomic', 'option', 'alpha', 'number', 'wild']:
1147-
for i in sorted(track[ip][kind], reverse=True):
1148-
running.extend(track[ip][kind][i])
1150+
for wordcnt in sorted(track[ip][kind], reverse=True):
1151+
# Triggers with a matching word count should be sorted
1152+
# by length, descending.
1153+
running.extend(sorted(track[ip][kind][wordcnt], key=len, reverse=True))
11491154
running.extend(sorted(track[ip]['under'], key=len, reverse=True))
11501155
running.extend(sorted(track[ip]['pound'], key=len, reverse=True))
11511156
running.extend(sorted(track[ip]['star'], key=len, reverse=True))

tests/config.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@
77

88
class RiveScriptTestCase(unittest.TestCase):
99
"""Base class for all RiveScript test cases, with helper functions."""
10-
10+
1111
def setUp(self, **kwargs):
1212
self.rs = None # Local RiveScript bot object
1313
self.username = "localuser"
1414

1515

1616
def tearDown(self):
1717
pass
18-
18+
1919

2020
def new(self, code, **kwargs):
2121
"""Make a bot and stream in the code."""
@@ -25,8 +25,7 @@ def new(self, code, **kwargs):
2525

2626
def extend(self, code):
2727
"""Stream code into the bot."""
28-
lines = code.split("\n")
29-
self.rs.stream(lines)
28+
self.rs.stream(code)
3029
self.rs.sort_replies()
3130

3231

0 commit comments

Comments
 (0)