Ngraminator
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
-
TypeError: ngraminator is not a function
cause Attempting to use `require('ngraminator')` as a direct function call in CommonJS environments since v3.0.1, where it's now a named export.fixChange your import from `const ngraminator = require('ngraminator')` to `const { ngraminator } = require('ngraminator')`. -
ReferenceError: ngraminator is not defined
cause Accessing the `ngraminator` function directly in a browser environment after loading the UMD build (e.g., via `<script>`) for versions 3.0.1 and later.fixAccess the function via the `ngrm` global object: `ngrm.ngraminator(...)`.
Warnings
- breaking The CommonJS `require()` syntax changed from a default import to a named import. Previously `const ngraminator = require('ngraminator')` worked, but now it requires destructuring.
- breaking For browser environments using the UMD build via a script tag, the global object has changed. The `ngraminator()` function is no longer directly accessible; it's now nested under a new global variable.
- gotcha Version 3.0.1 and later changed the internal bundler from webpack to Rollup and the test framework from Tape to Ava/Playwright. While not directly affecting API usage, it signifies a major internal refactor that could lead to subtle behavioral differences or impact bundling toolchains.
- deprecated A security update was released in version 2.0.3. Users on older v2.x versions should upgrade to at least 2.0.3 or preferably to the latest v3.x branch to ensure security patches are applied.
Install
-
npm install ngraminator -
yarn add ngraminator -
pnpm add ngraminator
Imports
- ngraminator
import ngraminator from 'ngraminator'
import { ngraminator } from 'ngraminator' - ngraminator
const ngraminator = require('ngraminator')const { ngraminator } = require('ngraminator') - ngrm.ngraminator
// In browser script tag: ngraminator(wordArray, ngramLengthArray)
// In browser script tag: ngrm.ngraminator(wordArray, ngramLengthArray)
Quickstart
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);