Acrostic in Poems

In response to Dan Shiffman coming back to his channel – The Coding Train I felt a great burst of creativity, sparked from his return stream.

In his stream Dan created an acrostic poem program that accepts a word input, finds additional words beginning with each letter, returning an acrostic poem. It uses the Wordnik API. As usual his videos encourage to try it yourself, and to take it further.

I wanted to make an acrostic poem from the lines of an already made poem. I came across the PoetryDB API which has many ways to find poems, by author, title, keywords, or in my case – line count. This was my first time properly working with an API, as little as it is (in requests), I think it’s pretty fun!

A good portion of the development was working out a good way to search the poems. Originally looping through each line and checking for the corresponding character index of the inputted word. This was a massive performance hit… Performance and optimisation is something I need to work on. But I did make some improvements. I ended up joining each line into one string, with a custom divider, albeit hacky. This means I can use a regular expression to match for each character in order.

Here’s the RegExp pattern I used:

As of 11/09/2017 this RegExp can skip lines as long as the word is found within the poem. Eg: PO_E_M. In a way this is acceptable because the odds of finding a perfect solid-word are much lower than a broken up word. But I would like to figure out a clean way, and implement some options, such as:

  • Acrostic Types
    • Beginning
    • Ending
    • Double
    • Middle
  • Case Sensitive/Case Insensitive
  • Complete Word/Broken Word
  • Ignore Punctuation
  • Ignore Whitespace