Weighted Randomised Selection

Learn about weighted randomized selection for the favorite words.

We'll cover the following...

We can improve the behavior of our text generator by updating the process we use to select our next word. Currently, our algorithm is simply choosing a word entirely at random. This is in contrast to the behavior we want—randomly selecting a word but preserving the relative probability of word sequences. The changes to achieve this can be found in the code below:

Press + to interact
<?php
class MarkovGenerator
{
protected $dictionary = [];
protected $totals = [];
public function loadText($text)
{
// ...
foreach ($this->dictionary as $sequence => $values) {
$this->totals[$sequence] = array_sum($values);
}
}
protected function getWord($seed)
{
$rand = rand(1, $this->totals[$seed]);
foreach ($this->dictionary[$seed] as $word => $count) {
$rand -= $count;
if ($rand <= 0) {
return $word;
}
}
return null;
}
public function generateText($seed = null, $length = 100)
{
if ($seed == null) {
$seed = array_rand($this->dictionary);
}
// Set the output to the initial seed word.
$output = $seed;
for ($i = 0; $i < $length; $i++) {
// Stop generating if the word does
// not exist inside our dictionary.
if (! array_key_exists($seed, $this->dictionary)) {
break;
}
$nextWord = array_rand($this->dictionary[$seed]);
$nextWord = $this->getWord($seed);
if ($nextWord == null) {
break;
}
$output .= ' ' . $nextWord;
// Reset the seed to continue our generation.
$seed = $nextWord;
}
return $output;
}
}

We are creating a new ...