本文整理汇总了Python中TreeNode.TreeNode.checkString方法的典型用法代码示例。如果您正苦于以下问题:Python TreeNode.checkString方法的具体用法?Python TreeNode.checkString怎么用?Python TreeNode.checkString使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类TreeNode.TreeNode
的用法示例。
在下文中一共展示了TreeNode.checkString方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from TreeNode import TreeNode [as 别名]
# 或者: from TreeNode.TreeNode import checkString [as 别名]
class Dictionary:
"""
A word dictionary saved as a Tree
"""
def __init__(self):
"""
Default constructor
"""
self.root = TreeNode()
def loadFromPath(self, path):
"""
Load a dictionary from a file
Args:
path (str): the path to the file to read
Rises:
IOException: If the file can't be opened
"""
self.root = TreeNode(' ');
node = TreeNode()
f = codecs.open(path, 'r', 'utf-8')
for line in f:
line = removeAccentsInString(line)
node.addString(line.rstrip())
f.close()
self.root.addChild(node)
def loadJSONFromPath(self, path):
"""
Load a dictionary from a JSON file
Args:
path (str): the path to the file to read
Rises:
IOException: If the file can't be opened
"""
self.root = TreeNode(' ');
content = codecs.open(path, 'r', 'utf-8').read()
data = json.loads(content)
self.root = TreeNode()
self.root.loadJSON(data)
def checkWord(self, word):
"""
Check a word in the dictionary
Args:
word (str): The word to check
Returns:
True if the word exists in the dictionary. False otherwise
"""
return self.root.checkString(u' ' + word);
def printTree(self):
"""
Prints the tree to the standard output
"""
print 'Tree:'
print self.root.toString(0)
print '\n'
def toJSON(self):
"""
Saves the whole dictionary as a JSON object
"""
return json.dumps(self.root, default=lambda o: o.__dict__)
def findWordsInPattern (self, pattern, letters):
"""
Find all the words from the dictionary that matches the pattern
Args:
pattern (str): The pattern where the word should fit in. It must be a mix of [a-z] + ' '
letters (str): The available letters to form the word ([a-z] + *, where the * represents the wildcard)
Returns:
A list of strings with all the words found
"""
words = []
letters = ' ' + letters
words = self.root.findWordsInPattern(pattern, letters, u'')
return words;