2016-01-16 16:41:25 +00:00
|
|
|
import numpy as np
|
|
|
|
|
|
|
|
from spacy.attrs import HEAD, DEP
|
|
|
|
from spacy.symbols import nsubj, dobj, punct, amod, nmod, conj, cc, root
|
|
|
|
from spacy.en import English
|
2016-05-02 12:25:10 +00:00
|
|
|
from spacy.syntax.iterators import english_noun_chunks
|
2016-01-16 16:41:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_not_nested():
|
2016-04-08 14:45:27 +00:00
|
|
|
nlp = English(parser=False, entity=False)
|
2016-01-16 16:41:25 +00:00
|
|
|
sent = u'''Peter has chronic command and control issues'''.strip()
|
|
|
|
tokens = nlp(sent)
|
|
|
|
tokens.from_array(
|
|
|
|
[HEAD, DEP],
|
|
|
|
np.asarray(
|
|
|
|
[
|
|
|
|
[1, nsubj],
|
|
|
|
[0, root],
|
|
|
|
[4, amod],
|
|
|
|
[3, nmod],
|
|
|
|
[-1, cc],
|
|
|
|
[-2, conj],
|
|
|
|
[-5, dobj]
|
|
|
|
], dtype='int32'))
|
2016-05-02 12:25:10 +00:00
|
|
|
tokens.noun_chunks_iterator = english_noun_chunks
|
2016-01-16 16:41:25 +00:00
|
|
|
word_occurred = {}
|
|
|
|
for chunk in tokens.noun_chunks:
|
|
|
|
for word in chunk:
|
|
|
|
word_occurred.setdefault(word.text, 0)
|
|
|
|
word_occurred[word.text] += 1
|
|
|
|
for word, freq in word_occurred.items():
|
|
|
|
assert freq == 1, (word, [chunk.text for chunk in tokens.noun_chunks])
|
2016-04-08 14:45:27 +00:00
|
|
|
|