Class: WordCountAnalyzer::Contraction

Inherits:
Object
  • Object
show all
Defined in:
lib/word_count_analyzer/contraction.rb

Constant Summary collapse

CONTRACTIONS =
{
  "i'm"               => "I am",
  "i'll"              => "I will",
  "i'd"               => "I would",
  "i've"              => "I have",
  "you're"            => "you are",
  "you'll"            => "you will",
  "you'd"             => "you would",
  "you've"            => "you have",
  "he's"              => "he is",
  "he'll"             => "he will",
  "he'd"              => "he would",
  "she's"             => "she is",
  "she'll"            => "she will",
  "she'd"             => "she would",
  "it's"              => "it is",
  "'tis"              => "it is",
  "it'll"             => "it will",
  "it'd"              => "it would",
  "we're"             => "we are",
  "we'll"             => "we will",
  "we'd"              => "we would",
  "we've"             => "we have",
  "they're"           => "they are",
  "they'll"           => "they will",
  "they'd"            => "they would",
  "they've"           => "they have",
  "that's"            => "that is",
  "that'll"           => "that will",
  "that'd"            => "that would",
  "who's"             => "who is",
  "who'll"            => "who will",
  "who'd"             => "who would",
  "what's"            => "what is",
  "what're"           => "what are",
  "what'll"           => "what will",
  "what'd"            => "what would",
  "where's"           => "where is",
  "where'll"          => "where will",
  "where'd"           => "where would",
  "when's"            => "when is",
  "when'll"           => "when will",
  "when'd"            => "when would",
  "why's"             => "why is",
  "why'll"            => "why will",
  "why'd"             => "why would",
  "how's"             => "how is",
  "how'll"            => "how will",
  "how'd"             => "how would",
  "she'd've"          => "she would have",
  "'tisn't"           => "it is not",
  "isn't"             => "is not",
  "aren't"            => "are not",
  "wasn't"            => "was not",
  "weren't"           => "were not",
  "haven't"           => "have not",
  "hasn't"            => "has not",
  "hadn't"            => "had not",
  "won't"             => "will not",
  "wouldn't"          => "would not",
  "don't"             => "do not",
  "doesn't"           => "does not",
  "didn't"            => "did not",
  "can't"             => "cannot",
  "couldn't"          => "could not",
  "shouldn't"         => "should not",
  "mightn't"          => "might not",
  "mustn't"           => "must not",
  "would've"          => "would have",
  "should've"         => "should have",
  "could've"          => "could have",
  "might've"          => "might have",
  "must've"           => "must have",
  "o'"                => "of",
  "o'clock"           => "of the clock",
  "ma'am"             => "madam",
  "ne'er-do-well"     => "never-do-well",
  "cat-o'-nine-tails" => "cat-of-nine-tails",
  "jack-o'-lantern"   => "jack-of-the-lantern",
  "will-o'-the-wisp"  => "will-of-the-wisp",
  "'twas"             => "it was"
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(token:, following_token:, tgr:, **args) ⇒ Contraction

Returns a new instance of Contraction.



87
88
89
90
91
92
# File 'lib/word_count_analyzer/contraction.rb', line 87

def initialize(token:, following_token:, tgr:, **args)
  @token = token
  @following_token = following_token
  @tgr = tgr
  @hyphen = args[:hyphen] || 'count_as_one'
end

Instance Attribute Details

#following_tokenObject (readonly)

Returns the value of attribute following_token.



86
87
88
# File 'lib/word_count_analyzer/contraction.rb', line 86

def following_token
  @following_token
end

#hyphenObject (readonly)

Returns the value of attribute hyphen.



86
87
88
# File 'lib/word_count_analyzer/contraction.rb', line 86

def hyphen
  @hyphen
end

#tgrObject (readonly)

Returns the value of attribute tgr.



86
87
88
# File 'lib/word_count_analyzer/contraction.rb', line 86

def tgr
  @tgr
end

#tokenObject (readonly)

Returns the value of attribute token.



86
87
88
# File 'lib/word_count_analyzer/contraction.rb', line 86

def token
  @token
end

Instance Method Details

#contraction?Boolean

Returns:

  • (Boolean)


94
95
96
97
98
# File 'lib/word_count_analyzer/contraction.rb', line 94

def contraction?
  common_contraction? ||
  (apostrophe_s_token? &&
    following_is_not_a_noun?)
end

#expanded_countObject



100
101
102
103
104
105
106
107
108
109
110
# File 'lib/word_count_analyzer/contraction.rb', line 100

def expanded_count
  if self.contraction?
    if common_contraction?
      calculate_contraction_length
    else
      2
    end
  else
    1
  end
end

#replaceObject



112
113
114
115
116
117
118
119
120
# File 'lib/word_count_analyzer/contraction.rb', line 112

def replace
  if CONTRACTIONS.has_key?(token.downcase)
    CONTRACTIONS[token.downcase]
  elsif apostrophe_s_token? && following_is_not_a_noun?
    ' word word '
  else
    token
  end
end