本文整理汇总了Python中treelib.Tree.size方法的典型用法代码示例。如果您正苦于以下问题:Python Tree.size方法的具体用法?Python Tree.size怎么用?Python Tree.size使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类treelib.Tree
的用法示例。
在下文中一共展示了Tree.size方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: load
# 需要导入模块: from treelib import Tree [as 别名]
# 或者: from treelib.Tree import size [as 别名]
class RST_DT:
def load(self, path2file):
self.id_EDUs = []
self.EDU = {}
self.treeNS = Tree()
self.tree = Tree()
# nombre max d'espace pour init id_parents
with open(path2file, "r") as f:
max_space = 0
nb_line = 0
for i, line in enumerate(f):
nb_space = 0
for c in line:
if c == " ":
nb_space += 1
else:
break
if nb_space > max_space:
max_space = nb_space
nb_line += 1
with open(path2file, "r") as f:
id_parents = [0] * max_space
NS_parents = [0] * max_space
for i, line in enumerate(f):
# nombre d'espace détermine le parent
nb_space = 0
for c in line:
if c == " ":
nb_space += 1
else:
break
space = nb_space / 2
id_parents[space] = i
parent = id_parents[space - 1]
reg = "\(([\w\-\[\]]+)|(_!.+!_)" # récupération du contenu
match = re.findall(reg, line)[0]
if match[0] == "":
content = match[1] # feuille EDU
self.id_EDUs.append(i)
# print content
self.EDU[i] = re.findall("_!(.*)!_", content)
else:
content = match[0]
reg2 = "\[(N|S)\]" # récupération NS
match2 = re.findall(reg2, content)
NS_parents[space] = match2 # ['N','S']
# création du noeud
if i == 0:
self.tree.create_node(content, 0)
self.treeNS.create_node("Root", 0)
else:
id_NS = len(self.tree.is_branch(parent)) # 0 ou 1 car arbre binaire
self.tree.create_node(content, i, parent=parent)
self.treeNS.create_node(NS_parents[space - 1][id_NS], i, parent=parent)
def toDEP(self):
###############################
# Etape 1 : construction du head_tree
# parcours en largeur de tree afin de récupérer chaque id_node
# pour chaque profondeur (init à 0) _! sans compter !_ les feuilles (EDUs)
nodes_depth = [-1] * self.tree.size()
for i in xrange(self.tree.size()):
id_nodes = [0]
depth = [999] * self.tree.size()
while id_nodes: # False if empty
id_node = id_nodes.pop(0)
node = self.tree.get_node(id_node)
if node.bpointer != None:
node_parent = self.tree.get_node(node.bpointer)
depth[node.identifier] = depth[node_parent.identifier] + 1
else:
depth[node.identifier] = 0
if id_node == i:
# print 'noeud ',i,' en profondeur', depth[node.identifier]
if node.fpointer:
nodes_depth[i] = depth[i]
break
if node.fpointer:
id_nodes.append(node.fpointer[0])
id_nodes.append(node.fpointer[1])
# print nodes_depth
id_nodes_depth = []
for d in xrange(self.tree.depth()):
id_nodes_depth.append([])
for i in xrange(self.tree.size()):
if nodes_depth[i] == d:
id_nodes_depth[d].append(i)
# print id_nodes_depth
#
# construction du head_tree
head_tree = [-1] * self.treeNS.size()
# pour chaque noeud (non EDU/feuille) en partant de la plus grande profondeur dans l'arbre
for d in range(len(id_nodes_depth) - 1, -1, -1):
for id_node in id_nodes_depth[d]:
#.........这里部分代码省略.........