utils.py 801 Bytes
Newer Older
1
2
3
4
from sklearn.metrics import f1_score


def doc_to_text(doc):
Lintang Sutawika's avatar
Lintang Sutawika committed
5
    output = """Please identify whether the premise entails or contradicts the hypothesis in the following premise
6
    and hypothesis. The answer should be exact entailment, contradiction, or neutral.
Lintang Sutawika's avatar
Lintang Sutawika committed
7

8
9
    Premise: {premise}
    Hypothesis: {hypothesis}
Lintang Sutawika's avatar
Lintang Sutawika committed
10

11
12
    Is it entailment, contradiction, or neutral?"""

Lintang Sutawika's avatar
Lintang Sutawika committed
13
    text = output.format(premise=doc["premise"], hypothesis=doc["hypothesis"])
14
15
16
17
    return text


def doc_to_target(doc):
Lintang Sutawika's avatar
Lintang Sutawika committed
18
    replacements = {0: "entailment", 1: "neutral", 2: "contradiction"}
19
20
21
22
23
24
25
26
27
    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