-
Notifications
You must be signed in to change notification settings - Fork 0
/
markov_chain_v1.py
43 lines (40 loc) · 1.46 KB
/
markov_chain_v1.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#dataset_1 = hstack((X_1_dev,Y_1_dev))
def markovChainModel(data,STATE_LEN):
from collections import OrderedDict
model = OrderedDict()
stateIndex = 0
nextIndexFunction = lambda x: x + STATE_LEN
while (nextIndexFunction(stateIndex) <= len(data)):
nextIndex = nextIndexFunction(stateIndex)
state = tuple(data[stateIndex:stateIndex + STATE_LEN])
next = tuple(data[nextIndex:nextIndex + STATE_LEN])
# Store the number of times we have encountered this next state after the current state.
try:
model[state][next] += 1
except:
if (stateIndex < len(data) - 1):
model[state] = {next:1}
else:
# End of string.
model[state] = {"EOS":0}
stateIndex = nextIndex
return model
def predict(model,state_length,state=-1,count=0,maxCount=500):
# Check for base case.
if (state ==-1):
state = list(model.keys())[0]
#print(state)
chain = [state]
# Predict the next state.
nextStates_dict = model[state]
nextStates = nextStates_dict.keys()
nextStates = sorted(nextStates,key=lambda x: nextStates_dict[x],reverse=True)
nextState = nextStates[0]
print(nextState)
if nextState != "EOS":
#Not done!
nextState = nextStates[0]
chain.extend(predict(model,state_length,state=nextState,count=count,maxCount=maxCount))
count += 1
return chain
string = "Hello, I am sam"