Skip to content

Instantly share code, notes, and snippets.

@kevinpet
Created April 17, 2009 22:21
Show Gist options
  • Save kevinpet/97300 to your computer and use it in GitHub Desktop.
Save kevinpet/97300 to your computer and use it in GitHub Desktop.
class Mention
attr_accessor :topic, :prominence, :topic_sentiment, :other_sentiment
def initialize(topic, prominence, topic_sentiment, other_sentiment)
@topic = topic
@prominence = prominence
if(topic == "Apples")
topic_sentiment = topic_sentiment ** 3
elsif topic == "Bananas"
topic_sentiment = Math.sqrt topic_sentiment
end
@topic_sentiment = topic_sentiment
@other_sentiment = other_sentiment
end
def other_prominence
1.0 - prominence
end
def overall_sentiment
topic_sentiment * prominence + other_sentiment * other_prominence
end
def discover_probability(factor)
prob = prominence ** (1 / factor)
end
end
def pretty(k,v)
display = ""
hash.each do |k,v|
display << "%10s " % k
display << "%02.1d" % v
end
end
Topics = [ "Apples", "Oranges", "Bananas" ]
Items = [ 10, 100, 1000, 10000 ]
Factor = [ 0.25, 0.5, 1, 2, 4 ]
Items.each do |items|
Factor.each do |factor|
mentions = []
items.times do
mentions << Mention.new(Topics[rand(3)], rand, rand, rand)
end
correct = Hash.new
Topics.each { |t| correct[t] = 0.0 }
numCorrect = Hash.new
Topics.each { |t| numCorrect[t] = 0.0 }
approximate = Hash.new
Topics.each { |t| approximate[t] = 0.0 }
numApprox = Hash.new
Topics.each { |t| numApprox[t] = 0.0 }
randomized = Hash.new
Topics.each { |t| randomized[t] = 0.0 }
numRandom = Hash.new
Topics.each { |t| numRandom[t] = 0.0 }
mentions.each do |m|
correct[m.topic] = correct[m.topic] + m.prominence * m.topic_sentiment
numCorrect[m.topic] += m.prominence
approximate[m.topic] = approximate[m.topic] +
m.discover_probability(factor) * m.overall_sentiment
numApprox[m.topic] += m.discover_probability(factor)
if rand < m.discover_probability(factor) then
randomized[m.topic] += m.overall_sentiment
numRandom[m.topic] += 1.0
end
end
puts ""
puts "Calculating for #{items} items, factor of #{factor}:"
maxCorrect = correct.values.inject { |m,x| m > x ? m : x }
scaled = []
Topics.each_with_index do |v, i|
scaled[i] = "%10s %2.0f%% " % [v, 100 * correct[v] / numCorrect[v] ]
end
puts "Correct #{scaled.to_s}"
maxApprox = approximate.values.inject { |m,x| m > x ? m : x}
maxCorrect = correct.values.inject { |m,x| m > x ? m : x }
Topics.each_with_index do |v, i|
scaled[i] = "%10s %2.0f%% " % [v, 100 * approximate[v] / numApprox[v] ]
end
puts "Approx #{scaled.to_s}"
maxRand = randomized.values.inject { |m,x| m > x ? m : x}
maxCorrect = correct.values.inject { |m,x| m > x ? m : x }
Topics.each_with_index do |v, i|
scaled[i] = "%10s %2.0f%% " % [v, 100 * randomized[v] / numRandom[v] ]
end
puts "Random #{scaled.to_s}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment