import codecs
import os

def load_file(filename, flexclass, udict):
	print "Reading words from", filename
	file = codecs.open(filename, "r", "cp1250")

	if udict.has_key(flexclass):
		words = udict[flexclass]
	else:
		words = {}
		udict[flexclass] = words

	for line in file:
		line = line.strip()
		sp = line.split(":=")
		if len(sp) != 2:
			continue
		word = sp[0].strip();
		count = int(sp[1])
		if words.has_key(word):
			words[word] = words[word] + count
		else:
			words[word] = count

	file.close()
	
def read_dir(dir):
	udict = {}
	all = os.listdir(dir)
	print all
	for file in all:
		sp = file.split("_")
		if len(sp) < 2:
			continue
		if sp[-1] == 'stats':
			continue
		p = os.path.join(dir,file)
		print p
		load_file(p, sp[-1], udict)

	return udict

def write_dict(filename, dict):
	print "Writing udict to file",filename
	file = codecs.open(filename, "w", "cp1250")
	for flexclass in dict:
		words = dict[flexclass]
		for word in words:
			word = u"%s:%s := %s\n" % (flexclass,word, words[word])
			file.write(word)

	file.close()

if __name__ == '__main__':
	import sys
	if len( sys.argv ) != 3 :
		print "Usage:\npython udict_builder dir_in file_out"
	else:
		udict = read_dir(sys.argv[1])
		write_dict(sys.argv[2], udict)
