Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,25 @@ wordfilter.addWords(['zebra','elephant'])
wordfilter.blacklisted('this string has zebra in it') # True
```

Or with Ruby:
(No gem yet - copy the `lib` directory to your project for now)

```ruby
require '../lib/wordfilter'

Wordfilter::blacklisted? "does this string have a bad word in it?" # false

#clear all words from the filter
Wordfilter::clear_list

#add some words to the filter
Wordfilter::add_words(['zebra', 'elephant'])
Wordfilter::blacklisted? "this string has a zebra in it" # true

```

Please note that the Ruby implementation is a global singleton, and is not thread safe.

## Documentation
This is a word filter adapted from code that I use in a lot of my twitter bots. It is based on [a list of words that I've hand-picked](https://github.com/dariusk/wordfilter/blob/master/lib/badwords.json) for exclusion from my bots: essentially, it's a list of things that I would not say myself. Generally speaking, they are "words of oppression", aka racist/sexist/ableist things that I would not say.

Expand Down
18 changes: 14 additions & 4 deletions lib/wordfilter.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require 'json'

module Wordfilter
BadWordsFileName = File.dirname(__FILE__) + "/badwords.json";
BadWordsFileName = File.dirname(__FILE__) + "/badwords.json"
@@blacklist = nil

def self.init_first_time
Expand All @@ -10,15 +10,16 @@ def self.init_first_time
end

def self.init
badwords_file = File.read(BadWordsFileName);
@@blacklist = JSON.parse(badwords_file);
badwords_file = File.read(BadWordsFileName)
@@blacklist = JSON.parse(badwords_file)
@@blacklist.map!(&:downcase)
return
end

def self.blacklisted? string
self.init_first_time

string_to_test = string.downcase
@@blacklist.map!{|badword| badword.downcase}

@@blacklist.each{|word|
return true if string_to_test.include? word
Expand All @@ -29,10 +30,19 @@ def self.blacklisted? string

def self.add_words words
self.init_first_time
words.map!(&:downcase)
@@blacklist += words
return
end

def self.remove_word word
self.init_first_time
@@blacklist.delete word.downcase
return
end

def self.clear_list
@@blacklist = []
return
end
end
28 changes: 18 additions & 10 deletions test/wordfilter_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,33 @@ def setup
end

def test_detects_bad_words_in_a_string
assert(Wordfilter::blacklisted?("this string contains the word skank"), "Failed to mark a bad string as bad.");
assert(Wordfilter::blacklisted?("this string contains the word SkAnK"), "Failed to mark a bad string as bad.");
refute(Wordfilter::blacklisted?("this string was clean!"), "Failed to allow a non-bad string.");
assert(Wordfilter::blacklisted?("this string contains the word skank"), "skank should be true")
assert(Wordfilter::blacklisted?("this string contains the word SkAnK"), "SkAnK should be true")
assert(Wordfilter::blacklisted?("tthis string contains the wordskank"), "wordskank should be true")
assert(Wordfilter::blacklisted?("this string contains the skankword"), "skankword should be true")
refute(Wordfilter::blacklisted?("this string is clean!"), "should be false")
end

def test_add_word_to_blacklist
Wordfilter::add_words(['clean']);
assert(Wordfilter::blacklisted?("this string was clean!"), "Failed to blacklist a string containing a new bad word.");
Wordfilter::add_words(['clean'])
assert(Wordfilter::blacklisted?("this string was clean!"), "Failed to blacklist a string containing a new bad word.")
end

def test_added_words_checked_case_insensitively
Wordfilter::add_words(['CLEAN']);
assert(Wordfilter::blacklisted?("this string was clean!"), "Failed to blacklist a string containing a new bad word because of casing differences.");
Wordfilter::add_words(['CLEAN'])
assert(Wordfilter::blacklisted?("this string was clean!"), "Failed to blacklist a string containing a new bad word because of casing differences.")
end

def test_clear_blacklist
Wordfilter::clear_list
refute(Wordfilter::blacklisted?("this string contains the word skank"), "Cleared word list still blacklisting strings.");
Wordfilter::add_words(['skank']);
assert(Wordfilter::blacklisted?("this string contains the word skank"), "Failed to blacklist a string containing a new bad word.");
refute(Wordfilter::blacklisted?("this string contains the word skank"), "Cleared word list still blacklisting strings.")
Wordfilter::add_words(['skank'])
assert(Wordfilter::blacklisted?("this string contains the word skank"), "Failed to blacklist a string containing a new bad word.")
end

def test_remove_single_word_from_blacklist
assert(Wordfilter::blacklisted?("I have a prescription."), "Prescription should be blacklisted.")
Wordfilter::remove_word "crip"
refute(Wordfilter::blacklisted?("I have a prescription."), "Prescription should no longer be blacklisted.")
end
end