@@ -83,17 +83,19 @@ public function __construct()
83
83
* Add a new token and score to the lexicon
84
84
* @param string $token
85
85
* @param float $meanSentimentRating
86
+ * @return void
86
87
*/
87
- public function addToLexicon (string $ token , float $ meanSentimentRating )
88
+ public function addToLexicon (string $ token , float $ meanSentimentRating ) : void
88
89
{
89
90
$ this ->lexicon [$ token ] = $ meanSentimentRating ;
90
91
}
91
92
92
93
/**
93
94
* Remove a token from the lexicon
94
95
* @param string $token
96
+ * @return void
95
97
*/
96
- public function deleteFromLexicon (string $ token )
98
+ public function deleteFromLexicon (string $ token ) : void
97
99
{
98
100
unset($ this ->lexicon [$ token ]);
99
101
}
@@ -124,12 +126,13 @@ public function isNegated(array $tokens, bool $includeNt = true) : bool
124
126
* approximates the max expected value
125
127
* @param float $score
126
128
* @param int $alpha
129
+ * @return float
127
130
*/
128
- public function normalize (float $ score , int $ alpha =15 )
131
+ public function normalize (float $ score , int $ alpha =15 ) : float
129
132
{
130
133
$ normalizedScore = $ score ;
131
134
132
- if (sqrt (($ score ^2 ) + $ alpha > 0 ) ) {
135
+ if (sqrt (($ score ^2 ) + $ alpha) > 1 ) {
133
136
$ normalizedScore = $ score /sqrt (($ score ^2 ) + $ alpha );
134
137
}
135
138
@@ -178,7 +181,7 @@ public function scalarIncDec(string $word, float $valence, bool $isCapDiff)
178
181
public function getPolarityScores (array $ tokens ) : array
179
182
{
180
183
$ sentiments = [];
181
- for ($ index = 0 ; $ index < count ($ tokens ); $ index ++)
184
+ for ($ index = 0 , $ indexMax = count ($ tokens ); $ index < $ indexMax ; $ index ++)
182
185
{
183
186
$ valence = 0.0 ;
184
187
$ lcToken = strtolower ($ tokens [$ index ]);
@@ -196,7 +199,7 @@ public function getPolarityScores(array $tokens) : array
196
199
return $ this ->scoreValence ($ sentiments , $ tokens );
197
200
}
198
201
199
- public function scoreValence (array $ sentiments , array $ tokens )
202
+ public function scoreValence (array $ sentiments , array $ tokens ): array
200
203
{
201
204
if ( !empty ($ sentiments )) {
202
205
$ sentimentSum = array_sum ($ sentiments );
@@ -247,7 +250,7 @@ public function getSentimentValence(float $valence, array $tokens, int $index)
247
250
if (isset ($ this ->getLexicon ()[$ lcToken ]))
248
251
{
249
252
//get the sentiment valence
250
- $ valence = $ this ->getLexicon ()[$ lcToken ];
253
+ $ valence = ( float ) $ this ->getLexicon ()[$ lcToken ];
251
254
//check if sentiment laden word is in ALL CAPS (while others aren't)
252
255
if ($ ucToken and $ isCapDiff ) {
253
256
if ($ valence > 0 ) {
@@ -262,17 +265,17 @@ public function getSentimentValence(float $valence, array $tokens, int $index)
262
265
if ($ index > $ startIndex && !isset ($ this ->getLexicon ()[ strtolower ($ tokens [$ index -($ startIndex +1 )])]))
263
266
{
264
267
// 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
266
269
// on their distance from the current item.
267
270
$ s = $ this ->scalarIncDec ($ tokens [$ index -($ startIndex +1 )], $ valence , $ isCapDiff );
268
- if ($ startIndex == 1 and $ s != 0 ) {
271
+ if ($ startIndex === 1 and $ s != = 0 ) {
269
272
$ s *= 0.95 ;
270
- } elseif ($ startIndex == 2 and $ s != 0 ) {
273
+ } elseif ($ startIndex === 2 and $ s != = 0 ) {
271
274
$ s *= 0.9 ;
272
275
}
273
276
$ valence += $ s ;
274
277
$ valence = $ this ->neverCheck ($ valence , $ tokens , $ startIndex , $ index );
275
- if ($ startIndex == 2 ) {
278
+ if ($ startIndex === 2 ) {
276
279
$ valence = $ this ->idiomsCheck ($ valence , $ tokens , $ index );
277
280
}
278
281
@@ -283,7 +286,7 @@ public function getSentimentValence(float $valence, array $tokens, int $index)
283
286
// "cooking with gas": 2, "in the black": 2, "in the red": -2,
284
287
// "on the ball": 2,"under the weather": -2}
285
288
}
286
- $ valence = $ this ->leastCheck ($ valence , $ tokens , $ index );
289
+ $ valence = $ this ->leastCheck (( float ) $ valence , $ tokens , $ index );
287
290
}
288
291
289
292
}
@@ -356,7 +359,7 @@ public function butCheck(array $tokens, array $sentiments)
356
359
return $ sentiments ;
357
360
}
358
361
359
- for ($ i = 0 ; $ i < count ($ sentiments ); $ i ++)
362
+ for ($ i = 0 , $ iMax = count ($ sentiments ); $ i < $ iMax ; $ i ++)
360
363
{
361
364
if ( $ index < $ i ) {
362
365
$ sentiments [$ i ] *= 0.5 ;
@@ -444,7 +447,7 @@ public function boostExclamationPoints(array $tokens) : float
444
447
return 0.0 ;
445
448
}
446
449
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
448
451
{
449
452
if ($ startIndex == 0 && $ this ->isNegated ([$ tokens [$ index -1 ]])) {
450
453
$ valence *= self ::N_SCALAR ;
@@ -460,8 +463,8 @@ public function neverCheck(float $valence, array $tokens, int $startIndex, int $
460
463
$ valence *= self ::N_SCALAR ;
461
464
}
462
465
} 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 " ) ) ||
465
468
($ tokens [$ index -1 ] == "so " || $ tokens [$ index -1 ] == "this " )) {
466
469
467
470
$ valence *= 1.25 ;
@@ -487,9 +490,12 @@ public function boostQuestionMarks(array $tokens) : float
487
490
}
488
491
}
489
492
return 0.0 ;
490
- }
491
-
492
-
493
+ }
494
+
495
+
496
+ /**
497
+ * @throws \Exception
498
+ */
493
499
protected function getTxtFilePath () : string
494
500
{
495
501
return get_storage_path ('sentiment ' .DIRECTORY_SEPARATOR .'vader_lexicon ' ).DIRECTORY_SEPARATOR .'vader_lexicon.txt ' ;
0 commit comments