Python Spacy-Chunk-NE令牌
假设我有一个文档,如下所示:Python Spacy-Chunk-NE令牌,python,tokenize,spacy,named-entity-recognition,Python,Tokenize,Spacy,Named Entity Recognition,假设我有一个文档,如下所示: import spacy nlp = spacy.load('en') doc = nlp('My name is John Smith') [t for t in doc] > [My, name, is, John, Smith] > [My, name, is, John Smith] Spacy足够聪明,能够认识到“John Smith”是一个多令牌命名实体: [e for e in doc.ents] > [John Smith
import spacy
nlp = spacy.load('en')
doc = nlp('My name is John Smith')
[t for t in doc]
> [My, name, is, John, Smith]
> [My, name, is, John Smith]
Spacy足够聪明,能够认识到“John Smith”是一个多令牌命名实体:
[e for e in doc.ents]
> [John Smith]
如何将命名实体分块成离散的标记,如下所示:
import spacy
nlp = spacy.load('en')
doc = nlp('My name is John Smith')
[t for t in doc]
> [My, name, is, John, Smith]
> [My, name, is, John Smith]
NER上的Spacy文档说,您可以使用
token.ent\u iob\u
和token.ent\u type\u
属性访问令牌实体注释
例如:
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']
这就足够了,但我希望结果是一个Spacy文档列表,如我的示例输出中所示。