PHP 代码创建否定词词典并搜索帖子是否包含否定词

标签 php search sentiment-analysis words

我正在尝试开发一个 PHP 应用程序,它接受用户的评论,然后匹配字符串以检查评论是正面还是负面。我在 negative.txt 文件中有负面词列表。如果单词列表中的单词匹配,那么我想要一个简单的整数计数器加 1。我尝试了一些链接并创建了 a 代码来检查注释是负面还是正面,但它只匹配最后一个单词文件的。这是我所做的代码。

    <?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?

最佳答案

我在您的 $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;
}

关于PHP 代码创建否定词词典并搜索帖子是否包含否定词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42800739/

相关文章:

PHP MVC模式+REST困惑

php - 如何从 .ns 文件中查看 mongodb 数据

php - 试图将 php 对象插入 mysql DateTime 对象 "ad_endDate"

javascript - 为什么即使我已经在那里添加了一些显示错误消息,零值仍然可以提交?

php - 通过(相似)名称查找文件 PHP

python - 在以 block 读取的字节流中查找模式

jquery - 从 api 自动完成

nlp - 使用 NLP 的实体识别和情感分析

python - 如何保存分类器textblob NaiveBayesClassifier的结果?

python - 在大多数情况下,VADER polar_scores返回输出为“Neutral”