简体   繁体   中英

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. 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. 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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