Skip to main content

Markov chain in JavaScript

I made a small Markov Chain joke generator during my coffee break sometime last week. This is in continuation to the last post, where we did a similar thing. I did this specifically to see how well it could be extended in a language which I have typically not used before for ML/NLP.


Let me run you guys through it.

First of all, the Markhov Chains need a bunch of data to tell it how exactly you want your sentences constructed.


str_arr=[sentence1, sentence2,...]


Next, we create a dictionary of all trigrams present across the sentences. To do this, we use all bigrams as keys, and the succeeding word as the corresponding values. The key-value pairs thus form a trigram.
As an example, consider the sentence : “The man had a dog.”
The dictionary for this sentence will have :
[ {[The, man] : [had]}, {[man, had] : [a]}, {[had, a] : [dog]} ]

Next up, using the dictionary that we just made to create sentences. Here we provide the first two words, and let the function work its magic to complete the sentence. The first two words are used as key to search the dictionary for a candidate third word, which is appended to the first two words. Then the second and third words are taken as key, and so on. If there are multiple words as succession candidates for a particular pair, any one of them becomes the Chosen One randomly. The process continues until no succeeding word is found, and the words collected till then form our new sentence.

That’s it! Some observations I would like to make here: One could try to extend the trigrams to n-grams, but complexity will be going up. Instead of selecting from candidate words randomly, one can have a probability-based selection as well. Instead of just sentences as input (and output) one can have paragraphs and even,(if we dare dream so high), whole essays.

Comments

Popular posts from this blog

In the right direction, perhaps ?

i've been toying around with a PHP framework called symfony . actually i wanted to know what MVC ( Model-View-Controller ) was, and i am really comfortable with PHP, so downloaded a copy and made a sandbox on my xampp htdocs directory. after i started playing around it just hit me that how much programming has simplified over the 12 years since i wrote my "hello world" program in c. where 150 people were needed to code a simple website back then, that too in about a year, now things can be done with just one or two programmers in about a fortnight. i think thats awesome. computers are finally doing what they were invented to do - to reduce human work. one really interesting thing is the askeet tutorial. here they guide you through making a replica of their site over 24 days, with each tutorial taking not more than one and a half hours to completely understand and implement. Also the symfony site itself is created on symfony, and that's really cool. ok, what hit me

a keeper from kiterunner

today was watching kiterunner. awesome movie. it was this dialog that hit me hard - "Now, no matter what the mullah teaches, there is only one sin, only one. And that is theft. Every other sin is a variation of theft... When you kill a man, you steal a life. You steal his wife's right to a husband, rob his children of a father. When you tell a lie, you steal someone's right to the truth. When you cheat, you steal the right to fairness... There is no act more wretched than stealing, Amir."

Mind and Friendships

This post seems to be ( atleast in my mind ) a long one. but before i write about the topic of the blog post, i would like to describe my opinions of connected topics which will lead to the actual post. First let us look at the growth of the mind as a graph covering fixed points, each of which are a topic of knowledge. A person's mind, at the beginning of life is like a line ( connecting 2 points, hunger and mother's breast ) which comes into existence only at some points and otherwise is simply a blank. after a few months, the lines become more permanent and stressed. after some more time, as the child grows and learns and understands more things, some more points add to this. if we imagine our mind to be the space enclosed in these points, then we would have a multidimensional graph. (think a really very weirdly shaped object.). However, the " The mind, once expanded to the dimensions of larger ideas, never returns to its original size. " - Oliver Wendell