Sha256: 16c03af2fd7f69d9071604245070c86cbe5a1ce6ca14062f63f27abba40368e4

Contents?: true

Size: 992 Bytes

Versions: 26

Compression:

Stored size: 992 Bytes

Contents

module Picky

  module Generators

    module Weights

      # Uses a logarithmic weight.
      # If for a key k we have x ids, the weight is:
      # w(x): log(x)
      # Special case: If x < 1, then we use 0.
      #
      class Logarithmic < Strategy

        # Generates a partial index from the given inverted index.
        #
        def generate_from inverted
          inverted.inject({}) do |hash, (text, ids)|
            weight = weight_for ids.size
            hash[text] ||= weight.round(2) if weight
            hash
          end
        end

        # Sets the weight value.
        #
        # If the size is 0 or one, we would get -Infinity or 0.0.
        # Thus we do not set a value if there is just one. The default, dynamically, is 0.
        #
        # BUT: We need the value, even if 0. To designate that there is a weight!
        #
        def weight_for amount
          return 0 if amount < 1
          Math.log amount
        end

      end

    end

  end

end

Version data entries

26 entries across 26 versions & 1 rubygems

Version Path
picky-3.5.0 lib/picky/generators/weights/logarithmic.rb
picky-3.4.3 lib/picky/generators/weights/logarithmic.rb
picky-3.4.2 lib/picky/generators/weights/logarithmic.rb
picky-3.4.1 lib/picky/generators/weights/logarithmic.rb
picky-3.4.0 lib/picky/generators/weights/logarithmic.rb
picky-3.3.3 lib/picky/generators/weights/logarithmic.rb
picky-3.3.2 lib/picky/generators/weights/logarithmic.rb
picky-3.3.1 lib/picky/generators/weights/logarithmic.rb
picky-3.3.0 lib/picky/generators/weights/logarithmic.rb
picky-3.2.0 lib/picky/generators/weights/logarithmic.rb
picky-3.1.13 lib/picky/generators/weights/logarithmic.rb
picky-3.1.12 lib/picky/generators/weights/logarithmic.rb
picky-3.1.11 lib/picky/generators/weights/logarithmic.rb
picky-3.1.10 lib/picky/generators/weights/logarithmic.rb
picky-3.1.9 lib/picky/generators/weights/logarithmic.rb
picky-3.1.8 lib/picky/generators/weights/logarithmic.rb
picky-3.1.7 lib/picky/generators/weights/logarithmic.rb
picky-3.1.6 lib/picky/generators/weights/logarithmic.rb
picky-3.1.5 lib/picky/generators/weights/logarithmic.rb
picky-3.1.4 lib/picky/generators/weights/logarithmic.rb