0.6.3 • Public • Published


NPM version Build Status Slack

Forked and edited for use in Fireflies.

"Natural" is a general natural language facility for nodejs. Tokenizing, stemming, classification, phonetics, tf-idf, WordNet, string similarity, and some inflections are currently supported.

It's still in the early stages, so we're very interested in bug reports, contributions and the like.

Note that many algorithms from Rob Ellis's node-nltools are being merged into this project and will be maintained from here onward.

While most of the algorithms are English-specific, contributors have implemented support for other languages. Thanks to Polyakov Vladimir, Russian stemming has been added! Thanks to David Przybilla, Spanish stemming has been added! Thanks to even more contributors, stemming and tokenizing in more languages have been added.

Aside from this README, the only documentation is this DZone article, this course on, and here on my blog. The README is up to date, the other sources are somewhat outdated.



If you're just looking to use natural without your own node application, you can install via NPM like so:

npm install natural

If you're interested in contributing to natural, or just hacking on it, then by all means fork away!


Word, Regexp, and Treebank tokenizers are provided for breaking text up into arrays of tokens:

var natural = require('natural');
var tokenizer = new natural.WordTokenizer();
console.log(tokenizer.tokenize("your dog has fleas."));
// [ 'your', 'dog', 'has', 'fleas' ]

The other tokenizers follow a similar pattern:

tokenizer = new natural.TreebankWordTokenizer();
console.log(tokenizer.tokenize("my dog hasn't any fleas."));
// [ 'my', 'dog', 'has', 'n\'t', 'any', 'fleas', '.' ]
tokenizer = new natural.RegexpTokenizer({pattern: /\-/});
// [ 'flea', 'dog' ]
tokenizer = new natural.WordPunctTokenizer();
console.log(tokenizer.tokenize("my dog hasn't any fleas."));
// [ 'my',  'dog',  'hasn',  '\'',  't',  'any',  'fleas',  '.' ]
tokenizer = new natural.OrthographyTokenizer({language: "fi"});
console.log(tokenizer.tokenize("Mikä sinun nimesi on?"));
// [ 'Mikä', 'sinun', 'nimesi', 'on' ]

Overview of available tokenizers:

Tokenizer Language Explanation
WordTokenizer Any Splits on anything except alphabetic characters, digits and underscore
WordPunctTokenizer Any Splits on anything except alphabetic characters, digits, punctuation and underscore
SentenceTokenizer Any Break string up in to parts based on punctation and quotation marks
CaseTokenizer Any? If lower and upper case are the same, the character is assumed to be whitespace or something else (punctuation)
RegexpTokenizer Any Splits on a regular expression that either defines sequences of word characters or gap characters
OrthographyTokenizer Finnish Splits on anything except alpabetic characters, digits and underscore
TreebankWordTokenizer Any
AggressiveTokenizer English
AggressiveTokenizerFa Farsi
AggressiveTokenizerFr French
AggressiveTokenizerRu Russian
AggressiveTokenizerEs Spanish
AggressiveTokenizerIt Italian
AggressiveTokenizerPl Polish
AggressiveTokenizerPt Portugese
AggressiveTokenizerNo Norwegian
AggressiveTokenizerSv Swedish
AggressiveTokenizerVi Vietnamese
TokenizerJa Japanese

String Distance

Natural provides an implementation of three algorithms for calculating string distance: Hamming distance, Jaro-Winkler, Levenshtein distance, and Dice coefficient.

Hamming distance measures the distance between two strings of equal length by counting the number of different characters. The third parameter indicates whether case should be ignored. By default the algorithm is case sensitive.

var natural = require('natural');
console.log(natural.HammingDistance("karolin", "kathrin", false));
console.log(natural.HammingDistance("karolin", "kerstin", false));
// If strings differ in length -1 is returned
console.log(natural.HammingDistance("short string", "longer string", false));



The Jaro–Winkler string distance measuring algorithm will return a number between 0 and 1 which tells how closely the strings match (0 = not at all, 1 = exact match):

var natural = require('natural');
console.log(natural.JaroWinklerDistance('not', 'same'));



If the distance between the strings is already known you can pass it as a third parameter. And you can force the algorithm to ignore case by passing a fourth parameter as follows:

var natural = require('natural');
console.log(natural.JaroWinklerDistance("dixon","dicksonx", undefined, true));

Natural also offers support for Levenshtein distances:

var natural = require('natural');
console.log(natural.LevenshteinDistance('one', 'one'));



The cost of the three edit operations are modifiable for Levenshtein:

console.log(natural.LevenshteinDistance("ones","onez", {
    insertion_cost: 1,
    deletion_cost: 1,
    substitution_cost: 1



Full Damerau-Levenshtein matching can be used if you want to consider character transpositions as a valid edit operation.

console.log(natural.DamerauLevenshteinDistance("az", "za"));



The transposition cost can be modified as well:

console.log(natural.DamerauLevenshteinDistance("az", "za", { transposition_cost: 0 }))



A restricted form of Damerau-Levenshtein (Optimal String Alignment) is available.

This form of matching is more space efficient than unrestricted Damerau-Levenshtein, by only considering a transposition if there are no characters between the transposed characters.


// Optimal String Alignment
console.log(natural.DamerauLevenshteinDistance('ABC', 'ACB'), { restricted: true });
console.log(natural.DamerauLevenshteinDistance('CA', 'ABC', { restricted: true }));
// Unrestricted Damerau-Levenshtein
console.log(natural.DamerauLevenshteinDistance('CA', 'ABC', { restricted: false }));

And Dice's co-efficient:

var natural = require('natural');
console.log(natural.DiceCoefficient('thing', 'thing'));
console.log(natural.DiceCoefficient('not', 'same'));



Approximate String Matching

Currently matching is supported via the Levenshtein algorithm.

var natural = require('natural');
var source = 'The RainCoat BookStore';
var target = 'All the best books are here at the Rain Coats Book Store';
console.log(natural.LevenshteinDistance(source, target, {search: true}));


{ substring: 'the Rain Coats Book Store', distance: 4 }

The following


Currently stemming is supported via the Porter and Lancaster (Paice/Husk) algorithms. The Indonesian and Japanese stemmers do not follow a known algorithm.

var natural = require('natural');

This example uses a Porter stemmer. "word" is returned.

console.log(natural.PorterStemmer.stem("words")); // stem a single word

in Russian:


in Spanish:


The following stemmers are available:

Language Porter Lancaster Other Module
Dutch X PorterStemmerNl
English X PorterStemmer
English X LancasterStemmer
Farsi (in progress) X PorterStemmerFa
French X PorterStemmerFr
Indonesian X StemmerId
Italian X PorterStemmerIt
Japanese X StemmerJa
Norwegian X PorterStemmerNo
Portugese X PorterStemmerPt
Russian X PorterStemmerRu
Swedish X PorterStemmerSv

attach() patches stem() and tokenizeAndStem() to String as a shortcut to PorterStemmer.stem(token). tokenizeAndStem() breaks text up into single words and returns an array of stemmed tokens.

console.log("i am waking up to the sounds of chainsaws".tokenizeAndStem());

The same thing can be done with a Lancaster stemmer:

console.log("i am waking up to the sounds of chainsaws".tokenizeAndStem());


Bayesian and logistic regression

Two classifiers are currently supported, Naive Bayes and logistic regression. The following examples use the BayesClassifier class, but the LogisticRegressionClassifier class could be substituted instead.

var natural = require('natural');
var classifier = new natural.BayesClassifier();

You can train the classifier on sample text. It will use reasonable defaults to tokenize and stem the text.

classifier.addDocument('i am long qqqq', 'buy');
classifier.addDocument('buy the q\'s', 'buy');
classifier.addDocument('short gold', 'sell');
classifier.addDocument('sell gold', 'sell');

Outputs "sell"

console.log(classifier.classify('i am short silver'));

Outputs "buy"

console.log(classifier.classify('i am long copper'));

You have access to the set of matched classes and the associated value from the classifier.


[ { label: 'buy', value: 0.39999999999999997 },
  { label: 'sell', value: 0.19999999999999998 } ]

From this:

console.log(classifier.getClassifications('i am long copper'));

The classifier can also be trained with and can classify arrays of tokens, strings, or any mixture of the two. Arrays let you use entirely custom data with your own tokenization/stemming, if you choose to implement it.

classifier.addDocument(['sell', 'gold'], 'sell');

The training process can be monitored by subscribing to the event trainedWithDocument that's emitted by the classifier, this event's emitted each time a document is finished being trained against:'trainedWithDocument', function (obj) {
       /* {
       *   total: 23 // There are 23 total documents being trained against
       *   index: 12 // The index/number of the document that's just been trained against
       *   doc: {...} // The document that has just been indexed
       *  }

A classifier can also be persisted and recalled so you can reuse a training'classifier.json', function(err, classifier) {
    // the classifier is saved to the classifier.json file!

To recall from the classifier.json saved above:

natural.BayesClassifier.load('classifier.json', null, function(err, classifier) {
    console.log(classifier.classify('long SUNW'));
    console.log(classifier.classify('short SUNW'));

A classifier can also be serialized and deserialized like so:

var classifier = new natural.BayesClassifier();
classifier.addDocument(['sell', 'gold'], 'sell');
classifier.addDocument(['buy', 'silver'], 'buy');
// serialize
var raw = JSON.stringify(classifier);
// deserialize
var restoredClassifier = natural.BayesClassifier.restore(JSON.parse(raw));
console.log(restoredClassifier.classify('i should sell that'));

Note: if using the classifier for languages other than English you may need to pass in the stemmer to use. In fact, you can do this for any stemmer including alternate English stemmers. The default is the PorterStemmer.

const PorterStemmerRu = require('./node_modules/natural/lib/natural/stemmers/porter_stemmer_ru');
var classifier = new natural.BayesClassifier(PorterStemmerRu);

Maximum Entropy Classifier

This module provides a classifier based on maximum entropy modelling. The central idea to maximum entropy modelling is to estimate a probability distribution that that has maximum entropy subject to the evidence that is available. This means that the distribution follows the data it has "seen" but does not make any assumptions beyond that.

The module is not specific to natural language processing, or any other application domain. There are little requirements with regard to the data structure it can be trained on. For training, it needs a sample that consists of elements. These elements have two parts:

  • part a: the class of the element
  • part b: the context of the element The classifier will, once trained, return the most probable class for a particular context.

We start with an explanation of samples and elements. You have to create your own specialisation of the Element class. Your element class should implement the generateFeatures method for inferring feature functions from the sample.

Samples and elements

Elements and contexts are created as follows:

var MyElement = require('MyElementClass');
var Context = require('Context');
var Sample = require('Sample');
var x = new MyElementClass("x", new Context("0"));
// A sample is created from an array of elements
var sample = new Sample();

A class is a string, contexts may be as complex as you want (as long as it can be serialised).

A sample can be saved to and loaded from a file:'sample.json', function(error, sample) {

A sample can be read from a file as follows.

sample.load('sample.json', MyElementClass, function(err, sample) {

You have to pass the element class to the load method so that the right element objects can be created from the data.

Features and feature sets

Features are functions that map elements to zero or one. Features are defined as follows:

var Feature = require('Feature');
function f(x) {
  if (x.b === "0") {
    return 1;
  return 0;
var feature = new Feature(f, name, parameters);

name is a string for the name of the feature function, parameters is an array of strings for the parameters of the feature function. The combination of name and parameters should uniquely distinguish features from each other. Features that are added to a feature set are tested for uniqueness using these properties.

A feature set is created like this

var FeatureSet = require('FeatureSet');
var set = new FeatureSet();
set.addFeature(f, "f", ["0"]);

In most cases you will generate feature functions using closures. For instance, when you generate feature functions in a loop that iterates through an array

var FeatureSet = require('FeatureSet');
var Feature = require('Feature');
var listOfTags = ['NN', 'DET', 'PREP', 'ADJ'];
var featureSet = new FeatureSet();
listofTags.forEach(function(tag) {
  function isTag(x) {
    if ( === tag) {
      return 1
    return 0;
  featureSet.addFeature(new Feature(isTag, "isTag", [tag]));

In this example you create feature functions that each have a different value for tag in their closure.

Setting up and training the classifier

A classifier needs the following parameter:

  • Classes: an array of classes (strings)
  • Features: an array of feature functions
  • Sample: a sample of elements for training the classifier

A classifier can be created as follows:

var Classifier = require('Classifier');
var classifier = new Classifier(classes, featureSet, sample);

And it starts training with:

var maxIterations = 100;
var minImprovement = .01;
var p = classifier.train(maxIterations, minImprovement);

Training is finished when either maxIterations is reached or the improvement in likelihood (of the sample) becomes smaller than minImprovement. It returns a probability distribution that can be stored and retrieved for later usage:'classifier.json', function(err, c) {
  if (err) {
  else {
    // Continue using the classifier
classifier.load('classifier.json', function(err, c) {
  if (err) {
  else {
    // Use the classifier

The training algorithm is based on Generalised Iterative Scaling.

Applying the classifier

The classifier can be used to classify contexts in two ways. To get the probabilities for all classes:

var classifications = classifier.getClassifications(context);
classifications.forEach(function(classPlusProbability) {
  console.log('Class ' + classPlusProbability.label + ' has score ' + classPlusProbability.value);

This returns a map from classes to probabilities. To get the highest scoring class:

var class = classifier.classify(context);

Simple example of maximum entropy modelling

A test is added to the spec folder based on simple elements that have contexts that are either "0" or "1", and classes are "x" and "y".

  "a": "x",
  "b": {
    "data": "0"

In the SE_Element class that inherits from Element, the method generateFeatures is implemented. It creates a feature function that tests for context "0".

After setting up your own element class, the classifier can be created and trained.

Application to POS tagging

A more elaborate example of maximum entropy modelling is provided for part of speech tagging. The following steps are taken to create a classifier and apply it to a test set:

  • A new element class POS_Element is created that has a word window and a tag window around the word to be tagged.
  • From the Brown corpus a sample is generated consisting of POS elements.
  • Feature functions are generated from the sample.
  • A classifier is created and trained.
  • The classifier is applied to a test set. Results are compared to a simple lexicon-based tagger.


Sentiment Analysis

This is a simple sentiment analysis algorithm based on a vocabulary that assigns polarity to words. The algorithm calculates the sentiment of a piece of text by summing the polarity of each word and normalizing with the length of the sentence. If a negation occurs the result is made negative. It is used as follows:

var Analyzer = require('natural').SentimentAnalyzer;
var stemmer = require('natural').PorterStemmer;
var analyzer = new Analyzer("English", stemmer, "afinn");
// getSentiment expects an array of strings
console.log(analyzer.getSentiment(["I", "like", "cherries"]));
// 0.6666666666666666

The constructor has three parameters:

  • Language: see below for supported languages.
  • Stemmer: to increase the coverage of the sentiment analyzer a stemmer may be provided. May be null.
  • Vocabulary: sets the type of vocabulary, "afinn", "senticon" or "pattern" are valid values.

Currently, the following languages are supported with type of vocabulary and availability of negations (in alphabetic order):

Language AFINN Senticon Pattern Negations
Basque X
Catalan X
Dutch X X
English X X X X
French X
Galician X
Italian X
Spanish X X X

More languages can be added by adding vocabularies and extending the map languageFiles in SentimentAnalyzer.js. In the tools folder below lib/natural/sentiment some tools are provided for transforming vocabularies in Senticon and Pattern format into a JSON format.

Acknowledgements and References

Thanks to Domingo Martín Mancera for providing the basis for this sentiment analyzer in his repo Lorca.

AFINN is a list of English words rated for valence with an integer between minus five (negative) and plus five (positive). The words have been manually labeled by Finn Årup Nielsen in 2009-2011. Scientific reference can be found here. We used afinn-165 which is available as nodejs module.

The senticon vocabulary is based on work by Fermin L. Cruz and others: Cruz, Fermín L., José A. Troyano, Beatriz Pontes, F. Javier Ortega. Building layered, multilingual sentiment lexicons at synset and lemma levels, Expert Systems with Applications, 2014.

The Pattern vocabularies are from the Pattern project of the CLiPS Research Center of University of Antwerpen. These have a PDDL license.


Phonetic matching (sounds-like) matching can be done with the SoundEx, Metaphone or DoubleMetaphone algorithms

var natural = require('natural');
var metaphone = natural.Metaphone;
var soundEx = natural.SoundEx;
var wordA = 'phonetics';
var wordB = 'fonetix';

To test the two words to see if they sound alike:

if(, wordB))
    console.log('they sound alike!');

The raw phonetics are obtained with process():


A maximum code length can be supplied:

console.log(metaphone.process('phonetics', 3));

DoubleMetaphone deals with two encodings returned in an array. This feature is experimental and subject to change:

var natural = require('natural');
var dm = natural.DoubleMetaphone;
var encodings = dm.process('Matrix');

Attaching will patch String with useful methods:


soundsLike is essentially a shortcut to

    console.log('they sound alike!');

The raw phonetics are obtained with phonetics():


Full text strings can be tokenized into arrays of phonetics (much like how tokenization-to-arrays works for stemmers):

console.log('phonetics rock'.tokenizeAndPhoneticize());

Same module operations applied with SoundEx:

if(, wordB))
    console.log('they sound alike!');

The same String patches apply with soundEx:

    console.log('they sound alike!');



Nouns can be pluralized/singularized with a NounInflector:

var natural = require('natural');
var nounInflector = new natural.NounInflector();

To pluralize a word (outputs "radii"):


To singularize a word (outputs "beer"):


Like many of the other features, String can be patched to perform the operations directly. The "Noun" suffix on the methods is necessary, as verbs will be supported in the future.



Numbers can be counted with a CountInflector:

var countInflector = natural.CountInflector;

Outputs "1st":


Outputs "111th":


Present Tense Verbs

Present Tense Verbs can be pluralized/singularized with a PresentVerbInflector. This feature is still experimental as of 0.0.42, so use with caution, and please provide feedback.

var verbInflector = new natural.PresentVerbInflector();

Outputs "becomes":


Outputs "become":


Like many other natural modules, attach() can be used to patch strings with handy methods.



n-grams can be obtained for either arrays or strings (which will be tokenized for you):

var NGrams = natural.NGrams;


console.log(NGrams.bigrams('some words here'));
console.log(NGrams.bigrams(['some',  'words',  'here']));

Both of the above output: [ [ 'some', 'words' ], [ 'words', 'here' ] ]


console.log(NGrams.trigrams('some other words here'));
console.log(NGrams.trigrams(['some',  'other', 'words',  'here']));

Both of the above output: [ [ 'some', 'other', 'words' ], [ 'other', 'words', 'here' ] ]

arbitrary n-grams

console.log(NGrams.ngrams('some other words here for you', 4));
console.log(NGrams.ngrams(['some', 'other', 'words', 'here', 'for',
    'you'], 4));

The above outputs: [ [ 'some', 'other', 'words', 'here' ], [ 'other', 'words', 'here', 'for' ], [ 'words', 'here', 'for', 'you' ] ]


n-grams can also be returned with left or right padding by passing a start and/or end symbol to the bigrams, trigrams or ngrams.

console.log(NGrams.ngrams('some other words here for you', 4, '[start]', '[end]'));

The above will output:

[ [ '[start]', '[start]', '[start]', 'some' ],
  [ '[start]', '[start]', 'some', 'other' ],
  [ '[start]', 'some', 'other', 'words' ],
  [ 'some', 'other', 'words', 'here' ],
  [ 'other', 'words', 'here', 'for' ],
  [ 'words', 'here', 'for', 'you' ],
  [ 'here', 'for', 'you', '[end]' ],
  [ 'for', 'you', '[end]', '[end]' ],
  [ 'you', '[end]', '[end]', '[end]' ] ]

For only end symbols, pass null for the start symbol, for instance:

console.log(NGrams.ngrams('some other words here for you', 4, null, '[end]'));

Will output:

[ [ 'some', 'other', 'words', 'here' ],
  [ 'other', 'words', 'here', 'for' ],
  [ 'words', 'here', 'for', 'you' ],
  [ 'here', 'for', 'you', '[end]' ],
  [ 'for', 'you', '[end]', '[end]' ],
  [ 'you', '[end]', '[end]', '[end]' ] ]


For Chinese like languages, you can use NGramsZH to do a n-gram, and all apis are the same:

var NGramsZH = natural.NGramsZH;
console.log(NGramsZH.bigrams(['',  '',  '', '']));
console.log(NGramsZH.trigrams(['',  '', '',  '']));
console.log(NGramsZH.ngrams('一个中文测试', 4));
console.log(NGramsZH.ngrams(['', '', '', '', '',
    ''], 4));


Term Frequency–Inverse Document Frequency (tf-idf) is implemented to determine how important a word (or words) is to a document relative to a corpus. The following formulas are used for calculating tf and idf:

  • tf(t, d) is a so-called raw count, so just the count of the term in the document
  • idf(t, D) uses the following formula: 1 + ln(N / (1 + n_t)) where N is the number of documents, and n_t the number of documents in which the term appears. The 1 + in the denominator is for handling the possibility that n_t is 0.

The following example will add four documents to a corpus and determine the weight of the word "node" and then the weight of the word "ruby" in each document.

var natural = require('natural');
var TfIdf = natural.TfIdf;
var tfidf = new TfIdf();
tfidf.addDocument('this document is about node.');
tfidf.addDocument('this document is about ruby.');
tfidf.addDocument('this document is about ruby and node.');
tfidf.addDocument('this document is about node. it has node examples');
console.log('node --------------------------------');
tfidf.tfidfs('node', function(i, measure) {
    console.log('document #' + i + ' is ' + measure);
console.log('ruby --------------------------------');
tfidf.tfidfs('ruby', function(i, measure) {
    console.log('document #' + i + ' is ' + measure);

The above outputs:

node --------------------------------
document #0 is 1
document #1 is 0
document #2 is 1
document #3 is 2
ruby --------------------------------
document #0 is 0
document #1 is 1.2876820724517808
document #2 is 1.2876820724517808
document #3 is 0

This approach can also be applied to individual documents.

The following example measures the term "node" in the first and second documents.

console.log(tfidf.tfidf('node', 0));
console.log(tfidf.tfidf('node', 1));

A TfIdf instance can also load documents from files on disk.

var tfidf = new TfIdf();

Multiple terms can be measured as well, with their weights being added into a single measure value. The following example determines that the last document is the most relevant to the words "node" and "ruby".

var natural = require('natural');
var TfIdf = natural.TfIdf;
var tfidf = new TfIdf();
tfidf.addDocument('this document is about node.');
tfidf.addDocument('this document is about ruby.');
tfidf.addDocument('this document is about ruby and node.');
tfidf.tfidfs('node ruby', function(i, measure) {
    console.log('document #' + i + ' is ' + measure);

The above outputs:

document #0 is 1
document #1 is 1
document #2 is 2

The examples above all use strings, which causes natural to automatically tokenize the input. If you wish to perform your own tokenization or other kinds of processing, you can do so, then pass in the resultant arrays later. This approach allows you to bypass natural's default preprocessing.

var natural = require('natural');
var TfIdf = natural.TfIdf;
var tfidf = new TfIdf();
tfidf.addDocument(['document', 'about', 'node']);
tfidf.addDocument(['document', 'about', 'ruby']);
tfidf.addDocument(['document', 'about', 'ruby', 'node']);
tfidf.addDocument(['document', 'about', 'node', 'node', 'examples']);
tfidf.tfidfs(['node', 'ruby'], function(i, measure) {
    console.log('document #' + i + ' is ' + measure);

It's possible to retrieve a list of all terms in a document, sorted by their importance.

tfidf.listTerms(0 /*document index*/).forEach(function(item) {
    console.log(item.term + '' + item.tfidf);

A TfIdf instance can also be serialized and deserialized for save and recall.

var tfidf = new TfIdf();
tfidf.addDocument('document one', 'un');
tfidf.addDocument('document Two', 'deux');
var s = JSON.stringify(tfidf);
// save "s" to disk, database or otherwise
// assuming you pulled "s" back out of storage.
var tfidf = new TfIdf(JSON.parse(s));


Tries are a very efficient data structure used for prefix-based searches. Natural comes packaged with a basic Trie implementation which can support match collection along a path, existence search and prefix search.

Building The Trie

You need to add words to build up the dictionary of the Trie, this is an example of basic Trie set up:

var natural = require('natural');
var Trie = natural.Trie;
var trie = new Trie();
// Add one string at a time
// Or add many strings
trie.addStrings(["string1", "string2", "string3"]);



The most basic operation on a Trie is to see if a search string is marked as a word in the Trie.

console.log(trie.contains("test")); // true
console.log(trie.contains("asdf")); // false

Find Prefix

The find prefix search will find the longest prefix that is identified as a word in the trie. It will also return the remaining portion of the string which it was not able to match.

console.log(trie.findPrefix("tester"));     // ['test', 'er']
console.log(trie.findPrefix("string4"));    // [null, '4']
console.log(trie.findPrefix("string3"));    // ['string3', '']

All Prefixes on Path

This search will return all prefix matches along the search string path.

console.log(trie.findMatchesOnPath("tester")); // ['tes', 'test'];

All Keys with Prefix

This search will return all of the words in the Trie with the given prefix, or [ ] if not found.

console.log(trie.keysWithPrefix("string")); // ["string1", "string2", "string3"]


By default the trie is case-sensitive, you can use it in case-_in_sensitive mode by passing false to the Trie constructor.

trie.contains("TEST"); // false
var ciTrie = new Trie(false);
ciTrie.contains("TEsT"); // true

In the case of the searches which return strings, all strings returned will be in lower case if you are in case-_in_sensitive mode.


EdgeWeightedDigraph represents a digraph, you can add an edge, get the number vertexes, edges, get all edges and use toString to print the Digraph.

initialize a digraph:

var EdgeWeightedDigraph = natural.EdgeWeightedDigraph;
var digraph = new EdgeWeightedDigraph();

the api used is: add(from, to, weight).

get the number of vertexes:


you will get 7.

get the number of edges:


you will get 6.


ShortestPathTree represents a data type for solving the single-source shortest paths problem in edge-weighted directed acyclic graphs (DAGs). The edge weights can be positive, negative, or zero. There are three APIs: getDistTo(vertex), hasPathTo(vertex), pathTo(vertex).

var ShortestPathTree = natural.ShortestPathTree;
var spt = new ShortestPathTree(digraph, 5);

digraph is an instance of EdgeWeightedDigraph, the second param is the start vertex of DAG.


Will return the dist to vertex.


the output will be: 0.35


Will return the shortest path:


output will be:

[5, 4]


LongestPathTree represents a data type for solving the single-source longest paths problem in edge-weighted directed acyclic graphs (DAGs). The edge weights can be positive, negative, or zero. There are three APIs same as ShortestPathTree: getDistTo(vertex), hasPathTo(vertex), pathTo(vertex).

var LongestPathTree = natural.LongestPathTree;
var lpt = new LongestPathTree(digraph, 5);

digraph is an instance of EdgeWeightedDigraph, the second param is the start vertex of DAG.


Will return the dist to vertex.


the output will be: 2.06


Will return the longest path:


output will be:

[5, 1, 3, 6, 4]


One of the newest and most experimental features in natural is WordNet integration. Here's an example of using natural to look up definitions of the word node. To use the WordNet module, first install the WordNet database files using wordnet-db:

npm install wordnet-db

Keep in mind that the WordNet integration is to be considered experimental at this point, and not production-ready. The API is also subject to change. For an implementation with vastly increased performance, as well as a command-line interface, see wordpos.

Here's an example of looking up definitions for the word "node".

var wordnet = new natural.WordNet();
wordnet.lookup('node', function(results) {
    results.forEach(function(result) {

Given a synset offset and a part of speech, a definition can be looked up directly.

var wordnet = new natural.WordNet();
wordnet.get(4424418, 'n', function(result) {

If you have manually downloaded the WordNet database files, you can pass the folder to the constructor:

var wordnet = new natural.WordNet('/my/wordnet/dict');

As of v0.1.11, WordNet data files are no longer automatically downloaded.

Princeton University "About WordNet." WordNet. Princeton University. 2010.


A probabilistic spellchecker based on

This is best constructed with an array of tokens from a corpus, but a simple list of words from a dictionary will work.

var corpus = ['something', 'soothing'];
var spellcheck = new natural.Spellcheck(corpus);

It uses the trie datastructure for fast boolean lookup of a word

spellcheck.isCorrect('cat'); // false

It suggests corrections (sorted by probability in descending order) that are up to a maximum edit distance away from the input word. According to Norvig, a max distance of 1 will cover 80% to 95% of spelling mistakes. After a distance of 2, it becomes very slow.

spellcheck.getCorrections('soemthing', 1); // ['something']
spellcheck.getCorrections('soemthing', 2); // ['something', 'soothing']

POS Tagger

This is a part-of-speech tagger based on Eric Brill's transformational algorithm. Transformation rules are specified in external files.


var natural = require("natural");
var path = require("path");
var base_folder = path.join(path.dirname(require.resolve("natural")), "brill_pos_tagger");
var rulesFilename = base_folder + "/data/English/tr_from_posjs.txt";
var lexiconFilename = base_folder + "/data/English/lexicon_from_posjs.json";
var defaultCategory = 'N';
var lexicon = new natural.Lexicon(lexiconFilename, defaultCategory);
var rules = new natural.RuleSet(rulesFilename);
var tagger = new natural.BrillPOSTagger(lexicon, rules);
var sentence = ["I", "see", "the", "man", "with", "the", "telescope"];

This outputs the following:

Sentence {
   [ { token: 'I', tag: 'NN' },
     { token: 'see', tag: 'VB' },
     { token: 'the', tag: 'DT' },
     { token: 'man', tag: 'NN' },
     { token: 'with', tag: 'IN' },
     { token: 'the', tag: 'DT' },
     { token: 'telescope', tag: 'NN' } ] }


The lexicon is either a JSON file that has the following structure:

  "word1": ["cat1"],
  "word2": ["cat2", "cat3"],

or a text file:

word1 cat1 cat2
word2 cat3

Words may have multiple categories in the lexicon file. The tagger uses only the first category specified.

Specifying transformation rules

Transformation rules are specified as follows:


This means that if the category of the current position is OLD_CAT and the predicate is true, the category is replaced by NEW_CAT. The predicate may use the parameter in different ways: sometimes the parameter is used for specifying the outcome of the predicate:


This means that if the outcome of predicate CURRENT-WORD-IS-NUMBER is YES, the category is replaced by CD. The parameter can also be used to check the category of a word in the sentence:


Here the category of the previous word must be DT for the rule to be applied.


The tagger applies transformation rules that may change the category of words. The input sentence is a Sentence object with tagged words. The tagged sentence is processed from left to right. At each step all rules are applied once; rules are applied in the order in which they are specified. Algorithm:

Brill_POS_Tagger.prototype.applyRules = function(sentence) {
  for (var i = 0, size = sentence.taggedWords.length; i < size; i++) {
    this.ruleSet.getRules().forEach(function(rule) {
      rule.apply(sentence, i);
  return sentence;

The output is a Sentence object just like the input sentence.

Adding a predicate

Predicates are defined in module lib/RuleTemplates.js. In that file predicate names are mapped to metadata for generaring transformation rules. The following properties must be supplied:

  • Name of the predicate
  • A function that evaluates the predicate (should return a boolean)
  • A window [i, j] that defines the span of the predicate in the sentence relative to the current position
  • The number of parameter the predicate needs: 0, 1 or 2
  • If relevant, a function for parameter 1 that returns its possible values at the current position in the sentence (for generating rules in training)
  • If relevant, a function for parameter 2 that returns its possible values at the current position in the sentence (for training)

A typical entry for a rule templates looks like this:

    // maps to the predicate function
    "function": next_tag_is,
    // Minimum required window before or after current position to be a relevant predicate
    "window": [0, 1],
    // The number of parameters the predicate takes
    "nrParameters": 1,
    // Function that returns relevant values for parameter 1
    "parameter1Values": nextTagParameterValues

A predicate function accepts a Sentence object, the current position in the sentence that should be tagged, and the outcome(s) of the predicate. An example of a predicate that checks the category of the current word:

function next_tag_is(sentence, i, parameter) {
  if (< sentence.taggedWords.length - 1) {
    return(sentence.taggedWords[+ 1][1] === parameter);
  else {

A values function for a parameter returns an array all possible parameter values given a location in a tagged sentence.

function nextTagParameterValues(sentence, i) {
  if (< sentence.length - 1) {
    return [sentence[+ 1].tag];
  else {
    return [];


The trainer allows to learn a new set of transformation rules from a corpus. It takes as input a tagged corpus and a set of rule templates. The algorithm generates positive rules (rules that apply at some location in the corpus) from the templates and iteratively extends and optimises the rule set.

First, a corpus should be loaded. Currently, the format of Brown corpus is supported. Then a lexicon can be created from the corpus. The lexicon is needed for tagging the sentences before the learning algorithm is applied.

var natural = require(natural);
var text = fs.readFileSync(brownCorpusFile, 'utf8');
var corpus = new natural.Corpus(text, 1);
var lexicon = corpus.buildLexicon();

The next step is to create a set of rule templates from which the learning algorithm can generate transformation rules. Rule templates are defined in PredicateMapping.js.

var natural require('natural');
var templateNames = [
var templates = {
  return new natural.RuleTemplate(name);

Using lexicon and rule templates we can now start the trainer as follows.

var natural require('natural');
var Tester = require('natural.BrillPOSTrainer');
var trainer = new Trainer(/* optional threshold */);
var ruleSet = trainer.train(corpus, templates, lexicon);

A threshold value can be passed to constructor. Transformation rules with a score below the threshold are removed after training. The train method returns a set of transformation rules that can be used to create a POS tagger as usual. Also you can output the rule set in the right format for later usage.



Now we can apply the lexicon and rule set to a test set.

var tester = new natural.BrillPOSTester();
var tagger = new natural.BrillPOSTagger(lexicon, ruleSet);
var scores = tester.test(corpora[1], tagger);

The test method returns an array of two percentages: first percentage is the ratio of right tags after tagging with the lexicon; second percentage is the ratio of right tags after applying the transformation rules.

console.log("Test score lexicon " + scores[0] + "%");
console.log("Test score after applying rules " + scores[1] + "%");

Acknowledgements and References


When developing, please:

  • Write unit tests
  • Make sure your unit tests pass

The current configuration of the unit tests requires the following environment variable to be set:

export NODE_PATH=.


Copyright (c) 2011, 2012 Chris Umbel, Rob Ellis, Russell Mull

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.


WordNet License

This license is available as the file LICENSE in any downloaded version of WordNet. WordNet 3.0 license: (Download)

WordNet Release 3.0 This software and database is being provided to you, the LICENSEE, by Princeton University under the following license. By obtaining, using and/or copying this software and database, you agree that you have read, understood, and will comply with these terms and conditions.: Permission to use, copy, modify and distribute this software and database and its documentation for any purpose and without fee or royalty is hereby granted, provided that you agree to comply with the following copyright notice and statements, including the disclaimer, and that the same appear on ALL copies of the software, database and documentation, including modifications that you make for internal use or for distribution. WordNet 3.0 Copyright 2006 by Princeton University. All rights reserved. THIS SOFTWARE AND DATABASE IS PROVIDED "AS IS" AND PRINCETON UNIVERSITY MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED. BY WAY OF EXAMPLE, BUT NOT LIMITATION, PRINCETON UNIVERSITY MAKES NO REPRESENTATIONS OR WARRANTIES OF MERCHANT- ABILITY OR FITNESS FOR ANY PARTICULAR PURPOSE OR THAT THE USE OF THE LICENSED SOFTWARE, DATABASE OR DOCUMENTATION WILL NOT INFRINGE ANY THIRD PARTY PATENTS, COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS. The name of Princeton University or Princeton may not be used in advertising or publicity pertaining to distribution of the software and/or database. Title to copyright in this software, database and any associated documentation shall at all times remain with Princeton University and LICENSEE agrees to preserve same.

Package Sidebar


npm i natural-ff

Weekly Downloads






Unpacked Size

79.3 kB

Total Files


Last publish


  • sudotong