num_exact_match / num_exact_match.py
Luluuu's picture
Update num_exact_match.py
c561835 verified
import re
import datasets
import evaluate
_DESCRIPTION = """
Returns the rate at which the input predicted strings exactly match their references, ignoring any strings input as part of the regexes_to_ignore list.
"""
_KWARGS_DESCRIPTION = """
Args:
predictions: List of predicted texts.
references: List of reference texts.
Returns:
num_exact_match: List of number accuracy scores, one for each sentence in `predictions`. Possible values are between 0.0 and 2.0, inclusive.
Examples:
>>> num_exact_match = evaluate.load("num_exact_match")
>>> preds = ["Re Reality TV Star Diem Brown Dead at 32 galvanized Against Cancer", "Boy denies Medicaid coverage; 4-Month-Old Buys $1'"]
>>> refs = ["Reality TV Star Dies of Cancer at 32", "'Obese' 4-Month-Old Denied Insurance"]
>>> results = num_exact_match.compute(predictions=preds, references=refs)
>>> print(results["num_exact_match"])
[2.0, 1.0]
"""
_CITATION = """
"""
@evaluate.utils.file_utils.add_start_docstrings(_DESCRIPTION, _KWARGS_DESCRIPTION)
class NumExactMatch(evaluate.Metric):
def _info(self):
return evaluate.MetricInfo(
description=_DESCRIPTION,
citation=_CITATION,
inputs_description=_KWARGS_DESCRIPTION,
features=datasets.Features(
{
"predictions": datasets.Value("string", id="sequence"),
"references": datasets.Value("string", id="sequence"),
}
),
reference_urls=[],
)
def _compute(
self,
predictions,
references,
):
score_list = []
for p, r in zip(predictions, references):
# print(p)
# print(r)
num_in_pred = re.findall(r'\d+', p)
num_in_ground_truth = re.findall(r'\d+', r)
# print(num_in_pred)
# print(num_in_ground_truth)
if(num_in_pred == num_in_ground_truth):
# print("全對")
score_list.append(2.0)
else:
common_num = set(num_in_pred) & set(num_in_ground_truth)
if(common_num):
# print("半對")
score_list.append(1.0)
else:
# print("ε…¨ιŒ―")
score_list.append(0)
return {"num_exact_match": score_list}