import re
import os
from collections import Counter

f1 = open('newfile.txt','r')

category_dict = dict()
#Iterate tru the sample text files found in samples_folder, and make the categories dictionary
foldername = "samples_folder"
for txtfile in os.listdir(foldername):
	# obtain the words from a file by transforming its contents into a string, strip it of any
	# trailing blanks, and making an array by spliting it by newlines
	words = open(foldername+"/"+txtfile, "r").read().strip().split('\n')
	# txtfile[:-4] is the name of the file, split it by '_' and take the second element as
	# the name of the category
	category_dict.update(dict.fromkeys(words, txtfile[:-4].split('_')[1]))
	# set for faster word operation
	word_set = set(category_dict.keys())

	line_number = 1
	wordfound = list()
	for word in word_set:
		for line in f1:
			for m in re.finditer(word, line):
				wordfound.append(word)
				#from the words we found, we map them to the state they belong to, i.e. "+" -> Math, etc.
	statelist = map(lambda word: category_dict[word], wordfound)
	transition_counts = Counter(zip(statelist, statelist[1:]))

	#Something like "(Math, Blah Blah) = 9000" will print out, but a list of it
	for (x,y), c in transition_counts.iteritems():
		print "(%s, %s) = %d" % (x,y,c)
f1.close()
