|
import os |
|
import random |
|
|
|
|
|
shard_directory = "/path/to/shard/directory" |
|
|
|
|
|
shard_files = [f for f in os.listdir(shard_directory) if f.endswith('.jsonl')] |
|
|
|
|
|
def read_random_lines(filename, min_lines, max_lines): |
|
selected_lines = [] |
|
num_lines = random.randint(min_lines, max_lines) |
|
|
|
with open(filename, 'r') as file: |
|
lines = list(file) |
|
if len(lines) <= num_lines: |
|
return lines |
|
selected_lines = random.sample(lines, num_lines) |
|
|
|
return selected_lines |
|
|
|
|
|
def combine_shards(output_filename, num_combinations): |
|
with open(output_filename, 'w') as output_file: |
|
for _ in range(num_combinations): |
|
selected_shard_file = random.choice(shard_files) |
|
lines = read_random_lines(os.path.join(shard_directory, selected_shard_file), 5000, 10000) |
|
output_file.writelines(lines) |
|
|
|
|
|
combine_shards("/path/to/output/combined_shards.jsonl", 10) |
|
|