Skip to content

Commit

Permalink
Merge pull request #93 from Dinh-Hung-Tu/#92
Browse files Browse the repository at this point in the history
#92 Special handle for the case "[*]"
  • Loading branch information
kirsle authored May 19, 2017
2 parents 6b5ef3f + 0609226 commit 2d72a33
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 10 deletions.
7 changes: 7 additions & 0 deletions rivescript/sorting.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@ def __init__(self, pattern, index, weight, inherit = sys.maxsize):
# Without any words number of stars does not matter, they all mean match any.
self.star = sys.maxsize # Make sure "*" is last in the list, "* love *" > "*"

# Special handle for the case "[*]", since self.len is not re-set, self.len = -2 < 0. Thus, "[*]" > "*"
elif (self.option == 1) & (self.wordcount == -2):
self.wordcount = sys.maxsize
self.star = sys.maxsize
self.pound = sys.maxsize
self.under = sys.maxsize
self.option = sys.maxsize

def sort_trigger_set(triggers, exclude_previous=True, say=None):
"""Sort a group of triggers in optimal sorting order.
Expand Down
35 changes: 25 additions & 10 deletions tests/test_sorting.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,28 +46,43 @@ def test_sorting_triggers(self):
+ hi [*]
- 12
+ [*] hi [*]
- 13
+ [*] hi *
- 14
+ [*]
- 15
""")

sorted_triggers = {trig[0]:position for position, trig in enumerate(self.rs._brain.master._sorted["topics"]['random'])}

# 1) Atomic is first matched.
self.assertLess(sorted_triggers['hello'],sorted_triggers['hey [man]'])
self.assertLess(sorted_triggers['hello'], sorted_triggers['hey [man]'])

# 2) Sorted by number of words
self.assertLess(sorted_triggers['hel lo'],sorted_triggers['hello'])
self.assertLess(sorted_triggers['* hallo ween *'],sorted_triggers['* are *'])
self.assertLess(sorted_triggers['hel lo'], sorted_triggers['hello'])
self.assertLess(sorted_triggers['* hallo ween *'], sorted_triggers['* are *'])

# 3) Sorted by length by characters
self.assertLess(sorted_triggers['good morning'],sorted_triggers['hel lo'])
self.assertLess(sorted_triggers['good morning'], sorted_triggers['hel lo'])

# 4) Sorted by alphabetical order
self.assertLess(sorted_triggers['* are *'],sorted_triggers['* you *'])
self.assertLess(sorted_triggers['* are *'], sorted_triggers['* you *'])

# 5) Sorted by number of wildcard triggers
self.assertLess(sorted_triggers['hi *'],sorted_triggers['* you *'])
self.assertLess(sorted_triggers['hi *'], sorted_triggers['* you *'])

# 6) The `super catch all` (only single star `*`) should be least priority
self.assertEqual(sorted_triggers['*'],max(sorted_triggers.values()))
self.assertLess(sorted_triggers['hi [*]'],sorted_triggers['*']) # another check but will be covered by max check above


self.assertEqual(sorted_triggers['*'], max(sorted_triggers.values()))
self.assertLess(sorted_triggers['hi [*]'], sorted_triggers['*']) # another check but will be covered by max check above

# 7) The cousin of `super catch all` (only single star with option `[*]`) should be second-last, following the
# `soft` sorting convention that trigger with more non-star characters goes first (more specific matches first)
self.assertLess(sorted_triggers['[*]'], sorted_triggers['*'])
self.assertEqual(sorted_triggers['[*]'], max(sorted_triggers.values())-1)
self.assertLess(sorted_triggers['[*] hi [*]'], sorted_triggers['[*]'])
self.assertLess(sorted_triggers['[*] hi *'], sorted_triggers['*'])
self.assertLess(sorted_triggers['hi [*]'], sorted_triggers['[*]'])

0 comments on commit 2d72a33

Please sign in to comment.