Вот версия NLTK (может не так хорошо, как SpaCy):
from nltk import Tree
from nltk import ne_chunk, pos_tag, word_tokenize
def get_continuous_chunks(text, chunk_func=ne_chunk):
chunked = chunk_func(pos_tag(word_tokenize(text)))
continuous_chunk = []
current_chunk = []
for subtree in chunked:
if type(subtree) == Tree:
current_chunk.append(" ".join([token for token, pos in subtree.leaves()]))
elif current_chunk:
named_entity = " ".join(current_chunk)
if named_entity not in continuous_chunk:
continuous_chunk.append(named_entity)
current_chunk = []
else:
continue
return continuous_chunk
text = 'Scarlett Johansson is an American actress.'
get_continuous_chunks(text)