使用表中的数据计算相同的单词数据

时间:2012-12-05 08:52:02

标签: php mysql

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

$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;
    }
}

1 个答案:

答案 0 :(得分: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;