class ProfanityFilter::Base

Public Class Methods

banned?(word = '') click to toggle source
# File lib/profanity_filter.rb, line 45
def banned?(word = '')
  dictionary.include?(word.downcase) if word
end
clean(text, replace_method = '') click to toggle source
# File lib/profanity_filter.rb, line 53
def clean(text, replace_method = '')
  return text if text.blank?
  @replace_method = replace_method
  text.split(/(\s)/).collect{ |word| clean_word(word) }.join
end
clean_word(word) click to toggle source
# File lib/profanity_filter.rb, line 59
def clean_word(word)
   return word unless(word.strip.size > 2)

   if word.index(/[\W]/)
     word = word.split(/(\W)/).collect{ |subword| clean_word(subword) }.join
     concat = word.gsub(/\W/, '')
     word = concat if banned? concat
   end

   banned?(word) ? replacement(word) : word
 end
dictionary() click to toggle source
# File lib/profanity_filter.rb, line 41
def dictionary
  @@dictionary ||= YAML.load_file(@@dictionary_file)
end
profane?(text = '') click to toggle source
# File lib/profanity_filter.rb, line 49
def profane?(text = '')
  text == clean(text) ? false : true
end
replacement(word) click to toggle source
# File lib/profanity_filter.rb, line 71
def replacement(word)
  case @replace_method
  when 'dictionary'
    dictionary[word.downcase] || word
  when 'vowels'
    word.gsub(/[aeiou]/i, '*')
  when 'hollow'
    word[1..word.size-2] = '*' * (word.size-2) if word.size > 2
    word
  when 'stars'
    word = '*' * (word.size)
  else
    replacement_text
  end
end