Sha256: f7eb2dbfa56a6525dc0a4eddb85b0418337c0c432efe687ebfb8360226ec130f

Contents?: true

Size: 705 Bytes

Versions: 3

Compression:

Stored size: 705 Bytes

Contents

class Dict < Engine
  @@root = nil
  
  def initialize
    @@root ||= load_dict(dict_path)
    @word = ''
    @node = @@root
    super
  end
  
  def process(char)
    match = false
    word = nil
    
    if @node[char]
      @word << char
      @node = @node[char]
      match = true
    else
      if @node[:end] || @word.chars.to_a.length == 1
        word = @word
      else
        word = @word.chars.to_a
      end
      
      @node = @@root
      @word = ''
      match = false
    end
    
    [match, word]
  end
  
  private
  def load_dict(path)
    File.open(path, "rb") {|io| Marshal.load(io)}
  end
  
  def dict_path
    File.join(File.dirname(__FILE__), '../../dict/dict.hash')
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rseg-0.1.3 lib/engines/dict.rb
rseg-0.1.2 lib/engines/dict.rb
rseg-0.1.1 lib/engines/dict.rb