Sha256: aa14d64e2eef8ea50e46d71bf819b705441f5c61c1e28643fedfc5f6c942e67c

Contents?: true

Size: 1.37 KB

Versions: 4

Compression:

Stored size: 1.37 KB

Contents

module WordCountAnalyzer
  class Ellipsis
    # Rubular: http://rubular.com/r/i60hCK81fz
    THREE_CONSECUTIVE_REGEX = /\.{3}(?=\s+[A-Z])/

    # Rubular: http://rubular.com/r/mfdtSeuIf2
    FOUR_CONSECUTIVE_REGEX = /(?<=[^\.])\.{3}\.(?=[^\.])/

    # Rubular: http://rubular.com/r/YBG1dIHTRu
    THREE_SPACE_REGEX = /(\s\.){3}\s/

    # Rubular: http://rubular.com/r/2VvZ8wRbd8
    FOUR_SPACE_REGEX = /(?<=[a-z])(\.\s){3}\.(\z|$|\n)/

    OTHER_THREE_PERIOD_REGEX = /[^\.]\.{3}[^\.]/

    UNICODE_ELLIPSIS = /(?<=[^…])…{1}(?=[^…])/

    attr_reader :string
    def initialize(string:)
      @string = string
    end

    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

    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

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

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
word_count_analyzer-0.0.4 lib/word_count_analyzer/ellipsis.rb
word_count_analyzer-0.0.3 lib/word_count_analyzer/ellipsis.rb
word_count_analyzer-0.0.2 lib/word_count_analyzer/ellipsis.rb
word_count_analyzer-0.0.1 lib/word_count_analyzer/ellipsis.rb