Path: blob/master/5-Text-Generation.ipynb
164 views
Text Generation
Introduction
Markov chains can be used for very basic text generation. Think about every word in a corpus as a state. We can make a simple assumption that the next word is only dependent on the previous word - which is the basic assumption of a Markov chain.
Markov chains don't generate text as well as deep learning, but it's a good (and fun!) start.
Select Text to Imitate
In this notebook, we're specifically going to generate text in the style of Ali Wong, so as a first step, let's extract the text from her comedy routine.
Build a Markov Chain Function
We are going to build a simple Markov chain function that creates a dictionary:
The keys should be all of the words in the corpus
The values should be a list of the words that follow the keys
Create a Text Generator
We're going to create a function that generates sentences. It will take two things as inputs:
The dictionary you just created
The number of words you want generated
Here are some examples of generated sentences:
'Shape right turn– I also takes so that she’s got women all know that snail-trail.'
'Optimum level of early retirement, and be sure all the following Tuesday… because it’s too.'
Additional Exercises
Try making the generate_sentence function better. Maybe allow it to end with a random punctuation mark or end whenever it gets to a word that already ends with a punctuation mark.