PHP代码,用于创建否定词词典,并在帖子中包含否定词时进行搜索

PHP代码,用于创建否定词词典,并在帖子中包含否定词时进行搜索,php,search,sentiment-analysis,words,Php,Search,Sentiment Analysis,Words,我正在尝试开发一个PHP应用程序,它从用户那里获取评论,然后匹配字符串以检查评论是肯定的还是否定的。我在negative.txt文件中有否定词列表。如果从单词列表中匹配了一个单词,那么我需要一个简单的整数计数器以1递增。我尝试了一些链接,并创建了一个代码来检查评论是否定的还是肯定的,但它只匹配文件的最后一个字 <?php function teststringforbadwords($comment) { $file="BadWords.txt";

我正在尝试开发一个PHP应用程序,它从用户那里获取评论,然后匹配字符串以检查评论是肯定的还是否定的。我在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';
        }
    }
    ?>


尝试链接:

我在您的
$comments
和文件输入周围添加了
strtolower
功能。这样,如果有人拼写了
dumby
,而不是
dumby
,代码仍然会检测到坏单词

我还添加了
trim
,以删除不必要的、破坏性的空白(如换行符)

最后,我改变了你检查单词的方式。我使用了一个
preg_match
来分割所有的空白,这样我们只检查完整的单词,不会意外地禁止不正确的字符串

<?php 
    function teststringforbadwords($comment) 
    {
      $comment = strtolower($comment);
      $file="BadWords.txt";
      $fopen = fopen($file, "r");
      $fread = strtolower(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 $bannedWord)
      {
          foreach (preg_split('/\s+/',$comment) as $commentWord) {
              if (trim($bannedWord) === trim($commentWord)) {
                  return false;
              }
          }
    }
    return true;
}
1)您存储的
$row['0']
只有其他人为什么不索引单词。所以问题是你们忽略了文本文件中的一些单词

一些建议

1) 在文本文件
中逐个插入文本
,也就是像这样的换行,这样您就可以轻松地访问按换行分解,以避免多次分解和循环

 Example: sss.txt
 ...
 bad
 stupid
 ...
 ...
2) 对注释和坏字符串应用修剪和小写函数

希望它能像预期的那样工作


为什么只存储$row['0']而不存储其他索引词?JyoThi,如果字符串中有好的或中性的词,我会收到一条警告:stristr():C:\xampp\htdocs\emotation\searchbadwords1.php中的空指针在第46行字符串是干净的。我找到了问题的解决方案。它是BadWords.txt末尾的一个空行。谢谢你的帮助。
function teststringforbadwords($comment) 
{
  $file="sss.txt";
  $fopen = fopen($file, "r");
  $fread = fread($fopen,filesize("$file"));
  fclose($fopen);

  foreach(explode("\n",$fread) as $word) 
  {

    if(stristr(strtolower(trim($comment)),strtolower(trim($word))))
    {
        return false;
    }
  }
  return true;
}