| import json, sys, glob, lzma | |
| input_files = sys.argv[1] | |
| output_file = sys.argv[2] | |
| print(">>> Inputs", input_files) | |
| if "_" in input_files: | |
| input_files = glob.glob(input_files.replace("_", "*")) | |
| else: | |
| input_files = [ input_files ] | |
| print(">>> Inputs", input_files) | |
| def process(input_file): | |
| print(">>> read file", input_file) | |
| if "jsonl.xz" in input_file: fin = lzma.open(input_file, "rt") | |
| else: fin = open(input_file, "rt") | |
| line_txtlen_pairs = [] | |
| for line in fin: | |
| line_txtlen_pairs.append((line, len(line))) | |
| fin.close() | |
| return line_txtlen_pairs | |
| line_txtlen_pairs = [] | |
| from multiprocessing.pool import Pool | |
| with Pool() as pool: | |
| for pairs in pool.imap_unordered(process, input_files): | |
| line_txtlen_pairs += pairs | |
| print("=> total docs:", len(line_txtlen_pairs)) | |
| print("sorting docs by len ...") | |
| total_txtlen = 0 | |
| line_txtlen_pairs.sort(key=lambda x: x[1]) | |
| with open(output_file, "wt") as fout: | |
| for text, n in line_txtlen_pairs: | |
| if n < 1500: continue | |
| fout.write(text) | |
| total_txtlen += n | |
| print(n, end=" ") | |
| print(f"\nmin len {line_txtlen_pairs[0][1]}, max len {line_txtlen_pairs[-1][1]}") | |
| print(f"avg len {total_txtlen / len(line_txtlen_pairs)}") | |