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

import argparse,codecs


def main(inputFile,outputFile,changeFile):
	changes = build_dict(changeFile)
	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:
			print "Wrong line: " + tup
			outFile.write(tup)
		else:
			row=tup_sp[0]
			col=tup_sp[2]
			row_split = row.split(':')

			if changes.has_key(row_split[0]):
				out_tup = changes[row_split[0]]
				for p in range(1,row_split.__len__()):
					out_tup = out_tup + ':' + row_split[p]
				out_tup = out_tup + ';' + tup_sp[1]+';'
			else:
				out_tup = tup_sp[0]+';'+tup_sp[1]+';'
			
			col_split = col.split(':')
			if changes.has_key(col_split[0]):
				out_tup = out_tup + changes[col_split[0]]
				for p in range(1,col_split.__len__()):
					out_tup = out_tup + ':' + col_split[p]
				out_tup = out_tup + ';' + tup_sp[3]
			else:
				out_tup = out_tup + tup_sp[2]+';'+tup_sp[3]
			outFile.write(out_tup)

	inFile.close()
	outFile.close()

def build_dict(inputFile):
	inFile=codecs.open(inputFile,"r","utf-8")
	t=dict()
	for line in inFile:
		pair = line.strip().split()
		t[pair[0]] = pair[1]
	inFile.close()
	return t

if __name__ == '__main__':
	parser = argparse.ArgumentParser(description='Change name of classes in tuples file')
	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('-c', dest = 'changeFile', required=True,
			help = 'File with pairs of classes (inclass outclass)')

	args = parser.parse_args()

	main(args.inFile,args.outFile,args.changeFile)
