【问题标题】:PHP code to create a negative word dictionary and search if a post has negative words用于创建否定词词典并搜索帖子是否包含否定词的 PHP 代码
【发布时间】: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';
        }
    }
    ?>

链接已尝试:Check a string for bad words?

【问题讨论】:

  • 为什么你只存储 $row['0'] 为什么其他人不索引单词?

标签: php search sentiment-analysis words


【解决方案1】:

我在您的 $comments 和您从文件中输入的内容周围添加了 strtolower 函数。这样,如果有人拼写STUPID,而不是stupid,代码仍然会检测到坏词。

我还添加了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;
}

【讨论】:

    【解决方案2】:

    1) 你存储$row['0'] 只是为什么别人不索引词。所以问题是你忽略了文本文件中的一些单词。

    Some suggestion

    1) 在文本文件one by one 中插入文本,即这样的新行,这样您就可以通过换行轻松访问explode,以避免多次explode 和loop。

     Example: sss.txt
     ...
     bad
     stupid
     ...
     ...
    

    2) 对注释和错误字符串应用修剪和小写函数。

    希望它能按预期工作

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

    【讨论】:

    • JyoThi,如果字符串中有好词或中性词,我会收到警告
    • 警告:stristr():C:\xampp\htdocs\sentiment\searchbadwords1.php 中第 46 行字符串的空针是干净的
    • 嘿,我找到了问题的解决方案,它是 BadWords.txt 末尾的空白行。 Ty 为您提供帮助。
    猜你喜欢
    • 2014-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-21
    • 1970-01-01
    • 2017-11-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多