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