File size: 1,903 Bytes
3d68455
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import gzip
import json
import sys
from concurrent.futures import ThreadPoolExecutor
import random
from tqdm import tqdm

num_docs = 10

def select_random_documents(args):
    folder_path, lang_id, output_file = args
    # Find all JSON.gz files for the given language ID
    file_paths = [os.path.join(folder_path, f) for f in os.listdir(folder_path) if f.startswith(lang_id + "_") and f.endswith('.json.gz')]
    
    if not file_paths:
        print("No files found for the given language ID.")
        return

    # Select a random file
    random_file = random.choice(file_paths)
    
    # Read random file and select 10 random documents
    selected_documents = []
    with gzip.open(random_file, 'rt') as f:
        for line in f:
            json_obj = json.loads(line)
            selected_documents.append(json_obj)
            if len(selected_documents) >= num_docs:
                break
    
    # Write selected documents to output file
    with open(output_file, 'w') as f:
        json.dump(selected_documents, f, indent=4, ensure_ascii=False)
    
    print(f"Randomly selected {num_docs} documents from {random_file}")

folder_path = ""
lang_id = "hi"
output_file = 'random_documents.json'
snapshots = ["2018-17", "2018-22", "2018-26", "2018-30", "2018-34", "2018-39", "2018-43", "2018-47", "2018-51", "2019-04", "2019-09", "2019-13", "2019-18", "2019-22", "2019-26", "2019-30", "2019-35", "2019-39", "2019-43", "2019-47", "2019-51", "2020-05", "2020-10", "2020-16", "2020-24", "2020-29", "2020-34", "2020-40", "2020-45", "2020-50", "2021-04", "2021-10", "2021-17", "2021-21", "2021-25", "2021-31", "2021-39", "2021-43", "2021-49", "2022-05", "2022-21", "2022-27", "2022-33", "2022-40", "2022-49", "2023-06", "2023-14", "2023-23", "2023-40", "2023-50", "2024-10"]
snap = random.choice(snapshots)
select_random_documents(os.path.join(folder_path, snap), lang_id, output_file)