本文整理汇总了Python中dictionary.Dictionary.is_word方法的典型用法代码示例。如果您正苦于以下问题:Python Dictionary.is_word方法的具体用法?Python Dictionary.is_word怎么用?Python Dictionary.is_word使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类dictionary.Dictionary
的用法示例。
在下文中一共展示了Dictionary.is_word方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_dictionary
# 需要导入模块: from dictionary import Dictionary [as 别名]
# 或者: from dictionary.Dictionary import is_word [as 别名]
def test_dictionary(self):
d = Dictionary("unit_tests/mocks/mockDictionary.txt")
self.assertTrue(d.is_word("fabricate"))
self.assertFalse(d.is_word("illusion"))
示例2: __init__
# 需要导入模块: from dictionary import Dictionary [as 别名]
# 或者: from dictionary.Dictionary import is_word [as 别名]
class AnagramSolver:
def __init__(self, dic_file = 'dictionaries/english'):
self.outwords = {}
self.permutations = {}
self.dictionary = Dictionary(dic_file)
def swap(self, word, i, j):
wlist = list(word)
tmp = wlist[i]
wlist[i] = wlist[j]
wlist[j] = tmp
return ''.join(wlist)
def swapChar(self, word, index, char):
wlist = list(word)
wlist[index] = char
return ''.join(wlist)
def get_combos(self, letters):
check_words(letters, 0)
check_length(letters)
def get_permutations(self, word):
self.permutations = {}
for item in self.wild(word.replace('*',''), word.count('*')):
self.get_permutations_rec(item, 0)
return self.permutations.keys()
def get_permutations_rec(self, word, i):
while i<len(word):
# try it with letter[i] switched with all next letters
for j in range(i, len(word)):
test = self.swap(word, i, j)
self.permutations[test] = 1
if i < len(word) - 1:
self.get_permutations_rec(test, i+1)
i += 1
def wild(self, str, num_wild):
if num_wild > 0:
return [y + x for x in string.lowercase for y in self.wild(str, num_wild-1)]
else:
return [str]
def anagram(self, mask, chars):
# mask is of the form: e__ph_nt and characters can be a-z or *
# where lower case letters are literal, underscores can
# be filled in with chars from chars, and asterisks
# can be filled with any character a-z
# clear the output hash
self.outwords = {}
# get the list of character permutations
charPerms = self.get_permutations(chars)
# fill the blanks with the permutations
for x in range(len(charPerms)):
permMask = mask
lastBlank = -1
for i in range(len(charPerms[x])):
lastBlank = permMask.find("_", lastBlank+1)
if lastBlank == -1:
break
permMask = self.swapChar(permMask, lastBlank, charPerms[x][i])
# check if permMask is a dictionary word
if self.dictionary.is_word(permMask):
self.outwords[permMask] = 1
return self.outwords.keys()