import sys
from itertools import izip
import random
import os

'''
Randomly create 10%, 25%, 50%, 75% of the datasets

python create_datasets.py src_file tgt_file data_path frac_size
'''

file1 = sys.argv[1] # Training file source text
file2 = sys.argv[2] # Training file target text
data_path = sys.argv[3] # Data path

num_lines = sum(1 for line in open(file1))

fractions = [ 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9]

for frac_size in fractions:
    data_dir = data_path + '/' + str(frac_size)
    if not os.path.exists(data_dir):
        os.makedirs(data_dir)

    with open(file1) as textfile1, open(file2) as textfile2: 
        with open('%s/%s' % (data_dir, os.path.basename(file1)),
                  'w') as fp1, open('%s/%s' % (data_dir, os.path.basename(file2)), 'w') as fp2:

            total = range(num_lines)
            fracs_list = []

            inds = set(random.sample(list(total), int(float(frac_size)* num_lines)))

            print('Length of indexes:', len(inds))
            for index, x, y in izip(total, textfile1, textfile2):
                src = x.strip()
                tgt = y.strip()
                if index in inds:
                    fp1.write(src + '\n')
                    fp2.write(tgt + '\n')

    