Sha256: 737bea27a692a21679c44d6a51982641e25bbcdc5de1427a665e06fda1d184b9

Contents?: true

Size: 1.18 KB

Versions: 1

Compression:

Stored size: 1.18 KB

Contents

module WordCountAnalyzer
  class Ellipsis
    # 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 !~ FOUR_CONSECUTIVE_REGEX) ||
      !(string !~ THREE_SPACE_REGEX) ||
      !(string !~ FOUR_SPACE_REGEX) ||
      !(string !~ OTHER_THREE_PERIOD_REGEX) ||
      !(string !~ UNICODE_ELLIPSIS)
    end

    def replace
      string.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

1 entries across 1 versions & 1 rubygems

Version Path
word_count_analyzer-0.0.5 lib/word_count_analyzer/ellipsis.rb