Sha256: 971e27f7197508a7aa0620de4ab6cde3939fa2aeb25be83053baf4318c7cd29d

Contents?: true

Size: 1.72 KB

Versions: 9

Compression:

Stored size: 1.72 KB

Contents

# encoding: utf-8
#
module Internals

  module Generators

    module Similarity

      # DoubleLevensthone means that it's a combination of
      # * DoubleMetaphone
      # and
      # * Levenshtein
      # :)
      #
      class DoubleLevenshtone < Strategy

        attr_reader :amount

        #
        #
        def initialize amount = 10
          @amount = amount
        end

        # Encodes the given symbol.
        #
        # Returns a symbol.
        #
        def encoded sym
          codes = Text::Metaphone.double_metaphone sym.to_s
          codes.first.to_sym unless codes.empty?
        end

        # Generates an index for the given index (in exact index style).
        #
        # In the following form:
        # [:meier, :mueller, :peter, :pater] => { MR: [:meier], MLR: [:mueller], PTR: [:peter, :pater] }
        #
        def generate_from index
          hash = hashify index.keys
          sort hash
        end

        private

          # Sorts the index values in place.
          #
          def sort index
            index.each_pair.each do |code, ary|
              ary.sort_by_levenshtein! code
              ary.slice! amount, ary.size # size is not perfectly correct, but anyway
            end
            index
          end

          # Hashifies a list of symbols.
          #
          # Where:
          # { encoded_sym => [syms] }
          #
          def hashify list
            list.inject({}) do |total, element|
              if code = encoded(element)
                total[code] ||= []
                total[code] << element
              end
              total
            end
          end

      end
    
      # ... aka Phonetic.
      #
      Phonetic = DoubleLevenshtone

    end

  end
  
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
picky-2.0.0.pre2 lib/picky/internals/generators/similarity/double_levenshtone.rb
picky-2.0.0.pre1 lib/picky/internals/generators/similarity/double_levenshtone.rb
picky-1.5.4 lib/picky/internals/generators/similarity/double_levenshtone.rb
picky-1.5.3 lib/picky/internals/generators/similarity/double_levenshtone.rb
picky-1.5.2 lib/picky/internals/generators/similarity/double_levenshtone.rb
picky-1.5.1 lib/picky/internals/generators/similarity/double_levenshtone.rb
picky-1.5.0 lib/picky/internals/generators/similarity/double_levenshtone.rb
picky-1.4.3 lib/picky/internals/generators/similarity/double_levenshtone.rb
picky-1.4.2 lib/picky/internals/generators/similarity/double_levenshtone.rb