#!/usr/bin/python
#encoding:utf8

'''
python resample.py -i plwn_pwn_sumo-24.10.2013-alg-v3-reduced_step1.csv -n 5 -s 100
'''

import random
import argparse

def make_parser():
	desc = 'Sampler do pliku'
	parser = argparse.ArgumentParser(description = desc)
	parser.add_argument('-i', '--in-file', dest = 'in_file', required = True)
	parser.add_argument('-n', '--sample-count', dest = 'sample_count', required = True)
	parser.add_argument('-s', '--sample-size', dest = 'sample_size', required = True)
	return parser

def read_file(filename):
	lines = []
	with open(filename, 'rt') as fin:
		for line in fin:
			line = line.strip()
			if line.find('synset_str_plwn') != -1:
				continue
			lines.append(line)
	return lines

def sample(lines, s_number, s_size):
	samples = []

	random.shuffle(lines)
	up = 0
	for i in range(0, s_number):
		down = i * s_size
		up = i * s_size + s_size
		samples.append(lines[down : up])
		print '<', down, ',', up, ')'
	return samples

def save_samples(samples):
	for n, ss in enumerate(samples):
		out_file = 'sample_%d.csv' % (n)
		with open(out_file, 'wt') as fout:
			for s in ss:
				fout.write('%s\n' % s)

def main(argv = None):
	parser = make_parser()
	args = parser.parse_args()
	
	lines = read_file(args.in_file)
	samples = sample(lines, int(args.sample_count), int(args.sample_size))

	save_samples(samples)

if __name__ == '__main__':
	main()
