Skip to content

Commit

Permalink
Issues/53 (#148)
Browse files Browse the repository at this point in the history
* Test to reproduce the issue

* Fix and additional test cases
  • Loading branch information
clusterfudge authored Sep 16, 2021
1 parent bc522eb commit a9e0df0
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
4 changes: 3 additions & 1 deletion adapt/tools/text/trie.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,9 @@ def insert(self, iterable, index=0, data=None, weight=1.0):
self.children[iterable[index]].insert(iterable, index + 1, data)

def is_prefix(self, iterable, index=0):
if iterable[index] in self.children:
if index == len(iterable):
return True
elif iterable[index] in self.children:
return self.children[iterable[index]].is_prefix(iterable, index + 1)
else:
return False
Expand Down
11 changes: 11 additions & 0 deletions test/TrieTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,5 +179,16 @@ def match_func(data):
assert "Gonzo" in muppet_names
assert "Rowlf" in muppet_names

def test_is_prefix(self):
trie = Trie()
trie.insert("play", "PlayVerb")
trie.insert("the big bang theory", "Television Show")
trie.insert("the big", "Not a Thing")
trie.insert("barenaked ladies", "Radio Station")

assert trie.root.is_prefix("the")
assert trie.root.is_prefix("play")
assert not trie.root.is_prefix("Kermit")

def tearDown(self):
pass

0 comments on commit a9e0df0

Please sign in to comment.