Class: WordCountAnalyzer::Ellipsis

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

Constant Summary collapse

THREE_CONSECUTIVE_REGEX =
/\.{3}(?=\s+[A-Z])/
FOUR_CONSECUTIVE_REGEX =
/(?<=[^\.])\.{3}\.(?=[^\.])/
THREE_SPACE_REGEX =
/(\s\.){3}\s/
FOUR_SPACE_REGEX =
/(?<=[a-z])(\.\s){3}\.(\z|$|\n)/
OTHER_THREE_PERIOD_REGEX =
/[^\.]\.{3}[^\.]/
UNICODE_ELLIPSIS =
/(?<=[^…])…{1}(?=[^…])/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(string:) ⇒ Ellipsis

Returns a new instance of Ellipsis.



20
21
22
# File 'lib/word_count_analyzer/ellipsis.rb', line 20

def initialize(string:)
  @string = string
end

Instance Attribute Details

#stringObject (readonly)

Returns the value of attribute string.



19
20
21
# File 'lib/word_count_analyzer/ellipsis.rb', line 19

def string
  @string
end

Instance Method Details

#includes_ellipsis?Boolean

Returns:

  • (Boolean)


24
25
26
27
28
29
30
31
# File 'lib/word_count_analyzer/ellipsis.rb', line 24

def includes_ellipsis?
  !(string !~ THREE_CONSECUTIVE_REGEX) ||
  !(string !~ FOUR_CONSECUTIVE_REGEX) ||
  !(string !~ THREE_SPACE_REGEX) ||
  !(string !~ FOUR_SPACE_REGEX) ||
  !(string !~ OTHER_THREE_PERIOD_REGEX) ||
  !(string !~ UNICODE_ELLIPSIS)
end

#occurencesObject



42
43
44
45
46
# File 'lib/word_count_analyzer/ellipsis.rb', line 42

def occurences
  count = 0
  replace.split(' ').map { |token| count += 1 if token.strip.eql?('wseword') }
  count
end

#replaceObject



33
34
35
36
37
38
39
40
# File 'lib/word_count_analyzer/ellipsis.rb', line 33

def replace
  string.gsub(THREE_CONSECUTIVE_REGEX, ' wseword ')
        .gsub(FOUR_CONSECUTIVE_REGEX, ' wseword ')
        .gsub(THREE_SPACE_REGEX, ' wseword ')
        .gsub(FOUR_SPACE_REGEX, ' wseword ')
        .gsub(OTHER_THREE_PERIOD_REGEX, ' wseword ')
        .gsub(UNICODE_ELLIPSIS, ' wseword ')
end