Skip to content

Commit ad5b139

Browse files
committed
make some of vader sentiment class fields public
1 parent 9619882 commit ad5b139

File tree

1 file changed

+40
-13
lines changed

1 file changed

+40
-13
lines changed

src/Sentiment/Vader.php

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ class Vader
2424
CONST N_SCALAR = -0.74;
2525

2626
// for removing punctuation
27-
CONST PUNC_LIST = [".", "!", "?", ",", ";", ":", "-", "'", "\"", "!!", "!!!", "??", "???", "?!?", "!?!", "?!?!", "!?!?"];
27+
public $puncList = [".", "!", "?", ",", ";", ":", "-", "'", "\"", "!!", "!!!", "??", "???", "?!?", "!?!", "?!?!", "!?!?"];
2828

29-
CONST NEGATE = ["aint", "arent", "cannot", "cant", "couldnt", "darent", "didnt", "doesnt",
29+
public $negate = ["aint", "arent", "cannot", "cant", "couldnt", "darent", "didnt", "doesnt",
3030
"ain't", "aren't", "can't", "couldn't", "daren't", "didn't", "doesn't",
3131
"dont", "hadnt", "hasnt", "havent", "isnt", "mightnt", "mustnt", "neither",
3232
"don't", "hadn't", "hasn't", "haven't", "isn't", "mightn't", "mustn't",
@@ -38,7 +38,7 @@ class Vader
3838
// booster/dampener 'intensifiers' or 'degree adverbs'
3939
// http://en.wiktionary.org/wiki/Category:English_degree_adverbs
4040

41-
CONST BOOSTER_DICT = [
41+
protected $boosterDict = [
4242
"absolutely"=> Vader::B_INCR, "amazingly"=> Vader::B_INCR, "awfully"=> Vader::B_INCR, "completely"=> Vader::B_INCR, "considerably"=> Vader::B_INCR,
4343
"decidedly"=> Vader::B_INCR, "deeply"=> Vader::B_INCR, "effing"=> Vader::B_INCR, "enormously"=> Vader::B_INCR,
4444
"entirely"=> Vader::B_INCR, "especially"=> Vader::B_INCR, "exceptionally"=> Vader::B_INCR, "extremely"=> Vader::B_INCR,
@@ -59,7 +59,7 @@ class Vader
5959
];
6060

6161
// check for special case idioms using a sentiment-laden keyword known to VADER
62-
CONST SPECIAL_CASE_IDIOMS = [
62+
public $specialCaseIdioms = [
6363
"the shit"=> 3, "the bomb"=> 3, "bad ass"=> 1.5, "yeah right"=> -2,
6464
"cut the mustard"=> 2, "kiss of death"=> -1.5, "hand to mouth"=> -2
6565
];
@@ -70,6 +70,34 @@ class Vader
7070
*/
7171
protected $lexicon = [];
7272

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+
73101
/**
74102
*
75103
* Determine if input contains negation words
@@ -79,10 +107,9 @@ class Vader
79107
*/
80108
public function isNegated(array $tokens, bool $includeNt = true) : bool
81109
{
82-
$negatedWords = Vader::NEGATE;
83110
foreach($tokens as $word)
84111
{
85-
if(in_array($word, $negatedWords) ||
112+
if(in_array($word, $this->negate) ||
86113
($includeNt && strpos($word, "n't") !== false) ||
87114
( strpos($word, 'least') > 0 && strpos($word, 'at') !== 0 ) ) {
88115
return true;
@@ -119,9 +146,9 @@ public function scalarIncDec(string $word, float $valence, bool $isCapDiff)
119146
{
120147
$scalar = 0.0;
121148
$wordLower = strtolower($word);
122-
if(isset(Vader::BOOSTER_DICT[$wordLower]))
149+
if(isset($this->boosterDict[$wordLower]))
123150
{
124-
$scalar = Vader::BOOSTER_DICT[$wordLower];
151+
$scalar = $this->boosterDict[$wordLower];
125152
if($valence < 0) {
126153
$scalar *= -1;
127154
}
@@ -150,7 +177,7 @@ public function getPolarityScores(array $tokens) : array
150177
$valence = 0.0;
151178
$lcToken = strtolower($tokens[$index]);
152179
if( $lcToken === "kind" && strtolower($tokens[$index+1]) === 'of' ||
153-
isset(self::BOOSTER_DICT[$lcToken]) ) {
180+
isset(self::$this->boosterDict[$lcToken]) ) {
154181

155182
$sentiments[] = $valence;
156183
} else {
@@ -263,12 +290,12 @@ public function idiomsCheck(float $valence, array $tokens, int $index)
263290

264291
foreach( $bigrams + $trigrams as $ngram)
265292
{
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];
268295
}
269296

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];
272299
}
273300
}
274301

0 commit comments

Comments
 (0)