Sentiment Analysis for PHP tells you if a text is positive, negative or neutral.
use Risan\Sentiment\Sentiment;
$result = Sentiment::analyze('This package is awesome!');
$result->label; // Label::Positive
$result->compound; // 0.6588
You give it a string. You get back a label and a set of scores. The compound score runs from -1 (very negative) to +1 (very positive).
It works the same way for Indonesian:
$result = Sentiment::analyze('Filmnya bagus banget!', 'id');
$result->label; // Label::Positive
The package runs in plain PHP. It makes no API calls, needs no training data and has no dependencies.
When to use it
- Short, informal text. Reviews, comments, tweets, chat messages and support tickets.
- A fast, free first signal. You can score thousands of texts per second.
- Scores you can explain. Every score comes from words and rules you can read. You can change the word list.
When not to use it
The package reads the surface of the text. It does not understand sarcasm, and it does not know words from your own field.
use Risan\Sentiment\Sentiment;
Sentiment::analyze('Oh great, another delay.')->label;
// Label::Positive (a person would say negative)
If you need the best accuracy on one domain, a model trained on that domain will do better. Languages lists the limits for each language.
How it works
The package is a PHP version of VADER (Valence Aware Dictionary and sEntiment Reasoner). Two researchers, C.J. Hutto and Eric Gilbert, created it.
- A lexicon (a word list) gives each known word a score from -4 (very negative) to +4 (very positive).
- A few rules adjust those scores in context. They handle negation (
not good), intensifiers (very good), dampeners (kind of good), contrast (good, but…), ALL CAPS, repeated!and?, emoticons and emoji. - The package adds up the adjusted scores. It squashes the sum into the compound score, between -1 and +1.
English uses the original VADER lexicon and rules. Its scores are checked against the reference implementation. Indonesian uses the same engine with a lexicon and rules written for this project.
Requirements
- PHP 8.3 or newer
- The
mbstringextension (almost every PHP build has it)