2

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 this much appreciated.

Lee
  • 627
  • 1
  • 8
  • 20

2 Answers2

1

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).

Yahel
  • 37,023
  • 22
  • 103
  • 153
  • Thanks yc, that makes sense. I'm just not sure what sort of calculation I need to do once I parse all the tweets. – Lee Jan 10 '11 at 12:29
  • @Lee I don't know Ruby, but if you follow Michael Kohl's suggestion, and create an array of ALL of the @ mentions in the stream, then you only need to find the Mode value, and you can probably do that with this: http://stackoverflow.com/questions/412169 – Yahel Jan 10 '11 at 13:50
1

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.

Michael Kohl
  • 66,324
  • 14
  • 138
  • 158
  • Thanks Michael. So lets say I got the users latest 200 tweets. If I put all of these into one string, then split that string with the above code, that will get me an array of all the @mentions. How would I then extract the most popular from that array? (I'm a bit of a noob so if I'm pushing my luck asking for more help here I apologise) – Lee Jan 10 '11 at 12:27
  • This looks great, thanks Michael. I feel stupid even asking this but if I'm running this in a controller, how do I get set the final result (@foo) to a variable to then save? – Lee Jan 12 '11 at 20:13
  • You can just assign the result of the whole expression to the variable, so `your_var = tweets.map do |t|...`. Or just wrap all the code in a method and assign the return value to a variable. BTW: It's considered polite on SO to accept answers, which is done with the check mark right below the votes. – Michael Kohl Jan 12 '11 at 20:19
  • Works a treat, I owe you a pint :-) – Lee Jan 12 '11 at 20:32
  • My pleaure! :-) BTW: Rubylearning.org has a basic Ruby course starting this weekend and while they are not free anymore, USD20 for 8 weeks is rather cheap. Disclaimer: I'm one of the mentors there, but we are all volunteers, the money goes into running the site. – Michael Kohl Jan 12 '11 at 20:44