File size: 1,938 Bytes
bac9aed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
60
61
62
63
64
"""
Utility to check result of minhash-clustering
"""

import pandas as pd
import orjson
import gzip
import os
from common import listdir_fullpath
from tqdm import tqdm

def get_file_subpath(
    language,
    partition,
    extension
):
    return f"{language}_{partition}.{extension}"

if __name__ == "__main__":
    CLUSTERS_DIR = "/home1/BharatGPT_Data/RedPajamaV2/data/minhash_clusters/2023-14"
    DOCS_DIR = "/home1/BharatGPT_Data/RedPajamaV2/data/documents/2023-14"

    CLUSTERS_SHARD = "0187"
    LANGUAGE = "en"
    PARTITION = "head"
    CLUSTERS_EXTENSION = "clusters.parquet"
    DOCS_EXTENSION = "json.gz"

    df = pd.read_parquet(os.path.join(
            CLUSTERS_DIR,
            CLUSTERS_SHARD,
            get_file_subpath(LANGUAGE, PARTITION, CLUSTERS_EXTENSION)
        ), columns=["cluster_id"])
    required_cluster_id = df["cluster_id"][0]

    # get all docs with this cluster id
    for shard in tqdm(os.listdir(CLUSTERS_DIR)):
        df2 = pd.read_parquet(os.path.join(
            CLUSTERS_DIR,
            shard,
            get_file_subpath(LANGUAGE, PARTITION, CLUSTERS_EXTENSION)
        ), columns=["id", "cluster_id"])
        df2 = df2[df2["cluster_id"] == required_cluster_id]
        doc_ids = []
        for _, row in df2.iterrows():
            doc_id = int(row["id"].split('/')[-1])
            doc_ids.append(doc_id)
        if len(doc_ids) == 0:
            continue
        with gzip.open(os.path.join(
            DOCS_DIR,
            shard,
            get_file_subpath(LANGUAGE, PARTITION, DOCS_EXTENSION)
        )) as docs_file:
            next_idx = 0
            next_doc_id = doc_ids[next_idx]
            for idx, line in enumerate(docs_file):
                if idx == next_doc_id:
                    print(line)
                    print()
                    next_idx += 1
                    if next_idx < len(doc_ids):
                        next_doc_id = doc_ids[next_idx]