Sha256: cb9a672cc64ea7a34723afb87dbbd3cc6e10b17b68842fac7214f3ff1bef5984

Contents?: true

Size: 1023 Bytes

Versions: 5

Compression:

Stored size: 1023 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|
            text, ids = *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

5 entries across 5 versions & 1 rubygems

Version Path
picky-3.0.0.pre5 lib/picky/generators/weights/logarithmic.rb
picky-3.0.0.pre4 lib/picky/generators/weights/logarithmic.rb
picky-3.0.0.pre3 lib/picky/generators/weights/logarithmic.rb
picky-3.0.0.pre2 lib/picky/generators/weights/logarithmic.rb
picky-3.0.0.pre1 lib/picky/generators/weights/logarithmic.rb