Created
July 25, 2009 21:01
-
-
Save jbrechtel/154999 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygems' | |
require 'bishop' | |
require 'twitter' | |
class TwitterClassifier | |
@@spam_threshold = 0.3 | |
def initialize(knowledge_file=nil) | |
@bishop = Bishop::Bayes.new | |
@bishop.load(knowledge_file) if knowledge_file | |
end | |
def save(knowledge_file) | |
@bishop.save(knowledge_file) | |
end | |
def get_tweets(user) | |
Twitter::Search.new.from(user).per_page(50).extend(Enumerable).map do |tweet| tweet.text end | |
end | |
def classify(category, user) | |
tweets = get_tweets(user) | |
tweets.each do |tweet| @bishop.train(category, tweet) end | |
end | |
def how_spammy(user) | |
tweets = get_tweets(user) | |
spam_count = 0.0 | |
tweets.each do |tweet| | |
results = @bishop.guess(tweet) | |
normal_result = results.select do |result| result[0] == "normal" end.first | |
spam_result = results.select do |result| result[0] == "spam" end.first | |
spam_pct = spam_result.nil? ? 0 : spam_result[1] | |
normal_pct = normal_result.nil? ? 0 : normal_result[1] | |
result_difference = spam_pct || 0 - normal_pct || 0 | |
if result_difference > @@spam_threshold then spam_count = spam_count + 1 end | |
end | |
spam_count / tweets.nitems | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment