class Linter::BaseAssociation

Constants

SKIP_WORDS

Public Class Methods

add_recommendation(_result) click to toggle source
# File lib/linter/base_association.rb, line 47
def self.add_recommendation(_result)
  false
end
analyze(text) click to toggle source
# File lib/linter/base_association.rb, line 7
def self.analyze(text)
  result = OpenStruct.new(trend: '')

  wordlists.dig('words').each do |key, words|
    word_count_key = "#{key}_word_counts".to_sym
    result[word_count_key] = {}
    words.each do |word|
      result.send(word_count_key).merge!(word_count(text, word))
    end
  end

  result.trend = calculate_trend(result)
  result.recommendation = add_recommendation(result)
  result
end
recommendation_file() click to toggle source
# File lib/linter/base_association.rb, line 42
def self.recommendation_file
  file_path = File.join(__dir__, '../../data/recommendations.yml')
  @recommendation_file ||= YAML.load_file(file_path)
end
word_count(text, word) click to toggle source
# File lib/linter/base_association.rb, line 23
def self.word_count(text, word)
  if self::FULL_WORD
    regex = /\b#{word}\b/i
  else
    regex = /\b(#{word}\w*)\b/i
  end
  matches = text.scan(regex)
  return {} unless matches.any?

  # Use Enumerable#tally with Ruby 2.7
  matches
    .flatten
    .map(&:downcase)
    .reject { |m| SKIP_WORDS.include? m}
    .group_by { |v| v }
    .transform_values(&:size)
    .to_h
end