本文整理汇总了Python中trie.Trie.predictive_search方法的典型用法代码示例。如果您正苦于以下问题:Python Trie.predictive_search方法的具体用法?Python Trie.predictive_search怎么用?Python Trie.predictive_search使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类trie.Trie
的用法示例。
在下文中一共展示了Trie.predictive_search方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from trie import Trie [as 别名]
# 或者: from trie.Trie import predictive_search [as 别名]
class Predictor:
#load data and init trie
def __init__(self, dictionary, connection):
#initalize trie
self.trie = Trie()
for line in open(dictionary):
(yomi, lid, rid, cost, word) = line.strip().split("\t", 4)
lid, rid, cost = int(lid), int(rid), int(cost)
yomi, word = unicode(yomi, 'utf-8'), unicode(word, 'utf-8')
self.trie.insert(yomi, (word, lid, rid, cost))
#initialize connection
file = open(connection)
lsize, rsize = file.readline().strip().split(" ", 1)
lsize, rsize = int(lsize), int(rsize)
self.connection = [None] * rsize
for line in file:
(lid, rid, cost) = line.strip().split(" ", 2)
lid, rid, cost = int(lid), int(rid), int(cost)
if lid != 0:
break
self.connection[rid] = cost
file.close()
#search and ranking candidate
def predict(self, input):
results = self.trie.predictive_search(input)
entries = []
for yomi, values in results:
for word, lid, rid, cost in values:
total = cost + self.connection[lid]
rank = total - int(1000 * log(1 + len(yomi) - len(input)))
entry = Entry(yomi, word, lid, rid, cost, rank)
entries.append(entry)
entries.sort(key=lambda x:x.rank)
return entries