The package scores English and Indonesian. English is the default.
use Risan\Sentiment\Analyzer;
use Risan\Sentiment\Language;
use Risan\Sentiment\Sentiment;
// English
Sentiment::analyze('This is great!');
// Indonesian, by enum case or by code
Sentiment::analyze('Ini keren banget!', Language::Indonesian);
Sentiment::analyze('Ini keren banget!', 'id');
$analyzer = new Analyzer('id');
$analyzer->language(); // Language::Indonesian
Pick the language with the Language enum or its string code:
| Language | Enum case | Code |
|---|---|---|
| English | Language::English |
'en' |
| Indonesian | Language::Indonesian |
'id' |
An unknown code throws a native ValueError:
use Risan\Sentiment\Sentiment;
Sentiment::analyze('Hello', 'fr'); // throws ValueError
The package never guesses the language. If a text mixes languages, pick the main one. Or split the text and analyze each part with its own language.
English
English uses the original VADER lexicon and the full VADER rules. The lexicon has about 7,500 words, emoticons and slang. Ten human judges rated each entry.
The package checks its scores against the reference implementation (vaderSentiment 3.3.2) on a large set of test sentences. You get the same numbers as the Python original, with two small differences on purpose:
- The “but” rule scales each part by position. The reference scales repeated equal scores wrongly.
- The package ignores the emoji variation selector (U+FE0F), so
❤️scores like❤.
VADER was made for social media text. It also works well on reviews, comments and short messages.
Indonesian
Indonesian runs on the same engine with its own data. Here are two examples:
use Risan\Sentiment\Language;
use Risan\Sentiment\Sentiment;
$result = Sentiment::analyze('Filmnya bagus banget!', 'id');
$result->label; // Label::Positive
$result->compound; // 0.623
$result = Sentiment::analyze('Pelayanannya lambat.', 'id');
$result->label; // Label::Negative
$result->compound; // -0.4588
What the Indonesian data covers:
- A lexicon of Indonesian words with scores. It includes common affixed forms (
bahagia,membahagiakan,kekecewaan) and informal spellings (mantap,mantul,keren,zonk). - Negations:
tidak,tak,bukan,belum,jangan,tanpa,kurang, and informal forms such asgak,nggak,enggak,ga,tdk. - Intensifiers before the word (
sangat bagus,terlalu mahal) and after it (bagus banget,bagus sekali,keren parah). Dampeners (agak,cukup,kurang lebih) work the same way. - Contrast words:
tapi,tetapi,tp,namun,sayangnya. - Suffixes and repeats: the package strips
-nya,-lah,-kahand-punwhen the bare word is known. It looks up a repeated word such asbagus-bagusorbagus2asbagus. - Emoticons and emoji, shared with English.
The Indonesian lexicon was written for this project and has the MIT license. No existing Indonesian sentiment list was copied, because the well-known ones have no open license. See Credits & license.
Measured accuracy
The test split of IndoNLU SmSA measured the accuracy. It is a public Indonesian sentiment benchmark with three classes: positive, neutral and negative. The labels come from compound with the default threshold.
| Measure | Result |
|---|---|
| Accuracy | 81.2% |
| Macro F1 (from 0 to 1) | 0.744 |
| Majority-class baseline accuracy | 41.6% |
| Words written for the lexicon | 2,776 |
The baseline is the accuracy you get by always guessing the most common class. Compare it with the accuracy of the package.
A lexicon method does not match a fine-tuned neural model. The package publishes these numbers so you can judge for yourself.
Known limits
- Sarcasm and irony are read at face value.
- Regional languages (Javanese, Sundanese and others) and heavy code-mixing are only partly covered. The Indonesian lexicon includes English words that are common in Indonesian reviews (
good,best,worst). It does not fall back to the whole English lexicon. On the SmSA validation split, that fallback did not improve the macro F1. Many English words are also ordinary Indonesian words (sore,alas,no). - Words from your field, such as product names or medical and legal terms, are missing unless you add them.
- Words are not stemmed. An affixed form must be in the lexicon to be found, so rare forms can be missed.
- Ambiguous words get a careful score or are left out. For example,
parah(“severe”) is negative on its own. After another sentiment word it is an intensifier, as inkeren parah.
Examples
| Text | Label | Compound | Pos | Neu | Neg |
|---|---|---|---|---|---|
| Filmnya bagus banget! | positive | 0.6230 | 0.671 | 0.329 | 0.000 |
| Makanannya enak sekali. | positive | 0.5849 | 0.655 | 0.345 | 0.000 |
| Pelayanannya tidak ramah. | negative | -0.3570 | 0.000 | 0.446 | 0.554 |
| Tempatnya nyaman tapi harganya mahal. | negative | -0.1280 | 0.267 | 0.400 | 0.333 |
| Aku BENCI antrean panjang!!! | negative | -0.6817 | 0.000 | 0.394 | 0.606 |
| Hotelnya lumayan, tapi kamarnya kotor. | negative | -0.6428 | 0.162 | 0.324 | 0.514 |
| Kamera hp ini keren parah 😍 | positive | 0.7778 | 0.492 | 0.508 | 0.000 |
| Besok rapat jam 3 sore. | neutral | 0.0000 | 0.000 | 1.000 | 0.000 |