class Twexicon::Analyzer attr_reader :tweets, :username, :input, :int def initialize(username, tweets) @username = username @tweets = tweets puts "\nIndexed the #{tweets.size} most recent tweet(s) for @#{username}." run end 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 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 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 # Words from https://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) COMMON_WORDS = ["A", "About", "An", "And", "Are", "As", "At", "Be", "Been", "But", "By", "For", "From", "Get", "Had", "Has", "Have", "In", "Into", "Is", "It", "It's", "Its", "Just", "Not", "Of", "On", "Or", "Say", "So", "Some", "That", "The", "There", "These", "This", "Those", "To", "Up", "With", "I", "My", "Your", "They", "He", "You", "Do", "His", "We", "Her", "She", "Will", "All", "Would", "Their", "What", "Out", "If", "Who", "Which", "Go", "Me", "When", "Make", "Can", "Time", "No", "Him", "Take", "Year", "Could", "Them", "See", "Other", "Than", "Then", "Now", "Look", "Only", "Come", "Over", "Also", "Back", "After", "Use", "How", "Our", "Well", "Way", "Even", "Because", "Any", "Day", "Most", "Us", # other additions "Wouldn't", "Couldn't", "Shouldn't", "Mustn't", "Would've", "Could've", "Should've", "Must've", "Hadn't", "Wasn't", "Weren't", "Ain't", "Aint", "Here", "Seem", "Seems", "That's", "Took", "Much", "More", "You're", "We're", "We've", "I've", "I'm", "Don't", "Got", "Soon", # contraction endings until I fix the parsing error "Re", "Ll", "Ve", # Letters until I fix the contraction parsing error "B", "C", "D", "E", "F", "G", "H", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", # Common words in other languages that *aren't* uncommon English words (so, "Con" doesn't count) "En"] 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 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 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 def get_links collection = [] tweets.each{|num, tweet| collection << tweet.values[0][:links]} if collection.flatten.compact.empty? puts "\nIt appears that @#{username} hasn't found anything worth sharing on the interwebs." else puts "\n@#{username} directed traffic to the following sites:" collection.flatten.compact.uniq.each{|l| puts l} end end 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 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 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 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 end