Skip to main content

Originally a proof of concept, I’ve used this in enough projects that I’ve decided to publish it tomake it easier to import. The name is a play on words similar to Markup/Markdown. Hey, I’m funny sometimes,

Basic Use

Use the included from_sentences() and from_words() if your data already behaves nicely. These methods return a Markov object which is the workhorse of this whole thing. Your input sequences of words or sentences should be delimited by newlines. Use next_word() and next_sentence() to generate your desired forat of output.

Advanced Use

Instantiate with m = markoff.Markov(seeds) where seeds is an iterable of sub-iterables. That could be a list of lists of words or letters if you want to emulate the functionality of from_sentences() and from_words(). Each sub-iterable being a chain in the set of chains you want to model.

You can supply it with just one chain or many.

Then use m.generate(max_length=100) to produce a single chain limited to max_length automatically terminating at known ending state (usually a word ending with punctuation).

Examples

Don’t forget to begin with import pymarkoff ## Generating Sentences ##

Input

m = pymarkoff.from_sentences(
"""The quick brown fox jumped over the lazy dog.
Jack and Jill ran up the hill to fetch a pail of water.
Whenever the black fox jumped the squirrel gazed suspiciously."""
)

print([m.next_sentence() for i in range(10)])

Output

[
    'The quick brown fox jumped over the black fox jumped the lazy dog.',
    'The quick brown fox jumped the squirrel gazed suspiciously.',
    'Whenever the squirrel gazed suspiciously.',
    'Jack and Jill ran up the lazy dog.',
    'Jack and Jill ran up the hill to fetch a pail of water.',
    'Jack and Jill ran up the black fox jumped the hill to fetch a pail of water.',
    'Whenever the lazy dog.',
    'The quick brown fox jumped over the lazy dog.',
    'Jack and Jill ran up the hill to fetch a pail of water.',
    'Jack and Jill ran up the squirrel gazed suspiciously.'
 ]

Generating Words

Input

seeds = """Ana
Bastion
D.Va
Genji
Hanzo
Junkrat
Lúcio
McCree
Mei
Mercy
Pharah
Reaper
Reinhardt
Roadhog
Soldier: 76
Symmetra
Torbjörn
Tracer
Widowmaker
Winston
Zarya
Zenyatta"""

brain = pymarkoff.from_words(mystr)
print([brain.next_word() for i in range(10)])

Output

['Zen', 'D.Vaperein', 'Za', 'To', 'Merya', 'Metrdo', 'So', 'Junjör', 'Ph', 'Mera']

More advanced use

The Markov.feed() method can be used to add more data into the model of a Markov object. This lets you add sentences or words one at a time, if you want.

Notes

This module is still under development and is mostly for me to play around with and learn Markov Chains. Cheers.

Metadata

Release files for pymarkoff 0.4.6

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pymarkoff 0.4.6
File Size Uploaded
pymarkoff-0.4.6.tar.gz 6.2 kB Details

Release files / pymarkoff-0.4.6.tar.gz

Download URL pymarkoff-0.4.6.tar.gz
Size 6.2 kB
Tags Source
SHA-256 checksum
How to use checksums
c1b7d52ae3fd8670ccfb80310e56f8ae5ed23dd5611384ba81be173f019d06e5
BLAKE2b-256 checksum
How to use checksums
a1e66742b9427ea0d18d23087af69a8d2ca657b681210ac5dd7d19c856202821
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.4.6 This release

1 release file

0.4.5

1 release file

0.4.4

1 release file

0.4.3

1 release file

0.4.2

1 release file

0.4.1

1 release file

0.4.0

1 release file

0.3.4

1 release file

0.3.3

1 release file

0.3.2

1 release file

0.3.1

1 release file

0.2.5

1 release file

0.2.4

1 release file

0.2.3

1 release file

0.2.2

1 release file

0.2.1

1 release file

0.2.0

1 release file

0.1.3

1 release file

0.1.2

1 release file

0.1.1

1 release file

0.1.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page