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

def main():
	parser = argparse.ArgumentParser(description='accumulate.py -- A script for summing up counts in tuples')
	parser.add_argument('--infile', action='store', required=True, help='Input file with tuples')
	parser.add_argument('--outfile', action='store',required=True, help='Output file')

	args = parser.parse_args()

	data = {} # tuple <l,r,p> -> count
	print "Parsing", args.infile
	with codecs.open(args.infile, 'r', 'utf8') as infile:
		for line in infile:
			line = line.strip()
			try:
				l,r,p,c = line.split(';')
				count = int(c)
				t = (l,r,p)
				if t in data:
					data[t] += count
				else:
					data[t] = count
			except:
				print "Wrong line:'%s', skipping" % (line,)
	print "Writing", args.outfile
	with codecs.open(args.outfile, 'w', 'utf8') as outfile:
		for t in data:
			outfile.write('%s;%s;%s;' % t)
			outfile.write('%d\n' % data[t])
	print "All done"

if __name__ == '__main__':
	main()


