Skip to content
sentiment-analysis
Menu

Quick start

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.

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