import os import pickle from tqdm import tqdm # for progress bar 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}...") # Create mapping doc_mapping = {} dataset = irds.load(collection) # Use tqdm to show progress for doc in tqdm(dataset.docs): doc_mapping[doc.doc_id] = { 'title': doc.title, 'text': doc.text } # Save to file 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") # Function to load and use the mapping 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" # Load mapping if not already loaded 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__": # Create the mappings # create_doc_mappings() # Example usage docid = "8e45c80f-f63b-4eca-9976-79185811cd7d" # replace with a real doc ID collection = "neuclir/1/fa" title, text = get_text_from_id_fast(docid, collection) print(title) print(text)