当前位置: 首页>>代码示例>>Python>>正文


Python TreeNode.loadJSON方法代码示例

本文整理汇总了Python中TreeNode.TreeNode.loadJSON方法的典型用法代码示例。如果您正苦于以下问题:Python TreeNode.loadJSON方法的具体用法?Python TreeNode.loadJSON怎么用?Python TreeNode.loadJSON使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在TreeNode.TreeNode的用法示例。


在下文中一共展示了TreeNode.loadJSON方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: __init__

# 需要导入模块: from TreeNode import TreeNode [as 别名]
# 或者: from TreeNode.TreeNode import loadJSON [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;
开发者ID:joserc87,项目名称:angry-solver,代码行数:82,代码来源:Dictionary.py


注:本文中的TreeNode.TreeNode.loadJSON方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。