2016-05-06 01:21:05 +03:00
|
|
|
from spacy.parts_of_speech cimport NOUN, PROPN, PRON
|
2016-03-16 17:53:35 +03:00
|
|
|
|
|
|
|
|
2016-05-02 15:25:10 +03:00
|
|
|
def english_noun_chunks(doc):
|
|
|
|
labels = ['nsubj', 'dobj', 'nsubjpass', 'pcomp', 'pobj',
|
2016-09-27 14:13:01 +03:00
|
|
|
'attr', 'ROOT', 'root']
|
2016-09-30 21:19:53 +03:00
|
|
|
np_deps = [doc.vocab.strings[label] for label in labels]
|
|
|
|
conj = doc.vocab.strings['conj']
|
|
|
|
np_label = doc.vocab.strings['NP']
|
2016-05-20 11:11:05 +03:00
|
|
|
for i, word in enumerate(doc):
|
2016-05-06 01:21:05 +03:00
|
|
|
if word.pos in (NOUN, PROPN, PRON) and word.dep in np_deps:
|
2016-05-02 15:25:10 +03:00
|
|
|
yield word.left_edge.i, word.i+1, np_label
|
|
|
|
elif word.pos == NOUN and word.dep == conj:
|
|
|
|
head = word.head
|
|
|
|
while head.dep == conj and head.head.i < head.i:
|
|
|
|
head = head.head
|
|
|
|
# If the head is an NP, and we're coordinated to it, we're an NP
|
|
|
|
if head.dep in np_deps:
|
|
|
|
yield word.left_edge.i, word.i+1, np_label
|
2016-03-16 17:53:35 +03:00
|
|
|
|
|
|
|
|
|
|
|
# this iterator extracts spans headed by NOUNs starting from the left-most
|
|
|
|
# syntactic dependent until the NOUN itself
|
|
|
|
# for close apposition and measurement construction, the span is sometimes
|
|
|
|
# extended to the right of the NOUN
|
|
|
|
# example: "eine Tasse Tee" (a cup (of) tea) returns "eine Tasse Tee" and not
|
|
|
|
# just "eine Tasse", same for "das Thema Familie"
|
2016-05-02 15:25:10 +03:00
|
|
|
def german_noun_chunks(doc):
|
2016-09-27 14:13:01 +03:00
|
|
|
labels = ['sb', 'oa', 'da', 'nk', 'mo', 'ag', 'ROOT', 'root', 'cj', 'pd', 'og', 'app']
|
2016-09-30 21:19:53 +03:00
|
|
|
np_label = doc.vocab.strings['NP']
|
|
|
|
np_deps = set(doc.vocab.strings[label] for label in labels)
|
|
|
|
close_app = doc.vocab.strings['nk']
|
|
|
|
|
2016-05-03 18:19:05 +03:00
|
|
|
rbracket = 0
|
|
|
|
for i, word in enumerate(doc):
|
|
|
|
if i < rbracket:
|
|
|
|
continue
|
2016-05-06 02:41:26 +03:00
|
|
|
if word.pos in (NOUN, PROPN, PRON) and word.dep in np_deps:
|
2016-05-02 15:25:10 +03:00
|
|
|
rbracket = word.i+1
|
|
|
|
# try to extend the span to the right
|
|
|
|
# to capture close apposition/measurement constructions
|
|
|
|
for rdep in doc[word.i].rights:
|
2016-05-06 02:41:26 +03:00
|
|
|
if rdep.pos in (NOUN, PROPN) and rdep.dep == close_app:
|
2016-05-02 15:25:10 +03:00
|
|
|
rbracket = rdep.i+1
|
2016-05-04 08:40:38 +03:00
|
|
|
yield word.left_edge.i, rbracket, np_label
|
|
|
|
|
2016-05-02 15:25:10 +03:00
|
|
|
|
|
|
|
CHUNKERS = {'en': english_noun_chunks, 'de': german_noun_chunks}
|