A powerful Markov chain text generator that learns from input texts and generates coherent, stylistically similar content. Perfect for generating speeches, poetry, or any text that mimics the style of the source material.
- Markov Chain Model: Implements a first-order Markov chain for text generation
- Persistent Storage: Models are saved as JSON files for reuse
- Multiple Input Methods: Learn from direct input, text files, or interactive sessions
- Flexible Generation: Customize number of phrases, maximum length, and random seed
- Statistics: View vocabulary size and transition counts
- Command Line Interface: Full argparse support for scripting and automation
# Clone the repository
git clone https://github.com/Nitnaf10/Advanced-Markov-Machine.git
cd markov-text-generator
# No external dependencies required - uses only Python standard library# List all available models
python markov.py --list
# View statistics of a model
python markov.py -m model_name stats
# Generate text
python markov.py -m model_name generate -n 5 -l 30 -s 42
# Learn from a file
python markov.py -m model_name learn -f path/to/text.txt
# Learn from direct text input
python markov.py -m model_name learn -t "Your text here"| Option | Description |
|---|---|
-m, --model |
Name of the model to use (created if it doesn't exist) |
-l, --list |
List all available models |
| Option | Description |
|---|---|
-t, --text |
Text to learn from |
-f, --file |
Text file to import |
| Option | Description |
|---|---|
-n, --nb-phrases |
Number of phrases to generate (default: 1) |
-l, --max-len |
Maximum words per phrase (default: 20) |
-s, --seed |
Random seed for reproducibility (default: random) |
# Generate 3 phrases from the Macron model
python markov.py -m macron generate -n 3 -l 30 -s 42
# View model statistics
python markov.py -m macron stats
# Learn from a new speech
python markov.py -m macron learn -f discours_elysee.txtpython markov.py -m my_model learn
# Enter your text (Ctrl+D or Ctrl+Z to finish):
# This is my sample text that the model will learn from.
# It will analyze the patterns and generate similar text.
# (Press Ctrl+D to finish)# Generate 10 short phrases
python markov.py -m macron generate -n 10 -l 10 -s 123
# Generate 1 long paragraph
python markov.py -m macron generate -n 1 -l 100
markov-text-generator/
├── markov.py # Main script
├── models/ # Directory for model JSON files
│ └── macron.json # Example model
├── texts/ # Directory for input text files
│ └── sample.txt # Example text file
└── README.md # This file
Models are stored as JSON files with the following structure:
dico: Vocabulary listlang: Transition probabilities between wordsstart_words: Starting words and their punctuationpoints: Punctuation marks used
# In the script, modify the points list
model = MarkovModel("my_model", points=['.', '?', '!', '…'])# Use a custom directory for models
model = MarkovModel("my_model", models_dir="custom_models")Contributions are welcome! Here's how you can help:
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
# Test with a small example
python markov.py -m test_model learn -t "Hello world. This is a test."
python markov.py -m test_model generate -n 3
python markov.py -m test_model stats[!Tips]
- Better Results: Use larger texts for learning to get more coherent outputs
- Reproducibility: Always set a seed value when generating to get consistent results
- Model Management: Use
--listto see all your saved models- Text Files: Place your input texts in the
texts/directory for easy access