6. Matrices and Markov Chains with Python
Activity 6.01: Building a Text Predictor Using a Markov Chain
Solution:
There are a few ways to approach this problem, and it is worth mentioning that the approach we will be taking is perhaps the easiest way in which text prediction is used. In actual practice, text predictions are far more complicated and have many other factors that affect them, which we will briefly cover at the end of the activity.
- We will be using the transcript of the speech given by Winston Churchill at the House of Commons after the soldiers of the Allied forces were rescued from Dunkirk during World War II. The speech by itself is worth a read and can be easily found online if you are interested.
Note
You can download the transcript from https://packt.live/38rZy6v .
- This list is stored in a text file named
churchill.txt
. Read through that text file:# Churchill's speech churchill = open('churchill.txt').read() keywords = churchill.split...