diff --git a/src/parse_tree/parse_tree.py b/src/parse_tree/parse_tree.py new file mode 100644 index 0000000..c7c9d1f --- /dev/null +++ b/src/parse_tree/parse_tree.py @@ -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)]) diff --git a/src/parse_tree/parse_tree_item.py b/src/parse_tree/parse_tree_item.py new file mode 100644 index 0000000..3c0dcd7 --- /dev/null +++ b/src/parse_tree/parse_tree_item.py @@ -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}' + + + diff --git a/src/syntax.py b/src/syntax.py index 5ed073a..51bda3f 100644 --- a/src/syntax.py +++ b/src/syntax.py @@ -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))