aho-corasick-node icon indicating copy to clipboard operation
aho-corasick-node copied to clipboard

A Node implementation of the Aho-Corasick string matching algorithm based on DoubleArray Trie.

aho-corasick-node

A Node implementation of the Aho-Corasick string matching algorithm based on DoubleArray Trie.

Install

npm install aho-corasick-node --save

Usage

Build

const AhoCorasick = require('aho-corasick-node');

const keywords = ['b', 'ba', 'nan', 'ab'];
const builder = AhoCorasick.builder();
keywords.forEach(k => builder.add(k));
const ac = builder.build();

Match

const text = 'banana';
const hits = ac.match(text); // ['b', 'ba', 'nan']

Export

const buf = ac.export();
console.log(buf);
// {
//   base: 'string...',
//   check: 'string...',
//   failurelink: 'string...',
//   output: 'string...',
// };

Load

const loadedAC = AhoCorasick.from(buf);
const hits = loadedAC.match(text); // ['b', 'ba', 'nan']

Licence

MIT