Sentiment analysis for PHP
Scores English and Indonesian text as positive, negative or neutral. Pure PHP 8.3+, no API calls, no dependencies.
Install
composer require risan/sentiment-analysisRequires 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.6588Read 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(); // falseExport 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}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');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.5848See how it scores
compound runs from -1 to +1. The label uses ±0.05.
English
| Text | Label | Compound |
|---|---|---|
| This package is awesome! | positive | 0.6588 |
| The movie was good. | positive | 0.4404 |
| The movie was not good. | negative | -0.3412 |
| It isn't bad at all. | positive | 0.4310 |
| The service was extremely good. | positive | 0.4927 |
| The service was kind of good. | positive | 0.3832 |
| The plot was good, but the ending was terrible. | negative | -0.4939 |
| I LOVE this phone. | positive | 0.7125 |
| The concert was great!!! | positive | 0.7163 |
| Thanks for the help :) | positive | 0.8225 |
| Loved the show 😍 | positive | 0.7845 |
| Worst day ever 😭 | negative | -0.8020 |
| The meeting is at 3 pm. | neutral | 0.0000 |
Indonesian
| Text | Label | Compound |
|---|---|---|
| Filmnya bagus banget! | positive | 0.6230 |
| Makanannya enak sekali. | positive | 0.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 😍 | positive | 0.7778 |
| Besok rapat jam 3 sore. | neutral | 0.0000 |
What it handles
- Negation: "not good" is negative, "not bad at all" is positive.
- Intensifiers: "very", "kind of", "banget".
- Contrast: what follows "but" or "tapi" carries more weight.
- CAPS and "!!!" add emphasis.
- Emoji and emoticons.
- Custom words and thresholds.
- About 80,000 short texts per second on PHP 8.5.
The documentation covers every method, with a short example for each.