julianrisch commited on
Commit
45ff17a
1 Parent(s): 7de93e3

Create germandpr.py

Browse files
Files changed (1) hide show
  1. germandpr.py +118 -0
germandpr.py ADDED
@@ -0,0 +1,118 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # coding=utf-8
2
+ # Lint as: python3
3
+ """GermanDPR: A German-Language Dataset for Training Dense Passage Retrievers."""
4
+
5
+
6
+ import json
7
+
8
+ import datasets
9
+
10
+
11
+ logger = datasets.logging.get_logger(__name__)
12
+
13
+
14
+ _CITATION = """\\
15
+ @misc{möller2021germanquad,
16
+ title={GermanQuAD and GermanDPR: Improving Non-English Question Answering and Passage Retrieval},
17
+ author={Timo Möller and Julian Risch and Malte Pietsch},
18
+ year={2021},
19
+ eprint={2104.12741},
20
+ archivePrefix={arXiv},
21
+ primaryClass={cs.CL}
22
+ }
23
+ """
24
+
25
+ _DESCRIPTION = """\\
26
+ We take GermanQuAD as a starting point and add hard negatives from a dump of the full German Wikipedia following the approach of the DPR authors (Karpukhin et al., 2020). The format of the dataset also resembles the one of DPR. GermanDPR comprises 9275 question/answer pairs in the training set and 1025 pairs in the test set. For each pair, there are one positive context and three hard negative contexts.
27
+ """
28
+
29
+ _URL = "https://germanquad.s3.amazonaws.com/GermanDPR.zip"
30
+ _FILES = {
31
+ "train": "GermanDPR_train.json",
32
+ "test": "GermanDPR_test.json",
33
+ }
34
+
35
+
36
+ class GermanDPRConfig(datasets.BuilderConfig):
37
+ """BuilderConfig for GermanDPR."""
38
+
39
+ def __init__(self, **kwargs):
40
+ """BuilderConfig for GermanDPR.
41
+
42
+ Args:
43
+ **kwargs: keyword arguments forwarded to super.
44
+ """
45
+ super(GermanDPRConfig, self).__init__(**kwargs)
46
+
47
+
48
+ class GermanDPR(datasets.GeneratorBasedBuilder):
49
+ """GermanDPR: A German-Language Dataset for Training Dense Passage Retrievers."""
50
+
51
+ BUILDER_CONFIGS = [
52
+ GermanDPRConfig(
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
+ download_urls = _URL
98
+ dl_dir = dl_manager.download_and_extract(download_urls)
99
+ return [
100
+ datasets.SplitGenerator(
101
+ name=datasets.Split.TRAIN,
102
+ # These kwargs will be passed to _generate_examples
103
+ gen_kwargs={"filepath": "GermanDPR_train.json"},
104
+ ),
105
+ datasets.SplitGenerator(
106
+ name=datasets.Split.TEST,
107
+ # These kwargs will be passed to _generate_examples
108
+ gen_kwargs={"filepath": "GermanDPR_test.json"},
109
+ ),
110
+ ]
111
+
112
+ def _generate_examples(self, filepath):
113
+ """This function returns the examples in the raw (text) form."""
114
+ logger.info("generating examples from = %s", filepath)
115
+ with open(filepath, encoding="utf-8") as f:
116
+ germandpr = json.load(f)
117
+ for qa in germandpr:
118
+ yield qa