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.