|
| 1 | +from ir_datasets import registry |
| 2 | +from ir_datasets.datasets.base import Dataset, YamlDocumentation |
| 3 | +from ir_datasets.util.download import RequestsDownload |
| 4 | +from ir_datasets.formats.base import BaseDocs |
| 5 | +from ir_datasets.indices import Docstore |
| 6 | +from ir_datasets.util import ZipExtractCache, home_path, Cache, DownloadConfig |
| 7 | +from ir_datasets.formats import BaseDocs, TrecQrels, JsonlQueries |
| 8 | +from ir_datasets.indices import PickleLz4FullStore |
| 9 | +import os |
| 10 | +import gzip |
| 11 | +import json |
| 12 | +from tqdm import tqdm |
| 13 | +from typing import NamedTuple |
| 14 | + |
| 15 | +NAME = "trec-tot" |
| 16 | + |
| 17 | + |
| 18 | +class JsonlDocumentOffset(NamedTuple): |
| 19 | + doc_id: str |
| 20 | + offset_start: int |
| 21 | + offset_end: int |
| 22 | + |
| 23 | + |
| 24 | +class TrecToT2025Doc(NamedTuple): |
| 25 | + doc_id: str |
| 26 | + title: str |
| 27 | + url: str |
| 28 | + text: str |
| 29 | + |
| 30 | + @staticmethod |
| 31 | + def _from_json(json_doc): |
| 32 | + return TrecToT2025Doc(json_doc["id"], json_doc["title"], json_doc["url"], json_doc["text"]) |
| 33 | + |
| 34 | + def default_text(self): |
| 35 | + return self.title + " " + self.text |
| 36 | + |
| 37 | + |
| 38 | +class JsonlWithOffsetsDocsStore(Docstore): |
| 39 | + def __init__(self, docs, offsets): |
| 40 | + self.__docs = docs |
| 41 | + self.__offsets = offsets |
| 42 | + self._docs_dict = None |
| 43 | + self._id_field = "doc_id" |
| 44 | + |
| 45 | + def offsets_iter(self): |
| 46 | + with gzip.open(self.__offsets.path(), "rt") as f: |
| 47 | + for i in f: |
| 48 | + i = json.loads(i) |
| 49 | + yield JsonlDocumentOffset(doc_id=i["id"], offset_start=i["offset_start"], offset_end=i["offset_end"]) |
| 50 | + |
| 51 | + def docs_dict(self): |
| 52 | + return PickleLz4FullStore( |
| 53 | + path=str(self.__offsets.path()) + '.pklz4', |
| 54 | + init_iter_fn=self.offsets_iter, |
| 55 | + data_cls=JsonlDocumentOffset, |
| 56 | + lookup_field="doc_id", |
| 57 | + index_fields=("doc_id",) |
| 58 | + ) |
| 59 | + |
| 60 | + def get_many_iter(self, doc_ids): |
| 61 | + offsets = self.docs_dict() |
| 62 | + |
| 63 | + with open(self.__docs.path(), "rb") as f: |
| 64 | + for doc in doc_ids: |
| 65 | + doc = offsets.get(doc) |
| 66 | + f.seek(doc.offset_start) |
| 67 | + raw_content_bytes = f.read(doc.offset_end - doc.offset_start) |
| 68 | + yield gzip.decompress(raw_content_bytes) |
| 69 | + |
| 70 | + |
| 71 | +class TrecToT2025DocsStore(JsonlWithOffsetsDocsStore): |
| 72 | + def get_many_iter(self, doc_ids): |
| 73 | + for i in super().get_many_iter(doc_ids): |
| 74 | + yield TrecToT2025Doc._from_json(json.loads(i)) |
| 75 | + |
| 76 | + |
| 77 | +class JsonlDocumentsWithOffsets(BaseDocs): |
| 78 | + def __init__(self, docs, offsets): |
| 79 | + self.__docs = docs |
| 80 | + self.__offsets = offsets |
| 81 | + |
| 82 | + def docs_iter(self): |
| 83 | + with gzip.open(self.__docs.path()) as f: |
| 84 | + for l in f: |
| 85 | + yield TrecToT2025Doc._from_json(json.loads(l)) |
| 86 | + |
| 87 | + def docs_cls(self): |
| 88 | + return TrecToT2025Doc |
| 89 | + |
| 90 | + def docs_store(self, field='doc_id'): |
| 91 | + return TrecToT2025DocsStore(self.__docs, self.__offsets) |
| 92 | + |
| 93 | + def docs_namespace(self): |
| 94 | + raise ValueError("ToDo: Implement this") |
| 95 | + |
| 96 | + def docs_count(self): |
| 97 | + return 6407814 |
| 98 | + |
| 99 | + def docs_lang(self): |
| 100 | + return "en" |
| 101 | + |
| 102 | + |
| 103 | +class TrecToT2025Dataset(Dataset): |
| 104 | + def __init__(self, docs_jsonl_file, offset_jsonl_file, queries=None, qrels=None, documentation=None): |
| 105 | + docs = JsonlDocumentsWithOffsets(docs_jsonl_file, offset_jsonl_file) |
| 106 | + |
| 107 | + if queries: |
| 108 | + queries = JsonlQueries(queries, lang='en', mapping={"text": "query", "query_id": "query_id"}) |
| 109 | + if qrels: |
| 110 | + qrels = TrecQrels(qrels, {0: 'Not Relevant', 1: 'Relevant'}) |
| 111 | + |
| 112 | + super().__init__(docs, queries, qrels, documentation) |
| 113 | + |
| 114 | + |
| 115 | +def register_dataset(): |
| 116 | + if f"{NAME}/2025" in registry: |
| 117 | + return |
| 118 | + |
| 119 | + dlc = DownloadConfig.context("trec-tot-2025", home_path() / NAME / "2025") |
| 120 | + |
| 121 | + documentation = YamlDocumentation(f'docs/{NAME}.yaml') |
| 122 | + doc_offsets = dlc['trec-tot-2025-offsets.jsonl.gz'] |
| 123 | + doc_corpus = dlc['trec-tot-2025-corpus.jsonl.gz'] |
| 124 | + registry.register(f"{NAME}/2025", TrecToT2025Dataset(doc_corpus, doc_offsets, documentation=documentation("2025"))) |
| 125 | + for i in ["train", "dev1", "dev2", "dev3"]: |
| 126 | + qrels = dlc[i + "-2025-qrel.txt"] |
| 127 | + queries = dlc[i + "-2025-queries.jsonl"] |
| 128 | + registry.register(f"{NAME}/2025/{i}", TrecToT2025Dataset(doc_corpus, doc_offsets, queries, qrels, documentation(f"2025/{i}"))) |
| 129 | + |
| 130 | + |
| 131 | +register_dataset() |
| 132 | + |
0 commit comments