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) ~ NilParameters
| Name | Type | Description |
|---|---|---|
| word | String | the word (matched case-insensitively) |
| valence | Float | its 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) ~ IntParameters
| Name | Type | Description |
|---|---|---|
| text | String | input text to analyze |
Return
| Type | Description |
|---|---|
| Int | summed 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) ~ FloatParameters
| Name | Type | Description |
|---|---|---|
| text | String | input text to analyze |
Return
| Type | Description |
|---|---|
| Float | compound 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) ~ SentimentResultParameters
| Name | Type | Description |
|---|---|---|
| text | String | input text to analyze |
Return
| Type | Description |
|---|---|
| SentimentResult | sentiment 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) ~ StringParameters
| Name | Type | Description |
|---|---|---|
| text | String | input text to classify |
Return
| Type | Description |
|---|---|
| String | sentiment 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() ~ NilExample
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) ~ FloatParameters
| Name | Type | Description |
|---|---|---|
| word | String | the word |
Return
| Type | Description |
|---|---|
| Float | its 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() ~ NilLoadLexicon # 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) ~ IntParameters
| Name | Type | Description |
|---|---|---|
| path | String | lexicon file |
Return
| Type | Description |
|---|---|
| Int | number of words added, or -1 if the file could not be read |
Example
added := SentimentAnalyzer->LoadLexicon("vader_lexicon.txt");
"Loaded {$added} words"->PrintLine();