|
import os |
|
import pickle |
|
from tqdm import tqdm |
|
import ir_datasets as irds |
|
|
|
def create_doc_mappings(): |
|
collections = ['neuclir/1/zh', 'neuclir/1/fa', 'neuclir/1/ru'] |
|
|
|
for collection in collections: |
|
print(f"Processing {collection}...") |
|
|
|
|
|
doc_mapping = {} |
|
dataset = irds.load(collection) |
|
|
|
|
|
for doc in tqdm(dataset.docs): |
|
doc_mapping[doc.doc_id] = { |
|
'title': doc.title, |
|
'text': doc.text |
|
} |
|
|
|
|
|
output_file = f"doc_mapping_{collection.replace('/', '_')}.pkl" |
|
with open(output_file, 'wb') as f: |
|
pickle.dump(doc_mapping, f, protocol=pickle.HIGHEST_PROTOCOL) |
|
|
|
print(f"Saved mapping to {output_file}") |
|
print(f"Number of documents: {len(doc_mapping)}\n") |
|
|
|
|
|
def get_text_from_id_fast(docid, collection): |
|
collection = collection.replace('zho', 'zh').replace('fas', 'fa').replace('rus', 'ru') |
|
mapping_file = f"doc_mapping/doc_mapping_{collection.replace('/', '_')}.pkl" |
|
|
|
|
|
if not hasattr(get_text_from_id_fast, 'cache'): |
|
get_text_from_id_fast.cache = {} |
|
|
|
if collection not in get_text_from_id_fast.cache: |
|
with open(mapping_file, 'rb') as f: |
|
get_text_from_id_fast.cache[collection] = pickle.load(f) |
|
|
|
doc = get_text_from_id_fast.cache[collection].get(docid) |
|
if doc: |
|
return doc['title'], doc['text'] |
|
return None, None |
|
|
|
if __name__ == "__main__": |
|
|
|
|
|
|
|
|
|
docid = "8e45c80f-f63b-4eca-9976-79185811cd7d" |
|
collection = "neuclir/1/fa" |
|
title, text = get_text_from_id_fast(docid, collection) |
|
print(title) |
|
print(text) |
|
|