def get_head_of(s, word_id, default=None): """ Return the single head of word_id in s, or None if it has no head. Die if word_id has more than one head. """ heads = heads_of(s, word_id) if heads: return the_only(heads) else: return default
def extract(markable): d = markable.attrib del d['{http://nite.sourceforge.net/}id'] if d: child_id = extract_id(rfutils.the_only(markable).attrib['href']) return child_id, d else: return None
def score(self, lin, f, sentence, h): phrase = depgraph.immediate_phrase_of(sentence, h) phrase_set = set(phrase) relevant_lin = [n for n in lin if n in phrase_set] # this indices_in should be unique: phrase_order_indices = rfutils.the_only(o.indices_in(phrase, relevant_lin)) the_score = score(f, phrase_order_indices, sentence, phrase, h) return the_score + sum(proj_lin_from.score(lin, f, sentence, n) for n in phrase if n != h)
def score(self, lin, f, sentence, h): phrase = depgraph.immediate_phrase_of(sentence, h) phrase_set = set(phrase) relevant_lin = [n for n in lin if n in phrase_set] # this indices_in should be unique: phrase_order_indices = rfutils.the_only( o.indices_in(phrase, relevant_lin)) the_score = score(f, phrase_order_indices, sentence, phrase, h) return the_score + sum( proj_lin_from.score(lin, f, sentence, n) for n in phrase if n != h)
def pruefer_code_for(tree): # from https://www.math.upenn.edu/~wilf/website/CombinatorialAlgorithms.pdf # Ch. 28, pp. 267--268 # This implementation closely follows this reference: #@inbook{nijenhuis1978labeled, # title="Combinatorial Algorithms", # chapter="28", # edition = "2", # pages="267--274" # author = "Albert Nijenhuis and Herbert S. Wilf", # publisher = "Academic Press", # address = "New York", # series = "Computer Science and Applied Mathematics", # editor = "Werner Rheinboldt", # year = "1978" # } tree = copy.deepcopy(tree) while len(tree.edges()) > 1: x = min(endpoints_of(tree)) _, a = the_only(tree.edges(x)) yield a tree.remove_edge(x, a)
def root_of(s): """ Return the single root node of a sentence; die if there are 0 roots or more than 1 root. """ return the_only(roots_of(s))
def head_of(s, word_id): """ Return the single head of word_id in s. Die if word_id has 0 or more than 1 heads. """ return the_only(heads_of(s, word_id))