patrickschur/markov-chain

An implementation of the Markov chain algorithm in PHP.

v1.0 2017-01-14 07:49 UTC

This package is auto-updated.

Last update: 2024-10-17 01:56:48 UTC


README

Build Status codecov Minimum PHP Version License

An implementation of the Markov chain algorithm in PHP.

Installation with Composer

$ composer require patrickschur/markov-chain

How to use

WordTokenizer

use MarkovChain\MarkovChain;
use MarkovChain\Tokenizer\WordTokenizer;
 
$c = new MarkovChain(new WordTokenizer());
 
$c->learn([
    'Lorem ipsum dolor sit amet, consetetur sadipscing elitr.',
    'At vero eos et accusam et justo duo dolores et ea rebum.',
]);
 
print_r($c->classify('et'));
/*
    [
        'accusam' => 0.33333333333333,
        'justo' => 0.33333333333333,
        'ea' => 0.33333333333333
    ]
*/

CharTokenizer

use MarkovChain\MarkovChain;
use MarkovChain\Tokenizer\CharTokenizer;
 
$c = new MarkovChain(new CharTokenizer());
 
$c->learn([
    'Lorem ipsum dolor sit amet, consetetur sadipscing elitr.',
    'At vero eos et accusam et justo duo dolores et ea rebum.',
]);
 
print_r($c->classify('a'));
/*
    [
        'm' => 0.4,
        'd' => 0.2,
        'c' => 0.2,
        'r' => 0.2
    ]
*/