utils.py 865 Bytes
Newer Older
JessicaOjo's avatar
JessicaOjo committed
1
from sklearn.metrics import f1_score
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


def doc_to_text(doc):
    output = """Please identify whether the premise entails or contradicts the hypothesis in the following premise 
    and hypothesis. The answer should be exact entailment, contradiction, or neutral.
    
    Premise: {premise}
    Hypothesis: {hypothesis}
    
    Is it entailment, contradiction, or neutral?"""

    text = output.format(premise=doc['premise'],
                         hypothesis=doc['hypothesis'])
    return text


def doc_to_target(doc):
    replacements = {
        0: 'entailment',
        1: 'neutral',
        2: 'contradiction'
    }
    return replacements[doc["label"]]


def weighted_f1_score(items):
    unzipped_list = list(zip(*items))
    golds = unzipped_list[0]
    preds = unzipped_list[1]
    fscore = f1_score(golds, preds, average="weighted")
    return fscore