JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 6
  • Score
    100M100P100Q63434F
  • License CC-BY-NC-SA-3.0

The easiest wat to get n-gram chunks from strings

Package Exports

  • simplengrams

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (simplengrams) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

SimleNGrams

The easiest way to get an array of n-grams from a string!

Usage

Asynchronous

const sng = require('simplengrams');
const text = 'A string of text...';
const opts = {logs: 3, strict: false};
const bigrams = await sng.ngrams(text, 2, opts);
console.log(bigrams);

Or, with .then() and .catch():

const sng = require('simplengrams');
const text = 'A string of text...';
const opts = {logs: 3, strict: false};
sng.ngrams(text, 3, opts)
  .then((arr) => {
    console.log(trigrams);
  })
  .catch((err) => {
    throw new Error(err);
  });

Synchronous

const sng = require('simplengrams');
const text = 'A string of text...';
const opts = {logs: 3, strict: false};
const bigrams = sng.ngramsSync(text, 2, opts);
console.log(bigrams);

Errors return an empty two-dimensional array, i.e. [[]]

If the n-gram size (i.e. '2' in the example above) is greater than the length of the input string, an empty two-dimensional array is returned, i.e. [[]].

Callback

const sng = require('simplengrams');
const text = 'A string of text...';
const opts = {logs: 3, strict: false};
ngramsSync(text, 2, opts, function(err, bigrams) {
  console.log(bigrams);
});

Example

Input

const sng = require('simplengrams');
const text = 'In the beginning God created the heavens and the earth.';
const opts = {logs: 3, strict: false};
const bigrams = sng(text, 2, opts);
console.log(bigrams);

Output

[
  [ 'In', 'the' ],
  [ 'the', 'beginning' ],
  [ 'beginning', 'God' ],
  [ 'God', 'created' ],
  [ 'created', 'the' ],
  [ 'the', 'heavens' ],
  [ 'heavens', 'and' ],
  [ 'and', 'the' ],
  [ 'the', 'earth' ],
  [ 'earth', '.' ]
]

The Options Object

The options object is optional, the defaults are:

{
  logs: 3,
  normalize: true,
  strict: false
}

logs

Number - valid options: 0, 1, 2, 3 (default)

Used to control console.log, console.warn, and console.error outputs.

  • 0 = suppress all logs
  • 1 = print errors only
  • 2 = print errors and warnings
  • 3 = print all console logs

normalize

boolean - valid options: true (default), or false

Normalise strings. E.g. when set to true, 'mañana' become 'manana'.

strict

Boolean - valid options: true, false (default)

When strict is set to true functions throw errors.

  • false = functions return empty arrays on error
  • true = functions throw on error

License

(C) 2017-19 P. Hughes. All rights reserved.

Shared under the Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported license.