crystina-z commited on
Commit
278c72d
·
verified ·
1 Parent(s): 1bf485b

Create miracl-reranking.py

Browse files
Files changed (1) hide show
  1. miracl-reranking.py +155 -0
miracl-reranking.py ADDED
@@ -0,0 +1,155 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ surprise_languages = ['de', 'yo']
27
+ new_languages = ['es', 'fa', 'fr', 'hi', 'zh'] + surprise_languages
28
+ languages = ['ar', 'bn', 'en', 'es', 'fa', 'fi', 'fr', 'hi', 'id', 'ja', 'ko', 'ru', 'sw', 'te', 'th', 'zh'] + surprise_languages
29
+
30
+ _DESCRIPTION = 'dataset load script for MIRACL'
31
+
32
+ _DATASET_URLS = {
33
+ lang: {
34
+ 'dev': [
35
+ f'https://huggingface.co/datasets/miracl/miracl/resolve/main/miracl-v1.0-{lang}/topics/topics.miracl-v1.0-{lang}-dev.tsv',
36
+ f'https://huggingface.co/datasets/miracl/miracl/resolve/main/miracl-v1.0-{lang}/qrels/qrels.miracl-v1.0-{lang}-dev.tsv',
37
+ ],
38
+ 'testB': [
39
+ f'https://huggingface.co/datasets/miracl/miracl/resolve/main/miracl-v1.0-{lang}/topics/topics.miracl-v1.0-{lang}-test-b.tsv',
40
+ ],
41
+ } for lang in languages
42
+ }
43
+ for lang in languages:
44
+ if lang in surprise_languages:
45
+ continue
46
+ _DATASET_URLS[lang]['train'] = [
47
+ f'https://huggingface.co/datasets/miracl/miracl/resolve/main/miracl-v1.0-{lang}/topics/topics.miracl-v1.0-{lang}-train.tsv',
48
+ f'https://huggingface.co/datasets/miracl/miracl/resolve/main/miracl-v1.0-{lang}/qrels/qrels.miracl-v1.0-{lang}-train.tsv',
49
+ ]
50
+
51
+
52
+ for lang in languages:
53
+ if lang in new_languages:
54
+ continue
55
+ _DATASET_URLS[lang]['testA'] = [
56
+ f'https://huggingface.co/datasets/miracl/miracl/resolve/main/miracl-v1.0-{lang}/topics/topics.miracl-v1.0-{lang}-test-a.tsv',
57
+ ]
58
+
59
+ def load_topic(fn):
60
+ qid2topic = {}
61
+ with open(fn, encoding="utf-8") as f:
62
+ for line in f:
63
+ qid, topic = line.strip().split('\t')
64
+ qid2topic[qid] = topic
65
+ return qid2topic
66
+
67
+
68
+ def load_qrels(fn):
69
+ if fn is None:
70
+ return None
71
+
72
+ qrels = defaultdict(dict)
73
+ with open(fn, encoding="utf-8") as f:
74
+ for line in f:
75
+ qid, _, docid, rel = line.strip().split('\t')
76
+ qrels[qid][docid] = int(rel)
77
+ return qrels
78
+
79
+
80
+ class MIRACLReranking(datasets.GeneratorBasedBuilder):
81
+ BUILDER_CONFIGS = [datasets.BuilderConfig(
82
+ version=datasets.Version('1.0.0'),
83
+ name=lang, description=f'MIRACL Reranking in language {lang}.'
84
+ ) for lang in languages
85
+ ]
86
+
87
+ def _info(self):
88
+ features = datasets.Features({
89
+ 'query_id': datasets.Value('string'),
90
+ 'query': datasets.Value('string'),
91
+
92
+ 'positive_passages': [{
93
+ 'docid': datasets.Value('string'),
94
+ 'text': datasets.Value('string'), 'title': datasets.Value('string')
95
+ }],
96
+ 'negative_passages': [{
97
+ 'docid': datasets.Value('string'),
98
+ 'text': datasets.Value('string'), 'title': datasets.Value('string'),
99
+ }],
100
+ })
101
+
102
+ return datasets.DatasetInfo(
103
+ # This is the description that will appear on the datasets page.
104
+ description=_DESCRIPTION,
105
+ # This defines the different columns of the dataset and their types
106
+ features=features, # Here we define them above because they are different between the two configurations
107
+ supervised_keys=None,
108
+ # Homepage of the dataset for documentation
109
+ homepage='https://project-miracl.github.io',
110
+ # License for the dataset if available
111
+ license='',
112
+ # Citation for the dataset
113
+ citation=_CITATION,
114
+ )
115
+
116
+ def _split_generators(self, dl_manager):
117
+ lang = self.config.name
118
+ downloaded_files = dl_manager.download_and_extract(_DATASET_URLS[lang])
119
+
120
+ splits = [
121
+ datasets.SplitGenerator(
122
+ name='dev',
123
+ gen_kwargs={
124
+ 'filepaths': downloaded_files['dev'],
125
+ },
126
+ ),
127
+ ]
128
+
129
+ return splits
130
+
131
+ def _generate_examples(self, filepaths):
132
+ def formulate_doc(title, text):
133
+ return f"{title} {text}"
134
+
135
+ lang = self.config.name
136
+ miracl_corpus = datasets.load_dataset('miracl/miracl-corpus', lang)['train']
137
+ docid2doc = {doc['docid']: (doc['title'], doc['text']) for doc in miracl_corpus}
138
+
139
+ topic_fn, qrel_fn = (filepaths) if len(filepaths) == 2 else (filepaths[0], None)
140
+ qid2topic = load_topic(topic_fn)
141
+ qrels = load_qrels(qrel_fn)
142
+ for qid in qid2topic:
143
+ data = {}
144
+ data['query'] = qid2topic[qid]
145
+
146
+ pos_docids = [docid for docid, rel in qrels[qid].items() if rel == 1] if qrels is not None else []
147
+ neg_docids = [docid for docid, rel in qrels[qid].items() if rel == 0] if qrels is not None else []
148
+ data['positive'] = [formulate_doc(
149
+ docid2doc[docid]['title'], docid2doc[docid]['text'],
150
+ ) for docid in pos_docids]
151
+ data['negative'] = [(
152
+ docid2doc[docid]['title'], docid2doc[docid]['text'],
153
+ ) for docid in neg_docids]
154
+
155
+ yield qid, data