Module: VaderSentimentRuby::WordHelper

Defined in:
lib/vader_sentiment_ruby/word_helper.rb

Overview

Helper module for word manipulations to simulate pythons methods behavior word_upcase?(word) is similar to Python’s word.isupper() strip_punctuation(word) is similar to Python’s word.strip(string.punctuation)

Constant Summary collapse

LETTERS_RANGE =
'A-Za-z'
PUNCTUATIONS =
'!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'

Class Method Summary collapse

Class Method Details

.strip_punctuation(token) ⇒ String

Removes all trailing and leading punctuation If the resulting string has two or fewer characters, then it was likely an emoticon, so return original string (ie ‘:)’ stripped would be ”, so just return ‘:)’ Example

strip_punctuation("'test'") # => "test"
strip_punctuation("'don't'") # => "don't"
strip_punctuation(":)") # => ":)"

Parameters:

  • token (String)

Returns:

  • (String)


34
35
36
37
38
39
40
41
42
43
44
# File 'lib/vader_sentiment_ruby/word_helper.rb', line 34

def strip_punctuation(token)
  original_set = token.split('')

  array = clean_leading_punctuations(original_set)
  array = clean_trailing_punctuations(array)
  stripped_token = array.join

  return token if stripped_token.size <= 2

  stripped_token
end

.word_upcase?(word) ⇒ Boolean

Checks that string contains at least one letter and all letters are in upcase Example

word_upcase?(':D') # => true
word_upcase?(':)') # => false

Parameters:

  • word (String)

Returns:

  • (Boolean)


19
20
21
# File 'lib/vader_sentiment_ruby/word_helper.rb', line 19

def word_upcase?(word)
  word == word.upcase && word.count(LETTERS_RANGE).positive?
end