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

Wicktionary Bookmarklet

A bookmarklet is defined by Wictionary as "A small piece of JavaScript code stored as a URL within a bookmark". I have been using bookmarklets to make my life easier from a long time. Some that I use regularly are [Read Now] : This makes it really easy for me to read pages which are unreadable. [Google Translate] : Translates pages [Mobilise This] : Formats the page for mobile viewing by Google. [Acronym lookup] : This helps me find the meanings for abbreviations. The links in [] brackets are bookmarklets. Just drag them to your bookmark bar. I picked these up from these two pages. There is also a whole website dedicated to them bookmarklets. As a developer, what i like about bookmarklets is that they are coded in JavaScript and i can meddle with them till my heart's content, without worrying about breaking anything. However, i wanted a bookmarklet that would allow me to look up meaning of words. Wiktionary was my open dictionary of choice. They d...

Books, Business, and Steve Jobs. A tribute.

This post is a tribute to Steve Jobs. I know that its been a long time, and i'm late and a thousand different things and so on, but I didn't want to rush things and lose the chance to say something I really felt about this man who has radically changed the way we look at things. I had been to the local stationary shop here in Manipal since my brother wanted to buy a few books. After we had done our thing, we looked around the shop to see what else was new. As we were staring at a 6-in-one book which carried a huge price, the shopkeeper came up to show us this book even as we said we were not going to buy it. This book had plastic sheets that one could attach to the spine of the book. The high price of the item was due to it allowing the lucky owner to decide on the allotment of pages per subject. As we were coming out, i started thinking of how things have changes since i was a kid. Back then we had just a handful of choices and our page size was decided by either an imaginar...

When you say........

......you don't believe in god, you admit there is a god to believe in This is the meme post that started this train of thought in my mind.   I have heard one of my classmates say a long time ago, if women truly believed that they were equal to men, then they would not fight for it. While I never accepted it, i didn't know how to speak against it either. but it was there, somewhere in my mind. I think i made some sense out of it, at last. This is stupidity. This is like saying 'If we Indians seriously believed that freedom was ours, we would never have had to fight for it'. The fight for independance was a fight to make the other party understand and/or accept your viewpoint. The first resonable method might be to consider the other person's viewpoint. And using that as a base point,then work, with suitable proof and arguments, raise, alter, or reconstruct their viewpoint to match ours. This method follows the logic that people can and will be fair in an...