Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

#flagged_words #4

Merged
merged 2 commits into from

2 participants

@cmhobbs

Add #flagged_words to return an array of words caught by Profanalyzer in a string.

@michaeledgar michaeledgar merged commit 788c3d7 into michaeledgar:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Aug 10, 2011
  1. Add tests for #flagged_words.

    Christopher M. Hobbs authored
This page is out of date. Refresh to see the latest.
View
42 lib/profanalyzer.rb
@@ -175,7 +175,47 @@ def self.profane?(*args)
@@settings = oldsettings if oldsettings
false
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
# characters substituted in.
#
View
17 test/test_profanalyzer.rb
@@ -18,6 +18,7 @@ def test_single_word
Profanalyzer.tolerance = 0
Profanalyzer.check_all = true
assert_equal(true, Profanalyzer.profane?("asshole"))
+ assert_equal(["asshole"], Profanalyzer.flagged_words("asshole"))
end
def test_single_racist_word
@@ -27,6 +28,8 @@ def test_single_racist_word
Profanalyzer.check_racist = true
assert_equal(true, Profanalyzer.profane?("spic"))
assert_equal(false, Profanalyzer.profane?("pussy"))
+ assert_equal(["spic"], Profanalyzer.flagged_words("spic"))
+ assert_equal([], Profanalyzer.flagged_words("pussy"))
end
def test_single_sexual_word
@@ -36,6 +39,8 @@ def test_single_sexual_word
Profanalyzer.check_sexual = true
assert_equal(true, Profanalyzer.profane?("vagina"))
assert_equal(false, Profanalyzer.profane?("nigger"))
+ assert_equal(["vagina"], Profanalyzer.flagged_words("vagina"))
+ assert_equal([], Profanalyzer.flagged_words("nigger"))
end
def test_tolerance
@@ -43,6 +48,8 @@ def test_tolerance
Profanalyzer.check_all = true
assert_equal(false, Profanalyzer.profane?("asskisser")) # badness = 3
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
def test_sexual_tolerance
@@ -52,6 +59,8 @@ def test_sexual_tolerance
Profanalyzer.check_sexual = true
assert_equal(false, Profanalyzer.profane?("vagina")) # badness = 3
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
def test_racist_tolerance
@@ -61,6 +70,8 @@ def test_racist_tolerance
Profanalyzer.check_racist = true
assert_equal(false, Profanalyzer.profane?("mick")) # badness = 3
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
def test_filter
@@ -102,4 +113,10 @@ def test_subtitutions
assert_equal("fark", Profanalyzer.filter("fuck"))
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
View
11 test/test_profanalyzer_advanced.rb
@@ -5,31 +5,42 @@ class TestProfanalyzer < Test::Unit::TestCase
def test_single_word_advanced
assert_equal(true, Profanalyzer.profane?("asshole", :tolerance => 0, :all => true))
+ assert_equal(["asshole"], Profanalyzer.flagged_words("asshole", :tolerance => 0, :all => true))
end
def test_single_racist_word_advanced
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(["spic"], Profanalyzer.flagged_words("spic", :tolerance => 0, :racist => true, :sexual => false))
+ assert_equal([], Profanalyzer.flagged_words("pussy", :tolerance => 0, :racist => true, :sexual => false))
end
def test_single_sexual_word_advanced
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(["vagina"], Profanalyzer.flagged_words("vagina", :tolerance => 0, :racist => false, :sexual => true))
+ assert_equal([], Profanalyzer.flagged_words("nigger", :tolerance => 0, :racist => false, :sexual => true))
end
def test_tolerance_advanced
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([], Profanalyzer.flagged_words("asskisser", :tolerance => 4, :all => true)) # badness = 3
+ assert_equal(["fuck"], Profanalyzer.flagged_words("fuck", :tolerance => 4, :all => true)) # badness = 5
end
def test_sexual_tolerance_advanced
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([], 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
def test_racist_tolerance_advanced
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([], 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
def test_filter_advanced
Something went wrong with that request. Please try again.