The Grep-ing of the Snark
Reading and Writing Electronic text is a class about writing poetry using existing source text and Python. It seemed appropriate to begin my experiments with my favorite poem, Lewis Carroll’s “The Hunting of the Snark.” The poem has many repeating words, which makes it an excellent subject for grep-style functions. It’s not exactly an epic, but it’s pretty long, so I chose to transform its lines into a series of much smaller poems.
The first two poems that I was pleased with were simple grep operations (i.e. I searched for lines that contained a set of specific words). My first poem is titled, “Forgot”:
He forgot when he entered the ship: He had wholly forgotten his name. But I wholly forgot (and it vexes me much) Where the Baker had met with the Snark.
This is the code I used:
1 2 3 4 5 6 7 8 9 10 | import sys baker = "Baker" snark = "Snark" forgot = "forgot" for line in sys.stdin: line = line.strip() if baker in line and snark in line or forgot in line: print line |
The next poem is titled “Friends”:
His intimate friends called him "Candle-ends," Such friends, as the Beaver and Butcher became, And cemented their friendship for ever! Let me tell you, my friends, the whole question depends
And the accompanying code:
1 2 3 4 5 6 7 8 | import sys friends = "friends" for line in sys.stdin: line = line.strip() if friends in line: print line |
After grep-ing for the words “silence” and “nothing,” I decided to play a little more with the formatting and shuffling of the resulting lines. I split the lines where there was an end to a sentence, and randomized the result. Unfortunately, it still kept the lines coupled, despite the new line I inserted (i.e. they still appear in order). I ran the program a bunch of times, and this is my favorite:
Each thought he was thinking of nothing but "Snark" And my heart is like nothing so much as a bowl Then, silence. Some fancied they heard in the air There was silence supreme! Not a shriek, not a scream, When it rose to its feet, there was silence like night, But the crew would do nothing but groan.
And here is the code I used to achieve it:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | import sys import random silence = "silence" nothing = "nothing" all_lines = list() for line in sys.stdin: line = line.strip() if silence in line or nothing in line: line = line.replace(". ", ".\n") line = line.replace("! ", "!\n") all_lines.append(line) random.shuffle(all_lines) for line in all_lines: print line |
My Github repository contains all of these programs and poems, as well as the source text.
Proof of nonsense 😉
See also: http://www.flickr.com/photos/bonnetmaker/8195084199/
001 “Just the place for a Snark!” the Bellman cried,
002 As he landed his crew with care;
003 Supporting each man on the top of the tide
004 By a finger entwined in his hair.
005 “Just the place for a Snark! I have said it twice:
006 That alone should encourage the crew.
007 Just the place for a Snark! I have said it thrice:
008 What I tell you three times is true.”
These lines help us to make a program which displays those statements in a statement list, which can be found in that list at least three times. That tool can help us to silence all those overanalyzing wiseguys who dare to look for sense and meaning in The Hunting of the Snark.
========== Source ==========
#! /usr/bin/haskell
import Data.List
statementList =
[“I’m very much afraid I didn’t mean anything but nonsense!”
,”Just the place for a Snark!”
,”Just the place for a Snark!”
,”6 * 7 = 42″
,”I’m very much afraid I didn’t mean anything but nonsense!”
,”6 * 7 = 39″
,”6 * 7 = 39″
,”Just the place for a Snark!”
,”6 * 7 = 42″
,”I’m very much afraid I didn’t mean anything but nonsense!”
,”6 * 7 = 39″
]
atLeastThrice :: [String] -> [String]
atLeastThrice sL = [head grp | grp = 3]
========== Result ==========
ghci> atLeastThrice statementList
[“6 * 7 = 39″,”I’m very much afraid I didn’t mean anything but nonsense!”,”Just the place for a Snark!”]