red hearts flying
deep with the chatter of day
walking home to the photo
the sound
upon red rocks
organ reedy as the highway
in twisted tapers with a winter solstice moon
under a broken bone of night sky
dreams of friends reunited
Markov Poet is a Markov chain-based poetry generator chain that simulates poetic line breaks by including a new line token in its model. It also includes the option of introducing an element of randomness, where by each step in walking through the chain has a chance of moving to any other state before continuing.
markov = Markov()
# This expects a plain text file.
markov.add_file('sample_corpus.txt')
haiku = """the piano room
pure ivory keys
under a layer of dust"""
markov.add_poem(haiku)
lines = ['old pond', 'frog leaping', 'splash']
markov.add_lines(lines)
# Randomness is optional.
randomness = 0.2
generator = markov.generator(randomness)
generator.generate_line()
number_of_lines = 7
print(generator.generate_formatted(number_of_lines))
To run tests:
$ pwd
/path/to/markov-poet
$ python3 -m tests.test
Also included is a demo driver.
usage: demo.py [-h] [--filename FILENAME] [--order ORDER]
[--randomness RANDOMNESS] [--number NUMBER]
Poem generator command line tools.
optional arguments:
-h, --help show this help message and exit
--filename FILENAME, -f FILENAME
specify a file to read from
--order ORDER, -o ORDER
specify the order of the Markov chain
--randomness RANDOMNESS, -r RANDOMNESS
introduce some randomness (between 0.0 and 1.0)
--number NUMBER, -n NUMBER
Print more than one haiku
- --filename/-f : specify the name of a file to read as the reference corpus. The expected format is of plain text. The model tokenizes words and line breaks but will ignore other white space.
- --randomness/-r (between 0.0 and 1.0): introduce an element of randomness into the generation. At 1.0, the chain is ignored and every word is randomly selected from the language. The default value is 0.0.
- --number/-n : specify the number of poems to output. The default is 3.
- --order/-o : specify the size of a state in the chain. The default value is 1.
Example:
$ pwd
/path/to/markov-poet
$ python3 -m sample.demo -f sample/haiku.txt
$ python3 -m sample.demo -f sample/kanye.txt --number 3 --order 2
- Sample haiku corpus from the Haiku Society of America
- Sample Kanye lyrics from Kanye West