A simple Markov chain implementation
Project description
Small Language Model
A simple implementation of a character-based small language model using a Markov chain. This model can be trained on input text to generate new text and predict the next character based on the current character.
Table of Contents
Features
- Trains a Markov chain model based on character transitions in the provided text.
- Generates text of a specified length using learned character probabilities.
- Predicts the next character based on the current character using weighted probabilities.
- Handles training data and character transitions efficiently.
Installation
To use the Small Language Model, you need Python installed on your machine. There are no additional dependencies, as it relies on Python's standard library.
Usage
- Import the Class: First, import the
SmallLanguageModelfrom your module. - Train the Model: Use the
trainmethod with the text data you want the model to learn from. - Generate Text: Utilize the
generate_textmethod to produce text of a desired length. - Predict Characters: Call
predict_next_characterwith the current character to get the next predicted character.
Example
Here’s a comprehensive example illustrating how to use SmallLanguageModel:
from your_module import SmallLanguageModel
# Sample text for training
sample_text = """
The quick brown fox jumps over the lazy dog.
Sphinx of black quartz, judge my vow.
Pack my box with five dozen liquor jugs.
How vexingly quick daft zebras jump!
The five boxing wizards jump quickly.
"""
# Create and train the model
model = SmallLanguageModel()
model.train(sample_text)
# Generate and print some text
generated_text = model.generate_text(length=200)
print("Generated text:")
print(generated_text)
# Print some statistics about the Markov chain
print("\nMarkov Chain Statistics:")
print(f"Number of unique characters: {len(model.markov_chain)}")
print("Transition probabilities for 'e':")
if "e" in model.markov_chain:
for next_char, prob in model.markov_chain["e"].items():
print(f" e -> {next_char}: {prob:.2f}")
# Test prediction
print("\nNext character predictions:")
for char in "thequickbrownfox":
next_char = model.predict_next_character(char)
print(f" After '{char}': '{next_char}'")
License
This project is licensed under the MIT License - see the LICENSE file for details.
Acknowledgments This simple model is inspired by the concepts of Markov chains and character-based language modeling. Special thanks to the contributors of the Python programming language for their ongoing support and development. text
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file markov_chain_1-0.1.0.tar.gz.
File metadata
- Download URL: markov_chain_1-0.1.0.tar.gz
- Upload date:
- Size: 5.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d428433cc84efe80755e2c53df9deb7ee5f2294b38d1d8d2b82432fba372d726
|
|
| MD5 |
709886b628b63e8671d846a4345f8202
|
|
| BLAKE2b-256 |
c853997313fd9b8e4720812a8e7e83168d877368a8607fbc2f9781a1c283e40b
|
File details
Details for the file markov_chain_1-0.1.0-py3-none-any.whl.
File metadata
- Download URL: markov_chain_1-0.1.0-py3-none-any.whl
- Upload date:
- Size: 5.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ee37f75c47ac2b04a32fcbeb33f9fbe1fc71d673bcbf15a2419839f040112d3e
|
|
| MD5 |
3701e8caa400d27aeb4c71313ed2d8e6
|
|
| BLAKE2b-256 |
97a8241b8d6035e9cd0d3fe90e851766d0495bbb10bb9c3301258d75d2d201fe
|