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