Improve parse tree
This commit is contained in:
parent
2d0f3d4673
commit
cab7a1c129
@ -1,5 +1,5 @@
|
||||
import warnings
|
||||
from typing import List
|
||||
from typing import List, Dict
|
||||
|
||||
from anytree import Node, RenderTree
|
||||
|
||||
@ -23,10 +23,21 @@ class ParseTree:
|
||||
return nodes
|
||||
|
||||
def __create_tree(self, nodes: List[ParseItem]) -> Node:
|
||||
root: Node
|
||||
for node in nodes:
|
||||
if node.parent_index() == 0:
|
||||
root = Node(node)
|
||||
tree_nodes_count: int = 0
|
||||
parents: Dict[int, Node] = {}
|
||||
root = None
|
||||
while tree_nodes_count < len(nodes):
|
||||
for node in nodes:
|
||||
if parents.get(node.index()) is not None:
|
||||
continue
|
||||
parent: Node = parents.get(node.parent_index())
|
||||
if parent is not None or node.parent_index() == 0:
|
||||
new_node = Node(node, parent)
|
||||
parents[node.index()] = new_node
|
||||
tree_nodes_count = tree_nodes_count + 1
|
||||
if node.parent_index() == 0:
|
||||
root = new_node
|
||||
break
|
||||
return root
|
||||
|
||||
def __repr__(self) -> str:
|
||||
|
@ -9,6 +9,9 @@ class ParseItem:
|
||||
self._parent_index: int = int(parsed_str[6])
|
||||
self._relation: str = parsed_str[7]
|
||||
|
||||
def index(self) -> int:
|
||||
return self._index
|
||||
|
||||
def parent_index(self) -> int:
|
||||
return self._parent_index
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user