Skip to main content

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

  1. Import the Class: First, import the SmallLanguageModel from your module.
  2. Train the Model: Use the train method with the text data you want the model to learn from.
  3. Generate Text: Utilize the generate_text method to produce text of a desired length.
  4. Predict Characters: Call predict_next_character with 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

markov_chain_1-0.1.0.tar.gz (5.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

markov_chain_1-0.1.0-py3-none-any.whl (5.4 kB view details)

Uploaded Python 3

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

Hashes for markov_chain_1-0.1.0.tar.gz
Algorithm Hash digest
SHA256 d428433cc84efe80755e2c53df9deb7ee5f2294b38d1d8d2b82432fba372d726
MD5 709886b628b63e8671d846a4345f8202
BLAKE2b-256 c853997313fd9b8e4720812a8e7e83168d877368a8607fbc2f9781a1c283e40b

See more details on using hashes here.

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

Hashes for markov_chain_1-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ee37f75c47ac2b04a32fcbeb33f9fbe1fc71d673bcbf15a2419839f040112d3e
MD5 3701e8caa400d27aeb4c71313ed2d8e6
BLAKE2b-256 97a8241b8d6035e9cd0d3fe90e851766d0495bbb10bb9c3301258d75d2d201fe

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page