File size: 4,183 Bytes
08cf776
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
afaad9d
 
 
 
 
08cf776
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7ff48d8
08cf776
 
 
7ff48d8
08cf776
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
afaad9d
08cf776
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""KQA Pro: A large-scale, diverse, challenging dataset of complex question answering over knowledge base."""

import json
import os

import datasets

logger = datasets.logging.get_logger(__name__)

_CITATION = """\
@inproceedings{KQAPro,
    title={{KQA P}ro: A Large Diagnostic Dataset for Complex Question Answering over Knowledge Base},
    author={Cao, Shulin and Shi, Jiaxin and Pan, Liangming and Nie, Lunyiu and Xiang, Yutong and Hou, Lei and Li, Juanzi and He, Bin and Zhang, Hanwang},
    booktitle={ACL'22},
    year={2022}
    }
"""

_DESCRIPTION = """\
A large-scale, diverse, challenging dataset of complex question answering over knowledge base.
"""

_URL = "https://thukeg.gitee.io/kqa-pro/"
_DOWNLOAD_URL = "https://cloud.tsinghua.edu.cn/f/df54ff66d1dc4ca7823e/?dl=1"
_URLS = {
    "train": "train.json",
    "val": "val.json",
    "test": "test.json"
}

_TRAIN_CONFIG_NAME = "train_val"
_TEST_CONFIG_NAME = "test"

class KQAProConfig(datasets.BuilderConfig):
    """BuilderConfig for KQA Pro."""

    def __init__(self, **kwargs):
        """BuilderConfig for KQA Pro.
        Args:
          **kwargs: keyword arguments forwarded to super.
        """
        super(KQAProConfig, self).__init__(**kwargs)


class KQAPro(datasets.GeneratorBasedBuilder):
    """KQAPro: A large scale knowledge-based question answering dataset."""

    BUILDER_CONFIGS = [
        KQAProConfig(
            name=_TRAIN_CONFIG_NAME,
            description="KQA Pro"
        ),
        KQAProConfig(
            name=_TEST_CONFIG_NAME,
            description="KQA Pro"
        ),
    ]


    def _info(self):
        if self.config.name == _TEST_CONFIG_NAME:
            return datasets.DatasetInfo(
                description=_DESCRIPTION,
                features=datasets.Features(
                    {
                        "question": datasets.Value("string"),
                        "choices": datasets.features.Sequence(datasets.Value("string")),
                    }
                ),
                supervised_keys=None,
                homepage=_URL,
                citation=_CITATION,
            )

        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            features=datasets.Features(
                {
                    "question": datasets.Value("string"),
                    "sparql": datasets.Value("string"),
                    "program": datasets.features.Sequence(
                        {
                            "function": datasets.Value("string"),
                            "dependencies":  datasets.features.Sequence(datasets.Value("int32")),
                            "inputs": datasets.features.Sequence(datasets.Value("string"))
                        }
                    ),
                    "choices": datasets.features.Sequence(datasets.Value("string")),
                    "answer": datasets.Value("string")
                }
            ),
            # No default supervised_keys (as we have to pass both question
            # and context as input).
            supervised_keys=None,
            homepage=_URL,
            citation=_CITATION,
        )


    def _split_generators(self, dl_manager):
        downloaded_files = dl_manager.download_and_extract(_URLS)

        if self.config.name == _TEST_CONFIG_NAME:
            return [
                datasets.SplitGenerator(name=datasets.Split.TEST, gen_kwargs={
                                        "filepath": downloaded_files["test"]})
            ]

        return [
            datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={
                                    "filepath": downloaded_files["train"]}),
            datasets.SplitGenerator(name=datasets.Split.VALIDATION, gen_kwargs={
                                    "filepath": downloaded_files["val"]})
        ]


    def _generate_examples(self, filepath):
        """This function returns the examples in the raw (text) form."""
        logger.info("generating examples from = %s", filepath)
        with open(filepath, encoding="utf-8") as f:
            kqa = json.load(f)
            for idx, sample in enumerate(kqa):
                yield idx, sample