Class: TweetWords
Class Method Summary collapse
-
.findTopWords(strArray) ⇒ Object
This method splits the tweet strings into words, counts each word’s frequency and returns the word set.
-
.getTweets(screen_name, max = 0) ⇒ Object
This method gets top tweets from Twitter by making an API call via HTTParty.
-
.topTweets(screen_name, maxNumber) ⇒ Object
This method gets top ‘n’ tweets from Twitter as requested by the user.
-
.topWords(userName, maximumNumberOfTweets) ⇒ Object
and calls the methods to get the tweets and extract words from them.
Class Method Details
.findTopWords(strArray) ⇒ Object
This method splits the tweet strings into words, counts each word’s frequency and returns the word set.
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/tweet-words.rb', line 90 def self.findTopWords(strArray) countOfWords = Hash.new strArray.each do |str| if str.nil? next end # strips the string strippedString = str.gsub(/[^0-9A-Za-z]/, ' ') # splits the string into words splitString = strippedString.split(" ") # count frequency of words. splitString.each do |word| if countOfWords.has_key?(word) countOfWords[word] += 1 else countOfWords[word] = 1 end end end return countOfWords end |
.getTweets(screen_name, max = 0) ⇒ Object
This method gets top tweets from Twitter by making an API call via HTTParty. The parameters passed are the ‘screen_name’ which is screen name of the user for whom to return results for and ‘max_id’ which returns results with an ID less than (that is, older than) or equal to the specified ID. The method returns the top 20 tweets as ‘responseTweets’.
74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/tweet-words.rb', line 74 def self.getTweets(screen_name, max=0) parameters = {:screen_name => screen_name } if max > 0 parameters[:max_id] = max end responseTweets = get('http://api.twitter.com/1/statuses/user_timeline.json', :query => parameters ) return responseTweets end |
.topTweets(screen_name, maxNumber) ⇒ Object
This method gets top ‘n’ tweets from Twitter as requested by the user. Twitter API only returns 20 most recent statuses posted by the user. Thus, this method invokes the request multiple times until the requested number is reached.
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/tweet-words.rb', line 40 def self.topTweets(screen_name, maxNumber) count = 0 id = 0 response = Array.new(1000) begin responseFromTwitter = getTweets(screen_name, id) #responseFromTwitter.parsed_response.each_with_index do |elem, index| responseFromTwitter.parsed_response.each do |elem| response[count] = elem['text'] count = count + 1 id = Integer(elem['id']) - 1 # storing max_id to be used in subsequent calls if count >=maxNumber break end end end while count < maxNumber return response end |
.topWords(userName, maximumNumberOfTweets) ⇒ Object
and calls the methods to get the tweets and extract words from them. It then sorts the words in decreasing order of frequencies and prints the word list.
21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/tweet-words.rb', line 21 def self.topWords(userName, maximumNumberOfTweets) tweets = topTweets(userName, maximumNumberOfTweets) wordSet = findTopWords(tweets) puts "Top words in descending order of frequency: " wordSet.sort_by { |word, count| count }.reverse.each do |wrd| puts "#{wrd[0]}" end return ' ' end |