class Twords

Count the occurrences of words in a tweeter's tweets

Twords.config do |config|
  config.rejects = %w[my us we an w/ because b/c or are this is from
                      be on the for to and at our of in rt a with &
                      that it by as if was]

  config.range   = 30
  config.up_to { Time.now }
  config.include_hashtags = false
  config.include_uris     = false
  config.include_mentions = false

  config.twitter_client do |twitter|
    twitter.consumer_key        = YOUR_TWITTER_CONSUMER_KEY
    twitter.consumer_secret     = YOUR_TWITTER_CONSUMER_SECRET
    twitter.access_token        = YOUR_TWITTER_ACCESS_TOKEN
    twitter.access_token_secret = YOUR_TWITTER_ACCESS_TOKEN_SECRET
  end
end

twords = Twords.new 'user_one', 'user_two'

twords.audit
# => true

twords.words
# => { "pizza"=>32, "burger"=>28, "pups"=>36, ... }

Instance methods

Constants

VERSION

The current gem version

Attributes

screen_names[R]

The screen names included in the analysis

@api public @return [Array<String>] if names are provided to initialize @return [Array] if no names are provided to initialize

words[R]

The words and their number of occurrences

@api public @return [Hash] returns the word(String) and counts(Integer) as key-value pairs

Public Class Methods

client() click to toggle source

Access the Twitter client

@api public @return [Twords::TwitterClient]

# File lib/twords.rb, line 63
def self.client
  config.client
end
config() { |config| ... } click to toggle source

Set configuration options. The same configuration is shared accross all objects in the Twords namespace. Configuration can be changed on the fly and will affect all instantiated objects.

@api public for block { |config| … } @yield [Twords::Configuration] call methods on an instance of Twords::Configuration to override the default configuration settings. @return [Twords::Configuration]

# File lib/twords.rb, line 46
def self.config
  @configuration ||= Configuration.new
  @configuration.tap { |config| yield config if block_given? }
end
new(*screen_names) click to toggle source

Initializes a new Twords object

@api public @param screen_names [Array<String>] any number of screen names to include in the analysis @return [Twords]

# File lib/twords/instance_methods.rb, line 31
def initialize(*screen_names)
  @screen_names = screen_names.flatten
  @words        = {}
  @audited      = false
end
reset_config!() click to toggle source

Resets all configuration options to default settings

@api public @return [Twords::Configuration]

# File lib/twords.rb, line 55
def self.reset_config!
  config.reset!
end

Public Instance Methods

audit() click to toggle source

Fetch tweets and count words. Short circuits and returns true if already audited.

@api public @return [true]

# File lib/twords/instance_methods.rb, line 50
def audit
  count_words unless audited?
  @audited = true
end
audit!() click to toggle source

Clear all results and audit from scratch

@api public @return [true] always returns true unless an error is raised

# File lib/twords/instance_methods.rb, line 59
def audit!
  instance_variables.reject { |ivar| %i[@screen_names @words].include?(ivar) }.each do |ivar|
    instance_variable_set(ivar, nil)
  end

  @audited = false

  audit
end
audited?() click to toggle source

Have the screen_names already been audited?

@api public @return [true] if already audited @return [false] if not audited yet

# File lib/twords/instance_methods.rb, line 42
def audited?
  @audited
end
percentages() click to toggle source

The frequency of each word as a share of the total_word_count

@api public @return [Hash] returns the word(String) and percentage(Float) as key-value pairs

# File lib/twords/instance_methods.rb, line 123
def percentages
  @_percentages ||= words.each_with_object({}) do |word_count, hash|
    hash[word_count.first] = percentage(word_count.last)
  end
end
sort_percentages() click to toggle source

Sorts percentages in descending order

@api public @return [Array<Array<String, Float>>]

# File lib/twords/instance_methods.rb, line 133
def sort_percentages
  @_sort_percentages ||= percentages.sort { |a, b| b.last <=> a.last }
end
sort_tweets() click to toggle source

Returns an array of tweets sorted by time created in descending order

@api public @return [Array<Twitter::Tweet>]

# File lib/twords/instance_methods.rb, line 91
def sort_tweets
  tweets.sort { |a, b| b.created_at <=> a.created_at }
end
sort_tweets!() click to toggle source

sort_tweets destructively

@api public @return [Array<Twitter::Tweet>]

# File lib/twords/instance_methods.rb, line 99
def sort_tweets!
  tweets.sort! { |a, b| b.created_at <=> a.created_at }
end
sort_words() click to toggle source

Sort words by frequency in descending order

@api public @return [Array<Array<String, Integer>>]

# File lib/twords/instance_methods.rb, line 73
def sort_words
  audit
  @_sort_words ||= words.sort { |a, b| b.last <=> a.last }
end
Also aliased as: words_forward
to_csv() click to toggle source

Generate a CSV formatted String of the sorted results, with column headers “word, count”

@api public @return [String] in CSV format

# File lib/twords/instance_methods.rb, line 141
def to_csv
  CSV.generate do |csv|
    csv << %w[word count]
    sort_words.each do |word_count|
      csv << word_count
    end
  end
end
to_json() click to toggle source

Generate a JSON formatted String of the sorted results, as one hash object with word-count key-value pairs.

@api public @return [String] in JSON format

# File lib/twords/instance_methods.rb, line 166
def to_json
  sort_words.to_h.to_json
end
total_word_count() click to toggle source

Total occurrences of all words included in analysis, i.e. sum of the count of all words.

@api public @return [Integer]

# File lib/twords/instance_methods.rb, line 115
def total_word_count
  @_total_word_count ||= words.values.reduce(:+)
end
tweets() click to toggle source

Returns all of the tweets that fall within the configured time range

@api public @return [Array<Twitter::Tweet>]

# File lib/twords/instance_methods.rb, line 83
def tweets
  @_tweets ||= client.filter_tweets(screen_names)
end
tweets_count() click to toggle source

Number of tweets being analyzed

@api public @return [Integer]

# File lib/twords/instance_methods.rb, line 107
def tweets_count
  @_tweets_count ||= tweets.count
end
words_forward()
Alias for: sort_words
write_to_csv(opts = {}) click to toggle source

Write the output of to_csv to a file.

@api public @return [Integer] representing the byte count of the file @param opts [Hash] File writing options. All except for :filename are passed to File#open. @option opts [String] :filename A relative pathname. Defaults to 'twords_report.csv'

# File lib/twords/instance_methods.rb, line 156
def write_to_csv(opts = {})
  filename = opts.fetch(:filename) { 'twords_report.csv' }
  write_file(filename, :to_csv, opts)
end
write_to_json(opts = {}) click to toggle source

Write the output of to_json to a file.

@api public @return [Integer] representing the byte count of the file @param opts [Hash] customizable file writing options. All but :filename arepassed to File#open @option opts [String] :filename A relative pathname. Defaults to 'twords_report.json'

# File lib/twords/instance_methods.rb, line 176
def write_to_json(opts = {})
  filename = opts.fetch(:filename) { 'twords_report.json' }
  write_file(filename, :to_json, opts)
end

Private Instance Methods

client() click to toggle source

@api private

# File lib/twords/instance_methods.rb, line 184
def client
  config.client
end
count_words() click to toggle source

@api private

# File lib/twords/instance_methods.rb, line 189
def count_words
  words.clear
  tweets.each do |tweet|
    words_array(tweet).each do |word|
      next if WordMatcher.should_be_skipped?(word)
      words.key?(word) ? words[word] += 1 : words[word] = 1
    end
  end
end
percentage(count) click to toggle source

@api private

# File lib/twords/instance_methods.rb, line 205
def percentage(count)
  (count / total_word_count.to_f * 100)
end
words_array(tweet) click to toggle source

@api private

# File lib/twords/instance_methods.rb, line 200
def words_array(tweet)
  tweet.attrs[:full_text].downcase.split(' ')
end
write_file(filename, method, opts = {}) click to toggle source

@api private

# File lib/twords/instance_methods.rb, line 210
def write_file(filename, method, opts = {})
  File.open(filename, 'w', opts) { |file| file.write send(method) }
end