Skip to content

Instantly share code, notes, and snippets.

@amankharwal
Last active December 12, 2023 12:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amankharwal/b56bf48d1d0ffb708199a6aec31b0840 to your computer and use it in GitHub Desktop.
Save amankharwal/b56bf48d1d0ffb708199a6aec31b0840 to your computer and use it in GitHub Desktop.
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