本文整理汇总了Python中trie.Trie.add方法的典型用法代码示例。如果您正苦于以下问题:Python Trie.add方法的具体用法?Python Trie.add怎么用?Python Trie.add使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类trie.Trie
的用法示例。
在下文中一共展示了Trie.add方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: trie_dictionary
# 需要导入模块: from trie import Trie [as 别名]
# 或者: from trie.Trie import add [as 别名]
def trie_dictionary(dictionary):
trie = Trie()
for key in dictionary.keys():
#print key
trie.add(key, dictionary[key])
return trie
示例2: gen_suffix_trie
# 需要导入模块: from trie import Trie [as 别名]
# 或者: from trie.Trie import add [as 别名]
def gen_suffix_trie (fname):
from trie import Trie, DATrie
trie = Trie ()
pytrie = DATrie ()
for s in valid_syllables:
trie.add (s[::-1], valid_syllables[s])
pytrie.construct_from_trie (trie)
pytrie.output_static_c_arrays (fname)
示例3: mkEncTrie
# 需要导入模块: from trie import Trie [as 别名]
# 或者: from trie.Trie import add [as 别名]
def mkEncTrie(dct):
"""
Makes an "encoding" trie from a description dict.
Only call from mkTries to ensure that the reverse trie gets built as well
"""
rv = Trie()
for key, value in dct.iteritems():
try:
if value[1] is not NoReverse:
raise Exception
except:
rv.add(key, value)
else:
rv.add(key, value[0])
return rv
示例4: __init__
# 需要导入模块: from trie import Trie [as 别名]
# 或者: from trie.Trie import add [as 别名]
class ControlIt:
def __init__(self):
self.the_trie = Trie()
def file_grab(self):
log = open("content/words1.txt", 'r')
loglist = log.readlines()
log.close()
cl = []
sendc = []
pattern = '^\w+-\s'
for index, line in enumerate(loglist, 0):
if re.search(pattern, line) is not None:
test = re.search(pattern, line)
firstFound = test.string.split(' ', 1)[0]
secondFound = loglist[index + 1]
if not secondFound.split(' ', 1)[0] == "ship":
sendc.append(secondFound.split(' ', 1)[0].lower())
cl.append(firstFound.__add__(secondFound.split(' ', 1)[0]).lower())
try:
with open("content/words1.txt", encoding='UTF-8') as f:
lines = f.read().translate({ord(i): None for i in ';:*.,[]|"�<>()!°&¶/'}).lower().split()
f.close()
except:
with open("content/words1.txt", encoding='latin_1') as f:
lines = f.read().translate({ord(i): None for i in ';:*.,[]|"�<>()!°&¶/'}).lower().split()
f.close()
for index, i in enumerate(lines):
if i.count('-') >= 3 or i == re.search('\w+-\n', i):
lines[index] = None
[lines.append(i) for i in cl]
for i in sendc:
lines = list((x for x in lines if re.sub(i, "", str(x))))
return lines
def grab_file(self):
start_list = self.file_grab()
for i in start_list:
self.the_trie.add(self.the_trie.trie, str(i))
mytrie = self.the_trie.get_trie()
self.the_trie.trie_pickle(mytrie)
# self.ct = self.the_trie
print(mytrie)
def main(self):
self.grab_file()
self.the_trie.create_binary_tree()
self.the_trie.create_avl_tree()
示例5: mkDecTrie
# 需要导入模块: from trie import Trie [as 别名]
# 或者: from trie.Trie import add [as 别名]
def mkDecTrie(dct):
"""
Makes a "decoding", or "reverse" trie from a description dict
Only call from mkTries to ensure that the encoding trie gets built as well
"""
rv = Trie()
for key, value in dct.iteritems():
try:
if value[1] is not NoReverse:
raise Exception
except:
try:
if not callable(value[1].find_prefix):
raise Exception
except:
rv.add(value, key)
else:
new_key = value[0]
new_val = (key, revTrie(value[1])) + value[2:]
rv.add(new_key, new_val)
return rv
示例6: create_dictionary
# 需要导入模块: from trie import Trie [as 别名]
# 或者: from trie.Trie import add [as 别名]
def create_dictionary():
with open(DICTIONARY_FILE) as f:
global dictionary
dictionary = Trie()
for word in f:
dictionary.add(word.strip())
示例7: TestWords
# 需要导入模块: from trie import Trie [as 别名]
# 或者: from trie.Trie import add [as 别名]
class TestWords(unittest.TestCase):
"""Tests for function words"""
def setUp(self):
unittest.TestCase.setUp(self)
self.mytrie = Trie()
self.mytrie.add('ant',1)
self.mytrie.add('ante',2)
self.mytrie.add('antic',3)
self.mytrie.add('antsy',4)
self.mytrie.add('antse',5)
self.mytrie.add('ban',6)
self.mytrie.add('banana',7)
def test_default_case(self):
"""Test words retrieves all words properly from Trie."""
expected = ['ante','antic','ant','antsy','antse','banana','ban']
actual = []
for words in self.mytrie.words():
actual.append(words)
#print 'actual',actual
#print 'expected',expected
self.assertTrue(sorted(actual)==sorted(expected))