本文整理汇总了Python中trie.Trie.insert方法的典型用法代码示例。如果您正苦于以下问题:Python Trie.insert方法的具体用法?Python Trie.insert怎么用?Python Trie.insert使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类trie.Trie
的用法示例。
在下文中一共展示了Trie.insert方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: find_compound_words
# 需要导入模块: from trie import Trie [as 别名]
# 或者: from trie.Trie import insert [as 别名]
def find_compound_words(words):
""" trie + BFS + pruning
Advantages of trie:
1. Predictable O(k) lookup time where k is the size of the key.
2. We can easily get all prefixes of a given word.
Drawbacks of tries:
1. Space-consuming, it is a trade-off between time-complexity and space\
complexity. We can use radix-tree to get optimized space, but in \
practice, it doesn't have a reasonable improvement and it takes more\
time than trie.
"""
compound_words = set([])
trie = Trie()
queue = collections.deque()
prefixes_dict = {}
for word in words:
prefixes = trie.has_prefixes(word)
for prefix in prefixes:
queue.append((word, word[len(prefix) :]))
trie.insert(word)
while queue:
word, suffix = queue.popleft()
# pruning
if word in compound_words:
continue
# find a compund word
if suffix in trie:
compound_words.add(word)
else:
prefixes = trie.has_prefixes(suffix)
for prefix in prefixes:
queue.append((word, suffix[len(prefix) :]))
return compound_words
示例2: getCommonPrefix
# 需要导入模块: from trie import Trie [as 别名]
# 或者: from trie.Trie import insert [as 别名]
def getCommonPrefix(fileSet, trieNode, separator='-_'):
debug = False
# debug = ('killswitch engage - temple from the within.mp3' in fileSet)
root = Trie('$$')
for f in fileSet:
root.insert(list(trieNode.children[f].key))
prefixList = []
def dfs(trieNode, curStr=''):
if debug:
print curStr, trieNode.num_successors, int(0.8*len(fileSet))
if (trieNode.num_successors >= int(0.8*len(fileSet)) and trieNode.num_successors > 1) or trieNode.num_successors >= 3:
res = False
for k in trieNode.children:
res = (dfs(trieNode.children[k], curStr+k) or res)
if res:
return True
elif trieNode.key in separator:
prefixList.append((curStr, trieNode.num_successors))
return True
else:
return False
else:
return False
dfs(root)
# if len(prefixList) > 0:
# print prefixList, "->\n", '\n\t'.join(fileSet)
return prefixList
示例3: test_contains_on_shorter
# 需要导入模块: from trie import Trie [as 别名]
# 或者: from trie.Trie import insert [as 别名]
def test_contains_on_shorter():
"""Test contains returns false on non-inserted longer word."""
from trie import Trie
trie = Trie()
token = 'pig'
trie.insert(token)
assert trie.contains('piglet') is False
示例4: find_top_k_with_trie
# 需要导入模块: from trie import Trie [as 别名]
# 或者: from trie.Trie import insert [as 别名]
def find_top_k_with_trie(k = 10):
"""
Too slow and large memory consuming.
time consuming: 147.656000137
(164, 'mh')
(164, 'sq')
(165, 'bi')
(165, 'mo')
(167, 'im')
(168, 'ux')
(169, 'br')
(169, 'gj')
(170, 'ij')
(171, 'qd')
"""
result = []
t = Trie()
# trie
with open(TDATA) as f:
for line in f:
t.insert(line.strip())
# heapq
for n in t.ipreorder(t.root):
if len(result) < k:
heapq.heappush(result, n)
else:
heapq.heappushpop(result, n)
return result
示例5: car_trie
# 需要导入模块: from trie import Trie [as 别名]
# 或者: from trie.Trie import insert [as 别名]
def car_trie():
"""Filled trie."""
from trie import Trie
t = Trie()
for word in WORDS:
t.insert(word)
return t
示例6: test_contains_on_partial
# 需要导入模块: from trie import Trie [as 别名]
# 或者: from trie.Trie import insert [as 别名]
def test_contains_on_partial():
"""Test contains returns false on partial match."""
from trie import Trie
trie = Trie()
token = 'piglet'
trie.insert(token)
assert trie.contains('pig') is False
示例7: test_contains_given_string_one_string_
# 需要导入模块: from trie import Trie [as 别名]
# 或者: from trie.Trie import insert [as 别名]
def test_contains_given_string_one_string_():
"""Test that a given string is in the list."""
from trie import Trie
trie = Trie()
token = 'pig'
trie.insert(token)
assert trie.contains(token)
示例8: test_insert_one_token
# 需要导入模块: from trie import Trie [as 别名]
# 或者: from trie.Trie import insert [as 别名]
def test_insert_one_token():
"""Test when token is inserted into the trie correctly."""
from trie import Trie
trie = Trie()
token = 'pig'
trie.insert(token)
assert trie.container == {'p': {'i': {'g': {'$': '$'}}}}
示例9: test_insert_on_empty_long
# 需要导入模块: from trie import Trie [as 别名]
# 或者: from trie.Trie import insert [as 别名]
def test_insert_on_empty_long(self):
trie = Trie()
data = 'ripper X!'
trie.insert( 'ab', data )
self.assertIsNone(trie.get('a')) # a is not in the dict
self.assertIsNotNone(trie.get('ab')) # ab is
self.assertEqual(trie.get('ab'), data)
示例10: test_contatins
# 需要导入模块: from trie import Trie [as 别名]
# 或者: from trie.Trie import insert [as 别名]
def test_contatins():
"""Test contains responds with true for a word that has been inserted."""
from trie import Trie
t = Trie()
t.insert("cat")
result = t.contains("cat")
assert result is True
示例11: test_contatins_false
# 需要导入模块: from trie import Trie [as 别名]
# 或者: from trie.Trie import insert [as 别名]
def test_contatins_false():
"""Test contains responds with false for a word that is not inserted."""
from trie import Trie
t = Trie()
t.insert("cat")
result = t.contains("dog")
assert result is False
示例12: test_overlapping_words
# 需要导入模块: from trie import Trie [as 别名]
# 或者: from trie.Trie import insert [as 别名]
def test_overlapping_words():
from trie import Trie
new_trie = Trie()
new_trie.insert('words')
new_trie.insert('trie')
new_trie.insert('trip')
assert new_trie.root == {'w': {'o': {'r': {'d': {'s': {'$': '$'}}}}},
't': {'r': {'i': {'e': {'$': '$'}, 'p': {'$': '$'}}}}}
示例13: test_2_trie
# 需要导入模块: from trie import Trie [as 别名]
# 或者: from trie.Trie import insert [as 别名]
def test_2_trie():
"""Test that we can insert two words into the Trie."""
from trie import Trie
new_trie = Trie()
new_trie.insert('words')
new_trie.insert('trie')
assert new_trie.root == {'w': {'o': {'r': {'d': {'s': {'$': '$'}}}}},
't': {'r': {'i': {'e': {'$': '$'}}}}}
示例14: test_basic
# 需要导入模块: from trie import Trie [as 别名]
# 或者: from trie.Trie import insert [as 别名]
def test_basic(self):
trie = Trie()
trie.insert("cat")
self.assertEqual(trie.__len__(), 1)
self.assertTrue(trie.__contains__("cat"))
self.assertEqual([x for x in trie.__iter__()], ["cat"])
self.assertTrue(trie.contains_prefix("ca"))
self.assertEqual([x for x in trie.prefix_iter("ca")], ["cat"])
示例15: test_remove_long
# 需要导入模块: from trie import Trie [as 别名]
# 或者: from trie.Trie import insert [as 别名]
def test_remove_long(self):
trie = Trie()
key = 'abc'
data = 123
trie.insert( key, data )
self.assertIsNotNone(trie.get(key))
self.assertEqual(trie.remove(key), data)
self.assertIsNone(trie.get(key))