Class: Matchers::ACMatcher

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(patterns) ⇒ ACMatcher

Returns a new instance of ACMatcher.



5
6
7
8
# File 'lib/aho_corasick.rb', line 5

def initialize(patterns)
  patterns = (patterns || []).compact.reject(&:empty?)
  @trie = Trie.new patterns
end

Instance Attribute Details

#trieObject (readonly)

Returns the value of attribute trie.



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

def trie
  @trie
end

Instance Method Details

#matches?(text) ⇒ Boolean

Returns:

  • (Boolean)


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

def matches?(text)
  node = @trie.root
  text.to_s.split('').each do |char|
    node = node.failure while node.children[char].nil? # Follow failure if it exists in case pattern doesn't match
    node = node.children[char]
    return true unless node.output.nil?
  end
  false
end