Ngraminator

3.0.2 · active · verified Sun Apr 19

Ngraminator is a lightweight JavaScript utility designed to generate ngrams from arrays of words. It supports various module environments, including Node.js (CommonJS and ES Modules) and browsers (UMD), making it versatile for both backend and frontend text processing tasks. The current stable version is 3.0.2. The package underwent a significant architectural change in v3.0.1, migrating from webpack to Rollup for bundling, which introduced breaking changes in how the library is imported via CommonJS and accessed globally in UMD environments. Its primary differentiator is its small footprint and broad environment compatibility for generating n-grams of specified lengths from input arrays, without additional complex dependencies. It maintains a moderate release cadence, with recent updates focusing on build system improvements and module compatibility.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to import `ngraminator` and generate various n-gram lengths from a string.

import { ngraminator } from 'ngraminator';

const text = "The quick brown fox jumps over the lazy dog";
const words = text.split(' ');

// Get unigrams (n=1), bigrams (n=2), and trigrams (n=3)
const ngrams = ngraminator(words, [1, 2, 3]);

console.log('Generated N-grams:');
ngrams.forEach(ngram => console.log(ngram.join(' ')));

// Example of specific ngram lengths and transformations
const str = "mary had a little lamb it's fleece";
const result = ngraminator(str.split(' '), [1, 2, 5]).map(item => item.join(' '));

console.log('\nSpecific N-grams and stringified output:');
console.log(result);

view raw JSON →