2017-04-15 11:05:15 +00:00
|
|
|
# coding: utf-8
|
2017-11-17 17:55:13 +00:00
|
|
|
# cython: profile=True
|
|
|
|
# cython: infer_types=True
|
2017-10-27 17:45:57 +00:00
|
|
|
"""Implements the projectivize/deprojectivize mechanism in Nivre & Nilsson 2005
|
2017-05-22 09:48:02 +00:00
|
|
|
for doing pseudo-projective parsing implementation uses the HEAD decoration
|
|
|
|
scheme.
|
|
|
|
"""
|
2016-11-25 23:29:17 +00:00
|
|
|
from __future__ import unicode_literals
|
2017-10-27 17:45:57 +00:00
|
|
|
|
2016-02-24 10:26:25 +00:00
|
|
|
from copy import copy
|
2016-02-22 13:40:40 +00:00
|
|
|
|
2019-11-11 16:35:27 +00:00
|
|
|
from spacy.gold import Example
|
2018-03-27 17:23:02 +00:00
|
|
|
from ..tokens.doc cimport Doc, set_children_from_heads
|
2018-04-03 13:50:31 +00:00
|
|
|
from ..errors import Errors
|
2017-11-17 17:55:13 +00:00
|
|
|
|
2016-03-01 09:09:08 +00:00
|
|
|
|
2017-05-22 09:48:02 +00:00
|
|
|
DELIMITER = '||'
|
|
|
|
|
2016-03-01 09:09:08 +00:00
|
|
|
|
2016-02-24 10:26:25 +00:00
|
|
|
def ancestors(tokenid, heads):
|
2017-10-27 17:45:57 +00:00
|
|
|
# Returns all words going from the word up the path to the root. The path
|
|
|
|
# to root cannot be longer than the number of words in the sentence. This
|
|
|
|
# function ends after at most len(heads) steps, because it would otherwise
|
|
|
|
# loop indefinitely on cycles.
|
2016-02-24 10:26:25 +00:00
|
|
|
head = tokenid
|
2016-02-22 13:40:40 +00:00
|
|
|
cnt = 0
|
|
|
|
while heads[head] != head and cnt < len(heads):
|
|
|
|
head = heads[head]
|
|
|
|
cnt += 1
|
|
|
|
yield head
|
2017-10-27 17:45:57 +00:00
|
|
|
if head is None:
|
2016-02-22 13:40:40 +00:00
|
|
|
break
|
|
|
|
|
|
|
|
|
|
|
|
def contains_cycle(heads):
|
2017-10-27 17:45:57 +00:00
|
|
|
# in an acyclic tree, the path from each word following the head relation
|
|
|
|
# upwards always ends at the root node
|
2016-02-24 10:26:25 +00:00
|
|
|
for tokenid in range(len(heads)):
|
|
|
|
seen = set([tokenid])
|
2017-10-27 17:45:57 +00:00
|
|
|
for ancestor in ancestors(tokenid, heads):
|
2016-02-22 13:40:40 +00:00
|
|
|
if ancestor in seen:
|
|
|
|
return seen
|
|
|
|
seen.add(ancestor)
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
2016-02-24 10:26:25 +00:00
|
|
|
def is_nonproj_arc(tokenid, heads):
|
2016-02-22 13:40:40 +00:00
|
|
|
# definition (e.g. Havelka 2007): an arc h -> d, h < d is non-projective
|
2016-02-24 10:26:25 +00:00
|
|
|
# if there is a token k, h < k < d such that h is not
|
2016-02-22 13:40:40 +00:00
|
|
|
# an ancestor of k. Same for h -> d, h > d
|
2016-02-24 10:26:25 +00:00
|
|
|
head = heads[tokenid]
|
2017-10-27 17:45:57 +00:00
|
|
|
if head == tokenid: # root arcs cannot be non-projective
|
2016-02-22 13:40:40 +00:00
|
|
|
return False
|
2017-10-27 17:45:57 +00:00
|
|
|
elif head is None: # unattached tokens cannot be non-projective
|
2016-02-22 13:40:40 +00:00
|
|
|
return False
|
|
|
|
|
2016-02-24 10:26:25 +00:00
|
|
|
start, end = (head+1, tokenid) if head < tokenid else (tokenid+1, head)
|
2017-10-27 17:45:57 +00:00
|
|
|
for k in range(start, end):
|
|
|
|
for ancestor in ancestors(k, heads):
|
|
|
|
if ancestor is None: # for unattached tokens/subtrees
|
2016-02-22 13:40:40 +00:00
|
|
|
break
|
2017-10-27 17:45:57 +00:00
|
|
|
elif ancestor == head: # normal case: k dominated by h
|
2016-02-22 13:40:40 +00:00
|
|
|
break
|
2017-10-27 17:45:57 +00:00
|
|
|
else: # head not in ancestors: d -> h is non-projective
|
2016-02-22 13:40:40 +00:00
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
2016-02-24 10:26:25 +00:00
|
|
|
def is_nonproj_tree(heads):
|
2016-02-22 13:40:40 +00:00
|
|
|
# a tree is non-projective if at least one arc is non-projective
|
2017-10-27 17:45:57 +00:00
|
|
|
return any(is_nonproj_arc(word, heads) for word in range(len(heads)))
|
2016-02-24 10:26:25 +00:00
|
|
|
|
|
|
|
|
2017-05-22 09:48:02 +00:00
|
|
|
def decompose(label):
|
|
|
|
return label.partition(DELIMITER)[::2]
|
|
|
|
|
|
|
|
|
|
|
|
def is_decorated(label):
|
2018-03-27 17:23:02 +00:00
|
|
|
return DELIMITER in label
|
|
|
|
|
2019-11-11 16:35:27 +00:00
|
|
|
def count_decorated_labels(gold_data):
|
2018-03-27 17:23:02 +00:00
|
|
|
freqs = {}
|
2019-11-11 16:35:27 +00:00
|
|
|
for example in gold_data:
|
2019-11-25 15:03:28 +00:00
|
|
|
proj_heads, deco_deps = projectivize(example.token_annotation.heads,
|
|
|
|
example.token_annotation.deps)
|
|
|
|
# set the label to ROOT for each root dependent
|
|
|
|
deco_deps = ['ROOT' if head == i else deco_deps[i]
|
|
|
|
for i, head in enumerate(proj_heads)]
|
|
|
|
# count label frequencies
|
|
|
|
for label in deco_deps:
|
|
|
|
if is_decorated(label):
|
|
|
|
freqs[label] = freqs.get(label, 0) + 1
|
2018-03-27 17:23:02 +00:00
|
|
|
return freqs
|
2017-05-22 09:48:02 +00:00
|
|
|
|
|
|
|
|
2019-11-11 16:35:27 +00:00
|
|
|
def preprocess_training_data(gold_data, label_freq_cutoff=30):
|
2017-05-22 09:48:02 +00:00
|
|
|
preprocessed = []
|
|
|
|
freqs = {}
|
2019-11-11 16:35:27 +00:00
|
|
|
for example in gold_data:
|
|
|
|
new_example = Example(doc=example.doc)
|
2019-11-25 15:03:28 +00:00
|
|
|
proj_heads, deco_deps = projectivize(example.token_annotation.heads,
|
|
|
|
example.token_annotation.deps)
|
|
|
|
# set the label to ROOT for each root dependent
|
|
|
|
deco_deps = ['ROOT' if head == i else deco_deps[i]
|
|
|
|
for i, head in enumerate(proj_heads)]
|
|
|
|
# count label frequencies
|
|
|
|
if label_freq_cutoff > 0:
|
|
|
|
for label in deco_deps:
|
|
|
|
if is_decorated(label):
|
|
|
|
freqs[label] = freqs.get(label, 0) + 1
|
|
|
|
proj_token_dict = example.token_annotation.to_dict()
|
|
|
|
proj_token_dict["heads"] = proj_heads
|
|
|
|
proj_token_dict["deps"] = deco_deps
|
|
|
|
new_example.set_token_annotation(**proj_token_dict)
|
2019-11-11 16:35:27 +00:00
|
|
|
preprocessed.append(new_example)
|
2017-05-22 09:48:02 +00:00
|
|
|
if label_freq_cutoff > 0:
|
2017-10-27 17:45:57 +00:00
|
|
|
return _filter_labels(preprocessed, label_freq_cutoff, freqs)
|
2017-05-22 09:48:02 +00:00
|
|
|
return preprocessed
|
|
|
|
|
|
|
|
|
|
|
|
def projectivize(heads, labels):
|
2017-10-27 17:45:57 +00:00
|
|
|
# Use the algorithm by Nivre & Nilsson 2005. Assumes heads to be a proper
|
|
|
|
# tree, i.e. connected and cycle-free. Returns a new pair (heads, labels)
|
|
|
|
# which encode a projective and decorated tree.
|
2017-05-22 09:48:02 +00:00
|
|
|
proj_heads = copy(heads)
|
|
|
|
smallest_np_arc = _get_smallest_nonproj_arc(proj_heads)
|
2017-10-27 17:45:57 +00:00
|
|
|
if smallest_np_arc is None: # this sentence is already projective
|
2017-05-22 09:48:02 +00:00
|
|
|
return proj_heads, copy(labels)
|
2017-10-27 17:45:57 +00:00
|
|
|
while smallest_np_arc is not None:
|
2017-05-22 09:48:02 +00:00
|
|
|
_lift(smallest_np_arc, proj_heads)
|
|
|
|
smallest_np_arc = _get_smallest_nonproj_arc(proj_heads)
|
|
|
|
deco_labels = _decorate(heads, proj_heads, labels)
|
|
|
|
return proj_heads, deco_labels
|
|
|
|
|
|
|
|
|
2017-11-17 17:55:13 +00:00
|
|
|
cpdef deprojectivize(Doc doc):
|
2017-10-27 17:45:57 +00:00
|
|
|
# Reattach arcs with decorated labels (following HEAD scheme). For each
|
|
|
|
# decorated arc X||Y, search top-down, left-to-right, breadth-first until
|
|
|
|
# hitting a Y then make this the new head.
|
2017-11-17 17:55:13 +00:00
|
|
|
for i in range(doc.length):
|
|
|
|
label = doc.vocab.strings[doc.c[i].dep]
|
|
|
|
if DELIMITER in label:
|
|
|
|
new_label, head_label = label.split(DELIMITER)
|
|
|
|
new_head = _find_new_head(doc[i], head_label)
|
2018-03-27 17:23:02 +00:00
|
|
|
doc.c[i].head = new_head.i - i
|
2017-11-23 12:48:00 +00:00
|
|
|
doc.c[i].dep = doc.vocab.strings.add(new_label)
|
2018-03-27 17:23:02 +00:00
|
|
|
set_children_from_heads(doc.c, doc.length)
|
2017-11-17 17:55:13 +00:00
|
|
|
return doc
|
2017-05-22 09:48:02 +00:00
|
|
|
|
2017-10-27 17:45:57 +00:00
|
|
|
|
2017-05-22 09:48:02 +00:00
|
|
|
def _decorate(heads, proj_heads, labels):
|
|
|
|
# uses decoration scheme HEAD from Nivre & Nilsson 2005
|
2018-04-03 13:50:31 +00:00
|
|
|
if (len(heads) != len(proj_heads)) or (len(proj_heads) != len(labels)):
|
|
|
|
raise ValueError(Errors.E082.format(n_heads=len(heads),
|
|
|
|
n_proj_heads=len(proj_heads),
|
|
|
|
n_labels=len(labels)))
|
2017-05-22 09:48:02 +00:00
|
|
|
deco_labels = []
|
2017-10-27 17:45:57 +00:00
|
|
|
for tokenid, head in enumerate(heads):
|
2017-05-22 09:48:02 +00:00
|
|
|
if head != proj_heads[tokenid]:
|
2017-10-27 17:45:57 +00:00
|
|
|
deco_labels.append(
|
|
|
|
'%s%s%s' % (labels[tokenid], DELIMITER, labels[head]))
|
2017-05-22 09:48:02 +00:00
|
|
|
else:
|
|
|
|
deco_labels.append(labels[tokenid])
|
|
|
|
return deco_labels
|
|
|
|
|
|
|
|
|
|
|
|
def _get_smallest_nonproj_arc(heads):
|
|
|
|
# return the smallest non-proj arc or None
|
|
|
|
# where size is defined as the distance between dep and head
|
|
|
|
# and ties are broken left to right
|
|
|
|
smallest_size = float('inf')
|
|
|
|
smallest_np_arc = None
|
2017-10-27 17:45:57 +00:00
|
|
|
for tokenid, head in enumerate(heads):
|
2017-05-22 09:48:02 +00:00
|
|
|
size = abs(tokenid-head)
|
2017-10-27 17:45:57 +00:00
|
|
|
if size < smallest_size and is_nonproj_arc(tokenid, heads):
|
2017-05-22 09:48:02 +00:00
|
|
|
smallest_size = size
|
|
|
|
smallest_np_arc = tokenid
|
|
|
|
return smallest_np_arc
|
|
|
|
|
|
|
|
|
|
|
|
def _lift(tokenid, heads):
|
|
|
|
# reattaches a word to it's grandfather
|
|
|
|
head = heads[tokenid]
|
|
|
|
ghead = heads[head]
|
|
|
|
# attach to ghead if head isn't attached to root else attach to root
|
|
|
|
heads[tokenid] = ghead if head != ghead else tokenid
|
|
|
|
|
|
|
|
|
|
|
|
def _find_new_head(token, headlabel):
|
|
|
|
# search through the tree starting from the head of the given token
|
|
|
|
# returns the id of the first descendant with the given label
|
|
|
|
# if there is none, return the current head (no change)
|
|
|
|
queue = [token.head]
|
|
|
|
while queue:
|
|
|
|
next_queue = []
|
|
|
|
for qtoken in queue:
|
|
|
|
for child in qtoken.children:
|
2017-10-27 17:45:57 +00:00
|
|
|
if child.is_space:
|
|
|
|
continue
|
|
|
|
if child == token:
|
|
|
|
continue
|
2017-05-22 09:48:02 +00:00
|
|
|
if child.dep_ == headlabel:
|
|
|
|
return child
|
|
|
|
next_queue.append(child)
|
|
|
|
queue = next_queue
|
|
|
|
return token.head
|
|
|
|
|
|
|
|
|
2019-11-11 16:35:27 +00:00
|
|
|
def _filter_labels(examples, cutoff, freqs):
|
2017-05-22 09:48:02 +00:00
|
|
|
# throw away infrequent decorated labels
|
|
|
|
# can't learn them reliably anyway and keeps label set smaller
|
|
|
|
filtered = []
|
2019-11-11 16:35:27 +00:00
|
|
|
for example in examples:
|
|
|
|
new_example = Example(doc=example.doc)
|
2019-11-25 15:03:28 +00:00
|
|
|
filtered_labels = []
|
|
|
|
for label in example.token_annotation.deps:
|
|
|
|
if is_decorated(label) and freqs.get(label, 0) < cutoff:
|
|
|
|
filtered_labels.append(decompose(label)[0])
|
|
|
|
else:
|
|
|
|
filtered_labels.append(label)
|
|
|
|
filtered_token_dict = example.token_annotation.to_dict()
|
|
|
|
filtered_token_dict["deps"] = filtered_labels
|
|
|
|
new_example.set_token_annotation(**filtered_token_dict)
|
2019-11-11 16:35:27 +00:00
|
|
|
filtered.append(new_example)
|
2017-05-22 09:48:02 +00:00
|
|
|
return filtered
|