PHP 用于创建否定词词典并搜索 post 是否包含否定词的代码
PHP code to create a negative word dictionary and search if a post has negative words
我正在尝试开发一个 PHP 应用程序,它从用户那里获取评论,然后匹配字符串以检查评论是正面的还是负面的。我在 negative.txt 文件中有否定词列表。如果从单词列表中匹配一个单词,那么我想要一个简单的整数计数器递增 1。我尝试了一些链接并创建了一个代码来检查评论是负面的还是正面的,但它只匹配最后一个单词file.Here 的代码是我所做的。
<?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';
}
}
?>
Link 尝试过: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;
}
1) 你只存储$row['0']
为什么别人不索引词。所以问题是你忽略了文本文件中的一些单词。
Some suggestion
1) 在文本文件 one by one
中插入文本,即像这样的新行,这样您就可以通过换行轻松访问爆炸以避免多次爆炸和循环。
Example: sss.txt
...
bad
stupid
...
...
2) 对注释和错误字符串应用 trim 和小写函数。
希望一切顺利
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;
}
我正在尝试开发一个 PHP 应用程序,它从用户那里获取评论,然后匹配字符串以检查评论是正面的还是负面的。我在 negative.txt 文件中有否定词列表。如果从单词列表中匹配一个单词,那么我想要一个简单的整数计数器递增 1。我尝试了一些链接并创建了一个代码来检查评论是负面的还是正面的,但它只匹配最后一个单词file.Here 的代码是我所做的。
<?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';
}
}
?>
Link 尝试过: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;
}
1) 你只存储$row['0']
为什么别人不索引词。所以问题是你忽略了文本文件中的一些单词。
Some suggestion
1) 在文本文件 one by one
中插入文本,即像这样的新行,这样您就可以通过换行轻松访问爆炸以避免多次爆炸和循环。
Example: sss.txt
...
bad
stupid
...
...
2) 对注释和错误字符串应用 trim 和小写函数。
希望一切顺利
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;
}