Sha256: 663f69e9cd0c93415ebfe0550f7d41e3da8f105143820cf50eb2ed8797ec0af8
Contents?: true
Size: 888 Bytes
Versions: 10
Compression:
Stored size: 888 Bytes
Contents
module Attentive class Trie attr_reader :depth def initialize(depth: 0) @depth = depth @children = {} end def [](token) @children[token] end 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 def fin? @children.key?(:fin) end def fin @children[:fin] end def fin!(finish) @children[:fin] = finish end 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 end end
Version data entries
10 entries across 10 versions & 1 rubygems