v2026.11.3
All Bundles
Bundle

Natural language processing toolkit. Provides tokenization, text preprocessing, TF-IDF vectorization, cosine similarity, and sentiment analysis. Works with plain strings — no external model required. Compile with -lib nlp.

SentimentAnalyzer

Rule-based sentiment analysis in the manner of VADER (Hutto and Gilbert, 2014), with a built-in English lexicon of valences on a -4..+4 scale, part of it VADER's own values (MIT licence). Negators ("not", "never", "don't") within three words flip and damp a word's valence, boosters ("very", "extremely") and dampeners ("slightly", "kind of") scale it, a word in capitals in mixed-case text and each '!' add emphasis, and "but" shifts weight to the clause after it. Unknown inflections fall back to their base form ("loved" finds "love", "terribly" finds "terrible").

Example

text := "The plot was not bad at all, but the acting was really terrible!";
score := System.NLP.SentimentAnalyzer->AnalyzeCompound(text);
label := System.NLP.SentimentAnalyzer->Classify(text);
result := System.NLP.SentimentAnalyzer->AnalyzeDetailed(text);
result->ToString()->PrintLine();

Operations

AddWord # function

Adds a word to the lexicon or changes its valence, for a domain's own vocabulary

function : AddWord(word:String, valence:Float) ~ Nil

Parameters

NameTypeDescription
wordStringthe word (matched case-insensitively)
valenceFloatits sentiment, on the -4.0 to 4.0 scale the built-in words use

Example

SentimentAnalyzer->AddWord("bullish", 2.0);
SentimentAnalyzer->AddWord("bearish", -2.0);
SentimentAnalyzer->Classify("Analysts are bullish on the stock")->PrintLine();

Analyze # function

Analyzes sentiment of text and returns the summed valence of its words, rounded

function : Analyze(text:String) ~ Int

Parameters

NameTypeDescription
textStringinput text to analyze

Return

TypeDescription
Intsummed valence, rounded to the nearest integer: above 0 leans positive, below 0 negative. AnalyzeCompound gives the normalized score Classify uses.

Example

score := SentimentAnalyzer->Analyze("This product is absolutely wonderful!");
"Score: {$score}"->PrintLine();

AnalyzeCompound # function

Analyzes sentiment of text and returns VADER's normalized compound score

function : AnalyzeCompound(text:String) ~ Float

Parameters

NameTypeDescription
textStringinput text to analyze

Return

TypeDescription
Floatcompound score from -1.0 (most negative) to 1.0 (most positive)

Example

compound := SentimentAnalyzer->AnalyzeCompound("Not bad at all, actually.");
"Compound: {$compound}"->PrintLine();

AnalyzeDetailed # function

Gets detailed sentiment analysis with scores

function : AnalyzeDetailed(text:String) ~ SentimentResult

Parameters

NameTypeDescription
textStringinput text to analyze

Return

TypeDescription
SentimentResultsentiment result with the rounded score, the compound score and the classification

Example

result := SentimentAnalyzer->AnalyzeDetailed("Amazing and wonderful experience!");
result->ToString()->PrintLine();

Classify # function

Classifies text sentiment as positive (compound score of 0.05 or more), negative (-0.05 or less) or neutral

function : Classify(text:String) ~ String

Parameters

NameTypeDescription
textStringinput text to classify

Return

TypeDescription
Stringsentiment label: "positive", "negative", or "neutral"

Example

label := SentimentAnalyzer->Classify("This is not good.");
label->PrintLine();

ClearLexicon # function

Removes every word from the lexicon, built-in ones included, so that LoadLexicon or AddWord can supply a lexicon of the program's own. Boosters and negators are kept.

function : ClearLexicon() ~ Nil

Example

SentimentAnalyzer->ClearLexicon();
SentimentAnalyzer->LoadLexicon("vader_lexicon.txt");

GetValence # function

Gets a word's valence from the lexicon, trying its base form when the word itself is not listed ("loved" finds "love")

function : GetValence(word:String) ~ Float

Parameters

NameTypeDescription
wordStringthe word

Return

TypeDescription
Floatits valence, or 0.0 when neither it nor its base form is listed

Example

SentimentAnalyzer->GetValence("terribly")->PrintLine();

Init # function

Loads the lexicon, boosters and negators; called by every other function, so calling it yourself is never necessary

function : Init() ~ Nil

LoadLexicon # function

Adds the words in a lexicon file, one per line: the word, a tab, its valence on the -4.0 to 4.0 scale, and anything after a further tab ignored. That is the layout of VADER's own vader_lexicon.txt (MIT licence), so a program can use VADER's full lexicon in place of, or on top of, the built-in one. Lines that are empty or start with '#' are skipped; a listed word replaces the built-in entry.

function : LoadLexicon(path:String) ~ Int

Parameters

NameTypeDescription
pathStringlexicon file

Return

TypeDescription
Intnumber of words added, or -1 if the file could not be read

Example

added := SentimentAnalyzer->LoadLexicon("vader_lexicon.txt");
"Loaded {$added} words"->PrintLine();