Class: AhoCorasick

Inherits:
Object
  • Object
show all
Defined in:
lib/aho_corasick.rb,
lib/aho_corasick/version.rb

Defined Under Namespace

Classes: TreeNode

Constant Summary collapse

VERSION =
"0.1.0"

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ AhoCorasick

Returns a new instance of AhoCorasick.



2
3
4
5
6
7
# File 'lib/aho_corasick.rb', line 2

def initialize(*args)
  terms = terms_for(args)
  @root = TreeNode.new
  unsafe_insert(terms)
  create_suffix_links
end

Instance Method Details

#insert(*args) ⇒ Object



19
20
21
22
23
# File 'lib/aho_corasick.rb', line 19

def insert(*args)
  terms = terms_for(args)
  unsafe_insert(terms)
  create_suffix_links
end

#match(string) ⇒ Object



9
10
11
12
13
14
15
16
17
# File 'lib/aho_corasick.rb', line 9

def match(string)
  matches = []
  node = string.each_char.inject(@root) do |node, char|
    matches += node.matches if node
    (node && node.find(char.to_sym)) || @root.find(char.to_sym)
  end
  matches += node.matches if node
  return matches
end