Poemgen – learning Python through automated “poetry” generation

In an attempt to learn some Python skills and delve a bit into the area of natural language, I decided to make this project I call “poemgen”. It’s at a very simple stage at the moment, but I plan to expand and improve it in the future.

The basic idea was this: write a program that produces sensical English statements in a “poetic” structure.

The way I accomplished this was by defining different parts of speech that could be used. Nouns and verbs were some of the first to come to mind, of course, but I later expanded to articles, adjectives, prepositions, and gerunds, among others. I assigned each part of speech a number to identify it by. For example, nouns are #1, verbs are #2, articles are #3, and so on. Each part of speech has an array of words in it.

Example from a preliminary version of the nouns list:

nouns = [“flower”, “tree”, “child”, “sun”, “moon”, “darkness”, “light”, “rain”, “beach”, “earth”, “dog”, “cat”, “lover”, “life”, “love”, “heart”, “fish”, “poetry”, “music”, “happiness”, “peace”, “serenity”, “quiet”, “blossoms”, “blooms”, “madness”, “anger”, “sadness”, “dance”, “books”];

After I compiled some sample words for each list, I then thought about how English sentences are formed. Sure, we say hundreds of sentences every day, but on closer inspection, most have a rudimentary structure that can be copied to make other sentences. I labelled each part I found as one of the id numbers for parts of speech above, and soon came to classify sentences as “314351” or some other combination of numbers. It may look cryptic at first glance, but there’s a method here. Each number refers to a part of speech I defined in my table, so putting the numbers together allows a sentence to be built using the parts of speech as elements. By breaking sentences down into their parts, we have the tools with which to build our sentences in a algorithmic way.

The rest of the python script parses these sentence structure id’s, and then goes and fetches a random word from the specified part of speech. It concatenates them together, and there you have a (hopefully) sensical English statement. For the “poem” part, I simply started a new line and choose a new sentence structure to use.

A lot of the things that I got out of the program at first didn’t make a lot of sense, and I’m still working on making it more believable. I’ve received some interesting outputs however.

Here’s an example of a “poem” that my program has generated:

 building a lively heart under a lovely sadness , shaking or singing

the shining happiness taste to a sad earth

the nice heart or the sad lover , growing

shaking the nice music with the beautiful darkness , thinking or loving

being a sad child into the lovely flower , being and singing

As you can see, there’s a lot of work to be done, but I plan to continually improve upon it in the future. It was a fun exercise and pretty neat for something I did as a first Python project.

To-do’s for the future:

  • expand word lists
  • capitalize first words in lines
  • fix the space between commas
  • add more sentence structures
  • add different tenses of words and teach the program how to understand them

If you’re interested, you can view the code for poemgen here.

Another year closing in and some things coming up

So here it is, I’m coming up on finishing my third year of college, and I’m thinking to myself, “What have I accomplished?” The answer doesn’t seem like much, but then again, we often forget the little things of our days and old memories become buried under the constant stress of student life. I plan to use this blog to detail my projects both in school and independently and to keep track of my progress.

Things I am working on now:

  • Conducting an experiment for my Experimental Economics class to test the influence of simple decision-making heuristics on a Bayesian choice task
  • Drafting a writeup of my methods and results
  • Pondering over thesis topics for next year
  • Looking into grad schools
  • Studying for the GRE
  • Working at an organization on campus to provide a customized content management system to numerous companies both on campus and externally
  • Also, trying to eat better and go to the gym more often. I’m going to get healthy!

Things I’m doing this summer:

  • Attending an REU (Research Experience for Undergraduates) at Clemson University. I’m really excited about this one and will be updating with posts about my work there.
  • Updating here with random programming projects or research ideas I run across.

Check back for updates on student life and research, from a unique female perspective.

Philosophy of the Muse Garden blog

For me, I am driven by two main philosophies, know more today about the world than I knew yesterday. And lessen the suffering of others. You’d be surprised how far that gets you.

Neil DeGrasse Tyson

This is my “backbone quote” for this blog and what it will entail.