A small, clean C++ project that implements a Markov chain–based sentence generator.
This project is intentionally written in a modular and beginner-friendly way, so each part of the pipeline (tokenizing, training, modeling and generation) is easy to understand, test and extend.
The goal is not to build a “smart” language model, but a correct and transparent stochastic text engine.
Given a text file, the program:
- tokenizes the text into words and punctuation,
- builds a Markov model of fixed order N,
- samples from that model to generate a new sequence of tokens,
- prints the generated text.
The output is a probabilistic remix of the original text, based only on local word history.
types
Common type aliases and hash helpers used across the project.tokenizer
Converts raw text into a sequence of tokens.model
Stores the Markov transition table:state (last N tokens) → possible next tokens.trainer
Builds the model from a token stream.generator
Samples tokens from a trained model.main
CLI entry point.
This project uses CMake.
From the project root:
cmake -S . -B build
cmake --build buildThis produces the executable:
build/markov_text_cli./build/markov_text_cli <input_text_file>For a chosen order of N, the model learns the transitions of the form:
[token_{i-N}, ..., token_{i-1}] → token_iDuring generation:
- a starting state is chosen,
- the next token is sampled from the learned transitions,
- the window is shifted forward,
- the process repeats for a fixed number of tokens.
The generator does not understand grammar or meaning — it only models local token statistics.
This project is released under the MIT License.