mikolalysenko / aho-corasick-automaton Goto Github PK
View Code? Open in Web Editor NEWGenerates an Aho-Corasick automata for matching multiple patterns in a stream
License: MIT License
Generates an Aho-Corasick automata for matching multiple patterns in a stream
License: MIT License
base case:
///////////////////////////////////////////////////////////////////////////////////////
var createTrie = require('array-trie');
var createAC = require('aho-corasick-automaton');
var trie = createTrie();
// First build the trie data structure
trie.set([2, 3], 1);
trie.set([1, 2, 3, 4, 5], 2);
trie.set([4, 7, 8], 3);
// Next construct the automata and use it to
var automata = createAC(trie);
// Now run it on some data
var data = [1, 2, 3, 4, 7, 8];
for (var state = automata, i = 0; i < data.length;) {
// Process next symbol
state = state.push(data[i++]);
// Print out all matches at position i
if (state.value !== undefined) {
console.log('matches at position', i, ':');
for (var cur = state; cur.value !== undefined; cur = cur.next) {
console.log(cur.value);
}
}
}
///////////////////////////////////////////////////////////////////////////////////
function createStateMachine(trie) {
//First pass: translate trie to automata
var root = trieToAutomata(trie)
root.next = root
//Second pass: iterate over trie in bfs order, attach prefixes
var Q = root.children.slice()
for(var i=0; i<Q.length; ++i) {
Q[i].next = root
}
var ptr = 0
while(ptr < Q.length) {
var r = Q[ptr++]
var sym = r.symbols
var children = r.children
var n = sym.length
for(var i=0; i<n; ++i) {
var a = sym[i]
var u = children[i]
u.next = root
Q.push(u)
var v = r.next
do {
var j = bounds.eq(v.symbols, a)
if(j >= 0) {
u.next = v.children[j]
break
} else {
v = v.next
// fix bug: start
if (v === root) {
j = bounds.eq(v.symbols, a);
if (j >= 0) {
u.next = v.children[j];
break;
}
}
// fix: end
}
} while(v !== root)
}
}
//Done: return root
return root
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.