gowitheflow
commited on
Commit
•
303f40e
1
Parent(s):
e3be419
Create alphanli.py
Browse files- alphanli.py +80 -0
alphanli.py
ADDED
@@ -0,0 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import json
|
2 |
+
import csv
|
3 |
+
import os
|
4 |
+
import datasets
|
5 |
+
|
6 |
+
logger = datasets.logging.get_logger(__name__)
|
7 |
+
|
8 |
+
_DESCRIPTION = "RAR-b alphanli Dataset"
|
9 |
+
_SPLITS = ["corpus", "queries", "qrels"]
|
10 |
+
|
11 |
+
URL = ""
|
12 |
+
_URLs = {subset: URL + f"{subset}.jsonl" if subset != "qrels" else URL + f"qrels/test.tsv" for subset in _SPLITS}
|
13 |
+
|
14 |
+
class RARb(datasets.GeneratorBasedBuilder):
|
15 |
+
"""RAR-b BenchmarkDataset."""
|
16 |
+
|
17 |
+
BUILDER_CONFIGS = [
|
18 |
+
datasets.BuilderConfig(
|
19 |
+
name=name,
|
20 |
+
description=f"This is the {name} in the RAR-b alphanli dataset.",
|
21 |
+
) for name in _SPLITS
|
22 |
+
]
|
23 |
+
DEFAULT_CONFIG_NAME = "qrels"
|
24 |
+
|
25 |
+
def _info(self):
|
26 |
+
return datasets.DatasetInfo(
|
27 |
+
description=_DESCRIPTION,
|
28 |
+
features=datasets.Features({
|
29 |
+
"_id": datasets.Value("string"),
|
30 |
+
"title": datasets.Value("string"),
|
31 |
+
"text": datasets.Value("string"),
|
32 |
+
}) if self.config.name != "qrels" else datasets.Features({
|
33 |
+
"query-id": datasets.Value("string"),
|
34 |
+
"corpus-id": datasets.Value("string"),
|
35 |
+
"score": datasets.Value("int32"),
|
36 |
+
}),
|
37 |
+
supervised_keys=None,
|
38 |
+
)
|
39 |
+
|
40 |
+
def _split_generators(self, dl_manager):
|
41 |
+
"""Returns SplitGenerators."""
|
42 |
+
if self.config.name == "qrels":
|
43 |
+
test_url = URL + "qrels/test.tsv"
|
44 |
+
test_path = dl_manager.download_and_extract(test_url)
|
45 |
+
return [
|
46 |
+
datasets.SplitGenerator(
|
47 |
+
name=datasets.Split.TEST,
|
48 |
+
gen_kwargs={"filepath": test_path},
|
49 |
+
),
|
50 |
+
]
|
51 |
+
else:
|
52 |
+
my_urls = _URLs[self.config.name]
|
53 |
+
data_dir = dl_manager.download_and_extract(my_urls)
|
54 |
+
return [
|
55 |
+
datasets.SplitGenerator(
|
56 |
+
name=self.config.name,
|
57 |
+
gen_kwargs={"filepath": data_dir},
|
58 |
+
),
|
59 |
+
]
|
60 |
+
|
61 |
+
def _generate_examples(self, filepath):
|
62 |
+
"""Yields examples."""
|
63 |
+
if self.config.name == "qrels":
|
64 |
+
with open(filepath, encoding="utf-8") as f:
|
65 |
+
reader = csv.reader(f, delimiter="\t")
|
66 |
+
header = next(reader) # skip header row
|
67 |
+
for i, row in enumerate(reader):
|
68 |
+
yield i, {
|
69 |
+
"query-id": row[0],
|
70 |
+
"corpus-id": row[1],
|
71 |
+
"score": int(row[2]),
|
72 |
+
}
|
73 |
+
else:
|
74 |
+
with open(filepath, encoding="utf-8") as f:
|
75 |
+
texts = f.readlines()
|
76 |
+
for i, text in enumerate(texts):
|
77 |
+
text = json.loads(text)
|
78 |
+
if 'metadata' in text: del text['metadata']
|
79 |
+
if "title" not in text: text["title"] = ""
|
80 |
+
yield i, text
|