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

def load_list(filename):
	data = {} # (w1,w2) -> tf
	for line in codecs.open(filename, 'r', 'utf8'):
		line = line.strip()
		w1,w2,tfstr = line.split(';')
		data[ (w1,w2) ] = int(tfstr)
	return data


def main():
	file_one = sys.argv[1]
	file_two = sys.argv[2]
	file_three = sys.argv[3]
	out = sys.argv[4]

	data_one = load_list(file_one)
	print "Pairs in %s: %d" % (file_one, len(data_one) )
	data_two = load_list(file_two)
	print "Pairs in %s: %d" % (file_two, len(data_two) )
	data_three = load_list(file_three)
	print "Pairs in %s: %d" % (file_three, len(data_three) )

	# find all pairs
	all_pairs = set([])
	for data in [data_one, data_two, data_three]:
		for pair in data:
			all_pairs.add(pair)
	
	print "Number of different pairs: ", len(all_pairs)

	data_out = [] # (tf, w1, w2)

	# find pairs occuring in at least two sets
	for pair in all_pairs:
		tf = 0
		count = 0
		if pair in data_one:
			tf = data_one[pair]
			count += 1
		if pair in data_two:
			tf += data_two[pair]
			count += 1
		if pair in data_three:
			tf += data_three[pair]
			count += 1
		if count > 1:
			data_out.append(  (tf, pair[0], pair[1] ) )
	
	# write output
	with codecs.open(out, 'w', 'utf8' ) as f:
		for (tf, w1, w2) in reversed(sorted(data_out)):
			f.write('%s;%s;%d\n' % (w1,w2,tf) )


if __name__ == '__main__':
	main()
