Skip to content

Instantly share code, notes, and snippets.

@amankharwal
Last active Jan 1, 2021
Embed
What would you like to do?
import numpy as np
def create_transition_matrix(text):
transition_matrix = np.zeros((27, 27))
for i in range(len(text)-1):
current_char = text[i].lower()
next_char = text[i+1].lower()
if (current_char in state) & (next_char in state):
current_char_id = char2id_dict[current_char]
next_char_id = char2id_dict[next_char]
transition_matrix[current_char_id][next_char_id] = transition_matrix[current_char_id][next_char_id] + 1
sum_of_each_row_all = np.sum(transition_matrix, 1)
for i in range (27):
single_row_sum = sum_of_each_row_all[i]
if (sum_of_each_row_all [i] == 0):
single_row_sum = 1
transition_matrix[ i,: ] = transition_matrix[ i,: ] / single_row_sum
return transition_matrix
TM_emma = create_transition_matrix(emma)
TM_caesar = create_transition_matrix(caesar)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment