APIS, GOOGLE LIBRARY, A MILLION MONKEYS, TEST DRIVEN DESIGN...AND YOU!

- 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!

(goodbye sunday)

(goodbye sunday)

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.


(but also, the idea of monkeys typing is hilarious.)

(but also, the idea of monkeys typing is hilarious.)


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…

(I know, he’s a scientist. Also if you got this far and are complaining, well that’s your fault)

(I know, he’s a scientist. Also if you got this far and are complaining, well that’s your fault)


)
Once more unto the breach, dear friends, once more;
Or close the wall up with our English dead.
In peace there’s nothing so becomes a man
As modest stillness and humility:
But when the blast of war blows in our ears,
Then imitate the action of the tiger;
Stiffen the sinews, summon up the blood,
Disguise fair nature with hard-favour’d rage;
Then lend the eye a terrible aspect;
Let pry through the portage of the head
Like the brass cannon; let the brow o’erwhelm it
As fearfully as doth a galled rock
O’erhang and jutty his confounded base,
Swill’d with the wild and wasteful ocean.
Now set the teeth and stretch the nostril wide,
Hold hard the breath and bend up every spirit
To his full height. On, on, you noblest English.
Whose blood is fet from fathers of war-proof!
Fathers that, like so many Alexanders,
Have in these parts from morn till even fought
And sheathed their swords for lack of argument:
Dishonour not your mothers; now attest
That those whom you call’d fathers did beget you.
Be copy now to men of grosser blood,
And teach them how to war. And you, good yeoman,
Whose limbs were made in England, show us here
The mettle of your pasture; let us swear
That you are worth your breeding; which I doubt not;
For there is none of you so mean and base,
That hath not noble lustre in your eyes.
I see you stand like greyhounds in the slips,
Straining upon the start. The game’s afoot:
Follow your spirit, and upon this charge
Cry ‘God for Harry, England, and Saint George!’

— (from Henry V, spoken by King Henry

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

Later on Sunday evening….

Later on Sunday evening….


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!

(As far as I got before I needed an e-reader)

(As far as I got before I needed an e-reader)


thats not fair.gif


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.

Screen Shot 2019-11-21 at 7.29.12 AM.png

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

monkeys.jpg

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.


Screen Shot 2019-11-21 at 7.29.18 AM.png

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!


DRUMROLL PLEASE…….

<3

<3

Epilogue

In conclusion…

art.jpeg

11/11 My misfits cover band will be awesome.

Oh, all I want to know
All I want

With just a touch of my burning hand
I'm gonna live my life to to destroy your world
Prime directive, exterminate
The whole fuckin' race

438468.jpg
       class Zombie < ActiveRecord::Base
		has many ::victim_zombies
		has_many :victims, through:: victim_zombies
	end
	class Victim < ActiveRecord::Base
		has many ::victim_zombies
		has_many :zombies, through:: victim_zombies
	end
	class Meal (or simplified, VICTIMZOMBIE) < ActiveRecord::Base
		belongs_to :victim
		belongs_to :zombie
	end

10/31, mapping method relationships

Rifle ———- Me .(one to one)

pyle.jpg

LEGION ——-< Many (one to many relationship)

twitter-1024x1024.png

Failed Spiderman Movies >- SPIDERMAN -< Actors Playing Spiderman

(Many to many relationship, in this case a joint relationship, with the joint being spiderman (possible real blog topic?)

turn off the dark.jpg
oldmeme.jpg
toby.jpg