Get n-grams from text
npm install n-gram[![Build][build-badge]][build]
[![Coverage][coverage-badge]][coverage]
[![Downloads][downloads-badge]][downloads]
[![Size][size-badge]][size]
Get [n-grams][wiki].
* What is this?
* When should I use this?
* Install
* Use
* API
* nGram(n)
* bigram(value)
* trigram(value)
* Types
* Compatibility
* Related
* Contribute
* Security
* License
This package gets you [bigrams][wiki] (or any n-gram, really).
You’re probably dealing with natural language, and know you need this, if
you’re here!
This package is [ESM only][esm].
In Node.js (version 12.20+, 14.14+, 16.0+), install with [npm][]:
``sh`
npm install n-gram
In Deno with [esm.sh][esmsh]:
`js`
import {nGram} from 'https://esm.sh/n-gram@2'
In browsers with [esm.sh][esmsh]:
`html`
`js
import {bigram, trigram, nGram} from 'n-gram'
bigram('n-gram') // ['n-', '-g', 'gr', 'ra', 'am']
nGram(2)('n-gram') // ['n-', '-g', 'gr', 'ra', 'am']
trigram('n-gram') // ['n-g', '-gr', 'gra', 'ram']
nGram(6)('n-gram') // ['n-gram']
nGram(7)('n-gram') // []
// Anything with a .length and .slice works: arrays too.`
bigram(['alpha', 'bravo', 'charlie']) // [['alpha', 'bravo'], ['bravo', 'charlie']]
This package exports the identifiers nGram, bigram, and trigram.
There is no default export.
Create a function that converts a given value to n-grams.
Want padding (to include partial matches)?
Use something like the following: nGram(2)(' ' + value + ' ')
Shortcut for nGram(2).
Shortcut for nGram(3).
This package is fully typed with [TypeScript][].
It exports no additional types.
This package is at least compatible with all maintained versions of Node.js.
As of now, that is Node.js 14.14+, 16.0+, and 18.0+.
It also works in Deno and modern browsers.
* wooorm/franc`
— natural language detection
Yes please!
See [How to Contribute to Open Source][contribute].
This package is safe.
[MIT][license] © [Titus Wormer][author]
[build-badge]: https://github.com/words/n-gram/workflows/main/badge.svg
[build]: https://github.com/words/n-gram/actions
[coverage-badge]: https://img.shields.io/codecov/c/github/words/n-gram.svg
[coverage]: https://codecov.io/github/words/n-gram
[downloads-badge]: https://img.shields.io/npm/dm/n-gram.svg
[downloads]: https://www.npmjs.com/package/n-gram
[size-badge]: https://img.shields.io/bundlephobia/minzip/n-gram.svg
[size]: https://bundlephobia.com/result?p=n-gram
[npm]: https://docs.npmjs.com/cli/install
[esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
[esmsh]: https://esm.sh
[license]: license
[author]: https://wooorm.com
[wiki]: https://en.wikipedia.org/wiki/N-gram
[typescript]: https://www.typescriptlang.org
[contribute]: https://opensource.guide/how-to-contribute/