A lightweight machine learning and vectorization library for Starlight.
npm install starlight-vecstarlight-ml
bash
npm install starlight-vec
``
> Note: Requires Node.js ≥ 14 and starlight-ml installed in your project.
---
Usage
`js
import * as ml from 'starlight-ml';
import { Vectorizer, vectorize, normalize } from 'starlight-vec';
// Sample documents
const docs = [
"I love machine learning",
"Starlight ML is amazing",
"Vectorization makes NLP tasks easier"
];
// Create and fit a vectorizer
const vec = vectorize(docs, ['is', 'a', 'the']); // optional stopwords
// Transform a new document
const docVector = vec.transform("I love NLP and Starlight");
console.log("TF-IDF vector:", docVector);
// Transform multiple documents
const batchVectors = vec.transformBatch(docs);
console.log("Batch TF-IDF vectors:", batchVectors);
// Compute cosine similarity between two documents
const similarity = Vectorizer.cosine(batchVectors[0], batchVectors[1]);
console.log(Similarity: ${similarity});
`
---
API
$3
Create a new vectorizer.
* stopwords – optional array of words to ignore during tokenization.
$3
Fit the vectorizer to an array of documents.
* texts – array of strings
$3
Transform a single document into a TF-IDF vector.
* Returns a normalized array of numbers.
* Throws an error if the vectorizer is not fitted.
$3
Transform multiple documents into vectors.
* Returns an array of normalized arrays.
$3
Compute cosine similarity between two vectors.
* Returns a number between 0 and 1.
$3
Normalize an array of numbers to the range [0, 1].
* Useful for consistent vector comparison.
$3
Convenience function to create, fit, and return a Vectorizer`.