0

我有句子,我想分成单词,然后用停用词表中的数据检查它们。我想计算相同数据的数量(总数)。但是,total 并没有给我相同数据的总数。如何总结我需要的数据?谢谢

$word ='temporal. the text mining in a evolutionary a theme patterns  theme threads  clustering'; 
$symbol    = array(".", ",", "\\", "-", "\"", "(", ")", "<", ">", "?", ";", ":", "+", "%", "\r", "\t", "\0", "\x0B");
$cleanMeta = str_replace($symbol, " ", $word);
$key     = strtolower($cleanMeta);
$key = explode(" ", trim($key));

foreach($key as $word_key){
    $query = mysql_query ("SELECT COUNT(stoplist_word) AS total FROM tb_stopword  WHERE stoplist_word = '$word_key'");
    while ($row = mysql_fetch_array($query)) {
        $row1 = $row['total'];
        echo $row1;
    }
}
4

1 回答 1

1

考虑到您已经清理了输入字符串,您不需要对每个单词都进行新查询的 foreach。你可以这样做:

$word ='temporal. the text mining in a evolutionary a theme patterns  theme threads  clustering'; 
$symbol    = array(".", ",", "\\", "-", "\"", "(", ")", "<", ">", "?", ";", ":", "+", "%", "\r", "\t", "\0", "\x0B");
$cleanMeta = str_replace($symbol, " ", $word);
$key       = trim(strtolower($cleanMeta));
$key       = str_replace("'","''",$key);
$keys      = "'".str_replace(" ","', '", $key)."'";

$query  = mysql_query ("SELECT COUNT(stoplist_word) AS total FROM tb_stopword  WHERE   stoplist_word IN ($keys)");
$row    = mysql_fetch_array($query);
$total = $row['total'];

echo $total;

如果你真的想要 foreach 使用:

$total = 0;
foreach($key as $word_key){
    $query = mysql_query ("SELECT COUNT(stoplist_word) AS total FROM tb_stopword  WHERE stoplist_word = '$word_key'");
    while ($row = mysql_fetch_array($query)) {
        $total += $row['total'];
    }
}
echo $total;
于 2012-12-05T09:01:45.160 回答