Class: Twexicon::Analyzer

Inherits:
Object
  • Object
show all
Defined in:
lib/twexicon/analyzer.rb

Constant Summary collapse

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)

["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"]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(username, tweets) ⇒ Analyzer

Returns a new instance of Analyzer.



4
5
6
7
8
9
# 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

Instance Attribute Details

#inputObject (readonly)

Returns the value of attribute input.



2
3
4
# File 'lib/twexicon/analyzer.rb', line 2

def input
  @input
end

#intObject (readonly)

Returns the value of attribute int.



2
3
4
# File 'lib/twexicon/analyzer.rb', line 2

def int
  @int
end

#tweetsObject (readonly)

Returns the value of attribute tweets.



2
3
4
# File 'lib/twexicon/analyzer.rb', line 2

def tweets
  @tweets
end

#usernameObject (readonly)

Returns the value of attribute username.



2
3
4
# File 'lib/twexicon/analyzer.rb', line 2

def username
  @username
end

Instance Method Details

#get_acronymsObject



156
157
158
159
160
161
162
163
164
165
# 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_hashtagsObject



167
168
169
170
171
172
173
174
175
176
# 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


134
135
136
137
138
139
140
141
142
143
# File 'lib/twexicon/analyzer.rb', line 134

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

#get_numbersObject



145
146
147
148
149
150
151
152
153
154
# 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_pixObject



123
124
125
126
127
128
129
130
131
132
# 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_shoutsObject



112
113
114
115
116
117
118
119
120
121
# 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_tweetObject



58
59
60
61
62
63
64
65
66
67
68
69
70
# 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_usernamesObject



178
179
180
181
182
183
184
185
186
187
# 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_wordsObject



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# 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?Boolean

Returns:

  • (Boolean)


47
48
49
50
51
52
53
54
55
56
# 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

#runObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# 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