@@ -83,17 +83,19 @@ public function __construct()
8383 * Add a new token and score to the lexicon
8484 * @param string $token
8585 * @param float $meanSentimentRating
86+ * @return void
8687 */
87- public function addToLexicon (string $ token , float $ meanSentimentRating )
88+ public function addToLexicon (string $ token , float $ meanSentimentRating ) : void
8889 {
8990 $ this ->lexicon [$ token ] = $ meanSentimentRating ;
9091 }
9192
9293 /**
9394 * Remove a token from the lexicon
9495 * @param string $token
96+ * @return void
9597 */
96- public function deleteFromLexicon (string $ token )
98+ public function deleteFromLexicon (string $ token ) : void
9799 {
98100 unset($ this ->lexicon [$ token ]);
99101 }
@@ -124,12 +126,13 @@ public function isNegated(array $tokens, bool $includeNt = true) : bool
124126 * approximates the max expected value
125127 * @param float $score
126128 * @param int $alpha
129+ * @return float
127130 */
128- public function normalize (float $ score , int $ alpha =15 )
131+ public function normalize (float $ score , int $ alpha =15 ) : float
129132 {
130133 $ normalizedScore = $ score ;
131134
132- if (sqrt (($ score ^2 ) + $ alpha > 0 ) ) {
135+ if (sqrt (($ score ^2 ) + $ alpha) > 1 ) {
133136 $ normalizedScore = $ score /sqrt (($ score ^2 ) + $ alpha );
134137 }
135138
@@ -178,7 +181,7 @@ public function scalarIncDec(string $word, float $valence, bool $isCapDiff)
178181 public function getPolarityScores (array $ tokens ) : array
179182 {
180183 $ sentiments = [];
181- for ($ index = 0 ; $ index < count ($ tokens ); $ index ++)
184+ for ($ index = 0 , $ indexMax = count ($ tokens ); $ index < $ indexMax ; $ index ++)
182185 {
183186 $ valence = 0.0 ;
184187 $ lcToken = strtolower ($ tokens [$ index ]);
@@ -196,7 +199,7 @@ public function getPolarityScores(array $tokens) : array
196199 return $ this ->scoreValence ($ sentiments , $ tokens );
197200 }
198201
199- public function scoreValence (array $ sentiments , array $ tokens )
202+ public function scoreValence (array $ sentiments , array $ tokens ): array
200203 {
201204 if ( !empty ($ sentiments )) {
202205 $ sentimentSum = array_sum ($ sentiments );
@@ -247,7 +250,7 @@ public function getSentimentValence(float $valence, array $tokens, int $index)
247250 if (isset ($ this ->getLexicon ()[$ lcToken ]))
248251 {
249252 //get the sentiment valence
250- $ valence = $ this ->getLexicon ()[$ lcToken ];
253+ $ valence = ( float ) $ this ->getLexicon ()[$ lcToken ];
251254 //check if sentiment laden word is in ALL CAPS (while others aren't)
252255 if ($ ucToken and $ isCapDiff ) {
253256 if ($ valence > 0 ) {
@@ -262,17 +265,17 @@ public function getSentimentValence(float $valence, array $tokens, int $index)
262265 if ($ index > $ startIndex && !isset ($ this ->getLexicon ()[ strtolower ($ tokens [$ index -($ startIndex +1 )])]))
263266 {
264267 // dampen the scalar modifier of preceding words and emoticons
265- // (excluding the ones that immediately preceed the item) based
268+ // (excluding the ones that immediately preceded the item) based
266269 // on their distance from the current item.
267270 $ s = $ this ->scalarIncDec ($ tokens [$ index -($ startIndex +1 )], $ valence , $ isCapDiff );
268- if ($ startIndex == 1 and $ s != 0 ) {
271+ if ($ startIndex === 1 and $ s != = 0 ) {
269272 $ s *= 0.95 ;
270- } elseif ($ startIndex == 2 and $ s != 0 ) {
273+ } elseif ($ startIndex === 2 and $ s != = 0 ) {
271274 $ s *= 0.9 ;
272275 }
273276 $ valence += $ s ;
274277 $ valence = $ this ->neverCheck ($ valence , $ tokens , $ startIndex , $ index );
275- if ($ startIndex == 2 ) {
278+ if ($ startIndex === 2 ) {
276279 $ valence = $ this ->idiomsCheck ($ valence , $ tokens , $ index );
277280 }
278281
@@ -283,7 +286,7 @@ public function getSentimentValence(float $valence, array $tokens, int $index)
283286 // "cooking with gas": 2, "in the black": 2, "in the red": -2,
284287 // "on the ball": 2,"under the weather": -2}
285288 }
286- $ valence = $ this ->leastCheck ($ valence , $ tokens , $ index );
289+ $ valence = $ this ->leastCheck (( float ) $ valence , $ tokens , $ index );
287290 }
288291
289292 }
@@ -356,7 +359,7 @@ public function butCheck(array $tokens, array $sentiments)
356359 return $ sentiments ;
357360 }
358361
359- for ($ i = 0 ; $ i < count ($ sentiments ); $ i ++)
362+ for ($ i = 0 , $ iMax = count ($ sentiments ); $ i < $ iMax ; $ i ++)
360363 {
361364 if ( $ index < $ i ) {
362365 $ sentiments [$ i ] *= 0.5 ;
@@ -444,7 +447,7 @@ public function boostExclamationPoints(array $tokens) : float
444447 return 0.0 ;
445448 }
446449
447- public function neverCheck (float $ valence , array $ tokens , int $ startIndex , int $ index )
450+ public function neverCheck (float $ valence , array $ tokens , int $ startIndex , int $ index ): float
448451 {
449452 if ($ startIndex == 0 && $ this ->isNegated ([$ tokens [$ index -1 ]])) {
450453 $ valence *= self ::N_SCALAR ;
@@ -460,8 +463,8 @@ public function neverCheck(float $valence, array $tokens, int $startIndex, int $
460463 $ valence *= self ::N_SCALAR ;
461464 }
462465 } elseif ($ startIndex == 2 ) {
463- if ($ tokens [$ index- 3 ] == "never " &&
464- ($ tokens [$ index- 2 ] == "so " || $ tokens [$ index- 2 ] == "this " ) ||
466+ if (( $ tokens [$ index - 3 ] == "never " &&
467+ ($ tokens [$ index - 2 ] == "so " || $ tokens [$ index - 2 ] == "this " ) ) ||
465468 ($ tokens [$ index -1 ] == "so " || $ tokens [$ index -1 ] == "this " )) {
466469
467470 $ valence *= 1.25 ;
@@ -487,9 +490,12 @@ public function boostQuestionMarks(array $tokens) : float
487490 }
488491 }
489492 return 0.0 ;
490- }
491-
492-
493+ }
494+
495+
496+ /**
497+ * @throws \Exception
498+ */
493499 protected function getTxtFilePath () : string
494500 {
495501 return get_storage_path ('sentiment ' .DIRECTORY_SEPARATOR .'vader_lexicon ' ).DIRECTORY_SEPARATOR .'vader_lexicon.txt ' ;
0 commit comments