Skip to content

Commit 14462b1

Browse files
author
Lars Hartmann
committed
Fixed polarity bug getting 'Undefined offset: 1' when looking for the word 'of' after 'kind' where kind is the last word in the sentence
1 parent 08472f1 commit 14462b1

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

src/Sentiment/Vader.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,9 @@ public function getPolarityScores(array $tokens) : array
176176
{
177177
$valence = 0.0;
178178
$lcToken = strtolower($tokens[$index]);
179-
if( $lcToken === "kind" && strtolower($tokens[$index+1]) === 'of' ||
180-
isset(self::$this->boosterDict[$lcToken]) ) {
179+
if( $lcToken === "kind"
180+
&& (array_key_exists($index+1, $tokens) && strtolower($tokens[$index+1]) === 'of') ||
181+
isset(self::$this->boosterDict[$lcToken]) ) {
181182

182183
$sentiments[] = $valence;
183184
} else {

tests/TextAnalysis/Sentiment/VaderTest.php

Lines changed: 13 additions & 2 deletions
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,20 @@ 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+
}
120131

121132
}

0 commit comments

Comments
 (0)