File size: 2,367 Bytes
9480091
 
 
66a2af6
9480091
 
 
 
66a2af6
 
9480091
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import json

import datasets
from datasets import Features, Sequence, Array2D, Value
from datasets.info import DatasetInfo


_DESCRIPTION = """\
GQA is a dataset containing 58K questions about subgraphs extracted from Wikidata.
The data are made from Lc-QuAD 2.0 and MCWQ datasets.
"""

_URLS = {
    "train": "train.jsonl",
    "validation": "validation.jsonl",
    "test": "test.jsonl",
}

class GQAConfig(datasets.BuilderConfig):
    """BuilderConfig for GQA."""

    def __init__(self, **kwargs):
        """BuilderConfig for GQA.
        Args:
          **kwargs: keyword arguments forwarded to super.
        """
        super(GQAConfig, self).__init__(**kwargs)


class GQA(datasets.GeneratorBasedBuilder):
    """GQA: A graph question answering dataset."""
    def _info(self) -> DatasetInfo:
        return DatasetInfo(
            description=_DESCRIPTION,
            features=Features(
                {
                    "id": Value("string"),
                    "question": Value("string"),
                    "answers": Sequence(Value("string")),
                    "sparql": Value("string"),
                    "subgraph": 
                        {
                            "entities": Sequence(Value("string")),
                            "relations": Sequence(Value("string")),
                            "adjacency": Array2D(shape=(None, 3), dtype='int64'),
                            "entity_labels": Sequence(datasets.Value("string")),
                            "relation_labels": Sequence(Value("string")),
                        }
                }
            )
        )

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

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

    def _generate_examples(self, filepath):
        with open(filepath, encoding="utf-8") as f:
            for row in f:
                sample = json.loads(row)
                id_ = sample["id"]
                yield id_, sample