我得到
的标记和名词短语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']
答案 0 :(得分:2)
您可以使用this article by Baeldung属性:
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']