module WordCloudable
  extend ActiveSupport::Concern

  included do
    # You can add callbacks here if needed, e.g.:
    # before_save :remove_stopwords
  end

  # Method to filter out stopwords from a text attribute
  def word_cloud
    stopwords = StopWord.pluck(:word).index_with { |_| 0 }.with_indifferent_access
    words = send(:cloud_text).split(/\s+/)
    words.collect! { |x| x.downcase.gsub(".", "").gsub(",", "").gsub("?", "").gsub("'", "") }
    filtered_words = words.reject { |word| stopwords[word] || word.include?(":") }
    sorted = filtered_words.tally.select { |x, y| y > 5 }.sort_by { |x, y| y * -1 }

    # Calculate the maximum frequency
    max_frequency = sorted.first&.last.to_f

    # Normalize the frequencies
    sorted.collect { |x, y| [x, (y / max_frequency) * 15] }[0..50]
  end
end
