Commit
·
f958b34
1
Parent(s):
4ee625a
Delete triviaQARC.py
Browse files- triviaQARC.py +0 -109
triviaQARC.py
DELETED
|
@@ -1,109 +0,0 @@
|
|
| 1 |
-
from collections import Counter
|
| 2 |
-
import datasets
|
| 3 |
-
|
| 4 |
-
class triviaQARC(datasets.Metric):
|
| 5 |
-
def _info(self):
|
| 6 |
-
return datasets.MetricInfo(
|
| 7 |
-
description= "Idk",
|
| 8 |
-
citation= "idk",
|
| 9 |
-
features=datasets.Features(
|
| 10 |
-
{
|
| 11 |
-
"predictions": {"id": datasets.Value("string"), "prediction_text": datasets.Value("string")},
|
| 12 |
-
"references": {
|
| 13 |
-
"id": datasets.Value("string"),
|
| 14 |
-
"answers": datasets.features.Sequence(
|
| 15 |
-
{
|
| 16 |
-
"text": datasets.Value("string"),
|
| 17 |
-
"answer_start": datasets.Value("int32"),
|
| 18 |
-
}
|
| 19 |
-
),
|
| 20 |
-
},
|
| 21 |
-
}
|
| 22 |
-
),
|
| 23 |
-
)
|
| 24 |
-
|
| 25 |
-
def _compute(self, predictions, references):
|
| 26 |
-
pred_dict = {prediction["id"]: prediction["prediction_text"] for prediction in predictions}
|
| 27 |
-
dataset = [
|
| 28 |
-
{
|
| 29 |
-
"paragraphs": [
|
| 30 |
-
{
|
| 31 |
-
"qas": [
|
| 32 |
-
{
|
| 33 |
-
"answers": [{"text": answer_text} for answer_text in ref["answers"]["text"]],
|
| 34 |
-
"id": ref["id"],
|
| 35 |
-
}
|
| 36 |
-
for ref in references
|
| 37 |
-
]
|
| 38 |
-
}
|
| 39 |
-
]
|
| 40 |
-
}
|
| 41 |
-
]
|
| 42 |
-
score = evaluate(dataset=dataset, predictions=pred_dict)
|
| 43 |
-
return score
|
| 44 |
-
|
| 45 |
-
def evaluate(dataset, predictions):
|
| 46 |
-
f1 = exact_match = total = recall = precision= 0
|
| 47 |
-
for article in dataset:
|
| 48 |
-
for paragraph in article["paragraphs"]:
|
| 49 |
-
for qa in paragraph["qas"]:
|
| 50 |
-
total += 1
|
| 51 |
-
if qa["id"] not in predictions:
|
| 52 |
-
message = "Unanswered question " + qa["id"] + " will receive score 0."
|
| 53 |
-
print(message, file=sys.stderr)
|
| 54 |
-
continue
|
| 55 |
-
ground_truths = list(map(lambda x: x["text"], qa["answers"]))
|
| 56 |
-
prediction = predictions[qa["id"]]
|
| 57 |
-
exact_match += metric_max_over_ground_truths(exact_match_score, prediction, ground_truths)
|
| 58 |
-
temp_f1, temp_precision, temp_recall = metric_max_over_ground_truths(f1_score, prediction, ground_truths)
|
| 59 |
-
f1 += temp_f1
|
| 60 |
-
precision += temp_precision
|
| 61 |
-
recall += temp_recall
|
| 62 |
-
|
| 63 |
-
exact_match = 100.0 * exact_match / total
|
| 64 |
-
f1 = 100.0 * f1 / total
|
| 65 |
-
|
| 66 |
-
return {"exact_match": exact_match, "f1": f1, "recall": recall, "precision": precision}
|
| 67 |
-
|
| 68 |
-
def normalize_answer(s):
|
| 69 |
-
"""Lower text and remove punctuation, articles and extra whitespace."""
|
| 70 |
-
|
| 71 |
-
def remove_articles(text):
|
| 72 |
-
return re.sub(r"\b(a|an|the)\b", " ", text)
|
| 73 |
-
|
| 74 |
-
def white_space_fix(text):
|
| 75 |
-
return " ".join(text.split())
|
| 76 |
-
|
| 77 |
-
def remove_punc(text):
|
| 78 |
-
exclude = set(string.punctuation)
|
| 79 |
-
return "".join(ch for ch in text if ch not in exclude)
|
| 80 |
-
|
| 81 |
-
def lower(text):
|
| 82 |
-
return text.lower()
|
| 83 |
-
|
| 84 |
-
return white_space_fix(remove_articles(remove_punc(lower(s))))
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
def f1_score(prediction, ground_truth):
|
| 88 |
-
prediction_tokens = normalize_answer(prediction).split()
|
| 89 |
-
ground_truth_tokens = normalize_answer(ground_truth).split()
|
| 90 |
-
common = Counter(prediction_tokens) & Counter(ground_truth_tokens)
|
| 91 |
-
num_same = sum(common.values())
|
| 92 |
-
if num_same == 0:
|
| 93 |
-
return 0
|
| 94 |
-
precision = 1.0 * num_same / len(prediction_tokens)
|
| 95 |
-
recall = 1.0 * num_same / len(ground_truth_tokens)
|
| 96 |
-
f1 = (2 * precision * recall) / (precision + recall)
|
| 97 |
-
return f1, precision, recall
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
def exact_match_score(prediction, ground_truth):
|
| 101 |
-
return normalize_answer(prediction) == normalize_answer(ground_truth)
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
def metric_max_over_ground_truths(metric_fn, prediction, ground_truths):
|
| 105 |
-
scores_for_ground_truths = []
|
| 106 |
-
for ground_truth in ground_truths:
|
| 107 |
-
score = metric_fn(prediction, ground_truth)
|
| 108 |
-
scores_for_ground_truths.append(score)
|
| 109 |
-
return max(scores_for_ground_truths)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|