#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys, codecs, argparse

verb_flexes = set(['fin', 'praet', 'impt', 'imps', 'inf', 'ppas', 'pact','pcon', 'pant', 'ger'])

def merge_verb(element):
	try:
		flex,lemma = element.split(':')
		if flex in verb_flexes:
			return 'verb:' + lemma
	except:
		pass
	return element

def main():
	parser = argparse.ArgumentParser(description='merge_verbs.py -- A script for merging verbs of IPI PAN Corpus tagset into single flex class')
	parser.add_argument('--infile', action='store', required=True, help='Input file with tuples')
	parser.add_argument('--outfile', action='store',required=True, help='Output file')
	parser.add_argument('--merge_first', action='store_true', help='Merge verbs of first lexical element of tuple')
	parser.add_argument('--merge_second', action='store_true', help='Merge verbs of second lexical element of tuple (i.e., third element of a tuple)')

	args = parser.parse_args()

	if args.merge_first == False and args.merge_second == False:
		print "Specify at least one of: merge_first or merge_second"
		sys.exit()

	with codecs.open(args.infile, 'r', 'utf8') as infile:
		with codecs.open(args.outfile, 'w', 'utf8') as outfile:
			for line in infile:
				line = line.strip()
				try:
					l,r,p,c = line.split(';')
					if args.merge_first:
						l = merge_verb(l)
					if args.merge_second:
						p = merge_verb(p)
					outfile.write('%s;%s;%s;%s\n' % (l,r,p,c) )
				except:
					print "Wrong line:'%s', skipping" % (line,)

if __name__ == '__main__':
	main()

