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
Metadata
Release files for markov-chain-1 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 | |
|---|---|---|---|
| markov_chain_1-0.1.0.tar.gz | 5.9 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| markov_chain_1-0.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 11.3 kB
Release files / markov_chain_1-0.1.0.tar.gz
| Download URL | markov_chain_1-0.1.0.tar.gz |
|---|---|
| Size | 5.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
d428433cc84efe80755e2c53df9deb7ee5f2294b38d1d8d2b82432fba372d726
|
|
BLAKE2b-256 checksum How to use checksums |
c853997313fd9b8e4720812a8e7e83168d877368a8607fbc2f9781a1c283e40b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.12.6
|
Release files / markov_chain_1-0.1.0-py3-none-any.whl
| Download URL | markov_chain_1-0.1.0-py3-none-any.whl |
|---|---|
| Size | 5.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
ee37f75c47ac2b04a32fcbeb33f9fbe1fc71d673bcbf15a2419839f040112d3e
|
|
BLAKE2b-256 checksum How to use checksums |
97a8241b8d6035e9cd0d3fe90e851766d0495bbb10bb9c3301258d75d2d201fe
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.12.6
|