假设我有一个文档,如下所示:
import spacy
nlp = spacy.load('en')
doc = nlp('My name is John Smith')
[t for t in doc]
> [My, name, is, John, Smith]Spacy足够智能,可以意识到'John Smith‘是一个多标记命名实体:
[e for e in doc.ents]
> [John Smith]我如何让它将命名实体分成离散的标记,如下所示:
> [My, name, is, John Smith]发布于 2018-08-12 05:56:00
NER上的Spacy文档说,您可以使用token.ent_iob_和token.ent_type_属性访问令牌实体注释。
https://spacy.io/usage/linguistic-features#accessing
示例:
import spacy
nlp = spacy.load('en')
doc = nlp('My name is John Smith')
ne = []
merged = []
for t in doc:
# "O" -> current token is not part of the NE
if t.ent_iob_ == "O":
if len(ne) > 0:
merged.append(" ".join(ne))
ne = []
merged.append(t.text)
else:
ne.append(t.text)
if len(ne) > 0:
merged.append(" ".join(ne))
print(merged)这将打印以下内容:
['My', 'name', 'is', 'John Smith']https://stackoverflow.com/questions/51802645
复制相似问题