feat: added PRES code
Browse files
pres.py
ADDED
@@ -0,0 +1,137 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""PRES retrieval dataset"""
|
2 |
+
|
3 |
+
|
4 |
+
import json
|
5 |
+
import tempfile
|
6 |
+
import patoolib
|
7 |
+
import csv
|
8 |
+
|
9 |
+
import os
|
10 |
+
|
11 |
+
import datasets
|
12 |
+
|
13 |
+
_DESCRIPTION = 'Reference: https://mklab.iti.gr/results/spanish-passage-retrieval-dataset/'
|
14 |
+
|
15 |
+
_HOMEPAGE_URL = 'https://mklab.iti.gr/results/spanish-passage-retrieval-dataset/'
|
16 |
+
_LANGUAGES = {'es': 'ES'}
|
17 |
+
_VERSION = '1.0.0'
|
18 |
+
|
19 |
+
|
20 |
+
URL = 'http://mklab.iti.gr/files/PRES_Dataset.rar'
|
21 |
+
|
22 |
+
|
23 |
+
class PRESConfig(datasets.BuilderConfig):
|
24 |
+
"""BuilderConfig for PRESConfig."""
|
25 |
+
|
26 |
+
def __init__(self, **kwargs):
|
27 |
+
super(PRESConfig, self).__init__(
|
28 |
+
version=datasets.Version(_VERSION, ''), **kwargs
|
29 |
+
),
|
30 |
+
|
31 |
+
|
32 |
+
class PRES(datasets.GeneratorBasedBuilder):
|
33 |
+
"""The XMarketDE category to product retrieval dataset"""
|
34 |
+
|
35 |
+
BUILDER_CONFIGS = [
|
36 |
+
datasets.BuilderConfig(
|
37 |
+
name=name,
|
38 |
+
description=f'{name.title()} of the Spanish Passage Retrieval dataset.',
|
39 |
+
)
|
40 |
+
for name in ['corpus', 'queries', 'qrels']
|
41 |
+
]
|
42 |
+
|
43 |
+
BUILDER_CONFIG_CLASS = PRESConfig
|
44 |
+
|
45 |
+
def __init__(self, *args, **kwargs):
|
46 |
+
super().__init__(*args, **kwargs)
|
47 |
+
self._data = None
|
48 |
+
|
49 |
+
def _info(self):
|
50 |
+
return datasets.DatasetInfo(
|
51 |
+
description=_DESCRIPTION,
|
52 |
+
features=datasets.Features(
|
53 |
+
{
|
54 |
+
"_id": datasets.Value("string"),
|
55 |
+
"query": datasets.Value("string"),
|
56 |
+
"document": datasets.Value("string"),
|
57 |
+
}
|
58 |
+
),
|
59 |
+
supervised_keys=None,
|
60 |
+
homepage=_HOMEPAGE_URL,
|
61 |
+
)
|
62 |
+
|
63 |
+
def _split_generators(self, dl_manager: datasets.DownloadManager):
|
64 |
+
downloaded_archive = dl_manager.download(URL)
|
65 |
+
return [
|
66 |
+
datasets.SplitGenerator(
|
67 |
+
name=datasets.Split.TEST,
|
68 |
+
gen_kwargs={
|
69 |
+
'archive': downloaded_archive,
|
70 |
+
'split': 'test',
|
71 |
+
},
|
72 |
+
),
|
73 |
+
]
|
74 |
+
|
75 |
+
def _generate_examples(
|
76 |
+
self,
|
77 |
+
archive: str,
|
78 |
+
split: str = None,
|
79 |
+
):
|
80 |
+
|
81 |
+
if not self._data:
|
82 |
+
with tempfile.TemporaryDirectory() as tmpdir:
|
83 |
+
extracted_path = os.path.join(tmpdir, 'PRES')
|
84 |
+
patoolib.extract_archive(archive, outdir=extracted_path)
|
85 |
+
|
86 |
+
with open(os.path.join(extracted_path, 'docs.json')) as f:
|
87 |
+
docs = json.load(f)
|
88 |
+
|
89 |
+
with open(os.path.join(extracted_path, 'topics.json')) as f:
|
90 |
+
topics = json.load(f)
|
91 |
+
|
92 |
+
with open(os.path.join(extracted_path, 'relevance_passages.json')) as f:
|
93 |
+
rel_passages = json.load(f)
|
94 |
+
|
95 |
+
corpus = []
|
96 |
+
queries = dict()
|
97 |
+
qrels = dict()
|
98 |
+
topic_to_queries = dict()
|
99 |
+
for topic in topics['topics']:
|
100 |
+
topic_to_queries[topic['number']] = []
|
101 |
+
for query in topic['queries']:
|
102 |
+
qid = query['number']
|
103 |
+
queries[qid] = query['text']
|
104 |
+
topic_to_queries[topic['number']].append(qid)
|
105 |
+
qrels[qid] = []
|
106 |
+
|
107 |
+
for annotated_topic in rel_passages['topics']:
|
108 |
+
topic = annotated_topic['number']
|
109 |
+
for annotation in annotated_topic['annotations']:
|
110 |
+
did = f'doc_{annotation["docNo"]}_{annotation["start"]}_{annotation["end"]}'
|
111 |
+
corpus.append({'_id': did, 'text': annotation['text']})
|
112 |
+
for qid in topic_to_queries[topic]:
|
113 |
+
qrels[qid].append(did)
|
114 |
+
|
115 |
+
self._data = {
|
116 |
+
'corpus': corpus,
|
117 |
+
'queries': queries,
|
118 |
+
'qrels': qrels,
|
119 |
+
}
|
120 |
+
|
121 |
+
if self.config.name == 'corpus':
|
122 |
+
for line in self._data['corpus']:
|
123 |
+
yield line['_id'], line
|
124 |
+
elif self.config.name == 'queries':
|
125 |
+
for qid, query in self._data['queries'].items():
|
126 |
+
yield qid, {
|
127 |
+
"_id": qid,
|
128 |
+
"text": query,
|
129 |
+
}
|
130 |
+
elif self.config.name == 'qrels':
|
131 |
+
for qid, dids in self._data['qrels'].items():
|
132 |
+
yield qid, {
|
133 |
+
"_id": qid,
|
134 |
+
"text": ' '.join(dids),
|
135 |
+
}
|
136 |
+
else:
|
137 |
+
raise ValueError(f'Unknown config name: {self.config.name}')
|