简体   繁体   English

如何使用 spacy 模型打印我的预测的 ner 标签(即使没有标签)?

[英]How can I print ner tags (even when there are no tags) of my prediction with spacy models?

I trained my spacy model for a NER problem.我针对NER问题训练了我的spacy model。 However, when I print my predictions, I get all the entities if there are an entity in the doc.但是,当我打印我的预测时,如果文档中有实体,我会得到所有实体。 If there are no entities, it doesn't print anything so for computing my scores with metrics like f1-score for example, I will get a false score.如果没有实体,它不会打印任何内容,因此例如使用 f1-score 等指标计算我的分数时,我会得到一个错误的分数。 Anyone knows how to deal with this problem Here is what I did:任何人都知道如何处理这个问题这是我所做的:

for i in range(len(test)):
   doc = nlp1(test[i][0])
   for ent in doc.ents:
       print(ent.text, ent.label_)

and here is an example of what I get:这是我得到的一个例子:

BFGA 004 CODEACTE
ADC codeacte3
ATM codeacte3
EEAF 004 CODEACTE
ATM codeacte3
ATM codeacte3
BFGA004 CODEACTE
NEKA 020 CODEACTE
GBPEOO1 CODEACTE
HHQE 002 CODEACTE
HEQE 002 CODEACTE
JDFEO01 CODEACTE
NFKA007 CODEACTE
QEEB152 CODEACTE

but I would like something like that:但我想要这样的东西:

JDFEO01 CODEACTE
NFKA007 CODEACTE
' '     ' '
QEEB152 CODEACTE

spaCy has built-in scorers for calculating F1 and other standard metrics, but if you need to print data for an external scorer you can do something like this. spaCy 具有用于计算 F1 和其他标准指标的内置计分器,但如果您需要为外部计分器打印数据,您可以执行类似的操作。

import spacy
nlp = spacy.load("my_model")

text = "this is my text"
for word in nlp(text):
    print(word, word.ent_iob_, word.ent_type_, sep="\t")

Note that the key thing about this is it prints all tokens, not just entities.请注意,关键是它打印所有令牌,而不仅仅是实体。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM