Module: Helper

Included in:
Andeltsa::TwitterRequest
Defined in:
lib/helper.rb

Class Method Summary collapse

Class Method Details

.display_ranking(ranked_words) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/helper.rb', line 50

def self.display_ranking(ranked_words)
  if ranked_words.length < 1

    exit
  end
  longest_key = ranked_words.keys.max_by(&:length)
  print "\n+#{'-' * longest_key.length}--+#{'-' * 11}+\n"
  printf "| %-#{longest_key.length}s | %-9s |\n", "Word", "Frequency"
  print "+#{'-' * longest_key.length}--+#{'-' * 11}+\n"
  ranked_words.each do |key, value|
    printf "| %-#{longest_key.length}s |    %-6s |\n", key, value
  end
  print "+#{'-' * longest_key.length}--+#{'-' * 11}+\n\n"
end

.exit_messageObject



65
66
67
68
# File 'lib/helper.rb', line 65

def self.exit_message
  puts "\nThank you for your time. pls follow @andela and"
  puts "@mubarakadeimam on Twitter"
end

.rank_words(okay_words) ⇒ Object



46
47
48
# File 'lib/helper.rb', line 46

def self.rank_words(okay_words)
  Helper.sort_hash_descend(Helper.words_counter(okay_words))
end

.remove_stop_words(tweets_words) ⇒ Object

Sort Hashes in descending frequencing



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/helper.rb', line 4

def self.remove_stop_words(tweets_words)
  tweets = tweets_words
  stop_words = []
  File.open("./stop_words.yml", "r").each do |x|
    stop_words << x
  end
  stop_words = stop_words.join(" ").split(",")
  stop_words_free = []
  tweets.each do |word|
    if stop_words.include?word
      nil
    else
      stop_words_free << word
    end
  end
  stop_words_free.join(" ")
end

.sort_hash_descend(value) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/helper.rb', line 22

def self.sort_hash_descend(value)
  sorted_hash = Hash.new
  value.sort_by { |_x, y| -y }.each do |val|
    sorted_hash[val[0]] = val[1]
  end
  sorted_hash
end

.words_counter(words) ⇒ Object

Counts the occurence of words in a string and returns ‘word => frequency’



31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/helper.rb', line 31

def self.words_counter(words)
  word_list = words.split(" ")
  word_map = Hash.new
  word_list.each do |word|
    word.downcase!
    next unless word.length > 2
    if word_map.key?(word)
      word_map[word] += 1
    else
      word_map[word] = 1
    end
  end
  word_map
end