Datasets:
File size: 1,140 Bytes
3bdb95c |
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 |
import os
import random
# Directory containing the shard JSONL files
shard_directory = "/path/to/shard/directory"
# Get a list of all JSONL files in the directory
shard_files = [f for f in os.listdir(shard_directory) if f.endswith('.jsonl')]
# Function to read a random number of lines (between min_lines and max_lines) from a file
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
# Function to combine shards
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)
# Example usage
combine_shards("/path/to/output/combined_shards.jsonl", 10)
|