I'm trying to figure out a way to find who a user mentions the most in their tweets.
i.e. if there was someone I constantly replied to or mentioned, what would be the best way to find that out?
I'm using Rails and the Twitter gem for this project. If I could return the users most recent 200 tweets and run some sort of query that might work, just not sure what sort of query I would run.
Any ideas how to solve thi开发者_如何学Gos much appreciated.
Unless the Twitter gem has a way to extract @ mentions from a single Tweet, you could just get them out with something like
"haha @foo @bar sdasda".split.select { |t| t.start_with? '@' } #=> ["@foo", "@bar"]
Map that over all of the user's tweets, get an array of mentions and then count them.
Edit to answer the question from your comment:
>> tweets = ["haha @foo @bar sdasda", "lalal @foo", "@bar @foo"] #=> ["haha @foo @bar sdasda", "lalal @foo", "@bar @foo"]
>> tweets.map do |t|
>> t.split.select { |t| t.start_with? '@' }
>> end.flatten.group_by { |e| e }.values.max_by(&:size).first #=> "@foo"
So basically you map
over all the tweets, extracting the @ mentions. Then you join
all the arrays together and find the most common element.
You can get any non-protected users tweets with a single unauthenticated API call to the user_timeline
:
http://api.twitter.com/1/statuses/user_timeline.json?screen_name=SCREENNAME&count=200
From there, you would just parse the body of the returned JSON/XML (depending on your preferences) for @ mentions in the contents of tweets (in the text
field).
精彩评论