Class: TextRank::RankFilter::SortByValue

Inherits:
Object
  • Object
show all
Defined in:
lib/text_rank/rank_filter/sort_by_value.rb

Overview

A rank filter which sorts the results by value

Instance Method Summary collapse

Constructor Details

#initialize(descending: true) ⇒ SortByValue

Returns a new instance of SortByValue.

Parameters:

  • descending (boolean) (defaults to: true)

    whether to sort in descending order



9
10
11
# File 'lib/text_rank/rank_filter/sort_by_value.rb', line 9

def initialize(descending: true)
  @descending = !!descending
end

Instance Method Details

#filter!(ranks, **_) ⇒ Hash<String, Float>

Perform the filter on the ranks

Parameters:

  • ranks (Hash<String, Float>)

    the results of the PageRank algorithm

Returns:

  • (Hash<String, Float>)


16
17
18
# File 'lib/text_rank/rank_filter/sort_by_value.rb', line 16

def filter!(ranks, **_)
  ranks.sort_by { |_, v| @descending ? -v : v }.to_h
end