Sha256: 8939904ca28909263a570b7021e9ee98ebb77800ae999b6085b9459566ab61f3

Contents?: true

Size: 961 Bytes

Versions: 2

Compression:

Stored size: 961 Bytes

Contents

module HarmoniousDictionary
  module RsegEngine
    class Dict < Engine    
      def initialize(&block)
        @dict_path = block.call
        @word = ''
        super
      end

      def dictionary
        @@root
      end
    
      def process(char)
        @root ||= load_dict(@dict_path)
        @node ||= @root

        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)
        begin
          File.open(path, "rb") {|io| Marshal.load(io) }
        rescue => e
          puts e
          exit
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 2 rubygems

Version Path
harmonious_check-0.0.2 lib/harmonious_dictionary/engines/dict.rb
harmonious_dictionary-0.0.1 lib/harmonious_dictionary/engines/dict.rb