PHP code example of fieg / markov

1. Go to this page and download the library: Download fieg/markov library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

fieg / markov example snippets


use Fieg\Markov\MarkovChain;

$sentences = [
    'my blue car',
    'red and blue flowers',
    'his blue car',
];

$chain = new MarkovChain();

foreach ($sentences as $sentence) {
    $tokens = explode(" ", $sentence);

    $chain->train($tokens);
}

$result = $chain->query("blue");

array(2) {
  'car' =>
  double(0.66666666666667)
  'flowers' =>
  double(0.33333333333333)
}