Skip to content

Instantly share code, notes, and snippets.

@microamp
Last active August 16, 2021 02:21
Show Gist options
  • Save microamp/d11e442e28a7b8743efaf2febdc9ff36 to your computer and use it in GitHub Desktop.
Save microamp/d11e442e28a7b8743efaf2febdc9ff36 to your computer and use it in GitHub Desktop.
Study Group - 2021-03-26

“Through the night and day”

The Problem

  • Never written a poem before

The Solution

  • Make the computer generate a poem for me

How?

Markov Chain

  • Wikipedia:
    	 A Markov chain is a stochastic model describing a sequence of possible events in which the probability of each event depends only on the state attained in the previous event.
        
  • e.g.
    • Phrase: “And the Golden Grouse And the Pobble who”
    • Prefixes + Suffixes
	   |------------------+--------|
	   | Prefix (size: 2) | Suffix |
	   |------------------+--------|
	   | And the          | Golden |
	   | the Golden       | Grouse |
	   | Golden Grouse    | And    |
	   | Grouse And       | the    |
	   | And the          | Pobble |
	   | the Pobble       | who    |
	   | Pobble who       | N/A    |
	   |------------------+--------|
  • Autogenerated:
    • “And the Pobble who”
    • “And the Golden Grouse And the Pobble who”
    • “And the Golden Grouse And the Golden Grouse And the Pobble who”
    • “And the Golden Grouse And the Golden Grouse And the Golden Grouse And the Pobble who”

Process

  1. Download a large number of poems off the Internet (But from where? Google it! There’s no API. Many are poorly formatted. HTML styling?)
  2. Build a Markov chain based on the text extracted
  3. Autogenerate phrases on the fly (variations: prefix length, the starting prefix, etc.)

Short Demo

“Through the night and day”

"Through the night and day"

Through the skyless moonless gardens and the swamp-perfume,
And I hear is bossy death telling me which way to heaven

Through the city streets they're flooding,
As with voices and with my childhood's faith

Through the afternoon into high August blue,
The vapour trails of two clocks

Through the year, is heavier at Christmastime,
The season of good fortune met scoffing and blame

Through the crowd, who, being merely human,
Must remember nothing of much but beauty

Through the heavy metal ring in a shift of cloud,
The night-blossom hangs heavy,
With the knowledge of death is past

Through the wide world and all in rest

Through the night and day
@microamp
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment