|
import json |
|
import os |
|
|
|
import datasets |
|
from beir.datasets.data_loader import GenericDataLoader |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
TYPE = "processed" |
|
SPLIT = "train" |
|
DOWNLOAD_DIR = "germandpr-beir-dataset" |
|
DOWNLOAD_DIR = os.path.join(DOWNLOAD_DIR, f'{TYPE}/{SPLIT}') |
|
DOWNLOAD_QREL_DIR = os.path.join(DOWNLOAD_DIR, f'qrels/') |
|
|
|
os.makedirs(DOWNLOAD_QREL_DIR, exist_ok=True) |
|
|
|
|
|
|
|
for subset_name in ["queries", "corpus", "qrels"]: |
|
subset = datasets.load_dataset("PM-AI/germandpr-beir", f'{TYPE}-{subset_name}', split=SPLIT) |
|
if subset_name == "qrels": |
|
out_path = os.path.join(DOWNLOAD_QREL_DIR, f'{SPLIT}.tsv') |
|
subset.to_csv(out_path, sep="\t", index=False) |
|
else: |
|
if subset_name == "queries": |
|
_row_to_json = lambda row: json.dumps({"_id": row["_id"], "text": row["text"]}, ensure_ascii=False) |
|
else: |
|
_row_to_json = lambda row: json.dumps({"_id": row["_id"], "title": row["title"], "text": row["text"]}, ensure_ascii=False) |
|
|
|
with open(os.path.join(DOWNLOAD_DIR, f'{subset_name}.jsonl'), "w", encoding="utf-8") as out_file: |
|
for row in subset: |
|
out_file.write(_row_to_json(row) + "\n") |
|
|
|
|
|
|
|
corpus, queries, qrels = GenericDataLoader(data_folder=DOWNLOAD_DIR).load(SPLIT) |
|
print(f'{SPLIT} corpus size: {len(corpus)}\n' |
|
f'{SPLIT} queries size: {len(queries)}\n' |
|
f'{SPLIT} qrels: {len(qrels)}\n') |
|
|
|
print("--------------------------------------------------------------------------------------------------------------\n" |
|
"Now you can use the downloaded files in BEIR framework\n" |
|
"Example: https://github.com/beir-cellar/beir/blob/v1.0.1/examples/retrieval/evaluation/dense/evaluate_sbert.py\n" |
|
"--------------------------------------------------------------------------------------------------------------") |
|
|