v2026.6.4
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.

TF_IDF

Term Frequency-Inverse Document Frequency (TF-IDF) vectorizer for document analysis

Example

documents := ["The cat sat on the mat", "The dog sat on the log", "Cats and dogs are pets"];
tfidf := System.NLP.TF_IDF->New();
tfidf->Fit(documents);

test_doc := "The cat and the dog";
vector := tfidf->Transform(test_doc);
vocab := tfidf->GetVocabulary();

Operations

Fit #

Fits the TF-IDF model on a corpus of documents

method : public : Fit(documents:String[]) ~ Nil

Parameters

NameTypeDescription
documentsStringarray of document strings

Example

docs := ["the cat sat on the mat", "the dog ran in the park", "cats and dogs are pets"];
tfidf := TF_IDF->New();
tfidf->Fit(docs);
"Vocabulary size: {$tfidf->GetVocabularySize()}"->PrintLine();

GetVocabulary #

Gets the vocabulary (word to index mapping)

method : public : GetVocabulary() ~ Hash<String,IntRef>

Return

TypeDescription
Hash<String,IntRef>vocabulary hash map

Example

vocab := tfidf->GetVocabulary();
keys := vocab->GetKeys()<String>;
each(k in keys) {
  k->PrintLine();
};

GetVocabularySize #

Gets the size of the vocabulary

method : public : GetVocabularySize() ~ Int

Return

TypeDescription
Intvocabulary size

New # constructor

Constructor

New()

Transform #

Transforms a document into a TF-IDF vector

method : public : Transform(document:String) ~ Float[]

Parameters

NameTypeDescription
documentStringinput document string

Return

TypeDescription
FloatTF-IDF vector as float array

Example

vec := tfidf->Transform("the cat chased the dog");
"Vector length: {$vec->Size()}"->PrintLine();