Class: ProfanityFilter::Base

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

Constant Summary collapse

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

Class Method Summary collapse

Class Method Details

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



42
43
44
45
46
# File 'lib/fu-fu/profanity_filter.rb', line 42

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



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/fu-fu/profanity_filter.rb', line 48

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 is_banned? concat
  end
  
  is_banned?(word) ? replacement(word) : word
end

.is_banned?(word = '') ⇒ Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/fu-fu/profanity_filter.rb', line 74

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

.profane?(text, replace_method = '') ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/fu-fu/profanity_filter.rb', line 38

def profane?(text, replace_method='')
  text != clean(text, replace_method)
end

.replacement(word) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/fu-fu/profanity_filter.rb', line 60

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
  else
    replacement_text
  end
end