Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

n-gram markov chain transition table

I'm trying to build an n-gram markov model from a given piece of text, and then access the transition table for it so I can calculate the conditional entropy for each sequence of words of length n (the grams). For example, in a 2-gram model, after reading in a corpus of text

"dogs chase cats dogs chase cats dogs chase cats dogs chase cats dogs chase cats dogs chase cats dogs chase cats dogs chase cats dogs chase cats dogs chase people"

and building an internal transition table, the state "dogs chase" may transition to the state "chase cats" with probability 0.9, and to state "chase people" with probability 0.1. If I know of the possible transitions, I can calculate the conditional entropy.

Are there any good python libraries for doing this? I've checked NLTK, SRILM, and others but haven't found much.

like image 392
Dan Avatar asked Nov 02 '22 00:11

Dan


1 Answers

It's only been a little just over 4 years since this post was first created, and I found myself having the same issue. While it is possible to do this manually, I've gone ahead and created the adaptationism package which provides a bit more functionality!

Not only are you able to access transition tables, but you can also do this for any N-gram combinations.

I will continue to build out this toolkit as time goes on, and please feel free to ping me with suggestions for future functionality!

like image 122
shadow_dev Avatar answered Nov 09 '22 15:11

shadow_dev