cc / .save /parquet2jsonl.py
binhvq's picture
HR-02 submit 83c28bf38d1e2785768a0981cd460525,e6c0a9e5897c7fdb07791b90530fabb7,ed57f3d8cd1d29286aeb2eeef91c0a98,81faf8ebf3735fe3d1acb98ca5a35617,ef55ac22144c718f4cbdd03e3fd57e09 (#78531)
d74eab0
import glob, pandas, sys, os, gzip, json, time, lzma
try: fast_mode = (sys.argv[2] == "fast")
except: fast_mode = False
offset = 10
index = int(sys.argv[1]) * offset
dumps = glob.glob("CC-MAIN-*")
for dump in dumps:
all_buckets = glob.glob(f"{dump}/CC-MAIN-*")
buckets = all_buckets[index : index + offset]
print("\n\n- - - - -\n", dump, "has", len(all_buckets), flush=True)
print(fast_mode, index, offset, buckets, flush=True) #DEBUG
for bucket in buckets:
files = glob.glob(f"{bucket}/*.parquet")
bucket = bucket.replace(f"{dump}/", f"{dump}_")
bucket = bucket.replace("CC-MAIN-", "")
output_file = f"jsonl/{bucket}.jsonl.lzma"
print(output_file, flush=True)
added_file_ids = set()
if os.path.exists(output_file):
ti_mcur = os.path.getmtime(output_file)
ti_mmax = 0
remains = []
for file in files:
ti_m = os.path.getmtime(file)
if ti_m > ti_mmax: ti_mmax = ti_m
if fast_mode and ti_m > ti_mcur:
remains.append(file)
if ti_mcur > ti_mmax: continue
if fast_mode:
files = remains
else:
skip_token = '"id": "000000000."'
with lzma.open(output_file, "rt") as fin:
for count, line in enumerate(fin):
if skip_token not in line:
file_id = line.split('"id": "')[1].split(".")[0] # "id": "{file_id}.{line_count}"
skip_token = f'"id": "{file_id}.'
assert file_id not in added_file_ids
added_file_ids.add(file_id)
with lzma.open(output_file, "at") as fout:
for file in files:
file_id = file.split("/")[-1].replace(".parquet", "")
if file_id in added_file_ids: continue
print(f"Adding {file_id} to {output_file}...", flush=True)
df = pandas.read_parquet(file)
for line_count, row in df.iterrows():
idd = f"{file_id}.{line_count}"
ss = json.dumps({"text": row[0], "id": idd}, ensure_ascii=False)
fout.write(ss + "\n")
print("fast_mode", fast_mode)