Skip to content

Instantly share code, notes, and snippets.

@cheecheeo
Created July 12, 2014 00:28
Show Gist options
  • Save cheecheeo/85872e07d89fe84b43fb to your computer and use it in GitHub Desktop.
Save cheecheeo/85872e07d89fe84b43fb to your computer and use it in GitHub Desktop.
Fun with Markov Chains in Haskell.
import NLP.Tokenize
import Data.MarkovChain
import System.Random
text = "The protagonist of Hamlet is Prince Hamlet of Denmark, son of the recently deceased King Hamlet, and nephew of King Claudius, his father's brother and successor. Claudius hastily married King Hamlet's widow, Gertrude, Hamlet's mother. Denmark has a long-standing feud with neighbouring Norway, and an invasion led by the Norwegian prince, Fortinbras, is expected.\nThe play opens on a cold winter midnight on \"platform before the castle\" of Elsinore, the Danish royal castle. The sentry Francisco is keeping trusty guard when two figures appear in the darkness. Bernardo, a sentry come to replace Francisco, calls out, \"Who's there?\" Francisco replies, \"Nay, answer me. Stand and unfold yourself.\" Friendly identity proven, Francisco retires to bed. En route, Francisco encounters Horatio and Marcellus who are coming to visit Bernardo. Bernardo and Marcellus discuss the recent appearance of a curious intruder which they describe as a \"dreaded sight\" which they have already bumped into twice on the battlements, but which Horatio is inclined to dismiss as \"but our fantasy.\" Marcellus has brought Horatio along to \"watch the minutes of this night\" in case the scary ghost appears again to fright. The ghost appears, and is described by the three witnesses as looking like the late King Hamlet. They endeavour to open a conversation with it, but \"it is offended\" and \"stalks away.\" The three men take this opportunity to discuss Danish politics, noting that Denmark has begun military preparation because Fortinbras has \"shark'd up a list of lawless resolutes / For food and diet.\" The ghost of Hamlet wanders back. When it declines to talk to them they attack it with daggers, but it escapes. Marcellus admits that this was a bad idea: \"We do it wrong... to offer it the show of violence / For it is... invulnerable.\" They decide to tell prince Hamlet that his father's ghost is up and about.\nThe scene shifts to \"room of state in the castle.\" Various royal figures come in. Claudius and Gertrude talk with Laertes about his upcoming trip to France. His father Polonius admits that he has signed off on this jaunt. The King and Queen then turn to Hamlet. Perturbed by Hamlet's continuing deep mourning for his father and his increasingly erratic behaviour, Claudius and Gertrude try to persuade him to lighten up. They tell him that fathers die all the time, but he does not appear comforted by this."
main = do
r <- getStdGen
putStrLn . unwords . take 100 $ Data.MarkovChain.run 2 (tokenize text) 0 r
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment