File size: 1,940 Bytes
7dae430
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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)