Skip to content

Commit 7cfd4bf

Browse files
committed
Make lexicon to be easily updated on the fly
1 parent ca2358f commit 7cfd4bf

File tree

3 files changed

+87
-7
lines changed

3 files changed

+87
-7
lines changed

README.md

Lines changed: 73 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ Run the following to include this via Composer
3333
composer require davmixcool/php-sentiment-analyzer
3434
```
3535

36-
### Usage
36+
### Simple Usage
3737

3838
```php
3939

@@ -52,17 +52,87 @@ print_r($output_text_with_emoji);
5252

5353
```
5454

55-
### Outputs
55+
### Simple Outputs
5656

5757
```
5858
David is smart, handsome, and funny. ---------------- ['neg'=> 0.0, 'neu'=> 0.337, 'pos'=> 0.663, 'compound'=> 0.7096]
5959
6060
😁 ------------------- ['neg' => 0, 'neu' => 0.5, 'pos' => 0.5, 'compound' => 0.4588]
6161
62-
Aproko doctor made me 🤣 ------------- ['neg' => 0, 'neu' => 1, 'pos' => 0, 'compound' => 0]
62+
Aproko doctor made me 🤣 ------------- ['neg' => 0, 'neu' => 0.714, 'pos' => 0.286, 'compound' => 0.4939]
6363
6464
```
6565

66+
67+
68+
### Advance Usage
69+
70+
You can now dynamically update the VADER (Valence) lexicon on the fly for words that are not in the dictionary. See Example below:
71+
72+
73+
```php
74+
75+
76+
Use Sentiment\Analyzer;
77+
78+
$sentiment = new Sentiment\Analyzer();
79+
80+
$strings = [
81+
'Weather today is rubbish',
82+
'This cake looks amazing',
83+
'His skills are mediocre',
84+
'He is very talented',
85+
'She is seemingly very agressive',
86+
'Marie was enthusiastic about the upcoming trip. Her brother was also passionate about her leaving - he would finally have the house for himself.',
87+
'To be or not to be?',
88+
];
89+
90+
//new words not in the dictionary
91+
$newWords = [
92+
'rubbish'=> '-1.5',
93+
'mediocre' => '-1.0',
94+
'agressive' => '-0.5'
95+
];
96+
97+
//Dynamically update the dictionary with the new words
98+
$sentiment->updateLexicon($newWords);
99+
100+
//Print results
101+
foreach ($strings as $string) {
102+
// calculations:
103+
$scores = $sentiment->getSentiment($string);
104+
// output:
105+
echo "String: $string\n";
106+
print_r(json_encode($scores));
107+
echo "<br>";
108+
}
109+
110+
111+
```
112+
113+
114+
### Advance Outputs
115+
116+
117+
```
118+
119+
Weather today is rubbish ------------- {"neg":0.455,"neu":0.545,"pos":0,"compound":-0.3612}
120+
121+
This cake looks amazing ------------- {"neg":0,"neu":0.441,"pos":0.559,"compound":0.5859}
122+
123+
His skills are mediocre ------------- {"neg":0.4,"neu":0.6,"pos":0,"compound":-0.25}
124+
125+
He is very talented ------------- {"neg":0,"neu":0.457,"pos":0.543,"compound":0.552}
126+
127+
She is seemingly very agressive ------------- {"neg":0.338,"neu":0.662,"pos":0,"compound":-0.2598}
128+
129+
Marie was enthusiastic about the upcoming trip. Her brother was also passionate about her leaving - he would finally have the house for himself. ------------- {"neg":0,"neu":0.761,"pos":0.239,"compound":0.765}
130+
131+
String: To be or not to be? ------------- {"neg":0,"neu":1,"pos":0,"compound":0}
132+
133+
```
134+
135+
66136
### License
67137

68138
This package is licensed under the [MIT license](https://github.com/davmixcool/php-sentiment-analyzer/blob/master/LICENSE).

src/Analyzer.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,15 @@ public function make_emoji_dict() {
8383
return $emoji_dict;
8484
}
8585

86+
public function updateLexicon($arr)
87+
{
88+
if(!is_array($arr)) return [];
89+
$lexicon = [];
90+
foreach ($arr as $word => $valence) {
91+
$this->lexicon[strtolower($word)] = is_numeric($valence)? $valence : 0;
92+
}
93+
}
94+
8695
private function IsKindOf($firstWord, $secondWord)
8796
{
8897
return "kind" === strtolower($firstWord) && "of" === strtolower($secondWord);
@@ -183,19 +192,20 @@ public function getSentiment($text)
183192
$sentiments = [];
184193
$words_and_emoticons = $this->current_sentitext->words_and_emoticons;
185194

186-
for ($i=0; $i<count($words_and_emoticons)-1; $i++) {
195+
for ($i=0; $i<=count($words_and_emoticons)-1; $i++) {
187196
$valence = 0.0;
188197
$wordBeingTested = $words_and_emoticons[$i];
189198

190199
//If this is a booster word add a 0 valances then go to next word as it does not express sentiment directly
191200
/* if ($this->IsBoosterWord($wordBeingTested)){
192201
echo "\t\tThe word is a booster word: setting sentiment to 0.0\n";
193202
}*/
194-
203+
//var_dump($i);
195204
//If the word is not in the Lexicon then it does not express sentiment. So just ignore it.
196205
if ($this->IsInLexicon($wordBeingTested)) {
206+
197207
//Special case because kind is in the lexicon so the modifier kind of needs to be skipped
198-
if ("kind" !=$words_and_emoticons[$i] && "of" != $words_and_emoticons[$i+1]) {
208+
if ("kind" !=$words_and_emoticons[$i] && "of" != $words_and_emoticons[$i]) {
199209
$valence = $this->getValenceFromLexicon($wordBeingTested);
200210

201211
$wordInContext = $this->getWordInContext($words_and_emoticons, $i);

src/Config/Config.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class Config
4040
"fricking"=> self::B_INCR, "frickin"=> self::B_INCR, "frigging"=> self::B_INCR, "friggin"=> self::B_INCR, "fully"=> self::B_INCR, "fucking"=> self::B_INCR,
4141
"greatly"=> self::B_INCR, "hella"=> self::B_INCR, "highly"=> self::B_INCR, "hugely"=> self::B_INCR, "incredibly"=> self::B_INCR,
4242
"intensely"=> self::B_INCR, "majorly"=> self::B_INCR, "more"=> self::B_INCR, "most"=> self::B_INCR, "particularly"=> self::B_INCR,
43-
"purely"=> self::B_INCR, "quite"=> self::B_INCR, "really"=> self::B_INCR, "remarkably"=> self::B_INCR,
43+
"purely"=> self::B_INCR, "quite"=> self::B_INCR, "seemingly" => self::B_INCR, "really"=> self::B_INCR, "remarkably"=> self::B_INCR,
4444
"so"=> self::B_INCR, "substantially"=> self::B_INCR,
4545
"thoroughly"=> self::B_INCR, "totally"=> self::B_INCR, "tremendous"=> self::B_INCR, "tremendously"=> self::B_INCR,
4646
"uber"=> self::B_INCR, "unbelievably"=> self::B_INCR, "unusually"=> self::B_INCR, "utterly"=> self::B_INCR,

0 commit comments

Comments
 (0)