Sha256: f4c97f7c75d135d2422ff6805ec9f03c795d8e63f16cb6d48e5ae2eeb9905b20

Contents?: true

Size: 1.57 KB

Versions: 31

Compression:

Stored size: 1.57 KB

Contents

# encoding: utf-8
#
module Cacher

  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

Version data entries

31 entries across 31 versions & 1 rubygems

Version Path
picky-1.4.1 lib/picky/cacher/similarity/double_levenshtone.rb
picky-1.4.0 lib/picky/cacher/similarity/double_levenshtone.rb
picky-1.3.4 lib/picky/cacher/similarity/double_levenshtone.rb
picky-1.3.3 lib/picky/cacher/similarity/double_levenshtone.rb
picky-1.3.2 lib/picky/cacher/similarity/double_levenshtone.rb
picky-1.3.1 lib/picky/cacher/similarity/double_levenshtone.rb
picky-1.3.0 lib/picky/cacher/similarity/double_levenshtone.rb
picky-1.2.4 lib/picky/cacher/similarity/double_levenshtone.rb
picky-1.2.3 lib/picky/cacher/similarity/double_levenshtone.rb
picky-1.2.2 lib/picky/cacher/similarity/double_levenshtone.rb
picky-1.2.1 lib/picky/cacher/similarity/double_levenshtone.rb
picky-1.2.0 lib/picky/cacher/similarity/double_levenshtone.rb
picky-1.1.7 lib/picky/cacher/similarity/double_levenshtone.rb
picky-1.1.6 lib/picky/cacher/similarity/double_levenshtone.rb
picky-1.1.5 lib/picky/cacher/similarity/double_levenshtone.rb
picky-1.1.4 lib/picky/cacher/similarity/double_levenshtone.rb
picky-1.1.3 lib/picky/cacher/similarity/double_levenshtone.rb
picky-1.1.2 lib/picky/cacher/similarity/double_levenshtone.rb
picky-1.1.1 lib/picky/cacher/similarity/double_levenshtone.rb
picky-1.1.0 lib/picky/cacher/similarity/double_levenshtone.rb