Skip to content

Instantly share code, notes, and snippets.

@plotti
Created June 8, 2012 13:40
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 plotti/2895661 to your computer and use it in GitHub Desktop.
Save plotti/2895661 to your computer and use it in GitHub Desktop.
Create Sorted lists
def generate_most_listed_members
seen_lists = []
seen_membersets = []
@tmp_persons = []
self.persons.each do |person|
#puts person.username
@tmp_persons << {:username => person.username, :list_count => 1,
:uri => "http://www.twitter.com/#{person.username}", :followers => person.followers_count,
:friends => person.friends_count}
end
self.lists.each do |list|
if list.name.include? self.keyword
if !seen_lists.include? list.uri #If we have not encountered a list with a similar uri before
if list.members
membernames = list.members.collect{|a| a[:username]} # Check if there are lists that contain the same people
max = 0
seen_membersets.each do |m|
overlap = m & membernames
result = overlap.count.to_f/m.count
if result > max
max = result
end
end
if max < 0.99 # If there happens to be no lists that already have the same members 99% similarity
puts "#{@tmp_persons.count} Analyzing list #{list.uri}. Overlap with memberset #{max}"
list.members.each do |member|
tmp_user = @tmp_persons.find{|i| i[:username] == member[:username]} # Look if that person is already on the list
if tmp_user != nil
tmp_user[:list_count] += 1
else
@tmp_persons << {:username => member[:username], :list_count => 1,
:uri => "http://www.twitter.com/#{member[:username]}", :followers => member[:followers_count],
:friends => member[:friends_count]}
end
end
else
puts "List overlap for #{list.uri} with #{membernames.count} members"
end
seen_membersets << membernames
end
seen_lists << list.uri
end
end
end
#Sort these persons according to their listings
sorted = @tmp_persons.sort{|a,b| a[:list_count] <=> b[:list_count]}.reverse
#Dump the results of this calculation to a csv file
outfile = File.open("data/" + self.keyword + "_sorted_members.csv",'w')
CSV::Writer.generate(outfile) do |csv|
csv << ["Username", "Followers", "List Count", "URI"]
sorted.each do |member|
csv << [member[:username], member[:followers], member[:list_count], member[:uri]]
end
end
outfile.close
return sorted
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment