Sha256: 230e244c609938df9a59022cc59acfd44063c552832ba55ee488c67576b74032

Contents?: true

Size: 1.07 KB

Versions: 17

Compression:

Stored size: 1.07 KB

Contents

module Picky

  # encoding: utf-8
  #
  module Generators

    module Similarity

      # It's actually a combination of double metaphone
      # and Levenshtein.
      #
      # It uses the double metaphone to get similar words
      # and ranks them using the levenshtein.
      #
      class Phonetic < Strategy

        attr_reader :amount

        #
        #
        def initialize amount = 3
          check_gem

          raise "In Picky 2.0+, the Similarity::Phonetic has been renamed to Similarity::DoubleMetaphone. Please use that one. Thanks!" if self.class == Phonetic
          @amount = amount
        end

        # Tries to require the text gem.
        #
        def check_gem # :nodoc:
          require 'text'
        rescue LoadError
          warn_gem_missing 'text', 'a phonetic Similarity'
          exit 1
        end

        # Sorts the index values in place.
        #
        def prioritize ary, code
          ary.sort_by_levenshtein! code
          ary.slice! amount, ary.size # Note: The ary.size is not perfectly correct.
        end

      end

    end

  end

end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
picky-4.4.1 lib/picky/generators/similarity/phonetic.rb
picky-4.4.0 lib/picky/generators/similarity/phonetic.rb
picky-4.3.2 lib/picky/generators/similarity/phonetic.rb
picky-4.3.1 lib/picky/generators/similarity/phonetic.rb
picky-4.3.0 lib/picky/generators/similarity/phonetic.rb
picky-4.2.4 lib/picky/generators/similarity/phonetic.rb
picky-4.2.3 lib/picky/generators/similarity/phonetic.rb
picky-4.2.2 lib/picky/generators/similarity/phonetic.rb
picky-4.2.1 lib/picky/generators/similarity/phonetic.rb
picky-4.2.0 lib/picky/generators/similarity/phonetic.rb
picky-4.1.0 lib/picky/generators/similarity/phonetic.rb
picky-4.0.9 lib/picky/generators/similarity/phonetic.rb
picky-4.0.8 lib/picky/generators/similarity/phonetic.rb
picky-4.0.7 lib/picky/generators/similarity/phonetic.rb
picky-4.0.6 lib/picky/generators/similarity/phonetic.rb
picky-4.0.5 lib/picky/generators/similarity/phonetic.rb
picky-4.0.4 lib/picky/generators/similarity/phonetic.rb