Skip to content
sentiment-analysis
Menu

Quick start

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:

TextLabelCompoundPosNeuNeg
This package is awesome!Englishpositive0.65880.5940.4060.000
The movie was good.Englishpositive0.44040.4920.5080.000
The movie was not good.Englishnegative-0.34120.0000.6240.376
It isn't bad at all.Englishpositive0.43100.4160.5840.000
The service was extremely good.Englishpositive0.49270.4440.5560.000
The service was kind of good.Englishpositive0.38320.3430.6570.000
The plot was good, but the ending was terrible.Englishnegative-0.49390.1490.5340.317
I LOVE this phone.Englishpositive0.71250.6220.3780.000
The concert was great!!!Englishpositive0.71630.6240.3760.000
Thanks for the help :)Englishpositive0.82250.8110.1890.000
Loved the show ๐Ÿ˜Englishpositive0.78450.5800.4200.000
Worst day ever ๐Ÿ˜ญEnglishnegative-0.80200.0000.3570.643
The meeting is at 3 pm.Englishneutral0.00000.0001.0000.000
Filmnya bagus banget!Indonesianpositive0.62300.6710.3290.000
Makanannya enak sekali.Indonesianpositive0.58490.6550.3450.000
Pelayanannya tidak ramah.Indonesiannegative-0.35700.0000.4460.554
Tempatnya nyaman tapi harganya mahal.Indonesiannegative-0.12800.2670.4000.333
Aku BENCI antrean panjang!!!Indonesiannegative-0.68170.0000.3940.606
Hotelnya lumayan, tapi kamarnya kotor.Indonesiannegative-0.64280.1620.3240.514
Kamera hp ini keren parah ๐Ÿ˜Indonesianpositive0.77780.4920.5080.000
Besok rapat jam 3 sore.Indonesianneutral0.00000.0001.0000.000

Where to go next

Edit this page on GitHub