Module: Statistic

Defined in:
lib/statistic/statistic.rb

Overview

module to generate the required statisitc result for the requested attribute

Class Method Summary collapse

Class Method Details

.generate_ranking_for(data, rank, criteria) ⇒ Hash

method to generate a top n ranking for the given criteria

Parameters:

  • data (Array)

    an array holding the selected entries

  • rank (Integer)

    the requested number of results

  • criteria (Symbol)

    the ranking criteria

Returns:

  • (Hash)

    the sorted n result with the highest ranking mapped as (attribute => occurrence)



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/statistic/statistic.rb', line 12

def self.generate_ranking_for(data, rank, criteria)
  mapping = Hash.new()
  data.each { |entry|
    attribute = Entry::EntryAttributeFactory.get_attribute_to(criteria, entry)
    if (mapping[attribute] == nil)
      mapping[attribute] = 1
    else
      mapping[attribute] += 1
    end
  }

  ranking = mapping.sort_by {|k, v| [v, k] }.reverse
  return ranking.first(rank)
end

.generate_ranking_for_index(index, number_of_results) ⇒ Hash

method to generate a top n ranking over the calculated index

Parameters:

  • index (Hash)

    the calculated index of the entries

  • number_of_results (Integer)

    the requested number of results

Returns:

  • (Hash)

    the sorted n result with the highest ranking mapped as (attribute => occurrence)



32
33
34
35
36
37
38
39
40
# File 'lib/statistic/statistic.rb', line 32

def self.generate_ranking_for_index(index, number_of_results)
  mapping = Hash.new()
  index.each_pair { |key, value|
    mapping[key] = value.length
  }

  ranking = mapping.sort_by {|k, v| [v, k] }.reverse
  return ranking.first(number_of_results)
end