Try to create query engine for parse tree

This commit is contained in:
Aleksey Filippov 2022-01-22 15:13:47 +04:00
parent 9ad8bcc667
commit d1d7e9c9e6
3 changed files with 8 additions and 1 deletions

View File

@ -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__':

View File

@ -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)])

View File

@ -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