and this gives life to thee

Generating Shakespearean Sonnets with Python

Janice Shiu

@contrepoint21

#pyconmy 2018 - 25 August 2018

What is a Shakespearean Sonnet?

William Shakespeare

  • Best known for plays such as Romeo and Juliet, Hamlet, and Macbeth
  • Also wrote 154 sonnets in a specific style

A sonnet is a type of poetry

The Genesis of an Idea...

  • A 3-month programming retreat
  • Two software engineers with a degree in English Literature

"There are only two hard things in computer science: cache invalidation and naming things."

Code Complete

  • A book about developing software

Poem Complete

  • A program to develop poetry such as Shakespearean sonnets

Talk Overview

  • Elements of a Shakespearean sonnet
  • Modelling and generating a Shakespearean sonnet
  • Optimisations
  • Demo
  • Poetry and code

Elements of a Sonnet

  • Form
  • Rhyme
  • Rhythm

Shakespearean Sonnet - Form

Shall I compare thee to a summer's day?
Thou art more lovely and more temperate:
Rough winds do shake the darling buds of May,
And summer's lease hath all too short a date:


Sometime too hot the eye of heaven shines,
And often is his gold complexion dimm'd;
And every fair from fair sometime declines,
By chance, or nature's changing course, untrimm'd;


But thy eternal summer shall not fade
Nor lose possession of that fair thou ow'st;
Nor shall Death brag thou wander'st in his shade,
When in eternal lines to time thou grow'st;


So long as men can breathe or eyes can see,
So long lives this, and this gives life to thee.

Stanza 1

- Quatrain

Stanza 2

- Quatrain

Stanza 3

- Quatrain

Stanza 4

- Couplet

Shakespearean Sonnet - Rhyme

Shall I compare thee to a summer's day?

Thou art more lovely and more temperate:

 

 

 

 

 

Sometime too hot the eye of heaven shines,

And often is his gold complexion dimm'd;

And every fair from fair sometime declines,

By chance, or nature's changing course, untrimm'd;


But thy eternal summer shall not fade

Nor lose possession of that fair thou ow'st;

Nor shall Death brag thou wander'st in his shade,

When in eternal lines to time thou grow'st;


So long as men can breathe or eyes can see,

So long lives this, and this gives life to thee.

a

And summer's lease hath all too short a date:

Rough winds do shake the darling buds of May

a

b

b

 

 

 

 

 

c

d

c

d


e

f

e

f


g

g

Shakespearean Sonnet - Rhythm

Shall  I

compare

thee  to

sum

mer's  day?

iambic pentameter

1

2

3

4

5

compare

  • 2 syllables
  • unstressed-stressed pattern is called an 'iamb'

Modelling a Shakespearean Sonnet in Code

Rhythm

  • "Shall I compare thee to a summer's day?"
  • Iambic pentameter: unstressed-stressed
  • Unstressed is 0, stressed is 1
  • "0101010101"

Form

  • 4 stanzas (3 quatrains, 1 couplet)
def generate_sonnet:
    generate_quatrain
    generate_quatrain
    generate_quatrain
    generate_couplet

Rhyme

  • abab cdcd efef gg

Find a Word's Rhyme and Rhythm

  • pronouncing
    • Python library
    • Uses Carnegie Mellon University's pronouncing dictionary

Shall I compare thee to a summer's day?

>>> import pronouncing as p
>>> phones_list = p.phones_for_word("compare")
>>> p.stresses(phones_list[0])
'01'
>>> phones_list = p.phones_for_word("summer")
>>> p.stresses(phones_list[0])
'10'
>>> import pronouncing as p
>>> "temperate" in p.rhymes("day")
False

Shall I compare thee to a summer's day?

Thou art more lovely and more temperate:

Rough winds do shake the darling buds of May,

And summer's lease hath all too short a date:

>>> "may" in p.rhymes("day")
True

a

b

a

b

Find a Word's Rhyme and Rhythm

Getting the Next Word

Given a word, we need a list of possible words that come after it

The word must also fit the meter and rhyme scheme

  • Select a random word 
  • Use an n-gram

more

art

thou

lovely

  • bigram: two items in a sequence

more

and

temperate

has

Malaysia

the

has

  • n-gram = n is the number of items in a sequence

N-Gram

  • a contiguous sequence of items from a sample

the

world

Malaysia

has

best

food

in

the

Creating a Bigram

thou

art

temperate

art

more

more

lovely

lovely

and

and

more

"Thou art more lovely and more temperate"

Seeding the Bigram

  • Shakespeare's works?
  • Project Gutenberg?
>>> import nltk
>>> from nltk.corpus import gutenberg
>>> len(gutenberg.words())
2621613
>>> generate_bigram(gutenberg.words)

shall

go

weep

this

compare

thee

leave

wish

for

I

and

arise

from

loudly

forgo

to

acquire

speak

Generating a Sonnet - Search Space

shall

I

arise

speak

Generating a Sonnet

shall

I

arise

speak

Generating a Sonnet

shall

compare

wish

I

Generating a Sonnet

shall

compare

wish

I

Generating a Sonnet

shall

compare

I

Generating a Sonnet

this

thee

shall

compare

I

Generating a Sonnet

thee

Optimisation

Should we Weight Words by Frequency?

shall

go

compare

weep

thee

compare

this

wish

leave

wish

for

I

I

and

arise

from

loudly

forgo

to

acquire

speak

this

thee

for

Too many Constraints!

  • Last syllable of 3rd line must rhyme with "day" and fit the rhythm
  • Last syllable of 4th line must rhyme with "date" and fit the rhythm

Shall I compare thee to a summer's day?

Thou art more lovely and more temperate:

Rough winds do shake the darling buds of May,

And summer's lease hath all too short a date:

a

b

a

b

How about first generating the words with the most constraints?

Generating a Line Backwards

may

yes

compare

and

darling

buds

fresh

wish

for

flowers

yellow

in

of

indeed

you

shall

will

thy

select a word that rhymes with "day"

Shall I compare thee to a summer's day?

Thou art more lovely and more temperate:

Stressed or Unstressed?

Shall I compare thee to a summer's day?

vs

They go to school at eight o'clock

  • Depending on context, single-syllable words can be stressed or unstressed
  • `pronouncing` only marks them as stressed syllables

we patched the library

Demo

Generated Shakespearean Sonnet

Sonnet 1535111301

Shall solve it surged in swarms of myrtle trees
Outlast the preacher the phenomenon
Of east was thin the cue of christ to seize
These links the exit scene the amazon

And vital fathers understood between
Its trials and projects you obtain and strove
The silent cannons thunder cloud serene
Be pick the lunge or caught in health that drove

Them bring return return again been by
Among it doubted their diminished din
Excludes the pangs concerning me comply
Present convenient silence love good gin

And asks if wee to dan remain and frogs
And women also buy another hogs
a
b
a
b

c
d
c
d

e
f
e
f

g
g

Generated Shakespearean Sonnet

Sonnet 1535111301

Shall solve it surged in swarms of myrtle trees
Outlast the preacher the phenomenon
Of east was thin the cue of christ to seize
These links the exit scene the amazon

And vital fathers understood between
Its trials and projects you obtain and strove
The silent cannons thunder cloud serene
Be pick the lunge or caught in health that drove

Them bring return return again been by
Among it doubted their diminished din
Excludes the pangs concerning me comply
Present convenient silence love good gin

And asks if wee to dan remain and frogs
And women also buy another hogs

Generated Shakespearean Sonnet

Sonnet 1535111301

Shall solve it surged in swarms of myrtle trees
Outlast the preacher the phenomenon
Of east was thin the cue of christ to seize
These links the exit scene the amazon

And vital fathers understood between
Its trials and projects you obtain and strove
The silent cannons thunder cloud serene
Be pick the lunge or caught in health that drove

Them bring return return again been by
Among it doubted their diminished din
Excludes the pangs concerning me comply
Present convenient silence love good gin

And asks if wee to dan remain and frogs
And women also buy another hogs

Generated Shakespearean Sonnet

Sonnet 1535111301

Shall solve it surged in swarms of myrtle trees
Outlast the preacher the phenomenon
Of east was thin the cue of christ to seize
These links the exit scene the amazon

And vital fathers understood between
Its trials and projects you obtain and strove
The silent cannons thunder cloud serene
Be pick the lunge or caught in health that drove

Them bring return return again been by
Among it doubted their diminished din
Excludes the pangs concerning me comply
Present convenient silence love good gin

And asks if wee to dan remain and frogs
And women also buy another hogs

Future work

  • Generate different types of poetry
    • Spenserian sonnets, limericks, etc
  • Machine learning

and this gives life to thee

As long as people read the sonnet, in some way, the person being written about is still alive

  • Does code come alive? If yes, when?
  • If code can come alive, then can code die? If yes, when?
  • Can code theoretically live forever, just like the memory of the person in the sonnet?

So long as men can breathe or eyes can see,
So long lives this, and this gives life to thee.

Thank you!

  • Adam Palay
  • Recurse Center
  • PyConMy organisers
  • You, the audience

and this gives life to thee

Generating Shakespearean Sonnets with Python

Janice Shiu

@contrepoint21

#pyconmy - 25 Aug 2018

https://github.com/contrepoint/poem_complete

Generating a Line Backwards

may

yes

compare

and

darling

buds

fresh

wish

for

flowers

yellow

in

of

indeed

you

shall

will

thy

select a word that rhymes with "day"

Shall I compare thee to a summer's day?

Thou art more lovely and more temperate:

TBA - make the tree go right to left

Elements of a Sonnet

  • Form
    • What is the structure of the sonnet?
    • How many lines are there?
  • Rhyme
    • Do any words rhyme?
    • Eg: "may" and "day"
  • Rhythm
    • In a sentence, which syllables are unstressed? Which syllables are stressed?

Finding meaning

  • Does the sonnet have a theme?
  • Is there any meaning to this sonnet?
  • Do we as humans intrinsically try to attach meaning to things?
  • If we use a different corpus of words, how would the sonnet change?

may

yes

compare

and

darling

buds

fresh

wish

for

flowers

yellow

in

of

indeed

you

shall

will

thy

select a word that rhymes with "day"

Shall I compare thee to a summer's day?

Thou art more lovely and more temperate:

Generating a Line Backwards

Creating a Bigram

thou

art

temperate

art

more

more

lovely

lovely

and

and

more

more

art

thou

lovely

more

and

temperate

more

art

thou

lovely

  • bigram: two items in a sequence

more

and

temperate

  • trigram: three items in a sequence

art

thou

more

art

more

lovely

more

art

thou

more

art

lovely

  • n-gram = n is the number of items in a sequence

N-Gram

  • a contiguous sequence of items from a sample

Generating a Sonnet

shall

go

compare

weep

in

search

the

wish

leave

perish

but

I

not

and

arise

from

arise

thee

compare

this

wish

thee

this

for

generating a line backwards

  • Select a word that rhymes with "day"
  • Use a reverse bigram to generate the line from the last word

Shall I compare thee to a summer's day?

Thou art more lovely and more temperate:

May

of

buds

darling

Easter Eggs

  • The voice reciting the sonnet

  • The sonnet title. Eg: Sonnet 1533877737

  • Still thinking...

Generate a Sonnet - Pseudocode

Shall I compare thee to a summer's day?
Thou art more lovely and more temperate:
Rough winds do shake the darling buds of May,
And summer's lease hath all too short a date:


Sometime too hot the eye of heaven shines,
And often is his gold complexion dimm'd;
And every fair from fair sometime declines,
By chance, or nature's changing course, untrimm'd;


But thy eternal summer shall not fade
Nor lose possession of that fair thou ow'st;
Nor shall Death brag thou wander'st in his shade,
When in eternal lines to time thou grow'st;


So long as men can breathe or eyes can see,
So long lives this, and this gives life to thee.

Stanza 1 - Quatrain

Stanza 2 - Quatrain

Stanza 3 - Quatrain

Stanza 4 - Couplet

def generate_sonnet:
    generate_quatrain
    generate_quatrain
    generate_quatrain
    generate_couplet

bigram

PyCon

Malaysia

awesome

is

PyCon

Malaysia

is

PyCon

Malaysia

awesome

is

awesome

{
    "PyCon": ["Malaysia"],
    "Malaysia": ["is"],
    "is": ["awesome"]
}

PyCon

Malaysia

awesome

is

an

conference

PyCon

Malaysia

awesome

is

an

conference

PyCon

Malaysia

awesome

is

an

conference

PyCon

Malaysia

awesome

is

an

conference

PyCon

Malaysia

awesome

is

an

conference

WIP - frequent dead ends

solution: reverse bigrams

probably need to sketch this out ugh

and this gives life to thee: Generating Shakespearean Sonnets with Python

By contrepoint

and this gives life to thee: Generating Shakespearean Sonnets with Python

  • 56
Loading comments...

More from contrepoint