Markov chain generator
A command-line Markov chain text generator, that learnsf rom a book and writes new text in its style.
The shy, unknown street? I shall be simply a puppy, and a pack of perspiration stood open. Peasants stood before his
- it reads a book and builds tables of which words follow which 1, 2 or 3-wordsequences
- generates text by picking the next word randomly, weighted by how often it appeared
- uses longest memory when there's a real choice and uses backoff to fall back to shorter memory when there's only one option to avoid plagiarizing the source text
- an originality checker that measures how much of the output is copied word for word
Requirements
- Python 3 (no exterlan packages; only the standard library)
Usage
Put a plain-text book from Project Gutenberg in the books/ folder, then:
python3 main.py <book_file> [options]
| Option | Default | Description |
|---|---|---|
--length |
30 | How many words to generate |
--max-n |
3 | Biggest memory size (n-gram order) |
--min-options |
2 | Backoff threshold: minimum number of possible next words before falling back to shorter memory |
--count |
1 | How many texts to generate |
--experiment |
off | Run the copy-score tests and the min_options experiment |
--override-sentences |
off | Don't force texts to start with a capital letter and end with . ! or ? |
--score |
off | Show how much of each text is copied word for word from the source material |
Examples
python3 main.py crimeandpunishment.txt
wretched little beast was still life and strength that moment without this three roubles; I may have had nothing to do with you. I do no exactly.” “Do tell you something more essential
python3 main.py crimeandpunishment.txt --count 3 --length 50
spoke rather loud noise and animated conversation with a sharp little nose off with my sister--the day before yesterday.’ ‘And where did you go, will all keep him on the score of the old, dirty, by this. Then he remembered quite different. “Yes, Mr. Captain, and I am an ass; forgive myself for
why he went,” Raskolnikov by way will sing ‘My Village’ only, for the smart lady who was so unfortunate girl were asking your _fiancé_ in and weighed upon him. Here is some water. The feeling that it was like the latch on an empty tea-glass on a staircase, he mounted to tell you
scripture, too,” she was growing up, moved a night cap with new mourning had done _that_, could never bear that hardly audibly and God has smitten you, has no skill in such attire. Suddenly he withdrew his short-sighted eyes. You might well not such a fool I was afraid of!” Porfiry Petrovitch, receiving
python3 main.py crimeandpunishment.txt --experiment```
away in her will, ha-ha!). And she would not overlook anything. For the family who wanted a handkerchief, had turned the pocket is honourable. The vast quantities he said that he would not
202521
203612
--- SANITY TEST ---
1.0
--- ACTUAL TEST ---
0.034482758620689655
--- 1 ---
{3: 900}
0.9045977011494255
corner was disproportionately obtuse. There was scarcely any furniture in the big room: in the corner and several chairs--all government furniture, of polished yellow wood. In the further wall there was a time....
--- 2 ---
{1: 380, 2: 361, 3: 159}
0.08275862068965514
They’ve taken them food... I spoke about her mistrustfully, but that probably from his face. “And here... however queer, almost sullen insistence. “You’re right: she put them all so fond of Sonia? Sonia
--- 3 ---
{1: 509, 2: 313, 3: 78}
0.051724137931034475
this hesitating question, Sonia....” He stopped for me...” He rang the glass, put a stop here was the gate. She read loudly, cold with her own account, hasn’t said suddenly. “He is himself,”
--- 4 ---
{1: 555, 2: 288, 3: 57}
0.035632183908045977
and went in. He looked carefully and exactly what the doctor says!” Katerina Ivanovna, pale, thin spare the bridge. He became at parting for a poor student, or hunchback, I forgot! A plain,
--- 5 ---
{1: 599, 2: 266, 3: 35}
0.028735632183908046
“but what help the habit he said this with some other general’s.... Only just getting home in my opinions.... You know my opinion, and describing a change it, the business, and then to
python3 main.py crimeandpunishment.txt --score
After brief hesitation at the police office? Where’s the greatest suspicion of any folly.” They simply looked rather irregular for that moment when he stood with the axe in the noose, he tried to hang himself, and that I did not ask forgiveness, but he was in a fever of dust of the street.
copied: 18%
Results
Tested on Crime and Punishment (about 200k words).
Memory size vs copying The longer the memory the more often there is one possible next word so the generator just copies the book:
| Memory | Keys with only one possible next word |
|---|---|
| 1 word | 59% |
| 2 words | 82% |
| 3 words | 94% |
Backoff vs copying Copy score = share of 5-0word chunks in the output that appear word for word in the book (average of 30 texts per setting):
| min_options | 3-word memory used | Copy score |
|---|---|---|
| 1 (no backoff) | 100% | 91% |
| 2 | 16% | 7.5% |
| 3 | 10% | 6.1% |
| 4 | 5% | 2.6% |
| 5 | 6% | 2.6% |
backoff cuts the copying form 91% to about 7.5. above 2 copying barely drops and the text gets more mashed up. thats why 2 is the default
Running tests
- Create and activate a .venv
- install pytest
- run pytest Thet ests cover tables, copy score, header removal, cleaning and sentence boundiaries
Known Limitations
- Sentence ends inside quotes like "Good-bye!" arent recognised as sentence ends
- a sentence start is detected by a capital letter so names in the middle of a sentence can also be piocked as starts
- in rare cases no sentence end is found within the limit and the program prints a warning
- for text without capitals or punctuation like chats use --override-sentences
Roadmap
- Generating text from other data sources like JSON
- Cleaning the source text depending on the type
- Saving tables to a file
Credits
- Book source: Project gutenberg, shoutout :)
Metadata
Release files for markovka 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| markovka-0.1.0.tar.gz | 12.7 kB | Without provenance | Details for markovka-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| markovka-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for markovka-0.1.0-py3-none-any.whl |
Total release size: 24.1 kB
Release files / markovka-0.1.0.tar.gz
| Download URL | markovka-0.1.0.tar.gz |
|---|---|
| Size | 12.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
0d25d1e2df87e27165696c68f5f372cdd83e3f00962ff7533bfa8d002ea033a2
|
|
BLAKE2b-256 checksum How to use checksums |
ccc8cfba75d20ebb773f9b1775eb6d438a0f55123b8f20596799b77d10e75f94
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.10.12
|
Release files / markovka-0.1.0-py3-none-any.whl
| Download URL | markovka-0.1.0-py3-none-any.whl |
|---|---|
| Size | 11.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
05cbd2f1a9653cdb5521f1d58c73babb91d51f961cc46211ac096c87b94fb5cf
|
|
BLAKE2b-256 checksum How to use checksums |
44b35524463b936eb9d1e4de9faa47085a9a3f91c56c538eb1d90cccb1a816cd
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.10.12
|
Security for markovka 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.