-
Notifications
You must be signed in to change notification settings - Fork 39
/
msmarco_document_v2.py
243 lines (211 loc) · 9.35 KB
/
msmarco_document_v2.py
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
import contextlib
import gzip
import io
from pathlib import Path
import json
from typing import NamedTuple, Tuple, List
import tarfile
import ir_datasets
from ir_datasets.indices import PickleLz4FullStore
from ir_datasets.util import Cache, DownloadConfig, GzipExtract, Lazy, Migrator, TarExtractAll
from ir_datasets.datasets.base import Dataset, YamlDocumentation, FilteredQueries, FilteredScoredDocs, FilteredQrels
from ir_datasets.formats import TsvQueries, TrecQrels, TrecScoredDocs, BaseDocs
from ir_datasets.datasets.msmarco_passage import DUA, DL_HARD_QIDS_BYFOLD, DL_HARD_QIDS
from ir_datasets.datasets.msmarco_document import TREC_DL_QRELS_DEFS
_logger = ir_datasets.log.easy()
NAME = 'msmarco-document-v2'
QRELS_DEFS = {
1: 'Document contains a passage labeled as relevant in msmarco-passage'
}
class MsMarcoV2Document(NamedTuple):
doc_id: str
url: str
title: str
headings: str
body: str
def default_text(self):
"""
title + headings + body
"""
return f'{self.title} {self.headings} {self.body}'
class MsMarcoV2Docs(BaseDocs):
def __init__(self, dlc):
super().__init__()
self._dlc = dlc
@ir_datasets.util.use_docstore
def docs_iter(self):
with self._dlc.stream() as stream, \
tarfile.open(fileobj=stream, mode='r|') as tarf:
for record in tarf:
if not record.name.endswith('.gz'):
continue
file = tarf.extractfile(record)
with gzip.open(file) as file:
for line in file:
data = json.loads(line)
yield MsMarcoV2Document(
data['docid'],
data['url'],
data['title'],
data['headings'],
data['body'])
def docs_cls(self):
return MsMarcoV2Document
def docs_store(self, field='doc_id'):
# NOTE: the MS MARCO v2 documents have this really neat quality that they contain the offset
# position in the source file: <https://microsoft.github.io/msmarco/TREC-Deep-Learning.html>.
# Unfortunately, it points to the position in the *uncompressed* file, so for this to work, we'd
# need to decompress the source files, inflating the size ~3.3x. The options would be to:
# 1) Always de-compress the source files, costing everybody ~3.3x the storage. Ouch.
# 2) De-compress the source files the first time that the docstore is requested. This would
# only cost the users who use the docstore 3.3x, but increases the complexity of the
# iteration code to handle both compressed and non-compressed versions. Would also need code
# to handle stuff like fancy slicing, which wouldn't be trivial. Would we also keep
# the original source file around? If so, it actually ends up being 4.3x.
# 3) Build a PickleLz4FullStore on demand, as normal. This would only cost the users who use
# the docstore ~2.7x (accounting for worse lz4 compression rate and keeping around original
# copy of the data), but is also slightly slower because of the O(log n) position lookups and
# decompression. (This may be offset because pickle parsing is faster than json though.)
# It also reduces the complexity of the code, as it does not require a new docstore
# implementation for this dataset, and is just doing the normal procedure.
return PickleLz4FullStore(
path=f'{self._dlc.path(force=False)}.pklz4',
init_iter_fn=self.docs_iter,
data_cls=self.docs_cls(),
lookup_field=field,
index_fields=['doc_id'],
key_field_prefix='msmarco_doc_', # cut down on storage by removing prefix in lookup structure
size_hint=66500029281,
count_hint=ir_datasets.util.count_hint(NAME),
)
# return MsMArcoV2DocStore(self)
def docs_count(self):
if self.docs_store().built():
return self.docs_store().count()
def docs_namespace(self):
return NAME
def docs_lang(self):
return 'en'
class MsMarcoV2AnchorTextDocument(NamedTuple):
doc_id: str
text: str
anchors: List[str]
def default_text(self):
"""
text + anchors
"""
return f'{self.text} ' + ' '.join(self.anchors)
class MsMarcoV2AnchorTextDocs(BaseDocs):
def __init__(self, dlc, count_hint):
super().__init__()
self._dlc = dlc
self._count_hint = count_hint
@ir_datasets.util.use_docstore
def docs_iter(self):
with self._dlc.stream() as stream:
for line in stream:
data = json.loads(line)
yield MsMarcoV2AnchorTextDocument(data['id'], ' '.join(data['anchors']), data['anchors'])
def docs_cls(self):
return MsMarcoV2AnchorTextDocument
def docs_store(self, field='doc_id'):
return PickleLz4FullStore(
path=f'{ir_datasets.util.home_path()}/{NAME}/anchor-text.pklz4',
init_iter_fn=self.docs_iter,
data_cls=self.docs_cls(),
lookup_field=field,
index_fields=['doc_id'],
count_hint=self._count_hint,
)
def docs_count(self):
if self.docs_store().built():
return self.docs_store().count()
def docs_namespace(self):
return f'{NAME}/anchor-text'
def docs_lang(self):
return 'en'
def _init():
base_path = ir_datasets.util.home_path()/NAME
documentation = YamlDocumentation(f'docs/{NAME}.yaml')
dlc = DownloadConfig.context(NAME, base_path, dua=DUA)
subsets = {}
collection = MsMarcoV2Docs(dlc['docs'])
subsets['train'] = Dataset(
collection,
TsvQueries(dlc['train_queries'], namespace='msmarco', lang='en'),
TrecQrels(dlc['train_qrels'], QRELS_DEFS),
TrecScoredDocs(GzipExtract(dlc['train_scoreddocs'])),
)
subsets['dev1'] = Dataset(
collection,
TsvQueries(dlc['dev1_queries'], namespace='msmarco', lang='en'),
TrecQrels(dlc['dev1_qrels'], QRELS_DEFS),
TrecScoredDocs(GzipExtract(dlc['dev1_scoreddocs'])),
)
subsets['dev2'] = Dataset(
collection,
TsvQueries(dlc['dev2_queries'], namespace='msmarco', lang='en'),
TrecQrels(dlc['dev2_qrels'], QRELS_DEFS),
TrecScoredDocs(GzipExtract(dlc['dev2_scoreddocs'])),
)
subsets['trec-dl-2019'] = Dataset(
collection,
TsvQueries(GzipExtract(dlc['trec-dl-2019/queries']), namespace='msmarco', lang='en'),
TrecQrels(GzipExtract(dlc['trec_dl_2019_qrels']), TREC_DL_QRELS_DEFS),
)
subsets['trec-dl-2020'] = Dataset(
collection,
TsvQueries(GzipExtract(dlc['trec-dl-2020/queries']), namespace='msmarco', lang='en'),
TrecQrels(GzipExtract(dlc['trec_dl_2020_qrels']), TREC_DL_QRELS_DEFS),
)
dl19_v2_judged = Lazy(lambda: {q.query_id for q in subsets['trec-dl-2019'].qrels_iter()})
subsets['trec-dl-2019/judged'] = Dataset(
FilteredQueries(subsets['trec-dl-2019'].queries_handler(), dl19_v2_judged),
subsets['trec-dl-2019'],
)
dl20_v2_judged = Lazy(lambda: {q.query_id for q in subsets['trec-dl-2020'].qrels_iter()})
subsets['trec-dl-2020/judged'] = Dataset(
FilteredQueries(subsets['trec-dl-2020'].queries_handler(), dl20_v2_judged),
subsets['trec-dl-2020'],
)
subsets['trec-dl-2021'] = Dataset(
collection,
TsvQueries(dlc['trec-dl-2021/queries'], namespace='msmarco', lang='en'),
TrecQrels(dlc['trec-dl-2021/qrels'], TREC_DL_QRELS_DEFS),
TrecScoredDocs(GzipExtract(dlc['trec-dl-2021/scoreddocs'])),
)
dl21_judged = Lazy(lambda: {q.query_id for q in subsets['trec-dl-2021'].qrels_iter()})
subsets['trec-dl-2021/judged'] = Dataset(
FilteredQueries(subsets['trec-dl-2021'].queries_handler(), dl21_judged),
FilteredScoredDocs(subsets['trec-dl-2021'].scoreddocs_handler(), dl21_judged),
subsets['trec-dl-2021'],
)
subsets['trec-dl-2022'] = Dataset(
collection,
TsvQueries(dlc['trec-dl-2022/queries'], namespace='msmarco', lang='en'),
TrecQrels(dlc['trec-dl-2022/qrels'], TREC_DL_QRELS_DEFS),
TrecScoredDocs(GzipExtract(dlc['trec-dl-2022/scoreddocs'])),
)
dl22_judged = Lazy(lambda: {q.query_id for q in subsets['trec-dl-2022'].qrels_iter()})
subsets['trec-dl-2022/judged'] = Dataset(
FilteredQueries(subsets['trec-dl-2022'].queries_handler(), dl22_judged),
FilteredScoredDocs(subsets['trec-dl-2022'].scoreddocs_handler(), dl22_judged),
subsets['trec-dl-2022'],
)
subsets['trec-dl-2023'] = Dataset(
collection,
TsvQueries(dlc['trec-dl-2023/queries'], namespace='msmarco', lang='en'),
TrecScoredDocs(GzipExtract(dlc['trec-dl-2023/scoreddocs'])),
)
subsets['anchor-text'] = Dataset(
MsMarcoV2AnchorTextDocs(
Cache(GzipExtract(dlc['anchor-text']), base_path / "anchor-text.json"),
count_hint=4821244
),
documentation('anchor-text')
)
ir_datasets.registry.register(NAME, Dataset(collection, documentation("_")))
for s in sorted(subsets):
ir_datasets.registry.register(f'{NAME}/{s}', Dataset(subsets[s], documentation(s)))
return collection, subsets
collection, subsets = _init()