Analyze a text
Sentiment::analyze() is the one-liner. It uses a shared default analyzer, built once per language.
use Risan\Sentiment\Sentiment;
$result = Sentiment::analyze('VADER is smart, handsome, and funny.');
$result->label; // Label::Positive
$result->compound; // 0.8316
$result->positive; // 0.746
$result->neutral; // 0.254
$result->negative; // 0.0
The compound score is the headline number: -1 is as negative as it gets, +1 as positive as it gets. positive, neutral and negative are ratios that add up to about 1. Understanding the scores explains each one.
Check the label
if ($result->isPositive()) {
// ...
}
$result->isNegative();
$result->isNeutral();
$result->label is a Label enum, so you can also match on it:
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.',
};
Use it as an array or JSON
$result->toArray();
// [
// 'label' => 'positive',
// 'compound' => 0.8316,
// 'positive' => 0.746,
// 'negative' => 0.0,
// 'neutral' => 0.254,
// ]
json_encode($result); // the same shape, as a JSON object
Analyze Indonesian
Pass a Language case, or its string code:
use Risan\Sentiment\Language;
use Risan\Sentiment\Sentiment;
$result = Sentiment::analyze('Filmnya bagus banget!', Language::Indonesian);
$result = Sentiment::analyze('Filmnya bagus banget!', 'id');
$result->label; // Label::Positive
Customize an analyzer
When you need your own words or a different threshold, build an Analyzer. Every with*() method returns a new instance and leaves the original untouched.
use Risan\Sentiment\Analyzer;
use Risan\Sentiment\Language;
$analyzer = (new Analyzer(Language::Indonesian))
->withWords(['cuan' => 2.5, 'bapuk' => -2.0])
->withThreshold(0.1);
$result = $analyzer->analyze('Investasinya cuan!');
Example scores
These sentences were scored by the library itself:
| 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 |