public
Created

  • Download Gist
query.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
class Query
include Frequency
attr_accessor :freq
 
def initialize(query)
@query = query
@tokens = tokenize
end
 
def clean_text
@query
end
 
def ids
@ids ||= @tokens.reduce(Message.corpus.index @tokens.first) do |memo, it|
memo & Message.corpus.index(it)
end.flatten.uniq
end
 
def docs(page)
mailings = Mailing.limited.paginate(:page => page, :per_page => 10).includes(:campaign).order('first_seen DESC').where('id in (?)', ids)
 
docs = Message.doc_length
 
scores = mailings.map do |m|
max_word = keys.map {|k| freq[k] }.max
 
score = @tokens.reduce(0.0) do |memo, tok|
memo += (m.freq[tok] / max_word) * Math.log(docs / (1.0 + Message.corpus.docs(tok))) if !m.freq[tok].nil?
memo
end
{:score => score, :mailing => m}
end
 
scores.sort_by {|s|
(s[:score] / (s[:mailing].keys.length * s[:mailing].freq.values.reduce(&:+))) * -1.0
}.map {|s| s[:mailing] }
end
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.