File size: 3,964 Bytes
ce4a725
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b4f4ace
ce4a725
 
 
 
 
 
 
 
 
5ab001a
 
ce4a725
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5ab001a
 
 
 
ce4a725
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
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
import json
import random
import string
import warnings
from typing import Dict, List, Optional, Union

import datasets as ds
import pandas as pd

_CITATION = """
@InProceedings{Kurihara_nlp2020,
  author =  "鈴木正敏 and 鈴木潤 and 松田耕史 and ⻄田京介 and 井之上直也",
  title =   "JAQKET: クイズを題材にした日本語 QA データセットの構築",
  booktitle =   "言語処理学会第26回年次大会",
  year =    "2020",
  url = "https://www.anlp.jp/proceedings/annual_meeting/2020/pdf_dir/P2-24.pdf"
  note= "in Japanese"
"""

_DESCRIPTION = """\
JAQKET: JApanese Questions on Knowledge of EnTitie
"""

_HOMEPAGE = "https://sites.google.com/view/project-aio/dataset"

_LICENSE = """\
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.
"""

_DESCRIPTION_CONFIGS = {
    "v2.0": "v2.0",
}

_URLS = {
    "v2.0": {
        "train": "https://huggingface.co/datasets/kumapo/JAQKET/resolve/main/train_jaqket_59.350.json",
        "valid": "https://huggingface.co/datasets/kumapo/JAQKET/resolve/main/dev_jaqket_59.350.json",
    },
}

def dataset_info_v2() -> ds.Features:
    features = ds.Features(
        {
            "qid": ds.Value("string"),
            "question": ds.Value("string"),
            "answers": ds.Sequence({
                "text": ds.Value("string"),
                "answer_start": ds.Value("int32"),
            }),
            "ctxs": ds.Sequence({
                "id": ds.Value("string"),
                "title": ds.Value("string"),
                "text": ds.Value("string"),
                "score": ds.Value("float32"),
                "has_answer": ds.Value("bool"),
            })
        }
    )
    return ds.DatasetInfo(
        description=_DESCRIPTION,
        citation=_CITATION,
        homepage=_HOMEPAGE,
        license=_LICENSE,
        features=features,
    )


class JAQKET(ds.GeneratorBasedBuilder):
    VERSION = ds.Version("0.1.0")
    BUILDER_CONFIGS = [
        ds.BuilderConfig(
            name="v2.0",
            version=VERSION,
            description=_DESCRIPTION_CONFIGS["v2.0"],
        ),
    ]

    def _info(self) -> ds.DatasetInfo:
        if self.config.name == "v2.0":
            return dataset_info_v2()
        else:
            raise ValueError(f"Invalid config name: {self.config.name}")

    def _split_generators(self, dl_manager: ds.DownloadManager):
        file_paths = dl_manager.download_and_extract(_URLS[self.config.name])
        return [
            ds.SplitGenerator(
                name=ds.Split.TRAIN,
                gen_kwargs={"file_path": file_paths["train"]},
            ),
            ds.SplitGenerator(
                name=ds.Split.VALIDATION,
                gen_kwargs={"file_path": file_paths["valid"]},
            ),
        ]

    def _generate_examples(
        self,
        file_path: Optional[str] = None,
        split_df: Optional[pd.DataFrame] = None,
    ):
        if file_path is None:
            raise ValueError(f"Invalid argument for {self.config.name}")

        with open(file_path, "r") as rf:
            json_data = json.load(rf)

        for json_dict in json_data:
            q_id = json_dict["qid"]
            question = json_dict["question"]
            answers = [
                {"text": answer, "answer_start": -1 } # -1: dummy
                for answer in json_dict["answers"]
            ]
            ctxs = [
                {
                    "id": ctx["id"],
                    "title": ctx["title"],
                    "text": ctx["text"],
                    "score": float(ctx["score"]),
                    "has_answer": ctx["has_answer"]

                }
                for ctx in json_dict["ctxs"]
            ]
            example_dict = {
                "qid": q_id,
                "question": question,
                "answers": answers,
                "ctxs": ctxs
            }
            yield q_id, example_dict