Skip to content

Commit

Permalink
Will now filter punctionation delimited profanity. Added more word re…
Browse files Browse the repository at this point in the history
…placement options.
  • Loading branch information
neilang committed Aug 5, 2009
1 parent a5937db commit d7a023a
Show file tree
Hide file tree
Showing 5 changed files with 299 additions and 30 deletions.
12 changes: 3 additions & 9 deletions config/dictionary.yml
@@ -1,13 +1,8 @@
# the filter currently checks for squeezed words that start out at 3 or more characters.
# example - will filter 'ass' and 'aasssss' but not 'as' - still working on a better way to avoid assess TODO - reserved word list.
# keep in mind, the squeezed version of words (shitter shiter) when adding terms
# the filter currently checks for words that are 3 or more characters.
---
as: "*ss"
ass: "*ss"
asses: "*ss*s"
ashole: "*ssh*l*"
asshole: "*ssh*l*"
asholes: "*ssh*l*s"
assholes: "*ssh*l*s"
bastard: b*st*rd
beastial: b**st**l
Expand All @@ -24,7 +19,6 @@ bitching: b*tch*ng
blowjob: bl*wj*b
blowjobs: bl*wj*bs
bullshit: b*llsh*t
bulshit: b*llsh*t
clit: cl*t
cock: c*ck
cocks: c*cks
Expand All @@ -38,7 +32,6 @@ cummer: c*mm*r
cumming: c*mm*ng
cums: c*ms
cumshot: c*msh*t
cunilingus: c*n*l*ng*s
cunillingus: c*n*ll*ng*s
cunnilingus: c*nn*l*ng*s
cunt: c*nt
Expand All @@ -55,6 +48,7 @@ cyberfucking: cyb*rf*ck*ng
damn: d*mn
dildo: d*ld*
dildos: d*ld*s
dick: d*ck
dink: d*nk
dinks: d*nks
ejaculate: "*j*c*l*t*"
Expand Down Expand Up @@ -203,4 +197,4 @@ slut: sl*t
sluts: sl*ts
smut: sm*t
spunk: sp*nk
twat: tw*t
twat: tw*t
45 changes: 36 additions & 9 deletions lib/profanity_filter.rb
Expand Up @@ -39,16 +39,43 @@ class Base
class << self
def clean(text, replace_method = '')
return text if text.blank?
text.split(/(\W)/).collect{|word| replace_method == 'dictionary' ? clean_word_dictionary(word) : clean_word_basic(word)}.join
end

def clean_word_dictionary(word)
dictionary.include?(word.downcase.squeeze) && word.size > 2 ? dictionary[word.downcase.squeeze] : word
end

def clean_word_basic(word)
dictionary.include?(word.downcase.squeeze) && word.size > 2 ? replacement_text : word
@replace_method = replace_method
text.split(/(\s)/).collect{ |word|
clean_word(word)
}.join
end

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

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

def is_banned?(word = '')
dictionary.include?(word.downcase)
end
end
end
end
94 changes: 86 additions & 8 deletions test/benign_filter_test.rb
Expand Up @@ -12,11 +12,23 @@ class Post < ActiveRecord::Base
end
end

module VowelsPostHelper
class Post < ActiveRecord::Base
profanity_filter :title, :body, :method => 'vowels'
end
end

module HollowPostHelper
class Post < ActiveRecord::Base
profanity_filter :title, :body, :method => 'hollow'
end
end

class BasicProfanityFilterTest < Test::Unit::TestCase
include BasicPostHelper

def profane_post(opts={})
Post.new({:title => 'A Fucking Title', :body => "This is some shitty post by a fucking user"}.merge(opts))
Post.new({:title => 'A Fucking Title', :body => "This is some f-u-c-k-i-n-g shitty-post by a fucking user"}.merge(opts))
end

def test_it_should_filter_specified_fields
Expand All @@ -25,9 +37,9 @@ def test_it_should_filter_specified_fields
assert_equal 'A @#$% Title', p.title
assert_equal 'A @#$% Title', p.title_clean
assert_equal 'A Fucking Title', p.title_original
assert_equal 'This is some @#$% post by a @#$% user', p.body
assert_equal 'This is some @#$% post by a @#$% user', p.body_clean
assert_equal 'This is some shitty post by a fucking user', p.body_original
assert_equal 'This is some @#$% @#$%-post by a @#$% user', p.body
assert_equal 'This is some @#$% @#$%-post by a @#$% user', p.body_clean
assert_equal 'This is some f-u-c-k-i-n-g shitty-post by a fucking user', p.body_original
end

def test_it_should_handle_nil_fields_bug_9
Expand All @@ -49,7 +61,7 @@ class DictionaryProfanityFilterTest < Test::Unit::TestCase
include DictionaryPostHelper

def profane_post(opts={})
Post.new({:title => 'A Fucking Title', :body => "This is some shitty post by a fucking user"}.merge(opts))
Post.new({:title => 'A Fucking Title', :body => "This is some f-u-c-k-i-n-g shitty-post by a fucking user"}.merge(opts))
end

def test_it_should_filter_specified_fields
Expand All @@ -58,9 +70,75 @@ def test_it_should_filter_specified_fields
assert_equal 'A f*ck*ng Title', p.title
assert_equal 'A f*ck*ng Title', p.title_clean
assert_equal 'A Fucking Title', p.title_original
assert_equal 'This is some sh*tty post by a f*ck*ng user', p.body
assert_equal 'This is some sh*tty post by a f*ck*ng user', p.body_clean
assert_equal 'This is some shitty post by a fucking user', p.body_original
assert_equal 'This is some f*ck*ng sh*tty-post by a f*ck*ng user', p.body
assert_equal 'This is some f*ck*ng sh*tty-post by a f*ck*ng user', p.body_clean
assert_equal 'This is some f-u-c-k-i-n-g shitty-post by a fucking user', p.body_original
end

def test_it_should_handle_nil_fields_bug_9
p = Post.new({:title => nil, :body => nil})
p.save
assert_equal nil, p.title
assert_equal nil, p.body
end

def test_it_should_handle_blank_fields_bug_9
p = Post.new({:title => "", :body => ""})
p.save
assert_equal "", p.title
assert_equal "", p.body
end
end

class VowelsProfanityFilterTest < Test::Unit::TestCase
include VowelsPostHelper

def profane_post(opts={})
Post.new({:title => 'A Fucking Title', :body => "This is some f-u-c-k-i-n-g shitty-post by a fucking user"}.merge(opts))
end

def test_it_should_filter_specified_fields
p = profane_post
p.save
assert_equal 'A F*ck*ng Title', p.title
assert_equal 'A F*ck*ng Title', p.title_clean
assert_equal 'A Fucking Title', p.title_original
assert_equal 'This is some f*ck*ng sh*tty-post by a f*ck*ng user', p.body
assert_equal 'This is some f*ck*ng sh*tty-post by a f*ck*ng user', p.body_clean
assert_equal 'This is some f-u-c-k-i-n-g shitty-post by a fucking user', p.body_original
end

def test_it_should_handle_nil_fields_bug_9
p = Post.new({:title => nil, :body => nil})
p.save
assert_equal nil, p.title
assert_equal nil, p.body
end

def test_it_should_handle_blank_fields_bug_9
p = Post.new({:title => "", :body => ""})
p.save
assert_equal "", p.title
assert_equal "", p.body
end
end

class HollowProfanityFilterTest < Test::Unit::TestCase
include HollowPostHelper

def profane_post(opts={})
Post.new({:title => 'A Fucking Title', :body => "This is some f-u-c-k-i-n-g shitty-post by a fucking user"}.merge(opts))
end

def test_it_should_filter_specified_fields
p = profane_post
p.save
assert_equal 'A F*****g Title', p.title
assert_equal 'A F*****g Title', p.title_clean
assert_equal 'A Fucking Title', p.title_original
assert_equal 'This is some f*****g s****y-post by a f*****g user', p.body
assert_equal 'This is some f*****g s****y-post by a f*****g user', p.body_clean
assert_equal 'This is some f-u-c-k-i-n-g shitty-post by a fucking user', p.body_original
end

def test_it_should_handle_nil_fields_bug_9
Expand Down
79 changes: 75 additions & 4 deletions test/destructive_filter_test.rb
Expand Up @@ -12,18 +12,31 @@ class Post < ActiveRecord::Base
end
end

module VowelsPostHelper
class Post < ActiveRecord::Base
profanity_filter! :title, :body, :method => 'vowels'
end
end

module HollowPostHelper
class Post < ActiveRecord::Base
profanity_filter! :title, :body, :method => 'hollow'
end
end


class BasicProfanityFilterTest < Test::Unit::TestCase
include BasicPostHelper

def profane_post(opts={})
Post.new({:title => 'A Fucking Title', :body => "This is some shitty post by a fucking user"}.merge(opts))
Post.new({:title => 'A Fucking Title', :body => "This is some f-u-c-k-i-n-g shitty-post by a fucking user"}.merge(opts))
end

def test_it_should_filter_specified_fields
p = profane_post
p.save
assert_equal 'A @#$% Title', p.title
assert_equal 'This is some @#$% post by a @#$% user', p.body
assert_equal 'This is some @#$% @#$%-post by a @#$% user', p.body
end

def test_it_should_handle_nil_fields_bug_9
Expand All @@ -45,14 +58,72 @@ class DictionaryProfanityFilterTest < Test::Unit::TestCase
include DictionaryPostHelper

def profane_post(opts={})
Post.new({:title => 'A Fucking Title', :body => "This is some shitty post by a fucking user"}.merge(opts))
Post.new({:title => 'A Fucking Title', :body => "This is some f-u-c-k-i-n-g shitty-post by a fucking user"}.merge(opts))
end

def test_it_should_filter_specified_fields
p = profane_post
p.save
assert_equal 'A f*ck*ng Title', p.title
assert_equal 'This is some sh*tty post by a f*ck*ng user', p.body
assert_equal 'This is some f*ck*ng sh*tty-post by a f*ck*ng user', p.body
end

def test_it_should_handle_nil_fields_bug_9
p = Post.new({:title => nil, :body => nil})
p.save
assert_equal nil, p.title
assert_equal nil, p.body
end

def test_it_should_handle_blank_fields_bug_9
p = Post.new({:title => "", :body => ""})
p.save
assert_equal "", p.title
assert_equal "", p.body
end
end

class VowelsProfanityFilterTest < Test::Unit::TestCase
include VowelsPostHelper

def profane_post(opts={})
Post.new({:title => 'A Fucking Title', :body => "This is some f-u-c-k-i-n-g shitty-post by a fucking user"}.merge(opts))
end

def test_it_should_filter_specified_fields
p = profane_post
p.save
assert_equal 'A F*ck*ng Title', p.title
assert_equal 'This is some f*ck*ng sh*tty-post by a f*ck*ng user', p.body
end

def test_it_should_handle_nil_fields_bug_9
p = Post.new({:title => nil, :body => nil})
p.save
assert_equal nil, p.title
assert_equal nil, p.body
end

def test_it_should_handle_blank_fields_bug_9
p = Post.new({:title => "", :body => ""})
p.save
assert_equal "", p.title
assert_equal "", p.body
end
end

class HollowProfanityFilterTest < Test::Unit::TestCase
include HollowPostHelper

def profane_post(opts={})
Post.new({:title => 'A Fucking Title', :body => "This is some f-u-c-k-i-n-g shitty-post by a fucking user"}.merge(opts))
end

def test_it_should_filter_specified_fields
p = profane_post
p.save
assert_equal 'A F*****g Title', p.title
assert_equal 'This is some f*****g s****y-post by a f*****g user', p.body
end

def test_it_should_handle_nil_fields_bug_9
Expand Down

0 comments on commit d7a023a

Please sign in to comment.