Enter the maze

Shaping poems

by Paul Curzon, Queen Mary University of London

Twitter recently doubled the number of characters allowed in tweets to 280 characters. Some think that this destroys the whole charm of tweets and the way they force conciseness. Brian Bilston tweeted this poem as a response which increases the number of words in each line according to a fibonacci number sequence. It is a famous sequence found often in nature such as in the numbers of petals on flowers and in bee breeding. It runs 1,1,2,3,5,8,13,21, ... with each number the sum of the previous two. There should be more combinations of maths, computing and poetry and it made me think poems are a nice way to explore number sequences through the shapes of poems.

As a result I enjoyed a morning of writing python programs and poems. IN particular here is a little python poem that takes any poem and prints it out in a shape that follows a number sequence.

def printpoem(poemtoprint, shapemethod):
	splitPoem = poemtoprint.split(" ")
	line = 1
	startword = 0
	endword = 0
	while endword < len(splitPoem):
		currentlinelength = shapemethod(line)
		endword = endword + currentlinelength
		thisline = splitPoem[startword:endword]
		thislinetoprint = '\t'.join(thisline)
		print(thislinetoprint)
		line = line + 1
		startword = startword + currentlinelength
	print()

You give it a poem such as Brian's and write a function that gives a number sequence and it formats the poem according to the method. So first we need to write a method that given a number n returns the nth fibonacci number. This is really easy using recursion as what you write corresponds closely to the way we said what it does. If n is 1 or 2 then the answer is 1, but otherwise it is the sum of the previous two fibonacci numbers.

def fibonacci(n):
	if n == 1 :
		return 1
	elif n == 2 :
		return 1
	else :
		return (fibonacci(n-1) + fibonacci(n-2))

So if we call it with Brian's poem and our fibonacci function...

poem = """I wrote a poem in a tweet but then each line grew to 
the word sum of the previous two until I began to worry 
about all these words coming with such frequency because 
as you can see, it can be easy to run out of space when 
a poem gets all Fibonacci sequency"""
printpoem(poem,fibonacci)

We get his poem formatted the right way...

I
wrote
a	poem
in	a	tweet
but	then	each	line	grew
to	the	word	sum	of	the	previous	two
until	I	began	to	worry	about	all	these	words	coming	with	such	frequency
because	as	you	can	see,	it	can	be	easy	to	run	out	of	space	when	a	poem	gets	all	Fibonacci	sequency

However, as I've written a generalised function to solve the problem, not a specific one, you can do lots more. You can provide other functions defining different number sequences and so print it out in different ways. For example to print it out in a rectangular way we need a function that always returns the same value. I've written this in a really general way too..

def rectangle (side):
	"""Return the same number - side -  every time"""
	def rectangleN(n):
		return side
	return rectangleN

So if we call the printpoem method with Brian's poem and our rectangle function, specifying 5 characters in a line ...

printpoem(poem,rectangle(5))

we get a more boring version of the poem (though at least it fits on the page):

I	wrote	a	poem	in
a	tweet	but	then	each
line	grew	to	the	word
sum	of	the	previous	two
until	I	began	to	worry
about	all	these	words	coming
with	such	frequency	because	as
you	can	see,	it	can
be	easy	to	run	out
of	space	when	a	poem
gets	all	Fibonacci	sequency

That gives us a way to explore number sequences by the effect they have on a poem's shape. You might want to try factorial for example, or simple arithmetic sequences like (1,3,5,7, ...)...

Of course we should really write poems to match the number sequence function we are using so here is my first attempt. I wrote a function that creates triangular poems. Actually that was easy as all it requires is the nice simple sequence 1,2,3,4, ... The poem on the other hand was a bit harder, but here goes


def righttriangle(n):
	return n
	
poem2 = "I like triangles of all types, but square cornered ones are especially right for me."

printpoem(poem2,righttriangle)

I get my formatted poem ...

I
like	triangles
of	all	types,
but	square	cornered	ones
are	especially	right	for	me.

What's the point of all this? Probably none, but I enjoyed myself writing poems and programs. Maybe it is a fun way to explore number sequences by writing programs and poems to visualise them. We definitely should have more mixtures of poems and computer science. Roll on National Poetry Day 2018