1

行で見つかったクエリの単語数に基づいて一致を評価しようとしています。私はこれをじっと見つめました:

SELECT Text, MATCH(`Text`) AGAINST ('$s') AS Grade

しかし、すぐにこれが機能しないことに気付きました。これはGrade、たとえば単語の順序、各単語の長さなどの多くのものに基づいているためです。

行に存在する単語の割合を知りたいだけです。

例えば:

$s = 'i want pizza'
`Text` = 'pizza I want' // In this case Grade should be 100 as all words are found

その他の例:

Text             | Grade
pizza I want too | 100 // All words were found, It doesn't matter if there are extra words
pizza I want     | 100
i want           | 66 // Only 66% of the words are present
want want want   | 33 // Only 33% of the words are present
4

1 に答える 1

1
$s = 'i want pizza';
$text = 'pizza I want';

//move to lower-case to ignore case-differences
$s = strtolower($s);
$text = strtolower($text);

//count the number of words in $s
$slen = count(explode(" ", $s));
//create an array of words from the text that we check
$arr = explode(" ", $text);
$count = 0;
//go over the words from $text and count words that appear on $s
foreach ($arr as $word) {
    if(strpos($s, $word) !== false){
        $count++;
    }
}
//display the percentage in format XX.XX
echo number_format((double)(100 * $count/$slen),2);
于 2012-07-17T07:17:59.877 に答える