File size: 2,887 Bytes
cea7647
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import os
import pandas as pd
import datasets

_DATA_URLS = {
    "train": "./ko_wiki_v1_squad.json",
}
_VERSION = "0.0.0"
_LICENSE = ""
_CITATION = ""
_HOMEPAGE = ""
_DESCRIPTION = ""


class CommonSenseMRCConfig(datasets.BuilderConfig):
    def __init__(self, data_url, features, **kwargs):
        super().__init__(version=datasets.Version(_VERSION, ""), **kwargs)
        self.features = features
        self.data_url = data_url
        

class CommonSenseMRC(datasets.GeneratorBasedBuilder):
    DEFAULT_CONFIG_NAME = "common_sense"
    BUILDER_CONFIGS = [
        CommonSenseMRCConfig(
            name="common_sense",
            data_url=_DATA_URLS,
            features=datasets.Features(
                {
                    "answers": datasets.Sequence(
                        feature={
                            "text": datasets.Value(dtype="string"),
                            "answer_start": datasets.Value(dtype="int32"),
                        },
                    ),
                    "context": datasets.Value(dtype="string"),
                    "guid": datasets.Value(dtype="string"),
                    "question": datasets.Value(dtype="string"),
                    "title": datasets.Value(dtype="string"),
                }
            )
        ),
    ]
    
    def _info(self):
        return datasets.DatasetInfo(
            features=self.config.features,
            description=_DESCRIPTION,
            homepage=_HOMEPAGE,
            citation=_CITATION,
            license=_LICENSE,
        )
    
    def _split_generators(self, dl_manager):
        data_path = dl_manager.download_and_extract(self.config.data_url)
        return [
            datasets.SplitGenerator(
                name=datasets.Split.TRAIN,
                # These kwargs will be passed to _generate_examples
                gen_kwargs={"data_file": data_path["train"]}
            ),
        ]
    
    def _generate_examples(self, data_file: str):
        idx = 0
        common_sense_mrc = pd.read_json(data_file)
        for example in common_sense_mrc["data"].tolist():
            paragraphs = example["paragraphs"]
            title = example["title"]
            for paragraph in paragraphs:
                qas = paragraph["qas"]
                context = paragraph["context"]
                for qa in qas:
                    text = [answers["text"] for answers in qa["answers"]]
                    answer_start = [answers["answer_start"] for answers in qa["answers"]]
                    features = {
                        "guid": str(qa["id"]),
                        "question": qa["question"],
                        "answers": {"text": text, "answer_start": answer_start},
                        "context": context,
                        "title": title,
                    }
                    yield idx, features
                    idx += 1