Datasets:
Nandan Thakur
commited on
Commit
·
152b8ec
1
Parent(s):
543c547
added initial script for loading queries and qrels
Browse files
beir.py
ADDED
@@ -0,0 +1,159 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import json
|
2 |
+
import csv
|
3 |
+
import os
|
4 |
+
import datasets
|
5 |
+
|
6 |
+
logger = datasets.logging.get_logger(__name__)
|
7 |
+
|
8 |
+
|
9 |
+
_DESCRIPTION = "BEIR Benchmark"
|
10 |
+
_DATASETS = ["fiqa", "trec-covid", ""]
|
11 |
+
|
12 |
+
URL = ""
|
13 |
+
_URLs = {
|
14 |
+
dataset: {
|
15 |
+
"queries": URL + f"{dataset}/queries.jsonl",
|
16 |
+
"qrels": {
|
17 |
+
"train": URL + f"{dataset}/qrels/train.tsv",
|
18 |
+
"dev": URL + f"{dataset}/qrels/dev.tsv",
|
19 |
+
"test": URL + f"{dataset}/qrels/test.tsv"
|
20 |
+
}} for dataset in _DATASETS}
|
21 |
+
|
22 |
+
|
23 |
+
class BEIR(datasets.GeneratorBasedBuilder):
|
24 |
+
"""BEIR BenchmarkDataset."""
|
25 |
+
|
26 |
+
BUILDER_CONFIGS = [
|
27 |
+
datasets.BuilderConfig(
|
28 |
+
name=dataset,
|
29 |
+
description=f"This is the {dataset} dataset in BEIR Benchmark.",
|
30 |
+
) for dataset in _DATASETS
|
31 |
+
]
|
32 |
+
|
33 |
+
|
34 |
+
def _info(self):
|
35 |
+
return datasets.DatasetInfo(
|
36 |
+
description=_DESCRIPTION,
|
37 |
+
features=datasets.Features({
|
38 |
+
"query": datasets.Value("string"),
|
39 |
+
"relevant": [{
|
40 |
+
"_id": datasets.Value("string"),
|
41 |
+
"score": datasets.Value("int32"),
|
42 |
+
}],
|
43 |
+
}),
|
44 |
+
supervised_keys=None,
|
45 |
+
)
|
46 |
+
|
47 |
+
def _split_generators(self, dl_manager):
|
48 |
+
"""Returns SplitGenerators."""
|
49 |
+
|
50 |
+
my_urls = _URLs[self.config.name]
|
51 |
+
|
52 |
+
# All train, dev and test splits available for these datasets
|
53 |
+
if self.config.name in ["msmarco", "nfcorpus", "hotpotqa", "fiqa", "fever"]:
|
54 |
+
data_dir = dl_manager.download_and_extract(my_urls)
|
55 |
+
return [
|
56 |
+
datasets.SplitGenerator(
|
57 |
+
name=datasets.Split.TRAIN,
|
58 |
+
# These kwargs will be passed to _generate_examples
|
59 |
+
gen_kwargs={"query_path": data_dir["queries"],
|
60 |
+
"qrels_path": data_dir["qrels"]["train"]}
|
61 |
+
),
|
62 |
+
datasets.SplitGenerator(
|
63 |
+
name="dev",
|
64 |
+
# These kwargs will be passed to _generate_examples
|
65 |
+
gen_kwargs={"query_path": data_dir["queries"],
|
66 |
+
"qrels_path": data_dir["qrels"]["dev"]}
|
67 |
+
),
|
68 |
+
datasets.SplitGenerator(
|
69 |
+
name=datasets.Split.TEST,
|
70 |
+
# These kwargs will be passed to _generate_examples
|
71 |
+
gen_kwargs={"query_path": data_dir["queries"],
|
72 |
+
"qrels_path": data_dir["qrels"]["test"]}
|
73 |
+
),
|
74 |
+
]
|
75 |
+
|
76 |
+
# Only train and test splits available for these datasets
|
77 |
+
elif self.config.name in ["nq", "scifact"]:
|
78 |
+
my_urls["qrels"].pop("dev", None)
|
79 |
+
data_dir = dl_manager.download_and_extract(my_urls)
|
80 |
+
|
81 |
+
return [
|
82 |
+
datasets.SplitGenerator(
|
83 |
+
name=datasets.Split.TRAIN,
|
84 |
+
# These kwargs will be passed to _generate_examples
|
85 |
+
gen_kwargs={"query_path": data_dir["queries"],
|
86 |
+
"qrels_path": data_dir["qrels"]["train"]}
|
87 |
+
),
|
88 |
+
datasets.SplitGenerator(
|
89 |
+
name=datasets.Split.TEST,
|
90 |
+
# These kwargs will be passed to _generate_examples
|
91 |
+
gen_kwargs={"query_path": data_dir["queries"],
|
92 |
+
"qrels_path": data_dir["qrels"]["test"]}
|
93 |
+
),
|
94 |
+
]
|
95 |
+
|
96 |
+
# Only dev and test splits available for these datasets
|
97 |
+
elif self.config.name in ["dbpedia", "quora"]:
|
98 |
+
my_urls["qrels"].pop("train", None)
|
99 |
+
data_dir = dl_manager.download_and_extract(my_urls)
|
100 |
+
return [
|
101 |
+
datasets.SplitGenerator(
|
102 |
+
name="dev",
|
103 |
+
# These kwargs will be passed to _generate_examples
|
104 |
+
gen_kwargs={"query_path": data_dir["queries"],
|
105 |
+
"qrels_path": data_dir["qrels"]["dev"]}
|
106 |
+
),
|
107 |
+
datasets.SplitGenerator(
|
108 |
+
name=datasets.Split.TEST,
|
109 |
+
# These kwargs will be passed to _generate_examples
|
110 |
+
gen_kwargs={"query_path": data_dir["queries"],
|
111 |
+
"qrels_path": data_dir["qrels"]["test"]}
|
112 |
+
),
|
113 |
+
]
|
114 |
+
|
115 |
+
# Only test split available for these datasets
|
116 |
+
else:
|
117 |
+
for split in ["train", "dev"]:
|
118 |
+
my_urls["qrels"].pop(split, None)
|
119 |
+
data_dir = dl_manager.download_and_extract(my_urls)
|
120 |
+
return [
|
121 |
+
datasets.SplitGenerator(
|
122 |
+
name=datasets.Split.TEST,
|
123 |
+
# These kwargs will be passed to _generate_examples
|
124 |
+
gen_kwargs={"query_path": data_dir["queries"],
|
125 |
+
"qrels_path": data_dir["qrels"]["test"]}
|
126 |
+
),
|
127 |
+
]
|
128 |
+
|
129 |
+
|
130 |
+
def _generate_examples(self, query_path, qrels_path):
|
131 |
+
"""Yields examples."""
|
132 |
+
|
133 |
+
queries, qrels = {}, {}
|
134 |
+
|
135 |
+
with open(query_path, encoding="utf-8") as fIn:
|
136 |
+
text = fIn.readlines()
|
137 |
+
|
138 |
+
for line in text:
|
139 |
+
line = json.loads(line)
|
140 |
+
queries[line.get("_id")] = line.get("text", "")
|
141 |
+
|
142 |
+
reader = csv.reader(open(qrels_path, encoding="utf-8"),
|
143 |
+
delimiter="\t", quoting=csv.QUOTE_MINIMAL)
|
144 |
+
|
145 |
+
next(reader)
|
146 |
+
|
147 |
+
for id, row in enumerate(reader):
|
148 |
+
query_id, corpus_id, score = row[0], row[1], int(row[2])
|
149 |
+
if query_id not in qrels:
|
150 |
+
qrels[query_id] = {corpus_id: score}
|
151 |
+
else:
|
152 |
+
qrels[query_id][corpus_id] = score
|
153 |
+
|
154 |
+
for i, query_id in enumerate(qrels):
|
155 |
+
yield i, {
|
156 |
+
"query": queries[query_id],
|
157 |
+
"relevant": [{"_id": doc_id, "score": score
|
158 |
+
} for doc_id, score in qrels[query_id].items()]
|
159 |
+
}
|