Thanks to visit codestin.com
Credit goes to github.com

Skip to content

ArloMichael/OpenMarkov

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 

Repository files navigation

OpenMarkov

A Python module for generating text with Markov chains

Installation

Clone the GitHub repository to your local machine:

git clone https://github.com/ArloMichael/OpenMarkov.git

Usage

  1. Import the Chain class from the module:
from markov import Chain
  1. Create an instance of the Chain class with an optional order parameter to specify the order of the Markov chain (default is 1):
chain = Chain(order=2)
  1. Train the Markov chain by providing a text corpus using the train method:
text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce interdum orci vitae massa efficitur finibus. Nunc nulla massa, malesuada elementum porta at, blandit et erat. Vivamus auctor vehicula libero nec ornare. Donec in eros nulla. Cras a interdum dolor, at viverra enim. Nam cursus nunc dignissim, blandit ex id, commodo tellus. Integer fringilla tortor id tellus egestas vehicula." 
chain.train(text)
  1. Generate text using the generate method. Specify the desired length of the generated text with the length parameter (default is 10). You can also provide an optional seed parameter to set the initial prefix:
generated_text = chain.generate(length=20, seed="Lorem ipsum")
print(generated_text)
  1. Save the trained Markov chain to a JSON file using the save method:
chain.save("model.json")
  1. Load a saved Markov chain from a JSON file using the load class method:
chain = Chain.load("model.json")

Example

Here's a complete example demonstrating the usage of the Markov chain text generator:

from markov import Chain  

# Create an instance of the Chain class
chain = Chain(order=2)

# Train the Markov chain
text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed lacinia augue vitae consequat fermentum."
chain.train(text)

# Generate text
generated_text = chain.generate(length=20, seed="Lorem ipsum")
print(generated_text)

# Save the Markov chain
chain.save("model.json")

# Load a saved Markov chain
loaded_chain = Chain.load("model.json")

About

A Python module for generating text with Markov chains

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages