import codecs,argparse

def main(listFile,colFile,rowFile,freq_col,freq_row):
	lFile=codecs.open(listFile,"r","utf-8")
	words_row=dict()
	words_col=dict()
	for f in lFile:
		inFile=codecs.open(f.strip(),"r","utf-8")
		for tup in inFile:
			tup_spl = tup.split(';')
			if tup_spl.__len__() ==4:
				count = int(tup_spl[3])
				word=tup_spl[0]
				if words_row.has_key(word):
					words_row[word]=words_row[word]+count
				else:
					words_row[word]=1	
				word=tup_spl[2]
				if words_col.has_key(word):
					words_col[word]=words_col[word]+count
				else:
					words_col[word]=1
			else:
				print "Wrong line: " + tup + " in file: " + f.strip()
		inFile.close()
	lFile.close()

	rFile=codecs.open(rowFile,"w","utf-8")
	for i in words_row:
		if words_row[i] >= freq_row:
			rFile.write(i)
			rFile.write('\n')
	rFile.close()
	cFile=codecs.open(colFile,"w","utf-8")
	for i in words_col:
		if words_col[i] >= freq_col:
			cFile.write(i)			
			cFile.write('\n')
	cFile.close()


if __name__ == '__main__':
	parser = argparse.ArgumentParser()
	parser.add_argument('-i', '--in', dest = 'listFile', required=True, 
			help = 'File with list of files with tuples')
	parser.add_argument('--out-col', dest = 'colFile', required=True, 
			help = 'Output file for columns')
	parser.add_argument('--out-row', dest = 'rowFile', required=True, 
			help = 'Output file for rows')
	parser.add_argument('--freq-col', dest = 'freq_col', default=0, 
			help = 'Minimal frequency for columns')
	parser.add_argument('--freq-row', dest = 'freq_row', default=0, 
			help = 'Minimal frequency for rows')
	args = parser.parse_args()

	main(args.listFile,args.colFile,args.rowFile,int(args.freq_col),int(args.freq_row))
