Advertisement
Guest User

Untitled

a guest
Apr 18th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.14 KB | None | 0 0
  1. def compute_transition_matrix(order, l0, l1, l2, C_t, C_tt, C_ttt, num_tokens):
  2. """
  3. """
  4. # for order = 2
  5. if order == 2:
  6. transition_matrix = defaultdict(lambda: defaultdict(int))
  7. for key1 in C_tt:
  8. for key2 in C_tt[key1]:
  9. if C_t[key1] != 0:
  10. transition_matrix[key1][key2] = l1 * (C_tt[key1][key2] / float(C_t[key1])) + l0 * (C_t[key2] / float(num_tokens))
  11.  
  12. # for order = 3
  13. else:
  14. transition_matrix = defaultdict(lambda: defaultdict(lambda: defaultdict(int)))
  15. for key1 in C_ttt:
  16. for key2 in C_ttt[key1]:
  17. for key3 in C_ttt[key2]:
  18. if C_tt[key1][key2] == 0:
  19. transition_matrix[key1][key2][key3] = l1 * (C_tt[key2][key3] / float(C_t[key2])) + l0 * (C_t[key3] / float(num_tokens))
  20. elif C_tt[key1][key2] != 0 and C_t[key2] != 0:
  21. transition_matrix[key1][key2][key3] = l2 * (C_ttt[key1][key2][key3] / float(C_tt[key1][key2])) + l1 * (C_tt[key2][key3] / float(C_t[key2])) + l0 * (C_t[key3] / float(num_tokens))
  22.  
  23. return transition_matrix
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement