crystina-z commited on
Commit
17f4a45
β€’
1 Parent(s): 15c5596

Update miracl.py

Browse files
Files changed (1) hide show
  1. miracl.py +142 -0
miracl.py CHANGED
@@ -0,0 +1,142 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # coding=utf-8
2
+ # Copyright 2020 The HuggingFace Datasets Authors and the current dataset script contributor.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the 'License');
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an 'AS IS' BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+
16
+ # Lint as: python3
17
+
18
+ import json
19
+ import datasets
20
+ from collections import defaultdict
21
+ from dataclasses import dataclass
22
+
23
+ _CITATION = '''
24
+ '''
25
+
26
+ languages = ['ar', 'bn', 'en', 'es', 'fa', 'fi', 'fr', 'hi', 'id', 'ja', 'ko', 'ru', 'sw', 'te', 'th', 'zh']
27
+ non_surprise_languages = languages
28
+
29
+ _DESCRIPTION = 'dataset load script for MIRACL'
30
+
31
+ _DATASET_URLS = {
32
+ lang: {
33
+ 'train': [
34
+ f'https://huggingface.co/datasets/miracl/miracl/resolve/main/miracl-v1.0-{lang}/topics/{lang}.topic.train.tsv',
35
+ f'https://huggingface.co/datasets/miracl/miracl/resolve/main/miracl-v1.0-{lang}/qrels/{lang}.qrels.train.tsv',
36
+ ],
37
+ 'dev': [
38
+ f'https://huggingface.co/datasets/miracl/miracl/resolve/main/miracl-v1.0-{lang}/topics/{lang}.topic.dev.tsv',
39
+ f'https://huggingface.co/datasets/miracl/miracl/resolve/main/miracl-v1.0-{lang}/qrels/{lang}.qrels.dev.tsv',
40
+ ]
41
+ } for lang in languages
42
+ }
43
+
44
+
45
+ def load_topic(fn):
46
+ qid2topic = {}
47
+ with open(fn, encoding="utf-8") as f:
48
+ for line in f:
49
+ qid, topic = line.strip().split('\t')
50
+ qid2topic[qid] = topic
51
+ return qid2topic
52
+
53
+
54
+ def load_qrels(fn):
55
+ qrels = defaultdict(dict)
56
+ with open(fn, encoding="utf-8") as f:
57
+ for line in f:
58
+ qid, _, docid, rel = line.strip().split('\t')
59
+ qrels[qid][docid] = int(rel)
60
+ return qrels
61
+
62
+
63
+ class MIRACL(datasets.GeneratorBasedBuilder):
64
+ BUILDER_CONFIGS = [datasets.BuilderConfig(
65
+ version=datasets.Version('1.0.0'),
66
+ name=lang, description=f'MIRACL dataset in language {lang}.'
67
+ ) for lang in languages
68
+ ]
69
+
70
+ def _info(self):
71
+ features = datasets.Features({
72
+ 'query_id': datasets.Value('string'),
73
+ 'query': datasets.Value('string'),
74
+
75
+ 'positive_passages': [{
76
+ 'docid': datasets.Value('string'),
77
+ 'text': datasets.Value('string'), 'title': datasets.Value('string')
78
+ }],
79
+ 'negative_passages': [{
80
+ 'docid': datasets.Value('string'),
81
+ 'text': datasets.Value('string'), 'title': datasets.Value('string'),
82
+ }],
83
+ })
84
+
85
+ return datasets.DatasetInfo(
86
+ # This is the description that will appear on the datasets page.
87
+ description=_DESCRIPTION,
88
+ # This defines the different columns of the dataset and their types
89
+ features=features, # Here we define them above because they are different between the two configurations
90
+ supervised_keys=None,
91
+ # Homepage of the dataset for documentation
92
+ homepage='https://project-miracl.github.io',
93
+ # License for the dataset if available
94
+ license='',
95
+ # Citation for the dataset
96
+ citation=_CITATION,
97
+ )
98
+
99
+ def _split_generators(self, dl_manager):
100
+ lang = self.config.name
101
+ downloaded_files = dl_manager.download_and_extract(_DATASET_URLS[lang])
102
+
103
+ splits = [
104
+ datasets.SplitGenerator(
105
+ name='train',
106
+ gen_kwargs={
107
+ 'filepaths': downloaded_files['train'],
108
+ },
109
+ ),
110
+ datasets.SplitGenerator(
111
+ name='dev',
112
+ gen_kwargs={
113
+ 'filepaths': downloaded_files['dev'],
114
+ },
115
+ ),
116
+ ]
117
+ return splits
118
+
119
+ def _generate_examples(self, filepaths):
120
+ lang = self.config.name
121
+ miracl_corpus = datasets.load_dataset('miracl/miracl-corpus', lang)['train']
122
+ docid2doc = {doc['docid']: (doc['title'], doc['text']) for doc in miracl_corpus}
123
+
124
+ topic_fn, qrel_fn = filepaths
125
+ qid2topic = load_topic(topic_fn)
126
+ qrels = load_qrels(qrel_fn)
127
+ for qid in qid2topic:
128
+ data = {}
129
+ data['query_id'] = qid
130
+ data['query'] = qid2topic[qid]
131
+
132
+ pos_docids = [docid for docid, rel in qrels[qid].items() if rel == 1]
133
+ neg_docids = [docid for docid, rel in qrels[qid].items() if rel == 0]
134
+ data['positive_passages'] = [{
135
+ 'docid': docid,
136
+ **dict(zip(['title', 'text'], docid2doc[docid]))
137
+ } for docid in pos_docids]
138
+ data['negative_passages'] = [{
139
+ 'docid': docid,
140
+ **dict(zip(['title', 'text'], docid2doc[docid]))
141
+ } for docid in neg_docids]
142
+ yield qid, data