class Twexicon::Analyzer

Constants

COMMON_WORDS

Words from en.wikipedia.org/wiki/Most_common_words_in_English –– Left out some common yet still interesting ones (People, Good, Think, Work, First, One, Two, Want, New, Give, Know)

Attributes

input[R]
int[R]
tweets[R]
username[R]

Public Class Methods

new(username, tweets) click to toggle source
# File lib/twexicon/analyzer.rb, line 4
def initialize(username, tweets)
  @username = username
  @tweets = tweets
  puts "\nIndexed the #{tweets.size} most recent tweet(s) for @#{username}."
  run
end

Public Instance Methods

get_acronyms() click to toggle source
# File lib/twexicon/analyzer.rb, line 156
def get_acronyms
  collection = []
  tweets.each{|num, tweet| collection << tweet.values[0][:acronyms]}
  if collection.flatten.compact.empty?
    puts "\nIt appears that @#{username} really likes to spell everything out."
  else
    puts "\n@#{username} was too lazy to type out the following acronyms:"
    collection.flatten.compact.uniq.each{|a| puts a}
  end
end
get_hashtags() click to toggle source
# File lib/twexicon/analyzer.rb, line 167
def get_hashtags
  collection = []
  tweets.each{|num, tweet| collection << tweet.values[0][:hashtags]}
  if collection.flatten.compact.empty?
    puts "\nIt appears that @#{username} isn't maximizing their #impressions."
  else
    puts "\n@#{username} ameliorated the lack of Thought Leadership on these topics:"
    collection.flatten.compact.uniq.each{|h| puts h}
  end
end
get_numbers() click to toggle source
# File lib/twexicon/analyzer.rb, line 145
def get_numbers
  collection = []
  tweets.each{|num, tweet| collection << tweet.values[0][:numbers]}
  if collection.flatten.compact.empty?
    puts "\nIt appears that @#{username} isn't a numbers person."
  else
    puts "\n@#{username} loves them some digits:"
    collection.flatten.compact.uniq.each{|n| puts n}
  end
end
get_pix() click to toggle source
# File lib/twexicon/analyzer.rb, line 123
def get_pix
  collection = []
  tweets.each{|num, tweet| collection << tweet.values[0][:pix]}
  if collection.flatten.compact.empty?
    puts "\nIt appears that @#{username} doesn't think a picture is worth 140 characters."
  else
    puts "\n@#{username} shared the following risky clicks:"
    collection.flatten.compact.uniq.each{|p| puts p}
  end
end
get_shouts() click to toggle source
# File lib/twexicon/analyzer.rb, line 112
def get_shouts
  collection = []
  tweets.each{|num, tweet| collection << tweet.values[0][:shouts]}
  if collection.flatten.compact.empty?
    puts "\nIt appears that @#{username} isn't excited by anything."
  else
    puts "\n@#{username} shouted about the following things:"
    collection.flatten.compact.uniq.each{|s| puts s}
  end
end
get_tweet() click to toggle source
# File lib/twexicon/analyzer.rb, line 58
def get_tweet
  if tweets.size == 1
    puts "\n@#{username}'s only tweet is: #{tweets[int].keys[1]}"
  else
    puts "\nWhich tweet would you like to view?"
    int = ""
    until int =~ /^\d+$/ && int.to_i >= 1 && int.to_i <= tweets.size
      puts "Please specify a number between 1 and #{tweets.size}."
      int = gets.strip.gsub(/\D/, "")
    end
    puts "\n@#{username} said: #{tweets[int.to_i].keys[0]}"
  end
end
get_usernames() click to toggle source
# File lib/twexicon/analyzer.rb, line 178
def get_usernames
  collection = []
  tweets.each{|num, tweet| collection << tweet.values[0][:usernames]}
  if collection.flatten.compact.empty?
    puts "\nIt appears that @#{username} is a bit of a solipsist."
  else
    puts "\n@#{username} thinkfluenced the following users:"
    collection.flatten.compact.uniq{|u| u.downcase}.each{|u| puts u}
  end
end
get_words() click to toggle source
# File lib/twexicon/analyzer.rb, line 83
def get_words
  words = {}
  word_array = []
  tweets.each_value do |v| # Count up instances of each word
    v.values[0][:words].each do |w|
      word = w.gsub(/[^\'\w]/, "").capitalize
      if words.has_key?(word) && !COMMON_WORDS.include?(word)
        words[word] += 1
      else
        words[word] = 1
      end
    end
  end
  words.each do |w, n| # Create strings for the words that occur >1 times
    case n
    when 1..9 then word_array << "000#{n}x #{w}"
    when 10..99 then word_array << "00#{n}x #{w}"
    when 100..999 then word_array << "0#{n}x #{w}"
    when 1000..9999 then word_array << "#{n}x #{w}"
    end
  end
  if word_array.empty?
    puts "\nIt appears that @#{username} is not much of a talker."
  else
    puts "\n@#{username}'s current favorite word(s):"
    puts word_array.sort.reverse.each{|w| w.sub!(/^0+/, "")}.take(10)
  end
end
is_valid?() click to toggle source
# File lib/twexicon/analyzer.rb, line 47
def is_valid?
  if input =~ /(^help$|^h$|^get$|^g$|^exit$|^e$|^words$|^w$|^shouts$|^s$|^pix$|^p$|^links$|^l$|^numbers$|^n$|^acronyms$|^a$|^hashtags$|^ht$|^usernames$|^handles$|^u$)/
    true
  elsif input == nil
    false
  else
    puts "Hm, I didn't recognize that. Please make a valid selection."
    puts "To display all options, type 'help'."
  end
end
run() click to toggle source
# File lib/twexicon/analyzer.rb, line 11
def run
  quit = false
  until quit
    @input = nil
    @int = ""
    puts "\nWhat would you like to do next? For options, type 'help'."
    until is_valid?
      @input = gets.strip.gsub(/\W/, "").downcase
    end
    case input
    when "help", "h"
      puts "\nTo retrieve one of @#{username}'s #{tweets.size} tweet(s), type 'GET'."
      puts "To view @#{username}'s ten favorite words, type 'WORDS'."
      puts "For @#{username}'s favorite things to scream about, type 'SHOUTS'."
      puts "To see the #hashtags that consume @#{username}'s thoughts, type 'HASHTAGS'."
      puts "For some Twitter handles that @#{username} has tweeted at, type 'USERNAMES'."
      puts "To see links for embedded pictures and/or videos, type 'PIX'."
      puts "To view any other links @#{username} deemed worthy of sharing, type 'LINKS'."
      puts "For some numbers that @#{username} mentioned, type 'NUMBERS'."
      puts "To see which short acronyms @#{username} is fond of, type 'ACRONYMS'."
      puts "To look up a different Twitter handle or to exit the program, type 'EXIT'."
      puts "Short commands, in order: 'g', 'w', 's', 'ht', 'u', 'p', 'l', 'n', 'a', 'e', and 'h' for 'help'."
    when "get", "g" then get_tweet
    when "words", "w" then get_words
    when "shouts", "s" then get_shouts
    when "pix", "p" then get_pix
    when "links", "l" then get_links
    when "numbers", "n" then get_numbers
    when "acronyms", "a" then get_acronyms
    when "hashtags", "ht" then get_hashtags
    when "usernames", "handles", "u" then get_usernames
    when "exit", "e" then quit = true
    end
  end
end