本文整理汇总了Python中pytrie.StringTrie.fromkeys方法的典型用法代码示例。如果您正苦于以下问题:Python StringTrie.fromkeys方法的具体用法?Python StringTrie.fromkeys怎么用?Python StringTrie.fromkeys使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pytrie.StringTrie
的用法示例。
在下文中一共展示了StringTrie.fromkeys方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: open
# 需要导入模块: from pytrie import StringTrie [as 别名]
# 或者: from pytrie.StringTrie import fromkeys [as 别名]
from pytrie import StringTrie as trie
from bitstring import BitArray
words = open('words.txt')
t = trie.fromkeys(words.read().splitlines())
def evaluate(target):
good = BitArray(len(target)) # all characters that are part of a meaningful word
bestLength = 0 # length of the longest word
bestPattern = BitArray(len(target)) # characters that form the longest word
for i in range(len(target)):
match = t.longest_prefix(key=target[i:], default="")
if len(match)>0:
temp = BitArray(len(target))
temp.set(1, range(i, i+len(match))) # set mathcing character positions to 1
good.set(1, range(i, i+len(match))) # set mathcing character positions to 1
if len(match)>bestLength:
bestLength = len(match)
bestPattern = temp
return bestPattern, good
示例2: longest_match_pytrie
# 需要导入模块: from pytrie import StringTrie [as 别名]
# 或者: from pytrie.StringTrie import fromkeys [as 别名]
def longest_match_pytrie(search):
if longest_match_pytrie.trie is None:
from pytrie import StringTrie
longest_match_pytrie.trie = StringTrie.fromkeys(hosts)
matches = longest_match_pytrie.trie.keys(prefix=search)
return max(matches, key=len) if matches else ''