Module: USaidWat::Application::CountCommand

Included in:
Posts, Tally
Defined in:
lib/usaidwat/count.rb

Instance Method Summary collapse

Instance Method Details

#algorithm(sort_by_count) ⇒ Object



22
23
24
# File 'lib/usaidwat/count.rb', line 22

def algorithm(sort_by_count)
  sort_by_count ? USaidWat::Algorithms::CountAlgorithm : USaidWat::Algorithms::LexicographicalAlgorithm
end

#partition(entries, sort_by_count) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/usaidwat/count.rb', line 6

def partition(entries, sort_by_count)
  longest_subreddit = 0
  buckets = Hash.new { |hash, key| hash[key] = 0 }
  entries.each do |e|
    subreddit = e.subreddit
    longest_subreddit = subreddit.length if subreddit.length > longest_subreddit
    buckets[subreddit] += 1
  end
  algo = algorithm(sort_by_count).new(buckets)
  subreddits = buckets.keys.sort { |a,b| algo.sort(a, b) }
  counts = subreddits.map { |s| buckets[s] }
  subreddit_counts = subreddits.zip(counts)
  partition_data = Struct.new(:longest, :counts)
  partition_data.new(longest_subreddit, subreddit_counts)
end