#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys
import time
from optparse import OptionParser

KEYS = [
		[' ', 'space'], ['\n','lb'], 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 'š', 'ž', 'z', 't', 'u', 'v', 'w',
		'õ', 'ä', 'ö', 'ü', 'x', 'y', ['.', 'dot'], [',', 'comma'], ['-', 'hypen'], [':', 'colon'], [';', 'scolon'],  ['!', 'excl'], ['?', 'quest'], ["'",'ucomma'], ['/','slash']
		
	#	,'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '(', ')', '_','{', '}', '[', ']','*', '<', '>', '|', '+', '#', '¤', '%', '&', '\\', '=', '~', '@', '€', '£', '$'
	]

keyprint = dict()
for k in KEYS:
	if isinstance(k,list):
		keyprint[k[0]] = k[1]
	else:
		keyprint[k] = k

class Stat:

	def __init__(self):
		self.table = dict()

	def __len(self):
		return len(self.table)

	def __getitem__(self,key):
		if not isinstance(key, tuple):
			return self.table.get(key,0)

		leveler = self.table
		for a in key[:-1]:
			if a not in leveler:
				return 0
			leveler = leveler[a]
		return leveler.get(key[-1],0)

	def __setitem__(self, key, value):
		if not isinstance(key, tuple):
			self.table[key] = value
		else:
			leveler = self.table
			for a in key[:-1]:
				leveler.setdefault(a, dict())
				leveler = leveler[a]
			a = key[-1]
			leveler[a] = value


	def __contains__(self,k):
		return self.table.has_key(k)

	def __iter__(self):
		return iter(self.table)

	def __str__(self):
		return str(self.table)



OUTFILE = 'stats.csv'

def main(argv=None):


	if argv is None:
		argv = sys.argv[1:]

	stat = Stat()

	parser = OptionParser(usage = "usage: %prog [options] FILE1 ...", version="%prog 0.1")
	#parser.add_option('-q', '--quiet',  action="store_false", dest = 'verbose', default=True, help="don't print status messages to stdout")
	parser.add_option('-f', '--file', dest='out_filename', default=OUTFILE, metavar="FILE", help="output file name")

	(options, datafiles) = parser.parse_args(args = argv)

	#stat[u'a',2,3] = 4
	#stat[1,u'ä',6] = 3
	#stat[1,5] = 3
	#stat[2] = 8
	#stat[3,4]= 5

	#print stat[1, 2, 3]
	#print stat[2]
	#print stat[1, u'ä']
	#print stat[8]
	#print stat[5, 4]
	#" a
	#a "
	#a , " a
	#a . "
	
	count = 'count'
	stat = dict()
	stat[count] = dict()
	for k1 in KEYS:
		if isinstance(k1,list):
			k1 = k1[1]
		stat[k1] = dict()
		for k2 in KEYS:
			if isinstance(k2,list):
				k2 = k2[1]
			stat[k1][k2] = 0
		stat[count][k1] = 0
		

	linecount = 0
	time_start = time.time()

	for filename in datafiles:
		print filename
		f = open(filename, 'r')
		for line in f:
			try:
				line = line.decode('utf-8')
			except:
				print line
				continue
				
			#first letter of the line, we need to initialize this "previous"
			prev = line[0].lower().encode('utf-8')
			if prev in keyprint:
				stat[count][keyprint[prev]] += 1

			for letter in line[1:]:
				letter = letter.lower().encode('utf-8')
				
				if letter in keyprint: 
					stat[count][keyprint[letter]] += 1
					
					if prev in keyprint:
						stat[keyprint[prev]][keyprint[letter]] += 1
				prev = letter

			linecount += 1

			if linecount % 10000 == 0:
				sys.stdout.write('|')
				sys.stdout.flush()

			#if linecount > 300000:
				#break
		sys.stdout.write('\n')
		f.close()

	time_end = time.time()

	
	print "Took time %.2f minutes" % ((time_end - time_start) /60.)
	#~ keys = stat[count].keys()
	#~ keys.sort()
	keys = KEYS

	for key in keys:
		if isinstance(key,list):
			key = key[1]
		print "'%s': %s," % (key, stat[count][key]),

	out = open(options.out_filename, 'w')

	for k1 in keys:
		if isinstance(k1, list):
			k1 = k1[1]		
		out.write("\t%s" % k1)
	out.write("\t%s\n" % count)

	for k1 in keys:
		if isinstance(k1, list):
			k1 = k1[1]
		out.write("%s" % k1)
		for k2 in keys:
			if isinstance(k2, list):
				k2 = k2[1]			
			out.write("\t%s" % stat[k1][k2])
		out.write("\t%s\n" % stat[count][k1])



if __name__ == "__main__":
	sys.exit(main())
