class AlphaStage(object): def __init__(self): self.tree = Tree() self.nest_deps = ["aux", "auxpass", "cc", "agent", "det", "advmod", "amod", "poss", "nummod"] self.add_to_first_deps = ["pcomp", "compound"] def process_child_token(self, parent_elem_id, token, root_id, pos): # ignore if (len(token.dep) == 0) or (token.dep == "punct"): return parent_elem = self.tree.get(parent_elem_id) # nest if (token.dep in self.nest_deps) or ((token.dep == "prep") and (not parent_elem.has_pos("VERB"))): child_elem_id = self.process_token(token, root_id) self.tree.get(root_id).nest(child_elem_id) self.tree.get(root_id).new_layer() return child_elem_id = self.process_token(token) # add to parent's first child if token.dep in self.add_to_first_deps: parent_elem.add_to_first_child(child_elem_id, pos) return # add child parent_elem.add_child(child_elem_id) def process_token(self, token, root_id=None): elem_id = self.tree.create_leaf(token) if root_id is None: root_id = elem_id for child_token in token.left_children: self.process_child_token(elem_id, child_token, root_id, Position.LEFT) for child_token in token.right_children: self.process_child_token(elem_id, child_token, root_id, Position.RIGHT) self.tree.get(elem_id).apply_layers() return elem_id def process_sentence(self, sentence): self.tree.root_id = self.process_token(sentence.root()) self.tree.remove_redundant_nesting() return self.tree
def __init__(self): self.tree = Tree() self.nest_deps = ["aux", "auxpass", "cc", "agent", "det", "advmod", "amod", "poss", "nummod"] self.add_to_first_deps = ["pcomp", "compound"]