本文整理汇总了Python中trie.Trie.suggestions方法的典型用法代码示例。如果您正苦于以下问题:Python Trie.suggestions方法的具体用法?Python Trie.suggestions怎么用?Python Trie.suggestions使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类trie.Trie
的用法示例。
在下文中一共展示了Trie.suggestions方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: TrieTestCase
# 需要导入模块: from trie import Trie [as 别名]
# 或者: from trie.Trie import suggestions [as 别名]
class TrieTestCase(unittest.TestCase):
""" Define test cases for Trie class """
def setUp(self):
""" Setup env before each test """
self.trie = Trie()
def tearDown(self):
""" Tear down env after each test """
pass
def test_insert(self):
"""
Test insert of the trie and assert if the
word was added successfully
"""
word = "bar"
self.trie.insert(word)
expected_result = [word]
actual_result = self.trie.suggestions(word)
self.assertEqual(expected_result, actual_result)
def test_suggestion_when_prefix_valid(self):
"""
Test suggestions of the trie and assert if all the word
mathcing the prefix are returned. There should be 2 matching
words for a prefix 'bar'
"""
words = ["bar", "barcelona", "foo", "foobar"]
valid_key = "bar"
for word in words:
self.trie.insert(word)
expected_result = ["bar", "barcelona"]
actual_result = self.trie.suggestions(valid_key)
self.assertEqual(expected_result, actual_result)
def test_suggestion_when_prefix_invalid(self):
"""
Test suggestions of the trie and assert if all the word
mathcing the prefix are returned. There should be no matching
words for a prefix 'foo'
"""
words = ["bar", "barcelona"]
invalid_key = "foo"
for word in words:
self.trie.insert(word)
expected_result = []
actual_result = [invalid_key] \
if invalid_key in self.trie.suggestions(invalid_key) else []
self.assertEqual(expected_result, actual_result)