in Programming

Haskell Poetry Parser

For our final project for CIS552: Advanced Programming, my partner and I implemented a poetry parser. The program identifies a body of text piped from STDIN as one of: haiku, rhyming poem (aba, or aabba), sonnet, or limerick.

The project source is located at: https://github.com/jamiely/poetry-parser.

The project involved using the CMU Pronouncing Dictionary found here, creating parsers for the dictionary, and base parsers that could be combined for various forms.

For example, we implemented a parser for syllables that allows us to create a haiku parser.

-- | Parses a Haiku
haiku :: PoemParser RhymeMap
haiku = do
  ns 5
  ns 7
  ns 5
  return Map.empty where
  ns = nSyllables

and a rhyming scheme parser that allowed us to recognize limericks:

aabba :: PoemParser RhymeMap
aabba = rhymeScheme &quot;aabba&quot; Map.empty</p>

<p>-- | The stress pattern of a limerick
-- note: syllable requirement is implicit
limerickStress :: PoemParser RhymeMap
limerickStress = do
  stressLine [D,U, D,D,U, D,D,U] 
  stressLine [D,U, D,D,U, D,D,U] 
  stressLine [D,U, D,D,U]<br />
  stressLine [D,U, D,D,U]<br />
  stressLine [D,U, D,D,U, D,D,U]</p>

<p>limerick :: PoemParser RhymeMap
limerick = limerickStress &lt;&amp;&gt; aabba 

For example, we can determine the type of the following poem:

An old silent pond… A frog jumps into the pond, splash! Silence again. – Basho

by running the program thusly:

&gt; ./Main &lt; extra/basho.haiku</p>

<p>

with the result:

Opened file
Poem: 
An old silent pond...
A frog jumps into the pond,
splash! Silence again.</p>

<p>Dictionary:</p>

<p>A  AH0
AGAIN  AH0 G EH1 N
AN  AE1 N
FROG  F R AA1 G
INTO  IH0 N T UW1
JUMPS  JH AH1 M P S
OLD  OW1 L D
POND  P AA1 N D
POND'S  P AA1 N D Z
SILENCE  S AY1 L AH0 N S
SILENT  S AY1 L AH0 N T
SPLASH  S P L AE1 SH
THE  DH AH0</p>

<p>Type: Haiku

And the following poem:

something something fox apple jacks word here pox

by running the program thusly:

&gt; ./Main &lt; extra/fox.rhyming</p>

<p>

with the result:

Opened file
Poem: 
something something fox
apple jacks
word here pox</p>

<p>Dictionary:</p>

<p>APPLE  AE1 P AH0 L
FOX  F AA1 K S
HERE  HH IY1 R
JACKS  JH AE1 K S
POX  P AA1 K S
SOMETHING  S AH1 M TH IH0 NG
WORD  W ER1 D</p>

<p>Type: Rhyming poem: aba