module SadPanda::Helpers
Helper methods for SadPanda
Public Instance Methods
emojies_in(text)
click to toggle source
Captures and returns emojies in the text
# File lib/sad_panda/helpers.rb, line 31 def emojies_in(text) (sad_emojies + happy_emojies).map do |emoji| text.scan(emoji) end.flatten end
frequencies_for(words)
click to toggle source
Returns a Hash of frequencies of each uniq word in the text
# File lib/sad_panda/helpers.rb, line 13 def frequencies_for(words) word_frequencies = {} words.each { |word| word_frequencies[word] = words.count(word) } word_frequencies end
happy_emojies()
click to toggle source
# File lib/sad_panda/helpers.rb, line 8 def happy_emojies [':)', ':-)', ':]', ':-]'] end
happy_emoticon?(words)
click to toggle source
Checks if words has a happy emoji
# File lib/sad_panda/helpers.rb, line 53 def happy_emoticon?(words) (happy_emojies & words).any? end
remove_stopwords_in(words)
click to toggle source
Strips the words array of stop words
# File lib/sad_panda/helpers.rb, line 26 def remove_stopwords_in(words) words - SadPanda::Bank::STOPWORDS end
sad_emojies()
click to toggle source
# File lib/sad_panda/helpers.rb, line 4 def sad_emojies [':(', ':-(', ':[', ':-['] end
sad_emoticon?(words)
click to toggle source
Checks if words has a sad emoji
# File lib/sad_panda/helpers.rb, line 58 def sad_emoticon?(words) (sad_emojies & words).any? end
sanitize(text)
click to toggle source
Removing non ASCII characters from text
# File lib/sad_panda/helpers.rb, line 38 def sanitize(text) text.gsub!(/[^a-z ]/i, '') text.gsub!(/((([A-Za-z]{3,9}:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[\w]*))?)/, '') text.gsub!(/(?=\w*h)(?=\w*t)(?=\w*t)(?=\w*p)\w*/, '') text.gsub!(/\s\s+/, ' ') text.downcase end
stems_for(words)
click to toggle source
Converts all the words to its stem form
# File lib/sad_panda/helpers.rb, line 20 def stems_for(words) stemmer = Lingua::Stemmer.new(language: 'en') words.map! { |word| stemmer.stem(word) } end
words_in(text)
click to toggle source
Removes all the unwated characters from the text
# File lib/sad_panda/helpers.rb, line 48 def words_in(text) emojies_in(text) + sanitize(text).split end