marc is a small, but flexible Markov chain generator
Project description
About
marc (markov chain) is a small, but flexible Markov chain generator.
Usage
marc is easy to use. To build a MarkovChain
pass the object a sequence of items:
from marc import MarkovChain
sequence = [
'Rock', 'Rock', 'Rock', 'Paper', 'Rock', 'Scissors',
'Paper', 'Paper', 'Scissors', 'Rock', 'Scissors',
'Scissors', 'Paper', 'Scissors', 'Rock', 'Rock', 'Rock',
'Paper', 'Scissors', 'Scissors', 'Scissors', 'Rock'
]
chain = MarkovChain(sequence)
The learned transition matrix can be accessed through the matrix
attribute:
print(chain.matrix)
# [[0.5, 0.25, 0.25], [0.2, 0.2, 0.6], [0.375, 0.25, 0.375]]
Though, the output is perhaps better viewed as a pandas DataFrame
:
import pandas as pd
df = pd.DataFrame(
chain.matrix,
index=chain.encoder.index_,
columns=chain.encoder.index_
)
print(df)
# Rock Paper Scissors
# Rock 0.500 0.25 0.250
# Paper 0.200 0.20 0.600
# Scissors 0.375 0.25 0.375
Use the next
method to generate the next state (seeded or unseeded):
chain.next('Rock')
# 'Rock'
chain.next()
# Paper
The next
method can also generate multiple states with the n
argument:
chain.next('Paper', n=5)
# ['Scissors', 'Paper', 'Rock', 'Paper', 'Scissors']
MarkovChain
objects are iterable. This means that they can be passed directly to the next
function:
next(chain)
# 'Scissors'
next(chain)
# Rock
Example
A fully worked example of marc in action (block text provided by quote):
import random
import re
from quote import quote
from marc import MarkovChain
quotes = quote('shakespeare', 250)
print(quotes[0])
# {'author': 'William Shakespeare',
# 'book': 'As You Like It',
# 'quote': 'The fool doth think he is wise, but the wise man knows himself to be a fool.'}
text = '\n'.join([q['quote'] for q in quotes])
text = text.lower()
tokens = re.findall(r"[\w']+|[.,!?;]", text)
tokens[:5]
# ['the', 'fool', 'doth', 'think', 'he']
chain = MarkovChain(tokens)
def generate_sentences(chain, n=2, length=(10, 20)):
for _ in range(n):
l = random.randint(length[0], length[1])
nonsense = ' '.join(chain.next(n=l))
print(nonsense)
generate_sentences(chain)
# and unless by some are fascinated by the hour upon the wind faithful
# those that hath had a very much as flaws go
Install
pip install -U marc
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
File details
Details for the file marc-2.0.tar.gz
.
File metadata
- Download URL: marc-2.0.tar.gz
- Upload date:
- Size: 3.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.4.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.6.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 587a22fc078333a6a35b7646422cca121115d3d630f09fdc5eb1ee4ef5d75e5f |
|
MD5 | fabb791a2db3466eedf53f4835cfdcb0 |
|
BLAKE2b-256 | 9195eabff55fc5d68ed7c982526de79768ee34ffb4f64ecb606a6410f9e8fdd1 |