Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Calculate pagerank of twitter follow network.
#!/usr/bin/env ruby
$:.unshift(File.dirname(__FILE__))
require 'twitter'
require 'mysql2-cs-bind'
require 'yaml'
require 'pagerank'
# parameter
DUMPING_FACTOR = 0.85
# write twitter token and database settings in config.yml
config = YAML::load_file("path/to/config.yml")
tw = Twitter::Client.new(config['twitter'])
client = Mysql2::Client.new(config['database'])
# twitter account name
user_name = "hongo35"
# get user ids which "user_name" is following
friend_ids = []
init = []
friend_cursor = -1
while friend_cursor != 0
friends = tw.friend_ids(user_name, :cursor => friend_cursor)
friends.ids.each do |id|
friend_ids.push(id)
init.push(1.0)
end
friend_cursor = friends.next_cursor
sleep(60)
end
# get foaf(friends of a friend) ids
friend_ids.each do |id|
begin
id_cursor = -1
while id_cursor != 0
foaf = tw.friend_ids(id, :cursor => id_cursor)
foaf.ids.each do |friend_id|
client.xquery("insert into links(user_id, friend_id) values(?,?)", id, friend_id)
end
id_cursor = foaf.next_cursor
sleep(60)
end
rescue
end
end
# friends follow network matrix
matrix_hash = {}
matrix_size = friend_ids.size
friend_ids.each do |row_id|
matrix_hash[row_id] = {}
friend_ids.each do |column_id|
if row_id == column_id
matrix_hash[row_id][column_id] = 1
else
matrix_hash[row_id][column_id] = 0
end
end
end
res = client.xquery("select * from links where friend_id in (#{friend_ids.join(',')})")
res.each do |r|
matrix_hash[r['user_id']][r['friend_id']] = 1
end
matrix = []
matrix_hash.each do |key, value|
matrix_tmp = []
value.each do |k,v|
matrix_tmp.push(v)
end
matrix.push(matrix_tmp)
end
# pagerank initial array
init.each_with_index do |val, i|
init[i] = val / matrix_size
end
# calculate pagerank
pr = PageRank.new(matrix)
rank = pr.calc(init, DUMPING_FACTOR)
rank.each_with_index do |r, i|
puts "#{friend_ids[i]},#{r}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment