From 36b6456a7159237218d27f6c8cd7c173900a306e Mon Sep 17 00:00:00 2001 From: Eli Brody Date: Sat, 30 Jan 2016 19:54:32 +0200 Subject: [PATCH 1/4] Update Ruby library to match latest javascript version. Minor code fixes. --- lib/wordfilter.rb | 8 +++++++- test/wordfilter_test.rb | 14 +++++++++++--- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/lib/wordfilter.rb b/lib/wordfilter.rb index 92ce409..dfc0fc7 100644 --- a/lib/wordfilter.rb +++ b/lib/wordfilter.rb @@ -12,13 +12,13 @@ def self.init_first_time def self.init badwords_file = File.read(BadWordsFileName); @@blacklist = JSON.parse(badwords_file); + @@blacklist.map!(&:downcase) 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 @@ -29,8 +29,14 @@ def self.blacklisted? string def self.add_words words self.init_first_time + words.map!(&:downcase) @@blacklist += words end + + def self.remove_word word + self.init_first_time + @@blacklist.delete word.downcase + end def self.clear_list @@blacklist = [] diff --git a/test/wordfilter_test.rb b/test/wordfilter_test.rb index cf29083..f3b560e 100644 --- a/test/wordfilter_test.rb +++ b/test/wordfilter_test.rb @@ -11,9 +11,11 @@ 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 @@ -32,4 +34,10 @@ def test_clear_blacklist 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 \ No newline at end of file From 67dacff034782557ec5afd8c0a7e14d2e7645f1d Mon Sep 17 00:00:00 2001 From: Eli Brody Date: Sat, 30 Jan 2016 20:14:24 +0200 Subject: [PATCH 2/4] Clean erroneous trailing semicolons from scripts. --- lib/wordfilter.rb | 6 +++--- test/wordfilter_test.rb | 24 ++++++++++++------------ 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/lib/wordfilter.rb b/lib/wordfilter.rb index dfc0fc7..ffc74d6 100644 --- a/lib/wordfilter.rb +++ b/lib/wordfilter.rb @@ -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 @@ -10,8 +10,8 @@ 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) end diff --git a/test/wordfilter_test.rb b/test/wordfilter_test.rb index f3b560e..5902603 100644 --- a/test/wordfilter_test.rb +++ b/test/wordfilter_test.rb @@ -11,28 +11,28 @@ def setup end def test_detects_bad_words_in_a_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"); + 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 From f415d7cb047c81038c4fa9009da18213a7e2edf6 Mon Sep 17 00:00:00 2001 From: Eli Brody Date: Sat, 30 Jan 2016 20:14:39 +0200 Subject: [PATCH 3/4] Add Ruby section to README.md --- README.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/README.md b/README.md index 87a2b9b..a816925 100644 --- a/README.md +++ b/README.md @@ -37,6 +37,23 @@ 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 + +``` + ## 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. From 67970cb6ca453947989f1008b54778fd37f793ef Mon Sep 17 00:00:00 2001 From: Eli Brody Date: Sat, 30 Jan 2016 20:23:42 +0200 Subject: [PATCH 4/4] Prevent returning filter list contents when calling Wordfilter methods. Add implementation note to readme. --- README.md | 2 ++ lib/wordfilter.rb | 4 ++++ 2 files changed, 6 insertions(+) diff --git a/README.md b/README.md index a816925..3d9e1da 100644 --- a/README.md +++ b/README.md @@ -54,6 +54,8 @@ 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. diff --git a/lib/wordfilter.rb b/lib/wordfilter.rb index ffc74d6..080f099 100644 --- a/lib/wordfilter.rb +++ b/lib/wordfilter.rb @@ -13,6 +13,7 @@ def self.init badwords_file = File.read(BadWordsFileName) @@blacklist = JSON.parse(badwords_file) @@blacklist.map!(&:downcase) + return end def self.blacklisted? string @@ -31,14 +32,17 @@ 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 \ No newline at end of file