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.
Tokenizer
Text tokenization utilities for breaking text into words, sentences, and n-grams
Example
text := "Hello, world! This is a test.";
words := System.NLP.Tokenizer->WordTokenize(text);
sentences := System.NLP.Tokenizer->SentenceTokenize(text);
bigrams := System.NLP.Tokenizer->WordNGrams(words, 2);Operations
CharNGrams # function
Generates character-level n-grams from text
function : CharNGrams(text:String, n:Int) ~ String[]Parameters
| Name | Type | Description |
|---|---|---|
| text | String | input text |
| n | Int | size of n-grams |
Return
| Type | Description |
|---|---|
| String | array of n-gram strings |
Example
trigrams := Tokenizer->CharNGrams("hello", 3);
each(g in trigrams) {
g->PrintLine();
};SentenceTokenize # function
Tokenizes text into sentences. A sentence ends at a run of '.', '!' or '?' (so "..." and "?!" are one boundary), with any closing quotes or brackets, followed by whitespace or the end of the text. A '.' after a known abbreviation (Dr, Mrs, e.g, U.S, Jan ...) or a single capital initial does not end a sentence, and neither does a '.' inside a number (3.50).
function : SentenceTokenize(text:String) ~ String[]Parameters
| Name | Type | Description |
|---|---|---|
| text | String | input text to tokenize |
Return
| Type | Description |
|---|---|
| String | array of sentence tokens |
Example
sents := Tokenizer->SentenceTokenize("Dr. Smith paid $3.50. Wait... really?! Yes.");
each(s in sents) {
s->PrintLine();
};WordNGrams # function
Generates word-level n-grams from tokenized text
function : WordNGrams(tokens:String[], n:Int) ~ String[]Parameters
| Name | Type | Description |
|---|---|---|
| tokens | String | array of word tokens |
| n | Int | size of n-grams |
Return
| Type | Description |
|---|---|
| String | array of n-gram strings (words joined by spaces) |
Example
words := Tokenizer->WordTokenize("the quick brown fox");
bigrams := Tokenizer->WordNGrams(words, 2);
each(g in bigrams) {
g->PrintLine();
};WordTokenize # function
Tokenizes text into words. A word is a run of letters and digits, including non-Latin letters; an apostrophe between letters (don't, O'Brien), a hyphen between letters or digits (state-of-the-art, COVID-19) and a '.' or ',' between digits (3.14, 5,000) stay inside it. Whitespace and punctuation separate words and are dropped. Case is kept.
function : WordTokenize(text:String) ~ String[]Parameters
| Name | Type | Description |
|---|---|---|
| text | String | input text to tokenize |
Return
| Type | Description |
|---|---|
| String | array of word tokens |
Example
words := Tokenizer->WordTokenize("I don't think 3.14 is state-of-the-art.");
each(w in words) {
w->PrintLine();
};