18
from .evaluate import evaluate
22
@inproceedings{Rajpurkar2016SQuAD10,
23
title={SQuAD: 100, 000+ Questions for Machine Comprehension of Text},
24
author={Pranav Rajpurkar and Jian Zhang and Konstantin Lopyrev and Percy Liang},
31
This metric wrap the official scoring script for version 1 of the Stanford Question Answering Dataset (SQuAD).
33
Stanford Question Answering Dataset (SQuAD) is a reading comprehension dataset, consisting of questions posed by
34
crowdworkers on a set of Wikipedia articles, where the answer to every question is a segment of text, or span,
35
from the corresponding reading passage, or the question might be unanswerable.
38
_KWARGS_DESCRIPTION = """
39
Computes SQuAD scores (F1 and EM).
41
predictions: List of question-answers dictionaries with the following key-values:
42
- 'id': id of the question-answer pair as given in the references (see below)
43
- 'prediction_text': the text of the answer
44
references: List of question-answers dictionaries with the following key-values:
45
- 'id': id of the question-answer pair (see above),
46
- 'answers': a Dict in the SQuAD dataset format
48
'text': list of possible texts for the answer, as a list of strings
49
'answer_start': list of start positions for the answer, as a list of ints
51
Note that answer_start values are not taken into account to compute the metric.
53
'exact_match': Exact match (the normalized answer exactly match the gold answer)
54
'f1': The F-score of predicted tokens versus the gold answer
57
>>> predictions = [{'prediction_text': '1976', 'id': '56e10a3be3433e1400422b22'}]
58
>>> references = [{'answers': {'answer_start': [97], 'text': ['1976']}, 'id': '56e10a3be3433e1400422b22'}]
59
>>> squad_metric = datasets.load_metric("squad")
60
>>> results = squad_metric.compute(predictions=predictions, references=references)
62
{'exact_match': 100.0, 'f1': 100.0}
66
@datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION, _KWARGS_DESCRIPTION)
67
class Squad(datasets.Metric):
69
return datasets.MetricInfo(
70
description=_DESCRIPTION,
72
inputs_description=_KWARGS_DESCRIPTION,
73
features=datasets.Features(
75
"predictions": {"id": datasets.Value("string"), "prediction_text": datasets.Value("string")},
77
"id": datasets.Value("string"),
78
"answers": datasets.features.Sequence(
80
"text": datasets.Value("string"),
81
"answer_start": datasets.Value("int32"),
87
codebase_urls=["https://rajpurkar.github.io/SQuAD-explorer/"],
88
reference_urls=["https://rajpurkar.github.io/SQuAD-explorer/"],
91
def _compute(self, predictions, references):
92
pred_dict = {prediction["id"]: prediction["prediction_text"] for prediction in predictions}
99
"answers": [{"text": answer_text} for answer_text in ref["answers"]["text"]],
102
for ref in references
108
score = evaluate(dataset=dataset, predictions=pred_dict)