- an adventure of learning through useless code.
(originally on my medium blog)
Over the last week we were introduced into APIs through the Google Library. Sadly (or not sadly,) I never got to use it. But after my (spectacular, marvelous, show-stopping) mod 1 project I came to a realization!
And now that I have the power of using APIS. It’s time to misuse it. A man once said “With great power comes great responsibility”. It’s time to find out what happens when you have great power and pretty much 0 responsibility. Enter… the infinite monkey theorem.
WITH INFINITE MONKEYS, COMES INFINITE POWER.
The infinite monkey theorem is a thought experiment created in the early 1900s to explain the idea of extremely improbable as compared to impossible. Moreover, (and in my case, most importantly). if thought about literally, IT WOULD BE HILARIOUS. It goes somewhat as such - If there were an infinite amount of monkeys typing for an infinite amount of time, there is a chance *though infinitesimally small* that the monkeys would produce Shakespeare. If we think that there are 26 letters in the alphabet and 28 key presses with the spacebar and enter key, at some point there is a chance that BY RANDOM a monkey could type out Shakespeare. This is *NOT* however, completely impossible, unlike me breathing in outer space, or my dear Mets winning the World Series.
So how do we, with the power of BIG DATA make something similar to this? First I’m gonna find a text. And what better text than one narrated by a monkey. One that describes our almost Sisyphean task…
So now that we have our goal, and have our monkeys. Let’s map out some goals for our program.
First, we need to get the text. To be kind to my fellow primates, I’m going to take it easy on them. First of all, we will be culling ONLY the first 4 lines, because any more than that in a test will make me turn into an angry ape. ALSO we will only be worried about characters and spaces, no punctuation, no capitalization. This gives our monkeys a 1/28 chance of getting the right character at every keystroke, which… might not be as bad? (It’s still very close to impossible.).
hen for our monkeys, they have to spit out random keys (26 letters in the alphabet + space + newline), that would fit the same amount of characters in the first 4 lines of Shakespeare. This is, well, a collection\ of deliverables and methods I’ve just mapped here:
Call -
Calls google api
Retrieves king henry's speech
Sharksphere -
From that speech cut the first 4 lines.
From that culled speech, convert all words into lowercase, remove punctuation.
Returns in a format that can be compared to our monkeys.
Monkeys -
Randomly chooses from 26 letters, spaces, and newlines (/n).
Returns the same amount of characters that are in the original text.
Saves this text to be tested
Test -
Will compare the Sharkspeare output with the Monkey output.
*also note, this will almost always fail in the end.
STEP ONE - CALL
Well guys, I’m going to call it, I can’t seem to find a way to get the full text of Henry the fifth online. I can find the text by using this call. Using https://www.googleapis.com/books/v1/volumes?q=henryV lets you into the text that I want to look for, but getting the full text of that speech is not available in the api as I see it. It is available at your local library!
STEP TWO- SHARKSPHERE
For step two, I had to format my quote into something comparable and actionable. I had to convert it into a format that I can send over to my monkeys. THERE’S RULES TO THIS GAME PEOPLE. So I decided to use an array as my format. This gives me some distinct advantages, with an array I can:
Lowercase all letters
Remove all special characters
Get the length of the array, so our monkeys know when to stop.
Compare to an array of monkeys.
This returns us a collection of letters in an array that make up the speech, removing all special characters and lowercasing all of them.
Now we have to move to the monkeys.
HEY HEY WE’RE THE MONKEYYYSSS - Step 3
To make a monkey typing randomly on a keyboard we have two random methods in ruby, RAND and SAMPLE. Rand gives you a random number (based on a seed of random WRITE A BLOG POST ABOUT THIS), which can be given an argument of a range. Sample takes the argument of an array, which is super useful for our situation. Here I created an array of all lowercase letters, a space, and /n, which is a newline character. After that I do this random action as many times as the Shakespeare quote is long.
AND FINALLY YYYY THE TEST ITSELF
Ok! Now we have our Shakespeare quote, formatted for our test, and our monkeys, ready and willing to knock this ball out of the park. LETS SEE HOW THIS GOES!