Is there some more efficient way to calculate and extract that array that is greater than or equal to 0.8.
I had to use two foreach to do it, which is not very performant.
The idea is to find the most similar expressions comparing this two arrays.
Thanks!
function getNgrams($match, $n = 2) {
$ngrams = array();
$len = strlen($match);
for($i = 0; $i < $len; $i++) {
if($i > ($n - 2)) {
$ng = '';
for($j = $n-1; $j >= 0; $j--) {
$ng .= $match[$i-$j];
}
$ngrams[] = $ng;
}
}
return $ngrams;
}
$question = 'happy birthday';
$rta = array('today', ' tomorrow ', 'monday', 'birthda');
$question_array = explode(' ',($question));
#print_r($question_array);
$ngram_question = array_map("getNgrams",$question_array);
$ngram_rta = array_map("getNgrams",$rta);
#print_r($ngram_rta);
foreach ($rta as &$valor) {
foreach ($question_array as &$valorq) {
$ngram_rta = getNgrams($valor);
$ngram_q = getNgrams($valorq);
if ((sizeof(array_intersect($ngram_rta,$ngram_q))/sizeof($ngram_rta)) >= 0.8)
{echo $valor;
echo "<br>";
}
}
}