php - 统计与表中数据相同的单词数据

标签 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;
    }
}

最佳答案

考虑到您已经清理了输入字符串,您不需要对每个单词进行新查询的 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;

关于php - 统计与表中数据相同的单词数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13719344/

相关文章:

php - 查找MySQL中商店列表的排名位置

mysql - mybatis选择两个时间之间的记录

php - 如何将 php 中的多个条目以及静态信息插入表中

javascript - PHP @move_uploaded_file 使用 Resumable.js 返回 false

php - mysql 如何在某些托管站点上编辑 my.ini

mysql - 结合SUB_DATE和TIMEDIFF在mysql中减去1小时

mysql - 2个单独表格的总和

mysql - 在 mysql 5.0.77 中定义触发器的语法错误

mysql - 如何在clsql中特殊mysql sock路径?

php - 哪些 PHP 框架适合纯 Web 服务应用程序?