require 'yaml' require 'rubygems' require 'twitter' class Hash def except(*keys) self.reject { |k,v| keys.include?(k) } end end screen_name = ARGV[0] user = YAML.load(File.read("#{screen_name}.yml")) people = YAML.load(File.read("#{screen_name}-people.yml")) friends = YAML.load(File.read("#{screen_name}-friends.yml")) friend_map = YAML.load(File.read("#{screen_name}-friend_map.yml")) recommend = 10 # do a pairwise intersection for the list of friends of each of our friends intersect_counts = Hash.new(0) friend_map.each_pair do |id, friend_ids| friend_map.except(id).each_pair do |compare_id, compare_friend_ids| (friend_map[id] & friend_map[compare_id]).each {|i| intersect_counts[i] += 1} end end friend_ids = friends.collect {|f| f.id} # show how many of the top 10 in this metric you're already following count = intersect_counts.except(user.id).sort {|a, b| b[1] <=> a[1]}.slice(0, recommend).inject(0) do |r, f| friend_ids.include?(f.first) ? r : r + 1 end puts "#{screen_name} is following #{count} of the top #{recommend} recommendations" # now show 10 that you should be following # take out the people we're already friends with and ourself new_intersects = intersect_counts.except(*friend_ids).except(user.id) # sort them and output the top 10 new friends puts "Top #{recommend} of the #{people.except(*friend_ids).except(user.id).size} possible new friends for #{screen_name}" new_intersects.sort {|a, b| b[1] <=> a[1]}.slice(0, recommend).each do |f| puts "#{people[f.first].name} (#{people[f.first].screen_name}) is followed by #{f.last / 2} pairs" end