簡體   English   中英

PHP計數器增加了一個額外的增量

[英]PHP counter adds an extra increment

我基本上有一個簡單的程序,該程序將一些文本作為表單的輸入,將文本中的所有單詞都匹配到兩個詞典。 一個詞典包含一個肯定詞列表,另一個包含一個否定詞列表。 對於每個正詞匹配,$ posMatchCount都會增加。 對於每個否定詞匹配,$ negMatchCount都會增加。 進行了簡單的比較,如果肯定詞更大,則程序返回“ Positive”,否則返回“ negative”。 如果肯定詞==否定詞,或者沒有正面或負面匹配,則返回“ Neutral”。 這是完整的代碼:

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

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

$ preprocessedDoc2 =“我喜歡這款手機,但討厭電池,我喜歡屏幕尺寸”;

/////////////////////////////////////////////////////////////////////////////////match doc text with POSITIVE sentiment lexicon

$matchedPosWords = NULL;//contains matched words
$posMatchCount = 0;//count of POS matches

$array1 = explode(' ', $preprocessedDoc2);
foreach($array1 as $word){

    if(preg_match("/\s{$word}\s/", $positiveLexicon)){
        $matchedPosWords = $matchedPosWords . $word . " - ";
        $posMatchCount++;
        $posMatch = true; //for subjectivity check
    }
    else{
        $posMatch= false; //for subjectivity check
    }
}

   echo "Matched POSITIVE words: <br><br>";
   echo "<div style=\"background-color:#66FF66\">";
   echo $matchedPosWords . " (Total: {$posMatchCount})";
   echo "</div>";
   echo "<br><br>";

/////////////////////////////////////////////////////////////////////////////////match doc text with NEGATIVE sentiment lexicon   

$matchedNegWords = NULL;//contains matched words
$negMatchCount = 0;//count of NEG matches

$array2 = explode(' ', $preprocessedDoc2);
foreach($array2 as $word2){

    if(preg_match("/\s{$word2}\s/", $negativeLexicon)){
        $matchedNegWords = $matchedNegWords . $word2 . " - ";
        $negMatchCount++;
        $negMatch = true; //for subjectivity check
    }
    else{
        $negMatch = false; //for subjectivity check
    }
}

   echo "Matched NEGATIVE words: <br><br>";
   echo "<div style=\"background-color:#FF5050\">";
   echo $matchedNegWords . " (Total: {$negMatchCount})";
   echo "</div>";
   echo "<br><br>";

/////////////////////////////////////////////////////////////////////////////////comparison between POSITIVE and NEGATIVE words

echo "analyzing document's sentiment ...<br><br>";

function checkPolarity($posWords, $negWords, $posMatch1, $negMatch1){//function to check polarity of doc


    if((($posMatch1==false) && ($negMatch1==false))||($posWords==$negWords)){
        return "<strong>NEUTRAL</strong>"; //if there are no POS or NEG matches, or matches are equal, return NEUTRAL

    }

    if($posWords > $negWords){
        return "<strong>POSITIVE</strong>"; //if count of POS matches is greater than count of NEG matches, return POSITIVE

    }

    else{
        return "<strong>NEGATIVE</strong>"; //if count of NEG matches is greater than count of POS matches, return NEGATIVE

    }



}

$polarity = checkPolarity($posMatchCount, $negMatchCount, $posMatch, $negMatch); //call function to check polarity   

echo "Polarity of the document is: " . $polarity; //display overall polarity
echo "<br><br>";

$polarity = "";



?>

</body>
</html>

但是,即使肯定詞的數量大於否定詞,有時也會返回“神經”。 有時它會額外增加。 例如,字符串輸入“我愛這部手機,但討厭電池,我喜歡屏幕尺寸”,則返回以下內容:

Matched POSITIVE words:

love - adore - - (Total: 3)


Matched NEGATIVE words:

hate - - (Total: 2)

即使只有兩個正匹配項和一個負匹配項,對於正匹配項,計數為3,對於負匹配項,計數為2。 我知道問題很快就會被發現,即使我似乎找不到它。 我會碰運氣的..

我認為代碼看起來沒有錯。 但是您輸入的輸出

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/>";

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM