def copy_subtree(self, troot, aroot): """\ Deep-copy a subtree, creating nodes with the same attributes, but different IDs. """ # assume the roots have been copied, just go through children for tnode in troot.get_children(ordered=1): # copy lemma, delete reflexive particles in verbs lemma = tnode.t_lemma or '' lemma = re.sub(r'_s[ei]$', '', lemma) # skip #Cor nodes if lemma != '#Cor': # create the new node anode = aroot.create_child() tnode.lex_anode = anode # set lemma and ord re.sub(r'_s[ie]$', '', lemma) anode.lemma = lemma anode.ord = tnode.ord # set coap afun, if needed if tnode.is_coap_root(): anode.afun = tnode.functor == 'APPS' and 'Apos' or 'Coord' anode.is_member = tnode.is_member anode.set_attr('wild/is_parenthesis', tnode.is_parenthesis) self.copy_subtree(tnode, anode) else: if tnode.get_children(): log_warn('#Cor node is not a leaf:' + tnode.id) self.copy_subtree(tnode, aroot)
def drop_anode(self, tnode): "Remove the lexical a-node corresponding to the given t-node" anode = tnode.lex_anode if not anode: log_warn("Can't find a-node to be dropped:" + tnode.id) return # this should not happen, but just to be sure - rehang children for achild in anode.get_children(): achild.parent = anode.parent # remove the node itself anode.remove()
def __can_apply_eff(self, or_topological): """Return true if the given node is OK for effective relations to be applied, false otherwise.""" if self.is_coap_root(): caller_name = inspect.stack()[1][3] message = caller_name + ' called on coap_root (' + self.id + ').' if or_topological: return False else: # this should not happen, so warn about it log_warn(message + ' Fallback to topological.') return False return True
def __get_eparents(self): if not self.parent: log_warn("Cannot find parents, using the root: " + self.id) return [self.root] # try getting coap root, if applicable node = self.__get_transitive_coap_root() or self # continue to parent node = node.parent if not node: return [self.__fallback_parent()] # if we are not in coap, return just the one node if not node.is_coap_root: return [node] # we are in a coap -> return members eff = node.get_coap_members() if eff: return eff return [self.__fallback_parent()]
def get_clause_root(self): "Return the root of the clause the current node resides in." # default to self if clause number is not defined if self.clause_number is None: log_warn('Clause number undefined in: ' + self.id) return self highest = self parent = self.parent # move as high as possible within the clause while parent and parent.clause_number == self.clause_number: highest = parent parent = parent.parent # handle coordinations - shared attributes if parent and parent.is_coap_root() and not highest.is_member: try: eff_parent = next( child for child in parent.get_children() if child.is_member and child.clause_number == self.clause_number) return eff_parent except StopIteration: # no eff_parent found pass return highest
def get_clause_root(self): "Return the root of the clause the current node resides in." # default to self if clause number is not defined if self.clause_number is None: log_warn('Clause number undefined in: ' + self.id) return self highest = self parent = self.parent # move as high as possible within the clause while parent and parent.clause_number == self.clause_number: highest = parent parent = parent.parent # handle coordinations - shared attributes if parent and parent.is_coap_root() and not highest.is_member: try: eff_parent = next(child for child in parent.get_children() if child.is_member and child.clause_number == self.clause_number) return eff_parent except StopIteration: # no eff_parent found pass return highest
def __fallback_parent(self): "Issue a warning and return the topological parent." log_warn("No effective parent, using topological: " + self.id) return self.parent