"README-ZH.md" did not exist on "9806f79185e9ef80cb7447884e76a5ea58fe8afe"
metrics.py 1.22 KB
Newer Older
lintangsutawika's avatar
format  
lintangsutawika committed
1
import re
lintangsutawika's avatar
lintangsutawika committed
2
3
import string

lintangsutawika's avatar
format  
lintangsutawika committed
4
5
6
from collections import Counter


lintangsutawika's avatar
lintangsutawika committed
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
def normalize_answer(s):
    """
    Taken from the official evaluation script for v1.1 of the SQuAD dataset.
    Lower text and remove punctuation, articles and extra whitespace.
    """

    def remove_articles(text):
        return re.sub(r"\b(a|an|the)\b", " ", text)

    def white_space_fix(text):
        return " ".join(text.split())

    def remove_punc(text):
        exclude = set(string.punctuation)
        return "".join(ch for ch in text if ch not in exclude)

    def lower(text):
        return text.lower()

    return white_space_fix(remove_articles(remove_punc(lower(s))))

lintangsutawika's avatar
format  
lintangsutawika committed
28

lintangsutawika's avatar
lintangsutawika committed
29
def f1_abstractive(predictions, references):
lintangsutawika's avatar
lintangsutawika committed
30
31
32
    """
    Taken from the official evaluation script for v1.1 of the SQuAD dataset.
    """
lintangsutawika's avatar
lintangsutawika committed
33
34
35
    prediction_tokens = normalize_answer(predictions[0]).split()
    references_tokens = normalize_answer(references[0]).split()
    common = Counter(prediction_tokens) & Counter(references_tokens)
lintangsutawika's avatar
lintangsutawika committed
36
37
38
39
    num_same = sum(common.values())
    if num_same == 0:
        return 0
    precision = 1.0 * num_same / len(prediction_tokens)
lintangsutawika's avatar
lintangsutawika committed
40
    recall = 1.0 * num_same / len(references_tokens)
lintangsutawika's avatar
lintangsutawika committed
41
    f1 = (2 * precision * recall) / (precision + recall)
lintangsutawika's avatar
lintangsutawika committed
42
    return f1