This page shows the basics in five short steps. Each step builds on the one before.
Analyze a text
use Risan\Sentiment\Sentiment;
$result = Sentiment::analyze('This package is awesome!');
$result->label; // Label::Positive
$result->compound; // 0.6588
Sentiment::analyze() is the shortest way to score a text. It returns a Result.
compound is the main score. It runs from -1 (very negative) to +1 (very positive). The label comes from compound.
Read the result
Keep using the same $result:
$result->positive; // 0.594
$result->neutral; // 0.406
$result->negative; // 0.0
$result->isPositive(); // true
$result->isNegative(); // false
$result->isNeutral(); // false
positive, neutral and negative are shares of the weighted sentiment signal. They add up to about 1. Understanding the scores explains each one.
The label is a Label enum, so you can also use match:
use Risan\Sentiment\Label;
$message = match ($result->label) {
Label::Positive => 'Glad you liked it!',
Label::Negative => 'Sorry to hear that.',
Label::Neutral => 'Thanks for the feedback.',
};
Export the result
$result->toArray();
// [
// 'label' => 'positive',
// 'compound' => 0.6588,
// 'positive' => 0.594,
// 'negative' => 0.0,
// 'neutral' => 0.406,
// ]
json_encode($result);
// {"label":"positive","compound":0.6588,"positive":0.594,
// "negative":0,"neutral":0.406}
A Result is JsonSerializable, so you can encode it directly. PHP writes the float 0.0 as 0 in JSON.
Analyze Indonesian
use Risan\Sentiment\Language;
use Risan\Sentiment\Sentiment;
$result = Sentiment::analyze(
'Filmnya bagus banget!',
Language::Indonesian,
);
$result->label; // Label::Positive
$result->compound; // 0.623
You can also pass the language code:
Sentiment::analyze('Filmnya bagus banget!', 'id');
An unknown code throws a ValueError:
Sentiment::analyze('Hello', 'fr'); // throws ValueError
Customize an analyzer
To add words or change the threshold, build an Analyzer. Every with*() method returns a new analyzer. The original stays the same.
use Risan\Sentiment\Analyzer;
use Risan\Sentiment\Language;
$analyzer = (new Analyzer(Language::Indonesian))
->withWords(['cuan' => 2.5])
->withoutWords(['kasar'])
->withThreshold(0.1);
$result = $analyzer->analyze('Investasinya cuan!');
$result->label; // Label::Positive
$result->compound; // 0.5848
Example scores
The library scored these sentences. The bar next to each compound score shows its size. It grows to the right for positive scores and to the left for negative ones.
| 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 |