Tool to generate text from Markov's chains, phrase generator based on calculated frequencies and randomness.
Text Generator (Markov Chain) - dCode
Tag(s) : Data Processing
dCode is free and its tools are a valuable help in games, maths, geocaching, puzzles and problems to solve every day!
A suggestion ? a feedback ? a bug ? an idea ? Write to dCode!
Markov Chains allow the prediction of a future state based on the characteristics of a present state. Suitable for text, the principle of Markov chain can be turned into a sentence generator.
In the textual context, a first-order Markov chain considers that a given word will be followed by certain words with specific probabilities, calculated from a training corpus.
The process involves three main steps:
— Corpus analysis: Parsing the source text and creating a dictionary of word-to-word transitions. The algorithm maintains a term tree where each node (word) points to its possible successors.
— Transition matrix construction: Calculating the frequencies and probabilities of each transition; each node is associated with a respective frequency value.
— Random generation: Using a pseudo-random generator to select the next word based on the calculated probabilities. This generates a string of words that are likely related.
Example: In the senstence 'the raven and the fox', the word the has a 50% chance of being followed by raven and 50% chance of being followed by fox, while the word and has a 100% chance of being followed by the.
Parameters
The number of words per sentence counts the words of 3 letters and more and thus ignores most of stop-words. This parameter is an average value, some sentences will be longer and others shorter.
Order 1: Each word depends only on the previous word
Order 2: Each word depends on the two previous words
Order n: Each word depends on the n previous words
The higher the order, the more the generated text respects the local structure of the original corpus, but requires more training data to avoid over-specialization.
dCode is limited to order 1.
The sentences generated by the text generator are often meaningless but seem to follow the grammar of the used language.
Several techniques improve coherence:
— Increasing the order: Moving from order 1 to order 2 or 3
— Corpus filtering: Using high-quality, thematically coherent texts
— Transition weighting: Favoring frequent transitions
— Syntactic constraints: Applying basic grammatical rules
— Post-processing: Correcting punctuation and capitalization after generation
To get better sentences, use artificial intelligences based on natural language semantics (ChatGPT, etc.), these AIs use the principle of Markov chains to determine the next word.
dCode retains ownership of the "Text Generator (Markov Chain)" source code. Any algorithm for the "Text Generator (Markov Chain)" algorithm, applet or snippet or script (converter, solver, encryption / decryption, encoding / decoding, ciphering / deciphering, breaker, translator), or any "Text Generator (Markov Chain)" functions (calculate, convert, solve, decrypt / encrypt, decipher / cipher, decode / encode, translate) written in any informatic language (Python, Java, PHP, C#, Javascript, Matlab, etc.) or any database download or API access for "Text Generator (Markov Chain)" or any other element are not public (except explicit open source licence). Same with the download for offline use on PC, mobile, tablet, iPhone or Android app.
Reminder: dCode is an educational and teaching resource, accessible online for free and for everyone.
The content of the page "Text Generator (Markov Chain)" and its results may be freely copied and reused, including for commercial purposes, provided that dCode.fr is cited as the source (Creative Commons CC-BY free distribution license).
Exporting the results is free and can be done simply by clicking on the export icons ⤓ (.csv or .txt format) or ⧉ (copy and paste).
To cite dCode.fr on another website, use the link:
In a scientific article or book, the recommended bibliographic citation is: Text Generator (Markov Chain) on dCode.fr [online website], retrieved on 2025-09-16,