Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Spacy-Chunk-NE令牌_Python_Tokenize_Spacy_Named Entity Recognition - Fatal编程技术网

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文档列表,如我的示例输出中所示。