From d1d7e9c9e65a57be80e5cb9a7636dd536d2616b6 Mon Sep 17 00:00:00 2001 From: Aleksey Filippov Date: Sat, 22 Jan 2022 15:13:47 +0400 Subject: [PATCH] Try to create query engine for parse tree --- src/main.py | 1 + src/parse_tree/parse_tree.py | 7 ++++++- src/parse_tree/parse_tree_node.py | 1 + 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/main.py b/src/main.py index 7e430a2..b624020 100644 --- a/src/main.py +++ b/src/main.py @@ -21,6 +21,7 @@ def _main(): syntax_server = 'http://syntaxnet.athene.tech' parse_tree = Syntax().run(text, syntax_server) print(f'Parse tree:\n{parse_tree}') + print(f'Nouns:\n{parse_tree.get_nouns()}') if __name__ == '__main__': diff --git a/src/parse_tree/parse_tree.py b/src/parse_tree/parse_tree.py index 39beb61..ed2cab9 100644 --- a/src/parse_tree/parse_tree.py +++ b/src/parse_tree/parse_tree.py @@ -1,6 +1,6 @@ from typing import List, Dict, Optional -from anytree import RenderTree +from anytree import RenderTree, Resolver from src.parse_tree.parse_tree_node import ParseTreeNode @@ -45,5 +45,10 @@ class ParseTree: break return root + def get_nouns(self) -> str: + r = Resolver('upos') + nodes: List[ParseTreeNode] = r.glob(self._tree, '*/NOUN/ADJ/*') + return ' '.join([node.lemma for node in reversed(nodes)]) + def __repr__(self) -> str: return '\n'.join([f'{pre}{node}' for pre, fill, node in RenderTree(self._tree)]) diff --git a/src/parse_tree/parse_tree_node.py b/src/parse_tree/parse_tree_node.py index d0aa55a..8fcae01 100644 --- a/src/parse_tree/parse_tree_node.py +++ b/src/parse_tree/parse_tree_node.py @@ -4,6 +4,7 @@ from anytree import NodeMixin class ParseTreeNode(NodeMixin): def __init__(self, index: int, lemma: str, upos: str, parent_index: int, relation: str, parent=None, children=None): + super(ParseTreeNode, self).__init__() self.index = index self.lemma = lemma self.upos = upos