<?php
$strings[] = 'seventy five yards out';
$strings[] = 'sixty yards out';
$strings[] = 'one hundred fifty yards out';
$inputString = 'seventy two yards out';
$inputWords = str_word_count($inputString, 1);
$probabilities = [];
foreach ($strings as $key => $string) {
$stringWords = str_word_count($string, 1);
$wordsCount = array_count_values($stringWords);
$commonWords = array_intersect($inputWords, array_keys($wordsCount));
if (count($commonWords) > 0) {
foreach ($commonWords as $commonWord) {
if (!isset($probabilities[$key])) $probabilities[$key] = 0;
$probabilities[$key] += $wordsCount[$commonWord];
}
$probabilities[$key] /= count($stringWords);
}
}
arsort($probabilities);
print_r($probabilities);
echo $strings[key($probabilities)];
- Output for 5.6.0 - 5.6.40, 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.34, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.7
- Array
(
[0] => 0.75
[1] => 0.66666666666667
[2] => 0.4
)
seventy five yards out
preferences:
214.77 ms | 404 KiB | 299 Q