[英]How does this for loop work in Spacy's custom NER training code?
我正在编写代码来训练 Spacy 的 NER 引擎中的自定义实体。 我一直在理解在线教程中的一小部分代码。 这是教程的链接。 以下是代码,我无法理解注释# add labels
下的两个 for 循环。 我是 python 的新手。
import spacy
################### Train Spacy NER.###########
def train_spacy():
TRAIN_DATA = convert_dataturks_to_spacy("dataturks_downloaded.json");
nlp = spacy.blank('en') # create blank Language class
# create the built-in pipeline components and add them to the pipeline
# nlp.create_pipe works for built-ins that are registered with spaCy
if 'ner' not in nlp.pipe_names:
ner = nlp.create_pipe('ner')
nlp.add_pipe(ner, last=True)
# add labels
for _, annotations in TRAIN_DATA:
for ent in annotations.get('entities'):
ner.add_label(ent[2])
显然,这个 for 循环正在向 NER 添加自定义标签。 我的问题是;
您的问题大多可以通过了解 function convert_dataturks_to_spacy
来回答。 此代码与您正在遵循的教程位于同一存储库中。
(text, {"entities": entities})
。 annotations
是每个元组的第二个元素。_
,将第二个元素分配给annotations
,然后做一些事情。 在 python 中, _
经常用作一次性变量,即代码中其他地方未使用但存在于数据中的变量。ent[2]
是被标记实体的 label。 查看代码,dataturks 中的实体是具有 3 个元素的元组 - 字符串中的开头 position、字符串中的结尾 position 和 label。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.