GithubHelp home page GithubHelp logo

mikolalysenko / aho-corasick-automaton Goto Github PK

View Code? Open in Web Editor NEW
6.0 6.0 2.0 104 KB

Generates an Aho-Corasick automata for matching multiple patterns in a stream

License: MIT License

JavaScript 100.00%

aho-corasick-automaton's People

Contributors

mikolalysenko avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

aho-corasick-automaton's Issues

fix a bug

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
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.