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

import argparse,codecs


def main(inputFile,outputFile,set_col_pos,set_row_pos,set_col_words,set_row_words,del_wrong):
	inFile=codecs.open(inputFile,"r","utf-8")
	outFile=codecs.open(outputFile,"w","utf-8")
	for tup in inFile:
		tup_sp = tup.split(';')
		if tup_sp.__len__()!=4:
			if del_wrong==False:
				print "Wrong line: " + tup
				outFile.write(tup)
		else:
			row=tup_sp[0]
			col=tup_sp[2]
			row_pos = row.split(':')[0]
			col_pos = col.split(':')[0]
			if not set_col_pos.__contains__(col_pos) and not set_row_pos.__contains__(row_pos) and not set_col_words.__contains__(col) and not set_row_words.__contains__(row):
				outFile.write(tup)
	inFile.close()
	outFile.close()

def build_set(inputFile):
	inFile=codecs.open(inputFile,"r","utf-8")
	t=set()
	for line in inFile:
		t.add(line.strip())
	inFile.close()
	return t

if __name__ == '__main__':
	parser = argparse.ArgumentParser(description='Remove unwanted tuples')
	parser.add_argument('-i', '--in', dest = 'inFile', required=True, 
			help = 'File with tuples')
	parser.add_argument('-o','--out', dest = 'outFile', required=True, 
			help = 'Output file')
	parser.add_argument('--col-pos', dest = 'col_pos',
			help = 'File with parts of speech to remove from columns')
	parser.add_argument('--row-pos', dest = 'row_pos',
			help = 'File with parts of speech to remove from rows')
	parser.add_argument('--col-words', dest = 'col_words',
			help = 'File with words to remove from columns')
	parser.add_argument('--row-words', dest = 'row_words',
			help = 'File with words to remove from rows')
	parser.add_argument('-d',dest = 'del_wrong', action='store_true',
			help='Remove wrong lines')

	args = parser.parse_args()

	set_col_pos = set()
	set_row_pos = set()
	set_col_words = set()
	set_row_words = set()

	if args.col_pos!=None:
		set_col_pos = build_set(args.col_pos)
	if args.row_pos!=None:
		set_row_pos = build_set(args.row_pos)
	if args.col_words!=None:
		set_col_words = build_set(args.col_words)
	if args.row_words!=None:
		set_row_words = build_set(args.row_words)

	main(args.inFile,args.outFile,set_col_pos,set_row_pos,set_col_words,set_row_words,args.del_wrong)
