Skip to content

Instantly share code, notes, and snippets.

@mooreniemi
Created May 2, 2012 01:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mooreniemi/2572905 to your computer and use it in GitHub Desktop.
Save mooreniemi/2572905 to your computer and use it in GitHub Desktop.
new alg test
require 'date'
require 'csv'
#Given a csv file output from Sermo App at https://app.sermo.com/reporting/reports/post_summary_by_date
#we need a csv of the posts in order of popularity, with the addition of a bayesian average to respond to different days.
#constructor of my post object with the csv fields to hold all the attributes
class Post <
# a post has csv categories a-q
Struct.new(:post_id, :title, :link, :categories, :publish_date, :avg_rating, :raters, :comments, :voters, :votes, :views, :author_id, :username, :first_name, :last_name, :email, :specialty, :pop)
end
# define an array to hold the all the posts of the week
posts = Array.new
#asks me what the file name for the week is and removes enter
csv = gets.chomp
# open csv file and then grab each field, skipping top row of field names
CSV.foreach(csv) {|fields|
if fields[0] == 'Post ID' then
next
end
# create a new Post constructor object
p = Post.new
#methods after field object to get rid of double-quotes and blanks
p.post_id = fields[0].tr_s('"', '').strip
p.title = fields[1].tr_s('"', '').strip
p.link = fields[2].tr_s('"', '').strip
p.categories = fields[3].tr_s('"', '').strip
#taking the hours and minutes off the date field
date = fields[4].split(' ')[0]
#then convert that date variable to the ruby Date object
p.publish_date = Date.parse(date)
p.avg_rating = fields[5].tr_s('"', '').strip.to_f
p.raters = fields[6].tr_s('"', '').strip.to_i
p.comments = fields[7].tr_s('"', '').strip.to_i
p.voters = fields[8].tr_s('"', '').strip.to_i
p.votes = fields[9].tr_s('"', '').strip.to_i
p.views = fields[10].tr_s('"', '').strip.to_i
p.author_id = fields[11].tr_s('"', '').strip
p.username = fields[12].tr_s('"', '').strip
p.first_name = fields[13].tr_s('"', '').strip
p.last_name = fields[14].tr_s('"', '').strip
p.email = fields[15].tr_s('"', '').strip
#for non-physician accounts a nil value of specialty appears which this skips
if fields[16].nil? then
next
end
p.specialty = fields[16].tr_s('"', '').strip
#puts the items p into the array posts
posts.push(p)
}
#need a bayesian constant to adjust against five star bandit/zero star bandit
#bayesian constant will be the average number raters on a post, or avg_num_raters
#for the rest of the pop formula: ( (avg_num_raters * avg_rating) + (this_num_raters * this_rating) ) / (avg_num_raters + this_num_raters)
#we need to get the average rating, average comments, average views, and average voters
#average number of ratings
avg_num_raters = posts.inject(0.0){|sum, p| sum + p.raters} / posts.size
ar = avg_num_raters
puts ar
avg_rating = posts.inject(0.0){|sum, p| sum + p.avg_rating} / posts.size
s = avg_rating
puts s
#average number of voters
avg_voters = posts.inject(0.0){|sum, p| sum + p.voters} / posts.size
o = avg_voters
puts o
#average number of views
avg_views = posts.inject(0.0){|sum, p| sum + p.views} / posts.size
v = avg_views
puts v
popped = Array.new
posts.each {|p|
#applies new popularity alg to each post
p.pop = p.comments + ( (ar * s) + (p.raters * p.avg_rating) ) / (o + p.voters) + (v*p.views)
popped.push(p)
}
#outputing a csv file with today's date
daily = File.open("Day of " + posts.last.publish_date.next.strftime("%m%d%y") + ".csv", "w")
#adding new field
popped.each {|p|
row = [p.pop, p.publish_date, p.post_id, p.author_id, p.username, p.first_name, p.last_name, p.email]
popped.puts row.join(",")
}
popped.close
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment