mutual.py 4.09 KB
Newer Older
Jonathan Tow's avatar
Jonathan Tow committed
1
2
3
4
"""
MuTual: A Dataset for Multi-Turn Dialogue Reasoning
https://www.aclweb.org/anthology/2020.acl-main.130/

5
6
7
8
9
MuTual is a retrieval-based dataset for multi-turn dialogue reasoning, which is
modified from Chinese high school English listening comprehension test data.

Homepage: https://github.com/Nealcly/MuTual

Jonathan Tow's avatar
Jonathan Tow committed
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
@inproceedings{mutual,
    title = "MuTual: A Dataset for Multi-Turn Dialogue Reasoning",
    author = "Cui, Leyang  and Wu, Yu and Liu, Shujie and Zhang, Yue and Zhou, Ming" ,
    booktitle = "Proceedings of the 58th Conference of the Association for Computational Linguistics",
    year = "2020",
    publisher = "Association for Computational Linguistics",
}
"""
import json
import zipfile
import shutil
import numpy as np
from pathlib import Path
from lm_eval.base import Task, rf
from lm_eval.metrics import mean
25
from best_download import download_file
Jonathan Tow's avatar
Jonathan Tow committed
26
27
28


class MuTualBase(Task):
29
    VERSION = 1
Jonathan Tow's avatar
Jonathan Tow committed
30
31
32
33
34
35
36
37
38
39
40
    BASE_PATH = Path("data/mutual")
    DATASET_NAME = None
    CHOICES = ['A', 'B', 'C', 'D']

    def __init__(self):
        super().__init__()

    def download(self):
        if self.BASE_PATH.exists():
            return
        Path.mkdir(self.BASE_PATH, parents=True)
Jonathan Tow's avatar
Jonathan Tow committed
41
        master_zip = Path("data/master.zip")
42
43
        download_file(
            "https://github.com/Nealcly/MuTual/archive/master.zip",
44
45
            local_file=str(master_zip),
            expected_checksum="bb325cf6c672f0f02699993a37138b0fa0af6fcfc77ec81dfbe46add4d7b29f9")
Jonathan Tow's avatar
Jonathan Tow committed
46
47
48
        with zipfile.ZipFile(master_zip, 'r') as zip:
            zip.extractall("data")
        Path("data/MuTual-master/data").rename(str(self.BASE_PATH))
Jonathan Tow's avatar
Jonathan Tow committed
49
        # Remove left over files and directories.
Jonathan Tow's avatar
Jonathan Tow committed
50
        master_zip.unlink()
Jonathan Tow's avatar
Jonathan Tow committed
51
        shutil.rmtree("data/MuTual-master")
Jonathan Tow's avatar
Jonathan Tow committed
52
53
54
55
56
57
58
59
60
61
62

    def has_training_docs(self):
        return True

    def has_validation_docs(self):
        return True

    def has_test_docs(self):
        return False

    def _load_docs(self, path):
Leo Gao's avatar
Leo Gao committed
63
        for file in sorted(path.iterdir()):
Jonathan Tow's avatar
Jonathan Tow committed
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
            if file.suffix != ".txt":
                continue
            with open(file, 'r', encoding='utf-8') as f:
                yield json.load(f)

    def training_docs(self):
        return self._load_docs(self.BASE_PATH / self.DATASET_NAME / "train")

    def validation_docs(self):
        return self._load_docs(self.BASE_PATH / self.DATASET_NAME / "dev")

    def test_docs(self):
        return NotImplemented

    def doc_to_text(self, doc):
Jonathan Tow's avatar
Jonathan Tow committed
79
        return self.detokenize(doc["article"])
Jonathan Tow's avatar
Jonathan Tow committed
80
81

    def doc_to_target(self, doc):
Jonathan Tow's avatar
Jonathan Tow committed
82
        return " " + self.detokenize(doc["options"][self.CHOICES.index(doc["answers"])])
Jonathan Tow's avatar
Jonathan Tow committed
83
84
85
86

    def construct_requests(self, doc, ctx):
        lls = []
        for option in doc["options"]:
87
            lls.append(rf.loglikelihood(ctx, f" {self.detokenize(option)}")[0])
Jonathan Tow's avatar
Jonathan Tow committed
88
89
        return lls

Jonathan Tow's avatar
Jonathan Tow committed
90
91
92
93
94
95
96
97
98
99
100
101
102
103
    def detokenize(self, text):
        text = text.replace(" '", "'")
        text = text.replace(" \n", "\n")
        text = text.replace("\n ", "\n")
        text = text.replace(" n't", "n't")
        text = text.replace("`` ", '"')
        text = text.replace("''", '"')
        # punctuation
        text = text.replace(" :", ":")
        text = text.replace(" ;", ";")
        text = text.replace(" !", "!")
        text = text.replace(" ?", "?")
        text = text.replace(" ,", ",")
        text = text.replace(" .", ".")
Leo Gao's avatar
Leo Gao committed
104
        return text
Jonathan Tow's avatar
Jonathan Tow committed
105

Jonathan Tow's avatar
Jonathan Tow committed
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
    def process_results(self, doc, results):
        gold = self.CHOICES.index(doc["answers"])
        r4_1 = np.argmax(results) == gold  # r4_1 = accuracy
        ranks = sorted(results, reverse=True)
        r4_2 = (ranks.index(results[gold]) == 1) + r4_1
        mrr = 1. / (ranks.index(results[gold]) + 1)  # `+ 1` for index offset
        return {
            "r@1": r4_1,
            "r@2": r4_2,
            "mrr": mrr
        }

    def aggregation(self):
        return {
            "r@1": mean,
            "r@2": mean,
            "mrr": mean
        }

    def higher_is_better(self):
        return {
            "r@1": True,
            "r@2": True,
            "mrr": True
        }


class MuTual(MuTualBase):
    DATASET_NAME = Path("mutual")


class MuTualPlus(MuTualBase):
    DATASET_NAME = Path("mutual_plus")