Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#flagged_words #4

Merged
merged 2 commits into from Aug 10, 2011
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
42 changes: 41 additions & 1 deletion lib/profanalyzer.rb
Expand Up @@ -175,7 +175,47 @@ def self.profane?(*args)
@@settings = oldsettings if oldsettings @@settings = oldsettings if oldsettings
false false
end end


# Returns an array of words that match the currently set rules against the
# provided string. The array will be empty if no words are matched.
#
# Example:
# Profanalyzer.filter("shit damn foo") #==> ["shit", "damn"]
# Profanalyzer.filter("profanalyzer is rad!") #==> []
#
## With custom settings
# Profanalyzer.check_all = false
# Profanalyzer.check_racist = false
# Profanalyzer.flagged_words("you're a mick") #==> []
#
# You can pass options to the method itself:
# Profanalyzer.flagged_words("you're a mick", :racist => false) #==> []
#
# Available options:
#
# [:+all+] Set to +true+ or +false+ to specify checking all words in the blacklist
# [:+sexual+] Set to +true+ or +false+ to specify sexual checking
# [:+racist+] Set to +true+ or +false+ to specify racial slur checking
# [:+tolerance+] Sets the tolerance. 0-5.
def self.flagged_words(*args)
flagged_words = []
str = args[0]

if (args.size > 1 && args[1].is_a?(Hash))
oldsettings = @@settings
self.update_settings_from_hash args[1]
end

banned_words = self.forbidden_words_from_settings
banned_words.each do |word|
if str =~ /\b#{word}\b/i
@@settings = oldsettings if oldsettings
flagged_words << word
end
end
return flagged_words
end

# Filters the provided string using the currently set rules, with #!@$%-like # Filters the provided string using the currently set rules, with #!@$%-like
# characters substituted in. # characters substituted in.
# #
Expand Down
17 changes: 17 additions & 0 deletions test/test_profanalyzer.rb
Expand Up @@ -18,6 +18,7 @@ def test_single_word
Profanalyzer.tolerance = 0 Profanalyzer.tolerance = 0
Profanalyzer.check_all = true Profanalyzer.check_all = true
assert_equal(true, Profanalyzer.profane?("asshole")) assert_equal(true, Profanalyzer.profane?("asshole"))
assert_equal(["asshole"], Profanalyzer.flagged_words("asshole"))
end end


def test_single_racist_word def test_single_racist_word
Expand All @@ -27,6 +28,8 @@ def test_single_racist_word
Profanalyzer.check_racist = true Profanalyzer.check_racist = true
assert_equal(true, Profanalyzer.profane?("spic")) assert_equal(true, Profanalyzer.profane?("spic"))
assert_equal(false, Profanalyzer.profane?("pussy")) assert_equal(false, Profanalyzer.profane?("pussy"))
assert_equal(["spic"], Profanalyzer.flagged_words("spic"))
assert_equal([], Profanalyzer.flagged_words("pussy"))
end end


def test_single_sexual_word def test_single_sexual_word
Expand All @@ -36,13 +39,17 @@ def test_single_sexual_word
Profanalyzer.check_sexual = true Profanalyzer.check_sexual = true
assert_equal(true, Profanalyzer.profane?("vagina")) assert_equal(true, Profanalyzer.profane?("vagina"))
assert_equal(false, Profanalyzer.profane?("nigger")) assert_equal(false, Profanalyzer.profane?("nigger"))
assert_equal(["vagina"], Profanalyzer.flagged_words("vagina"))
assert_equal([], Profanalyzer.flagged_words("nigger"))
end end


def test_tolerance def test_tolerance
Profanalyzer.tolerance = 4 Profanalyzer.tolerance = 4
Profanalyzer.check_all = true Profanalyzer.check_all = true
assert_equal(false, Profanalyzer.profane?("asskisser")) # badness = 3 assert_equal(false, Profanalyzer.profane?("asskisser")) # badness = 3
assert_equal(true, Profanalyzer.profane?("fuck")) # badness = 5 assert_equal(true, Profanalyzer.profane?("fuck")) # badness = 5
assert_equal([], Profanalyzer.flagged_words("asskisser")) # badness = 3
assert_equal(["fuck"], Profanalyzer.flagged_words("fuck")) # badness = 5
end end


def test_sexual_tolerance def test_sexual_tolerance
Expand All @@ -52,6 +59,8 @@ def test_sexual_tolerance
Profanalyzer.check_sexual = true Profanalyzer.check_sexual = true
assert_equal(false, Profanalyzer.profane?("vagina")) # badness = 3 assert_equal(false, Profanalyzer.profane?("vagina")) # badness = 3
assert_equal(true, Profanalyzer.profane?("cunt")) # badness = 5 assert_equal(true, Profanalyzer.profane?("cunt")) # badness = 5
assert_equal([], Profanalyzer.flagged_words("vagina")) # badness = 3
assert_equal(["cunt"], Profanalyzer.flagged_words("cunt")) # badness = 5
end end


def test_racist_tolerance def test_racist_tolerance
Expand All @@ -61,6 +70,8 @@ def test_racist_tolerance
Profanalyzer.check_racist = true Profanalyzer.check_racist = true
assert_equal(false, Profanalyzer.profane?("mick")) # badness = 3 assert_equal(false, Profanalyzer.profane?("mick")) # badness = 3
assert_equal(true, Profanalyzer.profane?("nigger")) # badness = 5 assert_equal(true, Profanalyzer.profane?("nigger")) # badness = 5
assert_equal([], Profanalyzer.flagged_words("mick")) # badness = 3
assert_equal(["nigger"], Profanalyzer.flagged_words("nigger")) # badness = 5
end end


def test_filter def test_filter
Expand Down Expand Up @@ -102,4 +113,10 @@ def test_subtitutions
assert_equal("fark", Profanalyzer.filter("fuck")) assert_equal("fark", Profanalyzer.filter("fuck"))
end end


def test_multiple_matches_in_flagged_words
Profanalyzer.tolerance = 0
Profanalyzer.check_all = true
assert_equal(["shit", "mick", "cocksucking"], Profanalyzer.flagged_words("You're a cocksucking piece of shit, you mick."))
end

end end
11 changes: 11 additions & 0 deletions test/test_profanalyzer_advanced.rb
Expand Up @@ -5,31 +5,42 @@ class TestProfanalyzer < Test::Unit::TestCase


def test_single_word_advanced def test_single_word_advanced
assert_equal(true, Profanalyzer.profane?("asshole", :tolerance => 0, :all => true)) assert_equal(true, Profanalyzer.profane?("asshole", :tolerance => 0, :all => true))
assert_equal(["asshole"], Profanalyzer.flagged_words("asshole", :tolerance => 0, :all => true))
end end


def test_single_racist_word_advanced def test_single_racist_word_advanced
assert_equal(true, Profanalyzer.profane?("spic", :tolerance => 0, :racist => true, :sexual => false)) assert_equal(true, Profanalyzer.profane?("spic", :tolerance => 0, :racist => true, :sexual => false))
assert_equal(false, Profanalyzer.profane?("pussy", :tolerance => 0, :racist => true, :sexual => false)) assert_equal(false, Profanalyzer.profane?("pussy", :tolerance => 0, :racist => true, :sexual => false))
assert_equal(["spic"], Profanalyzer.flagged_words("spic", :tolerance => 0, :racist => true, :sexual => false))
assert_equal([], Profanalyzer.flagged_words("pussy", :tolerance => 0, :racist => true, :sexual => false))
end end


def test_single_sexual_word_advanced def test_single_sexual_word_advanced
assert_equal(true, Profanalyzer.profane?("vagina", :tolerance => 0, :racist => false, :sexual => true)) assert_equal(true, Profanalyzer.profane?("vagina", :tolerance => 0, :racist => false, :sexual => true))
assert_equal(false, Profanalyzer.profane?("nigger", :tolerance => 0, :racist => false, :sexual => true)) assert_equal(false, Profanalyzer.profane?("nigger", :tolerance => 0, :racist => false, :sexual => true))
assert_equal(["vagina"], Profanalyzer.flagged_words("vagina", :tolerance => 0, :racist => false, :sexual => true))
assert_equal([], Profanalyzer.flagged_words("nigger", :tolerance => 0, :racist => false, :sexual => true))
end end


def test_tolerance_advanced def test_tolerance_advanced
assert_equal(false, Profanalyzer.profane?("asskisser", :tolerance => 4, :all => true)) # badness = 3 assert_equal(false, Profanalyzer.profane?("asskisser", :tolerance => 4, :all => true)) # badness = 3
assert_equal(true, Profanalyzer.profane?("fuck", :tolerance => 4, :all => true)) # badness = 5 assert_equal(true, Profanalyzer.profane?("fuck", :tolerance => 4, :all => true)) # badness = 5
assert_equal([], Profanalyzer.flagged_words("asskisser", :tolerance => 4, :all => true)) # badness = 3
assert_equal(["fuck"], Profanalyzer.flagged_words("fuck", :tolerance => 4, :all => true)) # badness = 5
end end


def test_sexual_tolerance_advanced def test_sexual_tolerance_advanced
assert_equal(false, Profanalyzer.profane?("vagina", :tolerance => 4, :racist => false, :sexual => true)) # badness = 3 assert_equal(false, Profanalyzer.profane?("vagina", :tolerance => 4, :racist => false, :sexual => true)) # badness = 3
assert_equal(true, Profanalyzer.profane?("cunt", :tolerance => 4, :racist => false, :sexual => true)) # badness = 5 assert_equal(true, Profanalyzer.profane?("cunt", :tolerance => 4, :racist => false, :sexual => true)) # badness = 5
assert_equal([], Profanalyzer.flagged_words("vagina", :tolerance => 4, :racist => false, :sexual => true)) # badness = 3
assert_equal(["cunt"], Profanalyzer.flagged_words("cunt", :tolerance => 4, :racist => false, :sexual => true)) # badness = 5
end end


def test_racist_tolerance_advanced def test_racist_tolerance_advanced
assert_equal(false, Profanalyzer.profane?("mick", :tolerance => 4, :racist => true, :sexual => false)) # badness = 3 assert_equal(false, Profanalyzer.profane?("mick", :tolerance => 4, :racist => true, :sexual => false)) # badness = 3
assert_equal(true, Profanalyzer.profane?("nigger", :tolerance => 4, :racist => true, :sexual => false)) # badness = 5 assert_equal(true, Profanalyzer.profane?("nigger", :tolerance => 4, :racist => true, :sexual => false)) # badness = 5
assert_equal([], Profanalyzer.flagged_words("mick", :tolerance => 4, :racist => true, :sexual => false)) # badness = 3
assert_equal(["nigger"], Profanalyzer.flagged_words("nigger", :tolerance => 4, :racist => true, :sexual => false)) # badness = 5
end end


def test_filter_advanced def test_filter_advanced
Expand Down