Showing posts with label code. Show all posts
Showing posts with label code. Show all posts

Tuesday, January 24, 2012

Snow, my stiff onions

This week, I'll be posting some wintry haiku generated by Poesytron.


Snow, my stiff onions.
Upper-- With boy go to the
snowfalls. Power on.


What I find fascinating in this one is how 'snow' shows up in both the first and last lines.  The Poesytron code attempts to pair consecutive words that get used together, so similar words often end up near each other.  Consider, for example:


ants! she the net the--
as by the snow birthday can't--
ninetieth blow blows


Here we get "the net the" and "blow blows."  In the second case, because Poesytron's database is so small (drawing on 500 haiku written by real people), I can actually track down how the similar words became paired together.  A bit of trawling through let me find these two haiku:

90th birthday
flickering candles
he can't blow out

            m. harper


second birthday —
he blows out all
the dandelion seeds

            collin barber

You can tell from comparing Harper's haiku to Poesytron's above the problem that comes from having such a limited database.  (That's something I'm working on.)  Once Poesytron picked the word birthday, it selected Harper's haiku (out of all haiku with 'birthday' in them in the database--a whopping three) for the next word, and it got stuck there, choosing can't, then ninetieth, then blow from the same haiku.  Then, assuming Poesytron picked birthday again, it then couldn't use it due to the syllable count.  So it went looking for another haiku that used birthday, settled on Barber's haiku, and chose blows for the next word.

This is exactly what I want to happen--words that often get used with the same words get used together.  There are more elegant ways I'd like to code this process, of course, but for now even my crude method is working.

Anyway, back to my original point--somehow, in the first haiku I shared, Poesytron went from snow to snowfall, just as it did from blow to blows, but yet it wound its way through several other words first--stiff, onions, boy....  In this case, though, it's not because Poesytron is 'smart' or that there is some cool linguistic chain that can be drawn through snow-stiff-onions-boy-snowfall, but because there are only so many words in the database (1,815, to be exact), so the laws of probability mean that there's a pretty good chance that two words which "go" together will end up in any given haiku.  (And if you're not convinced, read this.)

Monday, April 18, 2011

Haiku for April 18th: April Showers

I added a few lines of code to Poesytron so that it can create hundreds or even thousands of haiku at once.

Unfortunately, there's some glitch that I can't quite figure out, and after three tries (of trying to get 1,000 haiku), I only had 125.  I wish I had a clue about what's causing it to have errors!

Out of this batch of 125 haiku, nine had the word "rain" in them.  These three made the most sense to me:


or rain, migrating
the sound. Only whiten of
skeletons go with



April showers bring skeletons?


bay into, a-- comes,
full trampled the rain take the--
without dusk garden



April showers bring flowers.  Yes, right on, Poesytron!


Pollen rising inch.
Heat-- bodies, eyes, head, taste on--
lawn, rained it and she--


And flowers bring pollen, and pollen brings... well.  That's rather steamy.

Actually, that second line is probably one of Poesytron's most successful to date.  There is no haiku in the input that has all the words "bodies, eyes, head, taste", but since each successive pair is found together at some point in all the haiku, Poesytron pairs them together.  Even such simple rules for word selection can sometimes work.

Saturday, February 26, 2011

Frogs

The next step after creating random word haiku was to incorporate a way to select words that go together based on some criteria.  I thought that a simple way to do this would be to feed the program a database of human-written haiku, and have it select the next word out of a haiku that the previous word was in.

I created a test haiku database, with 500 haiku that I found by using the preview feature on Google Books.  Then I wrote the code to do this.  It was a little bit tricky to write the code, so I was really happy once I'd figured it out and it was ready to go.  I had built quite a few precautions into it--for example, there is one line that prevents the program from selecting the same word twice in a row.  Or so I thought.

I ran it for the first line, and this is what it gave me:

frog frog frog frog frog


Obviously I had some more work to do.

Sunday, February 20, 2011

How It All Began

The whole idea for writing computer code to generate haiku struck me one day while I was at work.

Ecologists use a lot of statistical programs, but a popular one is R, which is more than just a statistical program in that it is also a programming language.  I've been learning to use this program and language recently to work with ecological data and analyses.  A few weeks ago, I began a project where I wrote code in R to parse text.  We had species descriptions for over 9,000 plant species in South Africa, and I started to learn how to use regular expressions to extract usable bits of data from the paragraph descriptions (for example, the height of the plants or the months when they flower).

While I was learning how to manipulate text in this way, my mind wandered to imagining other possibilities for this type of code.  And that's how the idea was born.

Honestly, I may never have followed up on the idea if I hadn't shared it with my friend Jared, poet and editor of the online magazine The Jivin' Ladybug.  Jared got really, really excited, which got me even more excited, which meant that I went home after talking with him and spent the next two nights up late at night, working on code.

In order to get syllable counts for words, I downloaded a file of 185,000 words with hyphenation marks from the Moby Project, which is a public-domain lexicon.  I was very grateful to find something I could freely use to let me easily calculate syllables.

And that was all I needed in order to generate random-word haiku.

The code I wrote starts out by selecting a random word and then checks the syllable count.  If adding that word to the first line won't make the syllable count for the line go over 5, it adds the word to the line.  Then it does it again, until there are 5 syllables in the first line.  Then it does the same thing for the second line (only with 7 syllables instead of 5), and the third line.

Here is another random word haiku-- the second one that the program wrote:

urushiol glaze
smallholding jabbing Pliny
hypostyle hinder

For those of you who are familiar with R or other programming languages, the code used to generate random word haiku is after the cut.  For the rest of you, all that junk after 'Read More' is just the stuff that tells the computer do to what I described a couple paragraphs above.