Version 2 is a complete rewrite. It has a new namespace, a new API and a different scoring model. Treat the upgrade as a small migration, not a version bump.
Here is the same task in both versions. The v1 code is for reference only. It cannot run against v2.
v1
$analyzer = SentimentAnalysis\Analyzer::withDefaultConfig();
$result = $analyzer->analyze('This package is awesome!');
echo $result->category();
v2
use Risan\Sentiment\Sentiment;
$result = Sentiment::analyze('This package is awesome!');
echo $result->label->value; // positive
What changed
| v1 | v2 | |
|---|---|---|
| PHP | No version in composer.json; tested on PHP 5.5 to 7 |
8.3 or newer, plus ext-mbstring |
| Namespace | SentimentAnalysis\ |
Risan\Sentiment\ |
| Model | Word lists and a naive Bayes-style product | VADER: a lexicon of word scores plus rules for negation, boosters, caps, punctuation and emoji |
| Languages | English | English and Indonesian |
| Result | category() and scores() |
A Result object with label, compound, positive, negative and neutral |
| Customizing | Your own dictionary files and classes | withWords(), withoutWords() and withThreshold() |
| Interfaces | Contracts\*Interface |
None. Five small final classes |
Getting the scores
v1
$result->scores();
// ['positive' => ..., 'negative' => ..., 'neutral' => ...]
v2
$result->toArray();
// [
// 'label' => 'positive',
// 'compound' => 0.6588,
// 'positive' => 0.594,
// 'negative' => 0.0,
// 'neutral' => 0.406,
// ]
$result->positive; // 0.594
$result->negative; // 0.0
$result->neutral; // 0.406
Custom words
v1 read the words from your own data folder. It had one PHP file per list: positive.php, negative.php, neutral.php, negation.php and ignored.php.
$analyzer = new SentimentAnalysis\Analyzer(
new SentimentAnalysis\Dictionary('/path/to/data'),
new SentimentAnalysis\Tokenizer(),
new SentimentAnalysis\TokenValidator()
);
v2 gives each word a score from -4 to 4 instead:
use Risan\Sentiment\Analyzer;
$analyzer = (new Analyzer())
->withWords(['sick' => 2.0])
->withoutWords(['kill']);
Mapping table
| v1 | v2 |
|---|---|
Analyzer::withDefaultConfig() |
Sentiment::analyze($text), or new Analyzer() |
$analyzer->analyze($document) |
$analyzer->analyze($text). Same method name, new Result |
$result->category() |
$result->label (a Label enum), or $result->label->value for the string |
$result->scores() |
$result->toArray(), or the typed properties |
$result->scores()['positive'] |
$result->positive |
new Dictionary($dataDirectory) with your own files |
$analyzer->withWords([...]) and withoutWords([...]) |
Tokenizer, TokenValidator and the Contracts\*Interface interfaces |
No replacement. Clean up the text before you call analyze() |
Things that behave differently
- Scores are no longer probabilities. In v1, the three scores added up to 1 and the category was the largest one. In v2,
positive,negativeandneutralare shares of the weighted sentiment signal. The label comes from thecompoundscore and a threshold. - Neutral is a real result. v2 calls a text neutral when its compound score is close to zero. Empty text and text with no known word are neutral.
- Context matters.
not good,very good,GOOD!!!andgood, but…get different scores. v1 mostly counted words. See Understanding the scores. - Text handling is stricter and safer. Invalid UTF-8 never throws. Results do not depend on the
mb_internal_encoding()of the host. - No interfaces to implement. Did you use your own tokenizer, dictionary or validator? Move that work to
withWords()andwithoutWords(). Or clean up the text before you callanalyze().
Migration checklist
- Use PHP 8.3 or newer. Run
composer require risan/sentiment-analysis:^2.0. - Replace the
SentimentAnalysis\imports withRisan\Sentiment\. - Replace
Analyzer::withDefaultConfig()withSentiment::analyze(). If you analyze many texts with custom words, share onenew Analyzer()instead. - Replace
category()with$result->label. Replacescores()withtoArray()or the properties. - Check your cut-offs and stored scores against the new scale. Start with the default threshold and tune from there.
- Move your custom dictionaries to
withWords().