File size: 2,398 Bytes
61cad28
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c534252
 
61cad28
 
 
7dbac05
4baaac0
61cad28
 
c534252
61cad28
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c561835
 
61cad28
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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}