Exemplo n.º 1
0
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
Exemplo n.º 2
0
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 ''