Description

A generic markov chain library

Package Information

Version1.0.9 (2016-Apr-10)
Repositoryhttps://github.com/Mihail-K/markov
LicenseMIT
CopyrightCopyright © 2015-2016, Mihail K
AuthorsMihail-K
Registered byMihail K
Dependenciesnone

Installation

To use this package, put the following dependency into your project's dependencies section:

dub.json
dub.sdl

Readme

markov

A simple library that provides templated types for markov chains.

Usage

import std.stdio;

import markov;

void main()
{
    // Build states for 1, 2, and 3 leading elements.
    auto chain = MarkovChain!string(1, 2, 3);

    chain.train("foo", "bar", "foo");
    chain.train("bar", "foo", "bar");

    chain.seed("foo");

    foreach(i; 0 .. 10)
    {
        // bar foo bar foo bar . . .
        writeln(chain.generate);
    }
}

Serialization

Simple functions for serializing markov chains into JSON and binary are included.

void main()
{
    // . . .

    // Serialize the markov chain into JSON.
    string json = chain.encodeJSON;

    // Serialize the markov chain into a binary format.
    ubyte[] binary = chain.encodeBinary;

    // Unserialize the JSON-encoded markov chain.
    MarkovChain!string decoded1 = json.decodeJSON!string;

    // Unserialize the binary-encoded markov chain.
    MarkovChain!string decoded2 = binary.decodeBinary!string;
}

In addition, helpers to store encoded data in files are also present.

void main()
{
    // . . .

    // Write a markov chain to a file.
    chain.encodeJSON(File("markov", "w"));

    // Read a markov chain from a file.
    MarkovChain!string decoded = File("markov", "r").decodeJSON!string;
}

License

MIT

Available versions

1.0.9 1.0.8 1.0.7 1.0.6 1.0.5 1.0.4 1.0.3 1.0.2 1.0.1 1.0.0 ~master