Initial parse tree realisation

This commit is contained in:
Aleksey Filippov 2022-01-20 16:05:26 +04:00
parent 1d1a778ca8
commit 2d0f3d4673
3 changed files with 56 additions and 2 deletions

View File

@ -0,0 +1,33 @@
import warnings
from typing import List
from anytree import Node, RenderTree
from src.parse_tree.parse_tree_item import ParseItem
class ParseTree:
def __init__(self, raw_tree: str):
self._tree: Node = self.__create_tree(self.__create_nodes_array(raw_tree))
@staticmethod
def __create_nodes_array(raw_tree: str) -> List[ParseItem]:
nodes: List[ParseItem] = []
parsed_syntax_lines = raw_tree.split('\n')
for line in parsed_syntax_lines:
try:
nodes.append(ParseItem(line))
except AssertionError:
warnings.warn('Empty line')
return nodes
def __create_tree(self, nodes: List[ParseItem]) -> Node:
root: Node
for node in nodes:
if node.parent_index() == 0:
root = Node(node)
return root
def __repr__(self) -> str:
return '\n'.join([f'{pre}{node.name}' for pre, fill, node in RenderTree(self._tree)])

View File

@ -0,0 +1,19 @@
class ParseItem:
def __init__(self, syntax_result_line: str):
parsed_str = syntax_result_line.split('\t')
if len(parsed_str) != 10:
raise AssertionError(f'{syntax_result_line} is not CoNNL-U-2 line')
self._index: int = int(parsed_str[0])
self._lemma: str = parsed_str[1]
self._upos: str = parsed_str[3]
self._parent_index: int = int(parsed_str[6])
self._relation: str = parsed_str[7]
def parent_index(self) -> int:
return self._parent_index
def __repr__(self) -> str:
return f'{self._index} {self._lemma} {self._upos} {self._parent_index} {self._relation}'

View File

@ -1,5 +1,7 @@
import requests
from src.parse_tree.parse_tree import ParseTree
class Syntax:
@ -18,5 +20,5 @@ class Syntax:
return result
def run(self, text: str, server: str) -> str:
return self.__parsey(text, server)
def run(self, text: str, server: str) -> ParseTree:
return ParseTree(self.__parsey(text, server))