Как получить токены для новых фраз в spaCy? - PullRequest
2 голосов
/ 25 сентября 2019

Я получаю токены и фразы с

text = ("This is commonly referred to as global warming or climate change.")
doc = nlp(text)

for token in doc:
    print(token.i, token.text)

print("Noun phrases:", [chunk.text for chunk in doc.noun_chunks])

, и в результате получается

0 This
1 is
2 commonly
3 referred
4 to
5 as
6 global
7 warming
8 or
9 climate
10 change
11 .
Noun phrases: ['global warming', 'climate change']

Можно ли получить индекс токенов для имен существительных вместо слов?Например

Noun phrases: ['6,7', '9,10']

1 Ответ

2 голосов
/ 25 сентября 2019

Вы можете использовать свойства Span * start и end:

start   int     The index of the first token of the span.
end     int     The index of the first token after the span.

Итак, используйте

print("Noun phrases:", [(chunk.start,chunk.end-1) for chunk in doc.noun_chunks])
# => Noun phrases: [(6, 7), (9, 10)]

Или,если вам нужны строковые элементы через запятую,

 ["{},{}".format(chunk.start,chunk.end-1) for chunk in doc.noun_chunks]
 ## => Noun phrases: ['6,7', '9,10']
...