Module Prompt::Popular
In: lib/algorithms/prompt/popular.rb

Methods

prompts  

Included Modules

Algorithms::Prompt

Constants

ID = 3   algorithm ID

Public Class methods

Generate count number of primed prompts based on question ID, voter ID and stats.

Return

Hash with prompt IDs as keys and item IDs for that prompt as values.

Parameters

question_id<int>:Generate prompts for this question ID
voter_id<int>:Generate prompts for this voter ID
count<int>:Generate this number of prompts

[Source]

    # File lib/algorithms/prompt/popular.rb, line 16
16:     def prompts(question_id, voter_id, count)
17:       result = ActiveRecord::Base.connection.execute(
18:         "SELECT id,score FROM stats WHERE question_id=#{question_id} ORDER BY score;"
19:       )
20:       # TODO: choose min value with more justification
21:       return nil unless result.num_rows > 2
22:       # make all stats positive by adding |min(stats)| + 1 to all stats
23:       prompt_item_ids = {}
24:       norm = cur = 0
25:       stats = []
26:       stat = result.fetch_hash
27:       min = stat['score'].to_f.abs + 1
28:       while !stat.nil?
29:         norm += (adj = stat['score'].to_f + min)
30:         stats << [stat['id'].to_i, [cur, cur += adj]]
31:         stat = result.fetch_hash
32:       end
33:       result.free
34:       Prompt.transaction do
35:         count.times do |i|
36:           prompt = prompt_for_request(question_id, voter_id, ID)
37:           # choose the stat [0] <= r < [1]
38:           r = rand(norm)
39:           # detect treats hash as [key, value] array
40:           stat_id = stats.detect { |stat| stat[1][0] <= r && r < stat[1][1] }[0]
41:           item_ids = Stat.find(stat_id).item_ids
42:           prompt.item_ids = item_ids
43:           redo if bad_prompt?(prompt)
44:           prompt_item_ids[prompt.id] = item_ids
45:         end
46:       end
47:       prompt_item_ids
48:     end

[Validate]