All Projects → words → n-gram

words / n-gram

Licence: MIT license
Get n-grams from text

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to n-gram

stringdistance
A fuzzy matching string distance library for Scala and Java that includes Levenshtein distance, Jaro distance, Jaro-Winkler distance, Dice coefficient, N-Gram similarity, Cosine similarity, Jaccard similarity, Longest common subsequence, Hamming distance, and more..
Stars: ✭ 60 (-10.45%)
Mutual labels:  ngram
Word-Prediction-Ngram
Next Word Prediction using n-gram Probabilistic Model with various Smoothing Techniques
Stars: ✭ 25 (-62.69%)
Mutual labels:  unigram
n-grammer-pytorch
Implementation of N-Grammer, augmenting Transformers with latent n-grams, in Pytorch
Stars: ✭ 50 (-25.37%)
Mutual labels:  n-gram
strutil
Golang metrics for calculating string similarity and other string utility functions
Stars: ✭ 114 (+70.15%)
Mutual labels:  n-gram

n-gram

Build Coverage Downloads Size

Get n-grams.

Contents

What is this?

This package gets you bigrams, trigrams, all the n-grams!

When should I use this?

You’re probably dealing with natural language, and know you need this, if you’re here!

Install

This package is ESM only. In Node.js (version 12.20+, 14.14+, 16.0+), install with npm:

npm install n-gram

In Deno with esm.sh:

import {nGram} from 'https://esm.sh/n-gram@2'

In browsers with esm.sh:

<script type="module">
  import {nGram} from 'https://esm.sh/n-gram@2?bundle'
</script>

Use

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']]

API

This package exports the identifiers nGram, bigram, and trigram. There is no default export.

nGram(n)

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 + ' ')

bigram(value)

Shortcut for nGram(2).

trigram(value)

Shortcut for nGram(3).

Types

This package is fully typed with TypeScript. It exports no additional types.

Compatibility

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.

Related

Contribute

Yes please! See How to Contribute to Open Source.

Security

This package is safe.

License

MIT © Titus Wormer

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].