1

嗨,我对 tf-idf 有疑问。代码显示:“0”

这是代码:

$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 是一个数字,用于替换不存在的变量。

问候

4

1 回答 1

0

你的逻辑是错误的:如果你$frase用一个句子替换,你最终会得到$total_termastrue而不是数组,见这个例子

即使$total是一个数组,键也是字符串而不是数字,因为array_count_values.

即使它们的键是数字的,它们也可能是从零开始的,这意味着你的最后一个$frec_term = mysql_num_rows($SQL);将评估为0.

所以会有很多原因$tf_idf0它们都来自 $frec_term 是NULL/ undefined /的事实0

于 2012-12-11T21:22:41.077 回答