Class: ProfanityFilter::Base
- Inherits:
-
Object
- Object
- ProfanityFilter::Base
- Defined in:
- lib/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
- .banned?(word = '') ⇒ Boolean (also: profane?)
- .clean(text, replace_method = '') ⇒ Object
- .clean_word(word) ⇒ Object
- .replacement(word) ⇒ Object
Class Method Details
.banned?(word = '') ⇒ Boolean Also known as: profane?
42 43 44 |
# File 'lib/profanity_filter.rb', line 42 def banned?(word = '') dictionary.include?(word.downcase) if word end |
.clean(text, replace_method = '') ⇒ Object
47 48 49 50 51 |
# File 'lib/profanity_filter.rb', line 47 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
53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/profanity_filter.rb', line 53 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 |
.replacement(word) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/profanity_filter.rb', line 65 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 |