david-mccullars/text_rank

View on GitHub
lib/text_rank/rank_filter/sort_by_value.rb

Summary

Maintainability
A
0 mins
Test Coverage
module TextRank
  module RankFilter
    ##
    # A rank filter which sorts the results by value
    ##
    class SortByValue

      # @param descending [boolean] whether to sort in descending order
      def initialize(descending: true)
        @descending = !!descending
      end

      # Perform the filter on the ranks
      # @param ranks [Hash<String, Float>] the results of the PageRank algorithm
      # @return [Hash<String, Float>]
      def filter!(ranks, **_)
        ranks.sort_by { |_, v| @descending ? -v : v }.to_h
      end

    end
  end
end