2017-10-01 22:04:32 +03:00
|
|
|
# coding: utf8
|
|
|
|
from __future__ import unicode_literals, division, print_function
|
|
|
|
|
|
|
|
import plac
|
|
|
|
from timeit import default_timer as timer
|
2018-11-30 22:16:14 +03:00
|
|
|
from wasabi import Printer
|
2017-10-01 22:04:32 +03:00
|
|
|
|
2017-10-27 15:38:39 +03:00
|
|
|
from ..gold import GoldCorpus
|
2017-10-01 22:04:32 +03:00
|
|
|
from .. import util
|
|
|
|
from .. import displacy
|
2017-10-27 15:38:39 +03:00
|
|
|
|
2017-10-01 22:04:32 +03:00
|
|
|
|
|
|
|
@plac.annotations(
|
2018-11-30 22:16:14 +03:00
|
|
|
model=("Model name or path", "positional", None, str),
|
|
|
|
data_path=("Location of JSON-formatted evaluation data", "positional", None, str),
|
|
|
|
gold_preproc=("Use gold preprocessing", "flag", "G", bool),
|
|
|
|
gpu_id=("Use GPU", "option", "g", int),
|
|
|
|
displacy_path=("Directory to output rendered parses as HTML", "option", "dp", str),
|
|
|
|
displacy_limit=("Limit of parses to render as HTML", "option", "dl", int),
|
2019-04-22 15:23:44 +03:00
|
|
|
return_scores=("Return dict containing model scores", "flag", "R", bool),
|
2018-11-30 22:16:14 +03:00
|
|
|
)
|
|
|
|
def evaluate(
|
|
|
|
model,
|
|
|
|
data_path,
|
|
|
|
gpu_id=-1,
|
|
|
|
gold_preproc=False,
|
|
|
|
displacy_path=None,
|
|
|
|
displacy_limit=25,
|
2019-04-15 13:04:36 +03:00
|
|
|
return_scores=False,
|
2018-11-30 22:16:14 +03:00
|
|
|
):
|
2017-10-01 22:04:32 +03:00
|
|
|
"""
|
2017-10-27 15:38:39 +03:00
|
|
|
Evaluate a model. To render a sample of parses in a HTML file, set an
|
|
|
|
output directory as the displacy_path argument.
|
2017-10-01 22:04:32 +03:00
|
|
|
"""
|
2018-11-30 22:16:14 +03:00
|
|
|
msg = Printer()
|
2018-02-13 14:42:23 +03:00
|
|
|
util.fix_random_seed()
|
2017-10-06 21:17:47 +03:00
|
|
|
if gpu_id >= 0:
|
|
|
|
util.use_gpu(gpu_id)
|
2017-10-03 23:47:31 +03:00
|
|
|
util.set_env_log(False)
|
2017-10-01 22:04:32 +03:00
|
|
|
data_path = util.ensure_path(data_path)
|
2017-10-04 01:03:15 +03:00
|
|
|
displacy_path = util.ensure_path(displacy_path)
|
2017-10-01 22:04:32 +03:00
|
|
|
if not data_path.exists():
|
2018-12-08 13:49:43 +03:00
|
|
|
msg.fail("Evaluation data not found", data_path, exits=1)
|
2017-10-04 01:03:15 +03:00
|
|
|
if displacy_path and not displacy_path.exists():
|
2018-12-08 13:49:43 +03:00
|
|
|
msg.fail("Visualization output directory not found", displacy_path, exits=1)
|
2017-10-01 22:04:32 +03:00
|
|
|
corpus = GoldCorpus(data_path, data_path)
|
|
|
|
nlp = util.load_model(model)
|
2017-10-03 17:15:35 +03:00
|
|
|
dev_docs = list(corpus.dev_docs(nlp, gold_preproc=gold_preproc))
|
|
|
|
begin = timer()
|
|
|
|
scorer = nlp.evaluate(dev_docs, verbose=False)
|
|
|
|
end = timer()
|
|
|
|
nwords = sum(len(doc_gold[0]) for doc_gold in dev_docs)
|
2018-11-30 22:16:14 +03:00
|
|
|
results = {
|
2018-12-28 13:14:28 +03:00
|
|
|
"Time": "%.2f s" % (end - begin),
|
2018-11-30 22:16:14 +03:00
|
|
|
"Words": nwords,
|
2018-12-28 13:14:28 +03:00
|
|
|
"Words/s": "%.0f" % (nwords / (end - begin)),
|
2018-11-30 22:16:14 +03:00
|
|
|
"TOK": "%.2f" % scorer.token_acc,
|
|
|
|
"POS": "%.2f" % scorer.tags_acc,
|
|
|
|
"UAS": "%.2f" % scorer.uas,
|
|
|
|
"LAS": "%.2f" % scorer.las,
|
|
|
|
"NER P": "%.2f" % scorer.ents_p,
|
|
|
|
"NER R": "%.2f" % scorer.ents_r,
|
|
|
|
"NER F": "%.2f" % scorer.ents_f,
|
|
|
|
}
|
|
|
|
msg.table(results, title="Results")
|
|
|
|
|
2017-10-04 01:03:15 +03:00
|
|
|
if displacy_path:
|
|
|
|
docs, golds = zip(*dev_docs)
|
2018-11-30 22:16:14 +03:00
|
|
|
render_deps = "parser" in nlp.meta.get("pipeline", [])
|
|
|
|
render_ents = "ner" in nlp.meta.get("pipeline", [])
|
|
|
|
render_parses(
|
|
|
|
docs,
|
|
|
|
displacy_path,
|
|
|
|
model_name=model,
|
|
|
|
limit=displacy_limit,
|
|
|
|
deps=render_deps,
|
|
|
|
ents=render_ents,
|
|
|
|
)
|
2018-12-08 13:49:43 +03:00
|
|
|
msg.good("Generated {} parses as HTML".format(displacy_limit), displacy_path)
|
2019-04-15 13:04:36 +03:00
|
|
|
if return_scores:
|
|
|
|
return scorer.scores
|
2017-10-01 22:04:32 +03:00
|
|
|
|
|
|
|
|
2018-11-30 22:16:14 +03:00
|
|
|
def render_parses(docs, output_path, model_name="", limit=250, deps=True, ents=True):
|
|
|
|
docs[0].user_data["title"] = model_name
|
2017-10-04 01:03:15 +03:00
|
|
|
if ents:
|
2018-11-30 22:16:14 +03:00
|
|
|
with (output_path / "entities.html").open("w") as file_:
|
|
|
|
html = displacy.render(docs[:limit], style="ent", page=True)
|
2017-10-04 01:03:15 +03:00
|
|
|
file_.write(html)
|
|
|
|
if deps:
|
2018-11-30 22:16:14 +03:00
|
|
|
with (output_path / "parses.html").open("w") as file_:
|
|
|
|
html = displacy.render(
|
|
|
|
docs[:limit], style="dep", page=True, options={"compact": True}
|
|
|
|
)
|
2017-10-04 01:03:15 +03:00
|
|
|
file_.write(html)
|