Evaluation in a Spacy NER model

时光怂恿深爱的人放手 提交于 2019-12-03 06:14:38
Mpizos Dimitris

For those one having the same question in the following link:

spaCy/scorer.py

you can find different metrics including: fscore, recall and precision. An example using scorer:

import spacy
from spacy.gold import GoldParse
from spacy.scorer import Scorer

def evaluate(ner_model, examples):
    scorer = Scorer()
    for input_, annot in examples:
        doc_gold_text = ner_model.make_doc(input_)
        gold = GoldParse(doc_gold_text, entities=annot)
        pred_value = ner_model(input_)
        scorer.score(pred_value, gold)
    return scorer.scores

# example run

examples = [
    ('Who is Shaka Khan?',
     [(7, 17, 'PERSON')]),
    ('I like London and Berlin.',
     [(7, 13, 'LOC'), (18, 24, 'LOC')])
]

ner_model = spacy.load(ner_model_path) # for spaCy's pretrained use 'en_core_web_sm'
results = evaluate(ner_model, examples)

where input_ is the text(ex. "my name is John") and annot is the annotations (ex. [(11,16,"PEOPLE")]

The scorer.scores returns multiple scores. The example is taken from spaCy example in github (link does not work anymore)

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!