class Attentive::Trie

Attributes

depth[R]

Public Class Methods

new(depth: 0) click to toggle source
# File lib/attentive/trie.rb, line 5
def initialize(depth: 0)
  @depth = depth
  @children = {}
end
of_substitutions(substitutions) click to toggle source
# File lib/attentive/trie.rb, line 33
def self.of_substitutions(substitutions)
  substitutions.each_with_object(self.new) do |(tokens, substitution), trie|
    leaf = trie
    tokens.each_with_index do |token, i|
      raise "#{tokens.join} contains #{tokens[0...i].join}" if leaf.fin?
      leaf = leaf.add token
    end
    leaf.fin! substitution
  end
end

Public Instance Methods

[](token) click to toggle source
# File lib/attentive/trie.rb, line 10
def [](token)
  @children[token]
end
add(token) click to toggle source
# File lib/attentive/trie.rb, line 14
def add(token)
  raise "Can't add #{token.inspect} to trie because this leaf is a terminus" if fin?
  @children[token] ||= self.class.new(depth: depth + 1)
end
fin() click to toggle source
# File lib/attentive/trie.rb, line 23
def fin
  @children[:fin]
end
fin!(finish) click to toggle source
# File lib/attentive/trie.rb, line 27
def fin!(finish)
  @children[:fin] = finish
end
fin?() click to toggle source
# File lib/attentive/trie.rb, line 19
def fin?
  @children.key?(:fin)
end