julianrisch commited on
Commit
5ffddac
1 Parent(s): 7cc189d

Create germanquad.py

Browse files
Files changed (1) hide show
  1. germanquad.py +131 -0
germanquad.py ADDED
@@ -0,0 +1,131 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # coding=utf-8
2
+ # Lint as: python3
3
+ """GermanQuAD: A German-Language Question Answering Dataset."""
4
+
5
+ import json
6
+
7
+ import datasets
8
+
9
+
10
+ logger = datasets.logging.get_logger(__name__)
11
+
12
+
13
+ _CITATION = """\\
14
+ @misc{möller2021germanquad,
15
+ title={GermanQuAD and GermanDPR: Improving Non-English Question Answering and Passage Retrieval},
16
+ author={Timo Möller and Julian Risch and Malte Pietsch},
17
+ year={2021},
18
+ eprint={2104.12741},
19
+ archivePrefix={arXiv},
20
+ primaryClass={cs.CL}
21
+ }
22
+ """
23
+
24
+ _DESCRIPTION = """\\
25
+ In order to raise the bar for non-English QA, we are releasing a high-quality, human-labeled German QA dataset consisting of 13 722 questions, incl. a three-way annotated test set.
26
+ The creation of GermanQuAD is inspired by insights from existing datasets as well as our labeling experience from several industry projects. We combine the strengths of SQuAD, such as high out-of-domain performance, with self-sufficient questions that contain all relevant information for open-domain QA as in the NaturalQuestions dataset. Our training and test datasets do not overlap like other popular datasets and include complex questions that cannot be answered with a single entity or only a few words.
27
+ """
28
+
29
+ _URL = "https://germanquad.s3.amazonaws.com/GermanQuAD.zip"
30
+ _FILES = {
31
+ "train": "GermanQuAD_train.json",
32
+ "test": "GermanQuAD_test.json",
33
+ }
34
+
35
+
36
+ class GermanQuADConfig(datasets.BuilderConfig):
37
+ """BuilderConfig for GermanQuAD."""
38
+
39
+ def __init__(self, **kwargs):
40
+ """BuilderConfig for GermanQuAD.
41
+
42
+ Args:
43
+ **kwargs: keyword arguments forwarded to super.
44
+ """
45
+ super(GermanQuADConfig, self).__init__(**kwargs)
46
+
47
+
48
+ class GermanDPR(datasets.GeneratorBasedBuilder):
49
+ """GermanQuAD: A German-Language Question Answering Dataset."""
50
+
51
+ BUILDER_CONFIGS = [
52
+ GermanQuADConfig(
53
+ name="plain_text",
54
+ version=datasets.Version("1.0.0", ""),
55
+ description="Plain text",
56
+ ),
57
+ ]
58
+
59
+ def _info(self):
60
+ return datasets.DatasetInfo(
61
+ description=_DESCRIPTION,
62
+ features=datasets.Features(
63
+ {
64
+ "question": datasets.Value("string"),
65
+ "answers": datasets.features.Sequence(datasets.Value("string")),
66
+ "positive_ctxs": datasets.features.Sequence(
67
+ {
68
+ "title": datasets.Value("string"),
69
+ "text": datasets.Value("string"),
70
+ "passage_id": datasets.Value("string"),
71
+ }
72
+ ),
73
+ "negative_ctxs": datasets.features.Sequence(
74
+ {
75
+ "title": datasets.Value("string"),
76
+ "text": datasets.Value("string"),
77
+ "passage_id": datasets.Value("string"),
78
+ }
79
+ ),
80
+ "hard_negative_ctxs": datasets.features.Sequence(
81
+ {
82
+ "title": datasets.Value("string"),
83
+ "text": datasets.Value("string"),
84
+ "passage_id": datasets.Value("string"),
85
+ }
86
+ ),
87
+ }
88
+ ),
89
+ # No default supervised_keys (as we have to pass both question
90
+ # and context as input).
91
+ supervised_keys=None,
92
+ homepage="https://deepset.ai/germanquad",
93
+ citation=_CITATION,
94
+ )
95
+
96
+ def _split_generators(self, dl_manager):
97
+ downloaded_files = dl_manager.download_and_extract(_URL)
98
+
99
+ return [
100
+ datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"filepath": downloaded_files["train"]}),
101
+ datasets.SplitGenerator(name=datasets.Split.VALIDATION, gen_kwargs={"filepath": downloaded_files["test"]}),
102
+ ]
103
+
104
+ def _generate_examples(self, filepath):
105
+ """This function returns the examples in the raw (text) form."""
106
+ logger.info("generating examples from = %s", filepath)
107
+ with open(filepath, encoding="utf-8") as f:
108
+ germanquad = json.load(f)
109
+ for article in germanquad["data"]:
110
+ for paragraph in article["paragraphs"]:
111
+ context = paragraph["context"]
112
+ document_id = paragraph["document_id"]
113
+ for qa in paragraph["qas"]:
114
+ question = qa["question"]
115
+ id_ = qa["id"]
116
+
117
+ answer_starts = [answer["answer_start"] for answer in qa["answers"]]
118
+ answers = [answer["text"] for answer in qa["answers"]]
119
+
120
+ # Features currently used are "context", "question", and "answers".
121
+ # Others are extracted here for the ease of future expansions.
122
+ yield id_, {
123
+ "context": context,
124
+ "question": question,
125
+ "id": id_,
126
+ "answers": {
127
+ "answer_start": answer_starts,
128
+ "text": answers,
129
+ },
130
+ }
131
+