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.

1
2
3
4
5
6
7
8
-- | 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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
aabba :: PoemParser RhymeMap
aabba = rhymeScheme &;quot;aabba" Map.empty
 
-- | 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]
 
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:

1
&gt; ./Main &lt; extra/basho.haiku

with the result:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
Opened file
Poem:
An old silent pond...
A frog jumps into the pond,
splash! Silence again.
 
Dictionary:
 
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
 
Type: Haiku

And the following poem:

something something fox apple jacks word here pox

by running the program thusly:

1
&gt; ./Main &lt; extra/fox.rhyming

with the result:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Opened file
Poem:
something something fox
apple jacks
word here pox
 
Dictionary:
 
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
 
Type: Rhyming poem: aba