Sha256: dea09cb2be3800803b3dd4b03b819e74b150e289c4bde8c6cd1b65aec64b5b62

Contents?: true

Size: 951 Bytes

Versions: 1

Compression:

Stored size: 951 Bytes

Contents

module FilterWord
  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

1 entries across 1 versions & 1 rubygems

Version Path
filter_word-0.0.2 lib/filter_word/engines/dict.rb