Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

29 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Advanced Markov Machine

Python Version License

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.

Features

  • 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

Installation

# 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

Usage

Basic Commands

# 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"

Command Reference

Global Options

Option Description
-m, --model Name of the model to use (created if it doesn't exist)
-l, --list List all available models

Learn Command

Option Description
-t, --text Text to learn from
-f, --file Text file to import

Generate Command

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)

Examples

Example 1: Using the Macron Model

# 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.txt

Example 2: Interactive Learning

python 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)

Example 3: Generating Multiple Variations

# 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

Project Structure


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

Model Format

Models are stored as JSON files with the following structure:

  • dico: Vocabulary list
  • lang: Transition probabilities between words
  • start_words: Starting words and their punctuation
  • points: Punctuation marks used

Customization

Changing Punctuation Marks

# In the script, modify the points list
model = MarkovModel("my_model", points=['.', '?', '!', '…'])

Using Different Models Directory

# Use a custom directory for models
model = MarkovModel("my_model", models_dir="custom_models")

Contributing

Contributions are welcome! Here's how you can help:

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

Testing

# 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]

  1. Better Results: Use larger texts for learning to get more coherent outputs
  2. Reproducibility: Always set a seed value when generating to get consistent results
  3. Model Management: Use --list to see all your saved models
  4. Text Files: Place your input texts in the texts/ directory for easy access

About

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.

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages