tf-idf出错

Hi I have a problem with tf-idf. The code shows me: "0"

This is the code:

$terms = array_count_values( explode( ' ', $frase ) );
$total_term = asort( $terms );
$total_array = count($total_term);

for ($i=1; $i<=$total_array; $i++){
$SQL = mysql_query("SELECT webTitulo, webDescripcion, webkeywords, weburl FROM webs WHERE MATCH (webTitulo, webDescripcion, webkeywords, weburl) AGAINST ('$total_term[$i]')", $server_link) or die(mysql_error());
$frec_term = mysql_num_rows($SQL);
}
$sssql = mysql_query("SELECT uDR.webTitulo, uDR.webDescripcion, uDR.webkeywords, uDR.weburl, SUM(uDR.priority) as SPriority
FROM (

(SELECT s1.webTitulo, s1.webDescripcion, s1.weburl, s1.webkeywords, 3 as priority FROM webs s1 WHERE MATCH (webTitulo) AGAINST ('$frase'))

UNION

(SELECT s2.webTitulo, s2.webDescripcion, s2.weburl, s2.webkeywords, 1 as priority FROM webs s2 WHERE MATCH (webkeywords) AGAINST ('$frase'))

UNION

(SELECT s3.webTitulo, s3.webDescripcion, s3.weburl, s3.webkeywords, 2 as priority FROM webs s3 WHERE MATCH (webDescripcion) AGAINST ('$frase'))) uDR

GROUP BY uDR.webTitulo, uDR.weburl, uDR.webDescripcion, uDR.webkeywords

ORDER BY SPriority DESC ", $server_link) 
                         or die(mysql_error()); 
$totalRows = mysql_num_rows($sssql);
$tf_idf = $frec_term * log10($totalRows/70);
echo $tf_idf;

70 is a number to replace a variable that does not exist.

Greetings

Your logic is wrong: If you replace $frase with a sentence, you will end up with $total_term as true and not an array, see this example.

Even if $total were an array, the keys would be strings and not numbers because of array_count_values.

And even if they keys would be numerical, they would probably be zero-based, meaning that your last $frec_term = mysql_num_rows($SQL); would evaluate to 0.

So there are many reasons why $tf_idf will be 0 and they all come from the fact that $frec_term is NULL / undefined / 0.