Originally posted at https://tech.labs.oliverwyman.com/blog/2017/03/23/generating-beatnik-code/
Beatnik is an esoteric programming language that’s been recently amusing me (as they occasionally do). The core idea is that words in the source code are interpreted as their Scrabble scores, and those scores then do things (mostly involving messing with a stack). This leads then to the possibility of an alternate form of the language, where the scores are explicitly listed, and indeed an earlier implementation of the language defined this form, calling it Wottasquare.
I started building Peacenik as a Beatnik interpreter, but then realised there was a series of other interesting tools around the language I could incorporate. For starters, there’s a Wottasquare interpreter, and a “dumper” that converts from Wottasquare to Beatnik. Given Wottasquare is fractionally easier to write for, can we convert back the other way? Well, you could build a trivial converter that just picks a fixed word for each wanted score, but that’s kinda boring. If however we have some fun with Markov chains, then a bunch of other options become doable. Namely, given a random source text of decent length, we can make a Markov chain that represents the probability of words (and non-word characters) following each other in the source text, and use that to generate Beatnik programs by picking a next word with the right Scrabble score.
Here’s a fun example: take the text of Hamlet and use it to regenerate the ‘Hello World’ example. I’ve tidied it up a little bit for capitalisation and excessive non-word usage, but here’s what it spat out. Remember, this is a valid computer program!
Pol pyrrhus guil wholesome my lord, the court? Ah exeunt by
Saint Alexander. News, laid and will answer of my dear charitable
my lord heavily. That did. Pronouncing this but yet here if damned niobe. He then? This, read you that he himself that we a riotous
filial gore if he, walk’d my lord, Hamlet mount of thinking by saint fair hour of falsehood that, we would fain of that will.
If himself, means saint. Sounded that from guil whose of acres about,
of prithee she’s, of seduce than does well ended enter king. That but
flushing will he, hath live top little this tutor holds tush did
love, thou ha the day certain of you did command my lord but this act
hath made of my love of my fear ay face this arm entertainment than.
He, harbingers laertes of passion that done? Impatient fear assays of
their ends serpent that we can save gone king hear of fashion fire.
Thy man dost but virtue your lap forth that he tells your dear to
feel thy dear perhaps he knavery then goes ceremony my dear lady.
Fed arras, hearing then if father noble dust labour’d by Saint Norway.
Were Lord Gross Osric gets thy mind but if thou pursu’st enter king.
Of defeated that he walk’d that we.
Letter letters of fighting my lord? Rather my lord tumbled that can
travel Laertes. He lack Laertes long shall bruit did long will he I
well King of tedious done top. That ay Horatio, says we two eyes of
if your lap justice my dear Lord?
Will? But she says he hath made of grace court? Laid charitable thee
of wrote? My of these your sum is heir, vow he preaching he went.
Your, he very well, said, methinks fran if, Hamlet thee of God this
man hath borne, wi’ speaks. My state, savageness natural but if thou
thy state. Ham of trumpets your dust justly this did blazes, shot we
the purer of flowers than if christian hits put. Procession that would
pol gainst long distemper, Thou, of unsinew’d fall from top neck from.