utils.py 1.36 KB
Newer Older
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
from itertools import chain

from sklearn.metrics import accuracy_score

from lm_eval.utils import weighted_f1_score


def doc_to_target(doc):
    pos_tag_map = {
        0: "NOUN",
        1: "PUNCT",
        2: "ADP",
        3: "NUM",
        4: "SYM",
        5: "SCONJ",
        6: "ADJ",
        7: "PART",
        8: "DET",
        9: "CCONJ",
        10: "PROPN",
        11: "PRON",
        12: "X",
        13: "_",
        14: "ADV",
        15: "INTJ",
        16: "VERB",
        17: "AUX",
    }
    return [pos_tag_map[tag] for tag in doc["upos"]]


def acc_score(items):
    unzipped_list = list(zip(*items))

    golds, preds = unzipped_list[0], unzipped_list[1]

    # Flatten preds' inner lists
    flattened_preds = [list(chain.from_iterable(p)) for p in preds]

    # Calculate the accuracy for each gold-pred pair
    accuracy_scores = []
    for gold, pred in zip(golds, flattened_preds):
        # Ensure both lists are of the same length, otherwise truncate to match
        min_length = min(len(gold), len(pred))
        gold = gold[:min_length]
        pred = pred[:min_length]

        # Calculate accuracy for the current pair and add to the list
        accuracy = accuracy_score(gold, pred)
        accuracy_scores.append(accuracy)

    mean_accuracy = (
        sum(accuracy_scores) / len(accuracy_scores) if accuracy_scores else 0
    )
    return mean_accuracy