# -*- coding: utf-8 -*-

import argparse,codecs,random

def main(inputFile,outputFile,number,tf,patt):
	inFile=codecs.open(inputFile,"r","utf-8")
	words= []
	inFile.readline()
	for line in inFile:
		word = line.strip().split(';')
		if word[0].startswith(patt) and int(word[3]) >= tf:
			words.append(word[0])
	inFile.close()
	random.shuffle(words)
	outFile=codecs.open(outputFile,"w","utf-8")
	if number>words.__len__():
		number = words.__len__()
	for i in range(0,number):
		outFile.write(words[i]+'\n')
	outFile.close()

if __name__ == '__main__':
	parser = argparse.ArgumentParser(description='Get list of random words from row_features.csv')
	parser.add_argument('-i', '--in', dest = 'inFile', required=True, 
			help = 'File with row features')
	parser.add_argument('-o','--out', dest = 'outFile', required=True, 
			help = 'Output file with list of random words')
	parser.add_argument('-n', dest = 'number', required=True,
			help = 'Number of random words to get')
	parser.add_argument('-p','--pattern', dest = 'patt', default ='',
			help = 'Take only words which starts with that pattern')
	parser.add_argument('-t','--tf', dest = 'tf', required=True,
			help = 'Minimum TF of words to get')

	args = parser.parse_args()

	main(args.inFile,args.outFile,int(args.number),int(args.tf),args.patt)

