Markov Chain

Markov chains are a very simple but powerful method to predict the next value based on the previous value(s). The principle is not limited to text generation, but often used to generate text. It could be used for example to suggest next words on a mobile phone or a search engine.
Principle: Data (for e.g. text) is analyzed to create probability tables.

markov_chain.jpg
Source

It’s easy to program a markov chain, it’s easier to use a library like markovify.

Install markofivy

The library is not available through the conda package manager,so we’ll use pip. See Install external packages with pip.

conda activate your_environment
conda install pip
pip install markovify

Basic Usage

https://github.com/jsvine/markovify#basic-usage

import markovify

# Get raw text as string.
with open('example_cleaned.txt') as f:
    text = f.read()

# Build the model.
text_model = markovify.Text(text)

# Print five randomly-generated sentences
for i in range(5):
    print(text_model.make_sentence())

# Print three randomly-generated sentences of no more than 280 characters
for i in range(3):
    print(text_model.make_short_sentence(280))
Through language, other people's bodies can become flexible extensions of our attitude toward each thing in the world.
Through language, other people's bodies can become flexible extensions of our attitude toward each thing in the world.
Through language, other people's bodies can become flexible extensions of our attitude toward each thing in the world.
Category assignments go right to the pressures of evolution.
To enhance the chances of its consciousness; the bearers of its consciousness; the bearers of consciousness are patterns.
To enhance the chances of its consciousness; the bearers of consciousness are patterns.
Category assignments go right to the pressures of evolution.
Category assignments go right to the pressures of evolution.
text_model.make_sentence()
'Category assignments go right to the pressures of evolution.'