def getSecondLvNPsOfParseTree(parse_tree, nps, display_tree=False): if display_tree: Tree.pretty_print(parse_tree) for subtree in parse_tree: if isinstance(subtree, Tree) and subtree.label() == 'NP' and subtree.height() == 3: np = subtree start_flag = "B-NP" print('\nNP: ' + ' '.join(Tree.leaves(np))) # obtained = False # may or may not be a terminal for np_derivation in Tree.subtrees(np): getSecondLvNPsOfParseTree(np_derivation, nps, False) if np_derivation.label() in penni_tags: # if not obtained: # print('\nNP: ' + ' '.join(Tree.leaves(np))) # nps.append(Tree.leaves(np)) # obtained = True print(np_derivation.leaves()[0]+'\t'+np_derivation.label()+'\t'+start_flag) start_flag = "I-NP" nps.append(Tree.leaves(np)) elif isinstance(subtree, Tree) and subtree.label() != 'NP': getSecondLvNPsOfParseTree(subtree, nps, False) elif isinstance(subtree, Tree) and subtree.label() == 'NP' and subtree.height() != 3: getSecondLvNPsOfParseTree(subtree, nps, False) else: # reach terminal pass
def full_text(): jpype.attachThreadToJVM() data = request.get_json() text = data["text"] surface = p.single_processor(text) json_triples = [] result = {} print_triples = [] for triplet in surface: for triple in triplet: json_triple = {} json_triple["s"] = ' '.join(str(triple[0]).split()) json_triple["p"] = ' '.join(str(triple[1]).split()) json_triple["o"] = ' '.join(str(triple[2]).split()) print_triple = Tree('T', [triple[0], triple[1], triple[2]]) json_triples.append(json_triple) print_triples.append(print_triple) result["triples"] = json_triples for print_triple in print_triples: print_triple.pretty_print() return jsonify(result)
def getFirstLvNPsOfParseTree(parse_tree, nps, display_tree=False): if display_tree: Tree.pretty_print(parse_tree) # print(Tree.leaf_treeposition(parser_tree, 1)) get a child index by leaves list index # print(parser_tree[(0, 0, 1,)]) get a tree by index for subtree in parse_tree: if isinstance(subtree, Tree) and subtree.label() == 'NP': np = subtree start_flag = "B-NP" print('\nNP: '+' '.join(Tree.leaves(np))) # may or may not be a terminal for np_derivation in Tree.subtrees(np): # below gets smaller np scope # getNPsOfParseTree(np_derivation, nps, False) if np_derivation.label() in penni_tags: print(np_derivation.leaves()[0]+'\t'+np_derivation.label()+'\t'+start_flag) start_flag = "I-NP" nps.append(Tree.leaves(np)) elif isinstance(subtree, Tree) and subtree.label() != 'NP': getFirstLvNPsOfParseTree(subtree, nps, False) else: # reach terminal pass