Skip to content
sentiment-analysis

Sentiment analysis for PHP

Scores English and Indonesian text as positive, negative or neutral. Pure PHP 8.3+, no API calls, no dependencies.

Read the documentationGitHub

Install

composer require risan/sentiment-analysis

Requires PHP 8.3 or newer and the mbstring extension.

Usage

Analyze a text

Pass a string. You get a result with a label and a compound score.

use Risan\Sentiment\Sentiment;

$result = Sentiment::analyze(
    'This package is awesome!',
);

$result->label;    // Label::Positive
$result->compound; // 0.6588

More in Quick start →

Read the result

The three ratios add up to about 1. Three methods check the label.

$result->positive; // 0.594
$result->neutral;  // 0.406
$result->negative; // 0.0

$result->isPositive(); // true
$result->isNegative(); // false
$result->isNeutral();  // false

More in Understanding the scores →

Export the result

Turn the result into an array, or encode it as JSON.

$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}

More in Result reference →

Analyze Indonesian

Pass the language as an enum case, or as the code 'id'.

use Risan\Sentiment\Language;
use Risan\Sentiment\Sentiment;

$result = Sentiment::analyze(
    'Filmnya bagus banget!',
    Language::Indonesian,
);

$result->label;    // Label::Positive
$result->compound; // 0.623

Sentiment::analyze('Filmnya bagus banget!', 'id');

More in Languages →

Customize the analyzer

Add or remove words and change the threshold. Every call returns a new analyzer.

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

More in Customizing the lexicon →

See how it scores

compound runs from -1 to +1. The label uses ±0.05.

English

TextLabelCompound
This package is awesome!positive0.6588
The movie was good.positive0.4404
The movie was not good.negative-0.3412
It isn't bad at all.positive0.4310
The service was extremely good.positive0.4927
The service was kind of good.positive0.3832
The plot was good, but the ending was terrible.negative-0.4939
I LOVE this phone.positive0.7125
The concert was great!!!positive0.7163
Thanks for the help :)positive0.8225
Loved the show 😍positive0.7845
Worst day ever 😭negative-0.8020
The meeting is at 3 pm.neutral0.0000

Indonesian

TextLabelCompound
Filmnya bagus banget!positive0.6230
Makanannya enak sekali.positive0.5849
Pelayanannya tidak ramah.negative-0.3570
Tempatnya nyaman tapi harganya mahal.negative-0.1280
Aku BENCI antrean panjang!!!negative-0.6817
Hotelnya lumayan, tapi kamarnya kotor.negative-0.6428
Kamera hp ini keren parah 😍positive0.7778
Besok rapat jam 3 sore.neutral0.0000

What it handles

The documentation covers every method, with a short example for each.

Read the documentation