Class: ProfanityFilter::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/profanity_filter.rb

Constant Summary collapse

@@replacement_text =
'@#$%'
@@dictionary_file =
File.join(File.dirname(__FILE__), '../config/dictionary.yml')

Class Method Summary collapse

Class Method Details

.banned?(word = '') ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/profanity_filter.rb', line 45

def banned?(word = '')
  dictionary.include?(word.downcase) if word
end

.clean(text, replace_method = '') ⇒ Object



53
54
55
56
57
# 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) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
# 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

.dictionaryObject



41
42
43
# File 'lib/profanity_filter.rb', line 41

def dictionary
  @@dictionary ||= YAML.load_file(@@dictionary_file)
end

.profane?(text = '') ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/profanity_filter.rb', line 49

def profane?(text = '')
  text == clean(text) ? false : true
end

.replacement(word) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# 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