class WordCountAnalyzer::Date

Constants

DIGIT_ONLY_YEAR_FIRST_REGEX

Rubular: rubular.com/r/SRZ27XNlvR

DIGIT_ONLY_YEAR_LAST_REGEX

Rubular: rubular.com/r/mpVSeaKwdY

DMY_MDY_REGEX

Rubular: rubular.com/r/73CZ2HU0q6

DOW
DOW_ABBR
MONTHS
MONTH_ABBR
YMD_YDM_REGEX

Rubular: rubular.com/r/GWbuWXw4t0

Public Instance Methods

includes_date?(text) click to toggle source
# File lib/word_count_analyzer/date.rb, line 19
def includes_date?(text)
  includes_long_date?(text) || includes_number_only_date?(text)
end
occurrences(text) click to toggle source
# File lib/word_count_analyzer/date.rb, line 30
def occurrences(text)
  replace(text).scan(/wsdateword/).size
end
replace(text) click to toggle source
# File lib/word_count_analyzer/date.rb, line 23
def replace(text)
  counter = 0
  DOW_ABBR.map { |day| counter +=1 if text.include?('day') }
  text = redact_dates(counter, text)
  redact_regex(text)
end
replace_number_only_date(text) click to toggle source
# File lib/word_count_analyzer/date.rb, line 34
def replace_number_only_date(text)
  text.gsub(DMY_MDY_REGEX, ' wsdateword ')
      .gsub(YMD_YDM_REGEX, ' wsdateword ')
      .gsub(DIGIT_ONLY_YEAR_FIRST_REGEX, ' wsdateword ')
      .gsub(DIGIT_ONLY_YEAR_LAST_REGEX, ' wsdateword ')
end

Private Instance Methods

check_for_matches(day, month, text) click to toggle source
# File lib/word_count_analyzer/date.rb, line 113
def check_for_matches(day, month, text)
  !(text !~ /#{Regexp.escape(day)}(,)*\s#{Regexp.escape(month)}(\.)*\s\d+(rd|th|st)*(,)*\s\d{4}/i) ||
  !(text !~ /#{Regexp.escape(month)}(\.)*\s\d+(rd|th|st)*(,)*\s\d{4}/i) ||
  !(text !~ /\d{4}\.*\s#{Regexp.escape(month)}\s\d+(rd|th|st)*/i) ||
  !(text !~ /\d{4}(\.|-|\/)*#{Regexp.escape(month)}(\.|-|\/)*\d+/i) ||
  !(text !~ /#{Regexp.escape(month)}(\.)*\s\d+(rd|th|st)*/i) ||
  !(text !~ /\d{2}(\.|-|\/)*#{Regexp.escape(month)}(\.|-|\/)*(\d{4}|\d{2})/i)
end
includes_long_date?(text) click to toggle source
# File lib/word_count_analyzer/date.rb, line 86
def includes_long_date?(text)
  includes_long_date_1?(text) || includes_long_date_2?(text)
end
includes_long_date_1?(text) click to toggle source
# File lib/word_count_analyzer/date.rb, line 90
def includes_long_date_1?(text)
  DOW.each do |day|
    MONTHS.map { |month| return true if check_for_matches(day, month, text) }
    MONTH_ABBR.map { |month| return true if check_for_matches(day, month, text) }
  end
  false
end
includes_long_date_2?(text) click to toggle source
# File lib/word_count_analyzer/date.rb, line 98
def includes_long_date_2?(text)
  DOW_ABBR.each do |day|
    MONTHS.map { |month| return true if !(text !~ /#{Regexp.escape(day)}(\.)*(,)*\s#{Regexp.escape(month)}\s\d+(rd|th|st)*(,)*\s\d{4}/i) }
    MONTH_ABBR.map { |month| return true if !(text !~ /#{Regexp.escape(day)}(\.)*(,)*\s#{Regexp.escape(month)}(\.)*\s\d+(rd|th|st)*(,)*\s\d{4}/i) }
  end
  false
end
includes_number_only_date?(text) click to toggle source
# File lib/word_count_analyzer/date.rb, line 106
def includes_number_only_date?(text)
  !(text !~ DMY_MDY_REGEX) ||
  !(text !~ YMD_YDM_REGEX) ||
  !(text !~ DIGIT_ONLY_YEAR_FIRST_REGEX) ||
  !(text !~ DIGIT_ONLY_YEAR_LAST_REGEX)
end
redact_date(text, day, month) click to toggle source
# File lib/word_count_analyzer/date.rb, line 77
def redact_date(text, day, month)
  text.gsub(/#{Regexp.escape(day)}(,)*\s#{Regexp.escape(month)}(\.)*\s\d+(rd|th|st)*(,)*\s\d{4}\.?/i, ' wsdateword ')
                       .gsub(/#{Regexp.escape(month)}(\.)*\s\d+(rd|th|st)*(,)*\s\d{4}\.?/i, ' wsdateword ')
                       .gsub(/\d{4}\.*\s#{Regexp.escape(month)}\s\d+(rd|th|st)*\.?/i, ' wsdateword ')
                       .gsub(/\d{4}(\.|-|\/)*#{Regexp.escape(month)}(\.|-|\/)*\d+\.?/i, ' wsdateword ')
                       .gsub(/#{Regexp.escape(month)}(\.)*\s\d+(rd|th|st)*\.?/i, ' wsdateword ')
                       .gsub(/\d{2}(\.|-|\/)*#{Regexp.escape(month)}(\.|-|\/)*(\d{4}|\d{2})\.?/i, ' wsdateword ')
end
redact_dates(counter, text) click to toggle source
# File lib/word_count_analyzer/date.rb, line 43
def redact_dates(counter, text)
  if counter > 0
    text = redact_dow_abbr(text)
    text = redact_dow(text)
  else
    text = redact_dow(text)
    text = redact_dow_abbr(text)
  end
  text
end
redact_dow(text) click to toggle source
# File lib/word_count_analyzer/date.rb, line 61
def redact_dow(text)
  DOW.each do |day|
    MONTHS.map { |month| text = redact_date(text, day, month) }
    MONTH_ABBR.map { |month| text = redact_date(text, day, month) }
  end
  text
end
redact_dow_abbr(text) click to toggle source
# File lib/word_count_analyzer/date.rb, line 69
def redact_dow_abbr(text)
  DOW_ABBR.each do |day|
    MONTHS.map { |month| text = text.gsub(/#{Regexp.escape(day)}(\.)*(,)*\s#{Regexp.escape(month)}\s\d+(rd|th|st)*(,)*\s\d{4}\.?/i, ' wsdateword ') }
    MONTH_ABBR.map { |month| text = text.gsub(/#{Regexp.escape(day)}(\.)*(,)*\s#{Regexp.escape(month)}(\.)*\s\d+(rd|th|st)*(,)*\s\d{4}\.?/i, ' wsdateword ') }
  end
  text
end
redact_regex(text) click to toggle source
# File lib/word_count_analyzer/date.rb, line 54
def redact_regex(text)
  text.gsub(DMY_MDY_REGEX, ' wsdateword ')
      .gsub(YMD_YDM_REGEX, ' wsdateword ')
      .gsub(DIGIT_ONLY_YEAR_FIRST_REGEX, ' wsdateword ')
      .gsub(DIGIT_ONLY_YEAR_LAST_REGEX, ' wsdateword ')
end