本文整理汇总了Python中trie.Trie.addWord方法的典型用法代码示例。如果您正苦于以下问题:Python Trie.addWord方法的具体用法?Python Trie.addWord怎么用?Python Trie.addWord使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类trie.Trie
的用法示例。
在下文中一共展示了Trie.addWord方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: testTrie
# 需要导入模块: from trie import Trie [as 别名]
# 或者: from trie.Trie import addWord [as 别名]
class testTrie(unittest.TestCase):
def setUp(self):
self.trie = Trie()
self.trie.addWord("cat")
def test_check_is_word(self):
self.assertTrue(self.trie.isWord("cat"))
self.assertFalse(self.trie.isWord("ca"))
def test_remove_word(self):
self.trie.removeWord("cat")
self.assertFalse(self.trie.isWord("cat"))
def test_words_with_shared_letters(self):
self.trie.addWord("cab")
self.assertTrue(self.trie.isWord("cat"))
self.assertFalse(self.trie.isWord("ca"))
self.assertTrue(self.trie.isWord("cab"))
def test_node_children(self):
self.trie.addWord("cab")
self.trie.addWord("dog")
self.assertEqual(self.trie._sentinel.children.keys(), ["c","d"])
示例2: __init__
# 需要导入模块: from trie import Trie [as 别名]
# 或者: from trie.Trie import addWord [as 别名]
class Wordplay:
DEFAULT_MAX = 0
DEFAULT_KEY = lambda x:x
END_FRONT = 1
END_REAR = 2
END_BOTH = 3
def __init__(self,
wordlist='wordlists/simple.txt',
multipleWords=False,
minWordSize=1):
self._reverseTrie = Trie()
self._forwardTrie = Trie()
self._minWordSize = minWordSize
self._multipleWords = multipleWords
fp = open(wordlist)
for word in fp:
self._addWord(word)
fp.close()
def has(self, word):
return self._forwardTrie.has(word) and \
self._reverseTrie.has(word[::-1])
def pickRandomAnagram(self, cipher):
"""Picks a random anagram of cipher and returns it or None."""
result = list(self.solveRandomAnagram(cipher,1))
if len(result) == 0: return None
return result[0]
def pickRandomPalindrome(self, cipher):
result = list(self.solveRandomPalindrome(cipher, 1))
if len(result) == 0: return None
return result[0]
def pickFirst(self, cipher):
"""Picks the first anagram it can find."""
result = list(self.solve(cipher,1))
if len(result) == 0: return None
return result[0]
def solveRandomPalindrome(self, cipher, maxSolutions=DEFAULT_MAX):
return self.solvePalindrome(cipher, maxSolutions, lambda x:
random.random())
def solveRandomAnagram(self, cipher, maxSolutions=DEFAULT_MAX):
return self.solveAnagram(cipher, maxSolutions, lambda x: random.random())
def canRecur(self):
return self._multipleWords
def solveAnagram(self, cipher, maxSolutions=DEFAULT_MAX, sortKey=DEFAULT_KEY):
charMap = formatCipher(cipher)
solutions = 0
for solution in self._solveAnagramEntry(charMap, sortKey):
solutions += 1
yield solution
if solutions >= maxSolutions and maxSolutions > 0:
break
def solvePalindrome(self, cipher, maxSolutions=DEFAULT_MAX,
sortKey=DEFAULT_KEY):
if not possiblePalindrome(cipher):
raise StopIteration
charMap = formatCipher(cipher)
solutions = 0
for solution in self._solvePalindromeEntry(charMap,sortKey):
solutions += 1
yield solution
if solutions >= maxSolutions and maxSolutions > 0:
break
def _solvePalindromeEntry(self, charMap, sortKey, froot=None, rroot=None):
if froot == None: froot = self._forwardTrie._root
if rroot == None: rroot = self._reverseTrie._root
keys = set(charMap.keys()) & \
set(froot.keys()) & \
set(rroot.keys())
for key in sorted(keys, key=sortKey):
fnode = froot._get(key)
rnode = rroot._get(key)
for solution in self._solvePalindromeRecursive(charMap, sortKey, fnode,
rnode):
yield solution
def _solvePalindromeRecursive(self, charMap, sortKey, fnode, rnode):
count = min(charMap[fnode._letter], 2)
tmpMap = _deductKey(charMap, fnode._letter, count)
if count == 1 and len(tmpMap) != 0:
#.........这里部分代码省略.........