PHP计数器增加一个额外的增量

PHP计数器增加一个额外的增量,php,regex,increment,Php,Regex,Increment,我基本上有一个简单的程序,它从一个表单中获取一些文本作为输入,将文本中的所有单词匹配到两个词典中。一个词典包含肯定词列表,另一个词典包含否定词列表。对于每个正单词匹配,$posMatchCount递增。对于每个负单词匹配,$negMatchCount递增。一个简单的比较就完成了,如果肯定词更大,程序返回“肯定”,否则返回“否定”。如果肯定词==否定词,或者没有肯定或否定匹配,则返回“Neutral”。以下是完整的代码: <?php include("positive_lex

我基本上有一个简单的程序,它从一个表单中获取一些文本作为输入,将文本中的所有单词匹配到两个词典中。一个词典包含肯定词列表,另一个词典包含否定词列表。对于每个正单词匹配,$posMatchCount递增。对于每个负单词匹配,$negMatchCount递增。一个简单的比较就完成了,如果肯定词更大,程序返回“肯定”,否则返回“否定”。如果肯定词==否定词,或者没有肯定或否定匹配,则返回“Neutral”。以下是完整的代码:

        <?php
include("positive_lexicon.php");
include("negative_lexicon.php");
?>
<html>
<head>
    <title>Output</title>
</head>
<body>

<h1>Output</h1>  
<hr>
<?php

即使只有两个正匹配和一个负匹配,它也会为正匹配提供3个计数,为负匹配提供2个计数。我知道问题马上就会被发现,尽管我似乎找不到它。我会试试我的运气。

在我看来,代码看起来没有错。但是你所投入的产出

Matched POSITIVE words:

love - adore - - (Total: 3)


Matched NEGATIVE words:

hate - - (Total: 2)

你在最后一个条目中有一个单独的空间,用于正面或负面的匹配,我认为是错误的。< /P> 如果愿意,请将代码更改为此以进行调试和检查

echo "Foreach for Positive words started <br/>";
foreach($array1 as $word){

    if(preg_match("/\s{$word}\s/", $positiveLexicon) && trim($word) != "" ){
        echo $word."= <br/>"; // there should be no empty word in this
        $matchedPosWords = $matchedPosWords." - ". $word; // there should be no dash at the last, only word
        $posMatchCount++;
        $posMatch = true; //for subjectivity check
    }
    else{
        $posMatch= false; //for subjectivity check
    }
}
echo "Foreach for Positive words Ended <br/>";
echo“Foreach for Positive words start
”; foreach($array1作为$word){ if(preg_match(“/\s{$word}\s/”,$positiveelexicon)&&trim($word)!=”){ echo$word.=
“;//此字段中不应有空单词 $matchedPosWords=$matchedPosWords.“-”$word;//最后应该没有破折号,只有单词 $posMatchCount++; $posMatch=true;//用于主观性检查 } 否则{ $posMatch=false;//用于主观性检查 } } echo“Foreach for Positive words end
”;
Matched POSITIVE words:

love - adore - - (Total: 3)


Matched NEGATIVE words:

hate - - (Total: 2)
echo "Foreach for Positive words started <br/>";
foreach($array1 as $word){

    if(preg_match("/\s{$word}\s/", $positiveLexicon) && trim($word) != "" ){
        echo $word."= <br/>"; // there should be no empty word in this
        $matchedPosWords = $matchedPosWords." - ". $word; // there should be no dash at the last, only word
        $posMatchCount++;
        $posMatch = true; //for subjectivity check
    }
    else{
        $posMatch= false; //for subjectivity check
    }
}
echo "Foreach for Positive words Ended <br/>";