【发布时间】:2017-03-15 03:45:30
【问题描述】:
我正在尝试开发一个 PHP 应用程序,它从用户那里获取 cmets,然后匹配字符串以检查评论是正面的还是负面的。我在negative.txt 文件中有否定词列表。如果单词列表中匹配了一个单词,那么我想要一个简单的整数计数器递增 1。我尝试了一些链接并创建了一个代码来检查注释是负数还是正数,但它只匹配最后一个单词的文件。这是我所做的代码。
<?php
function teststringforbadwords($comment)
{
$file="BadWords.txt";
$fopen = fopen($file, "r");
$fread = fread($fopen,filesize("$file"));
fclose($fopen);
$newline_ele = "\n";
$data_split = explode($newline_ele, $fread);
$new_tab = "\t";
$outoutArr = array();
//process uploaded file data and push in output array
foreach ($data_split as $string)
{
$row = explode($new_tab, $string);
if(isset($row['0']) && $row['0'] != ""){
$outoutArr[] = trim($row['0']," ");
}
}
//---------------------------------------------------------------
foreach($outoutArr as $word) {
if(stristr($comment,$word)){
return false;
}
}
return true;
}
if(isset($_REQUEST["submit"]))
{
$comments = $_REQUEST["comments"];
if (teststringforbadwords($comments))
{
echo 'string is clean';
}
else
{
echo 'string contains banned words';
}
}
?>
【问题讨论】:
-
为什么你只存储 $row['0'] 为什么其他人不索引单词?
标签: php search sentiment-analysis words