#TODO add instructions on changing state from string to actual numerical value

import re
import os
import numpy as np
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 first 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

#DEBUG
print "Below is a listing of the dictionary keys:"
print category_dict.keys()
print ""
print "Below is a listing of the dictionary items"
print category_dict.items()
print ""

# array created to hold transition counts. 8 "current" states with 8 possibilites of another state
# column headings (future state) in order: math, logic, priv, stack ,nop, memory, branch, other
# row headings (current state) in order: math, logic, priv, stack, nop, memory, branch, other
# counts should be added as such: if current state = math and next state is math, z[0,0]+=1, if current 
# state = logic and the next state is stack, then z[1,3]+=1 etc.
#p = np.array([[0,0,0,0,0,0,0,0][0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0]])

p = np.zeros((8,8), dtype=np.int) 

#list to parse through the array the first time and generate a list of all states
wordfound = list()

for word in word_set:
        for line in f1:
                for m in re.finditer(word, line):
                        wordfound.append(word)

print wordfound

#create a statelist mapping the states found (words in the dictionary) to numbers to be used in the transition matrix

statelist = map(lambda word: category_dict[word], wordfound)
transition_counts = Counter(zip(statelist, statelist[1:]))

#print out transition pairs and put them in their associated position in the transition matrix counts
for (x,y), c in transition_counts.iteritems():
	print "(%s,%s) = %d" % (x,y,c)
	p[x,y] = c

#TODO: Add logic to get percentages

f1.close()

