Initial parse tree realisation
This commit is contained in:
parent
1d1a778ca8
commit
2d0f3d4673
33
src/parse_tree/parse_tree.py
Normal file
33
src/parse_tree/parse_tree.py
Normal 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)])
|
19
src/parse_tree/parse_tree_item.py
Normal file
19
src/parse_tree/parse_tree_item.py
Normal 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}'
|
||||
|
||||
|
||||
|
@ -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))
|
||||
|
Loading…
Reference in New Issue
Block a user