@@ -24,9 +24,9 @@ class Vader
24
24
CONST N_SCALAR = -0.74 ;
25
25
26
26
// for removing punctuation
27
- CONST PUNC_LIST = [". " , "! " , "? " , ", " , "; " , ": " , "- " , "' " , "\"" , "!! " , "!!! " , "?? " , "??? " , "?!? " , "!?! " , "?!?! " , "!?!? " ];
27
+ public $ puncList = [". " , "! " , "? " , ", " , "; " , ": " , "- " , "' " , "\"" , "!! " , "!!! " , "?? " , "??? " , "?!? " , "!?! " , "?!?! " , "!?!? " ];
28
28
29
- CONST NEGATE = ["aint " , "arent " , "cannot " , "cant " , "couldnt " , "darent " , "didnt " , "doesnt " ,
29
+ public $ negate = ["aint " , "arent " , "cannot " , "cant " , "couldnt " , "darent " , "didnt " , "doesnt " ,
30
30
"ain't " , "aren't " , "can't " , "couldn't " , "daren't " , "didn't " , "doesn't " ,
31
31
"dont " , "hadnt " , "hasnt " , "havent " , "isnt " , "mightnt " , "mustnt " , "neither " ,
32
32
"don't " , "hadn't " , "hasn't " , "haven't " , "isn't " , "mightn't " , "mustn't " ,
@@ -38,7 +38,7 @@ class Vader
38
38
// booster/dampener 'intensifiers' or 'degree adverbs'
39
39
// http://en.wiktionary.org/wiki/Category:English_degree_adverbs
40
40
41
- CONST BOOSTER_DICT = [
41
+ protected $ boosterDict = [
42
42
"absolutely " => Vader::B_INCR , "amazingly " => Vader::B_INCR , "awfully " => Vader::B_INCR , "completely " => Vader::B_INCR , "considerably " => Vader::B_INCR ,
43
43
"decidedly " => Vader::B_INCR , "deeply " => Vader::B_INCR , "effing " => Vader::B_INCR , "enormously " => Vader::B_INCR ,
44
44
"entirely " => Vader::B_INCR , "especially " => Vader::B_INCR , "exceptionally " => Vader::B_INCR , "extremely " => Vader::B_INCR ,
@@ -59,7 +59,7 @@ class Vader
59
59
];
60
60
61
61
// check for special case idioms using a sentiment-laden keyword known to VADER
62
- CONST SPECIAL_CASE_IDIOMS = [
62
+ public $ specialCaseIdioms = [
63
63
"the shit " => 3 , "the bomb " => 3 , "bad ass " => 1.5 , "yeah right " => -2 ,
64
64
"cut the mustard " => 2 , "kiss of death " => -1.5 , "hand to mouth " => -2
65
65
];
@@ -70,6 +70,34 @@ class Vader
70
70
*/
71
71
protected $ lexicon = [];
72
72
73
+ /**
74
+ * Initializes and loads the lexicon
75
+ */
76
+ public function __construct ()
77
+ {
78
+ $ this ->getLexicon (); // populate the lexicon
79
+ }
80
+
81
+ /**
82
+ * Add a new token and score to the lexicon
83
+ * @param string $token
84
+ * @param float $meanSentimentRating
85
+ */
86
+ public function addToLexicon (string $ token , float $ meanSentimentRating )
87
+ {
88
+ $ this ->lexicon [$ token ] = $ meanSentimentRating ;
89
+ }
90
+
91
+ /**
92
+ * Remove a token from the lexicon
93
+ * @param string $token
94
+ */
95
+ public function deleteFromLexicon (string $ token )
96
+ {
97
+ unset($ this ->lexicon [$ token ]);
98
+ }
99
+
100
+
73
101
/**
74
102
*
75
103
* Determine if input contains negation words
@@ -79,10 +107,9 @@ class Vader
79
107
*/
80
108
public function isNegated (array $ tokens , bool $ includeNt = true ) : bool
81
109
{
82
- $ negatedWords = Vader::NEGATE ;
83
110
foreach ($ tokens as $ word )
84
111
{
85
- if (in_array ($ word , $ negatedWords ) ||
112
+ if (in_array ($ word , $ this -> negate ) ||
86
113
($ includeNt && strpos ($ word , "n't " ) !== false ) ||
87
114
( strpos ($ word , 'least ' ) > 0 && strpos ($ word , 'at ' ) !== 0 ) ) {
88
115
return true ;
@@ -119,9 +146,9 @@ public function scalarIncDec(string $word, float $valence, bool $isCapDiff)
119
146
{
120
147
$ scalar = 0.0 ;
121
148
$ wordLower = strtolower ($ word );
122
- if (isset (Vader:: BOOSTER_DICT [$ wordLower ]))
149
+ if (isset ($ this -> boosterDict [$ wordLower ]))
123
150
{
124
- $ scalar = Vader:: BOOSTER_DICT [$ wordLower ];
151
+ $ scalar = $ this -> boosterDict [$ wordLower ];
125
152
if ($ valence < 0 ) {
126
153
$ scalar *= -1 ;
127
154
}
@@ -150,7 +177,7 @@ public function getPolarityScores(array $tokens) : array
150
177
$ valence = 0.0 ;
151
178
$ lcToken = strtolower ($ tokens [$ index ]);
152
179
if ( $ lcToken === "kind " && strtolower ($ tokens [$ index +1 ]) === 'of ' ||
153
- isset (self ::BOOSTER_DICT [$ lcToken ]) ) {
180
+ isset (self ::$ this -> boosterDict [$ lcToken ]) ) {
154
181
155
182
$ sentiments [] = $ valence ;
156
183
} else {
@@ -263,12 +290,12 @@ public function idiomsCheck(float $valence, array $tokens, int $index)
263
290
264
291
foreach ( $ bigrams + $ trigrams as $ ngram )
265
292
{
266
- if (isset (self ::SPECIAL_CASE_IDIOMS [$ ngram ])) {
267
- $ valence = self ::SPECIAL_CASE_IDIOMS [$ ngram ];
293
+ if (isset (self ::$ this -> specialCaseIdioms [$ ngram ])) {
294
+ $ valence = self ::$ this -> specialCaseIdioms [$ ngram ];
268
295
}
269
296
270
- if (isset (self ::BOOSTER_DICT [$ ngram ])) {
271
- $ valence += self ::BOOSTER_DICT [$ ngram ];
297
+ if (isset (self ::$ this -> boosterDict [$ ngram ])) {
298
+ $ valence += self ::$ this -> boosterDict [$ ngram ];
272
299
}
273
300
}
274
301
0 commit comments