Skip to content
sentiment-analysis
Menu

Languages

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 as gak, 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, -kah and -pun when the bare word is known. It looks up a repeated word such as bagus-bagus or bagus2 as bagus.
  • 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 in keren parah.

Examples

TextLabelCompoundPosNeuNeg
Filmnya bagus banget!positive0.62300.6710.3290.000
Makanannya enak sekali.positive0.58490.6550.3450.000
Pelayanannya tidak ramah.negative-0.35700.0000.4460.554
Tempatnya nyaman tapi harganya mahal.negative-0.12800.2670.4000.333
Aku BENCI antrean panjang!!!negative-0.68170.0000.3940.606
Hotelnya lumayan, tapi kamarnya kotor.negative-0.64280.1620.3240.514
Kamera hp ini keren parah 😍positive0.77780.4920.5080.000
Besok rapat jam 3 sore.neutral0.00000.0001.0000.000

Edit this page on GitHub