This page explains the numbers in a Result and what changes them.
use Risan\Sentiment\Sentiment;
$result = Sentiment::analyze('VADER is smart, handsome, and funny.');
$result->compound; // 0.8316
$result->positive; // 0.746
$result->neutral; // 0.254
$result->negative; // 0.0
$result->label; // Label::Positive
Compound
compound is the one number to use when you want a single score. It runs from -1 (extremely negative) to +1 (extremely positive), with four decimals.
In the example, 0.8316 is close to +1. The text is clearly positive.
The package computes it in three steps:
- Each word in the lexicon adds its score (-4 to +4). The rules below adjust it.
- The package adds up the scores.
- It squashes the sum so it always lands between -1 and +1.
Because of the last step, a compound of 0.5 does not mean “50% positive”. Read it as a position on a scale. Near 0 is neutral. Near -1 or +1 is strong.
Positive, neutral and negative
These three numbers are shares of the weighted sentiment signal. Each is between 0 and 1, with three decimals. They add up to about 1.
In the example, 74.6% of the weighted sentiment signal is positive and none is negative. The other 25.4% is neutral. Words with no sentiment, such as VADER and is, count toward the neutral share. Strong words weigh more than mild words.
They are not probabilities. A text with many neutral words and one mild positive word has a high neutral and still a positive compound. To decide, use compound or the label. Use the three shares to see how a text is built.
Label
The Label comes from compound and the threshold:
| Condition | Label |
|---|---|
compound >= threshold (and above 0) |
Label::Positive |
compound <= -threshold (and below 0) |
Label::Negative |
| anything else | Label::Neutral |
The default threshold is 0.05. The VADER authors recommend this value.
What changes a score
The package does more than add up word scores. Here is how emphasis changes the compound score of the same word:
use Risan\Sentiment\Sentiment;
Sentiment::analyze('not good')->compound; // -0.3412
Sentiment::analyze('good')->compound; // 0.4404
Sentiment::analyze('very good')->compound; // 0.4927
Sentiment::analyze('VERY good!!!')->compound; // 0.7005
These are the rules:
| Rule | Example | Effect |
|---|---|---|
| Negation | not good |
Flips the word and softens it |
| Intensifier | very good, extremely bad |
Strengthens the word |
| Dampener | kind of good, slightly bad |
Weakens the word |
| ALL CAPS | this is GREAT |
Strengthens the shouted word |
| Exclamation marks | great!!! |
Adds emphasis, up to 4 marks |
| Question marks | great??? |
Adds a little emphasis |
| Contrast | good food but terrible service |
The part after “but” weighs more |
| Emoticons and emoji | :), ❤️, 😠 |
Count as sentiment words |
Indonesian has the same rules. It adds intensifiers that come after the word (bagus banget), contrast words such as tapi and namun, and informal negations such as gak. See Languages.
Negation reaches three words back
A negation flips a sentiment word when it comes up to three words before it:
use Risan\Sentiment\Sentiment;
Sentiment::analyze('The food was not good.')->compound;
// -0.3412
Sentiment::analyze('The food was not at all good.')->compound;
// -0.3412
Sentiment::analyze('The food was not a thing that was good.')->compound;
// 0.4404
In the last text, not is five words away from good. It is too far to count, so good keeps its positive score.
Examples
| Text | Label | Compound | Pos | Neu | Neg |
|---|---|---|---|---|---|
| This package is awesome!English | positive | 0.6588 | 0.594 | 0.406 | 0.000 |
| The movie was good.English | positive | 0.4404 | 0.492 | 0.508 | 0.000 |
| The movie was not good.English | negative | -0.3412 | 0.000 | 0.624 | 0.376 |
| It isn't bad at all.English | positive | 0.4310 | 0.416 | 0.584 | 0.000 |
| The service was extremely good.English | positive | 0.4927 | 0.444 | 0.556 | 0.000 |
| The service was kind of good.English | positive | 0.3832 | 0.343 | 0.657 | 0.000 |
| The plot was good, but the ending was terrible.English | negative | -0.4939 | 0.149 | 0.534 | 0.317 |
| I LOVE this phone.English | positive | 0.7125 | 0.622 | 0.378 | 0.000 |
| The concert was great!!!English | positive | 0.7163 | 0.624 | 0.376 | 0.000 |
| Thanks for the help :)English | positive | 0.8225 | 0.811 | 0.189 | 0.000 |
| Loved the show 😍English | positive | 0.7845 | 0.580 | 0.420 | 0.000 |
| Worst day ever 😭English | negative | -0.8020 | 0.000 | 0.357 | 0.643 |
| The meeting is at 3 pm.English | neutral | 0.0000 | 0.000 | 1.000 | 0.000 |
| Filmnya bagus banget!Indonesian | positive | 0.6230 | 0.671 | 0.329 | 0.000 |
| Makanannya enak sekali.Indonesian | positive | 0.5849 | 0.655 | 0.345 | 0.000 |
| Pelayanannya tidak ramah.Indonesian | negative | -0.3570 | 0.000 | 0.446 | 0.554 |
| Tempatnya nyaman tapi harganya mahal.Indonesian | negative | -0.1280 | 0.267 | 0.400 | 0.333 |
| Aku BENCI antrean panjang!!!Indonesian | negative | -0.6817 | 0.000 | 0.394 | 0.606 |
| Hotelnya lumayan, tapi kamarnya kotor.Indonesian | negative | -0.6428 | 0.162 | 0.324 | 0.514 |
| Kamera hp ini keren parah 😍Indonesian | positive | 0.7778 | 0.492 | 0.508 | 0.000 |
| Besok rapat jam 3 sore.Indonesian | neutral | 0.0000 | 0.000 | 1.000 | 0.000 |
Tips
- Use the label to classify. Use
compoundto rank. - Empty or whitespace-only text gives all zeros and a neutral label.
- Text with no known words is neutral. The package only knows the words in its lexicon. Add your own words for slang from your field.
- Long text works better one sentence at a time. See Long text.