Skip to content

Commit ac33de1

Browse files
authoredMay 2, 2020
Merge pull request #64 from hartmann-lars/master
Fixed polarity bug getting 'Undefined offset: 1' along with normalization bug getting Divison by zero error.
2 parents 08472f1 + 2e900aa commit ac33de1

File tree

2 files changed

+45
-5
lines changed

2 files changed

+45
-5
lines changed
 

‎src/Sentiment/Vader.php

+9-3
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,12 @@ public function isNegated(array $tokens, bool $includeNt = true) : bool
126126
*/
127127
public function normalize(float $score, int $alpha=15)
128128
{
129-
$normalizedScore = $score/sqrt(($score^2) + $alpha);
129+
$normalizedScore = $score;
130+
131+
if (sqrt(($score^2) + $alpha > 0)) {
132+
$normalizedScore = $score/sqrt(($score^2) + $alpha);
133+
}
134+
130135
if ($normalizedScore < -1.0) {
131136
return -1.0;
132137
} elseif ($normalizedScore > 1.0) {
@@ -176,8 +181,9 @@ public function getPolarityScores(array $tokens) : array
176181
{
177182
$valence = 0.0;
178183
$lcToken = strtolower($tokens[$index]);
179-
if( $lcToken === "kind" && strtolower($tokens[$index+1]) === 'of' ||
180-
isset(self::$this->boosterDict[$lcToken]) ) {
184+
if( $lcToken === "kind"
185+
&& (array_key_exists($index+1, $tokens) && strtolower($tokens[$index+1]) === 'of') ||
186+
isset(self::$this->boosterDict[$lcToken]) ) {
181187

182188
$sentiments[] = $valence;
183189
} else {

‎tests/TextAnalysis/Sentiment/VaderTest.php

+36-2
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public function testGetPolarityScores()
9797
$examples[] = ['sent' => "Make sure you :) or :D today!", 'neg'=> 0.0, 'neu'=> 0.294, 'pos'=> 0.706, 'compound'=> 0.8633];
9898
$examples[] = ['sent' => "Today SUX!", 'neg'=> 0.0, 'neg'=> 0.779, 'neu'=> 0.221, 'pos'=> 0.0, 'compound'=> -0.5461];
9999
$examples[] = ['sent' => "Today only kinda sux! But I'll get by, lol", 'neg'=> 0.179, 'neu'=> 0.569, 'pos'=> 0.251, 'compound'=> 0.2228];
100-
100+
101101
$vader = new Vader;
102102

103103
foreach($examples as $test)
@@ -113,9 +113,43 @@ public function testIssue44OffsetError()
113113
return;
114114
}
115115

116-
$vader = new Vader;
116+
$vader = new Vader;
117117
$result = $vader->getPolarityScores([ 'great', 'for', 'the', 'jawbone']);
118118
$this->assertEquals(0.577, $result['pos']);
119119
}
120+
121+
public function testSentimentScoreKindOfCombo()
122+
{
123+
if( getenv('SKIP_TEST')) {
124+
return;
125+
}
126+
127+
$sentimentScores = vader(['kind']);
128+
129+
$this->assertEquals(0.6197, $sentimentScores['compound']);
130+
}
131+
132+
public function testNormalizeZeroSum()
133+
{
134+
if( getenv('SKIP_TEST')) {
135+
return;
136+
}
137+
$tokens = [
138+
'If','the','Fake','News','Opposition','Party',
139+
'is','pushing','with','all','their','might',
140+
'the','fact','that','President','Trump',
141+
'“ignored','early','warnings','about',
142+
'the','threat','','then','why','did',
143+
'Media','&','Dems','viciously',
144+
'criticize','me','when','I',
145+
'instituted','a','Travel','Ban',
146+
'on','China','They','said',
147+
'“early','&','not','necessary.”','Corrupt','Media'
148+
];
149+
150+
$sentimentScores = vader($tokens);
151+
152+
$this->assertEquals(-1, $sentimentScores['compound']);
153+
}
120154

121155
}

0 commit comments

Comments
 (0)
Please sign in to comment.