From 3e8651b6b6f13d049f059431e98283ef0ba6ea65 Mon Sep 17 00:00:00 2001 From: Eli Brody Date: Sat, 30 Jan 2016 19:54:32 +0200 Subject: [PATCH 1/7] 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 9e50a2f28d9fd61eacd917a6d981bd0428b8bd5c Mon Sep 17 00:00:00 2001 From: Eli Brody Date: Sat, 30 Jan 2016 20:14:24 +0200 Subject: [PATCH 2/7] 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 861842518a33a87e5e52649f3543b5bac8e58402 Mon Sep 17 00:00:00 2001 From: Eli Brody Date: Sat, 30 Jan 2016 20:14:39 +0200 Subject: [PATCH 3/7] 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 249966eb02a0a4822b8a5d525b9e0769876283d1 Mon Sep 17 00:00:00 2001 From: Eli Brody Date: Sat, 30 Jan 2016 20:23:42 +0200 Subject: [PATCH 4/7] 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 From 8c4b3eefc3115485e0cad13acaf2c8e65cd85cfc Mon Sep 17 00:00:00 2001 From: Eli Brody Date: Sat, 30 Jan 2016 23:49:09 +0200 Subject: [PATCH 5/7] Created first version of the ruby gem. --- .gitignore | 1 + README.md | 4 ++-- Rakefile | 5 ++++- wordfilter.gemspec | 16 ++++++++++++++++ 4 files changed, 23 insertions(+), 3 deletions(-) create mode 100644 wordfilter.gemspec diff --git a/.gitignore b/.gitignore index 1470899..a1e8ed2 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ *.pyc *.swp .coverage +*.gem \ No newline at end of file diff --git a/README.md b/README.md index 3d9e1da..666b113 100644 --- a/README.md +++ b/README.md @@ -38,10 +38,10 @@ wordfilter.blacklisted('this string has zebra in it') # True ``` Or with Ruby: -(No gem yet - copy the `lib` directory to your project for now) +Install with `gem install wordfilter`. ```ruby -require '../lib/wordfilter' +require 'wordfilter' Wordfilter::blacklisted? "does this string have a bad word in it?" # false diff --git a/Rakefile b/Rakefile index 85f1afb..ce99741 100644 --- a/Rakefile +++ b/Rakefile @@ -6,4 +6,7 @@ Rake::TestTask.new do |t| t.libs << "test" t.test_files = FileList['test/*.rb'] t.verbose = true -end \ No newline at end of file +end + +desc "Run tests" +task :default => :test \ No newline at end of file diff --git a/wordfilter.gemspec b/wordfilter.gemspec new file mode 100644 index 0000000..c28467a --- /dev/null +++ b/wordfilter.gemspec @@ -0,0 +1,16 @@ +Gem::Specification.new do |s| + s.name = "wordfilter" + s.version = "0.2.6" + s.date = "2016-01-30" + s.summary = "A small module meant for use in text generators that lets you filter strings for bad words." + s.description = < Date: Sun, 31 Jan 2016 00:07:58 +0200 Subject: [PATCH 6/7] Replace tabs with spaces. --- .editorconfig | 4 ++ lib/wordfilter.rb | 90 ++++++++++++++++++++--------------------- test/wordfilter_test.rb | 72 ++++++++++++++++----------------- 3 files changed, 85 insertions(+), 81 deletions(-) diff --git a/.editorconfig b/.editorconfig index 071ad35..b10b52e 100644 --- a/.editorconfig +++ b/.editorconfig @@ -18,3 +18,7 @@ trim_trailing_whitespace = false [*.py] indent_style = space indent_size = 4 + +[*.rb] +indent_style = space +indent_size = 4 \ No newline at end of file diff --git a/lib/wordfilter.rb b/lib/wordfilter.rb index 080f099..9cf8126 100644 --- a/lib/wordfilter.rb +++ b/lib/wordfilter.rb @@ -1,48 +1,48 @@ require 'json' module Wordfilter - BadWordsFileName = File.dirname(__FILE__) + "/badwords.json" - @@blacklist = nil - - def self.init_first_time - return if(!@@blacklist.nil?) - self.init - end - - def self.init - 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.each{|word| - return true if string_to_test.include? word - } - - return false - end - - 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 + BadWordsFileName = File.dirname(__FILE__) + "/badwords.json" + @@blacklist = nil + + def self.init_first_time + return if(!@@blacklist.nil?) + self.init + end + + def self.init + 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.each{|word| + return true if string_to_test.include? word + } + + return false + end + + 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 diff --git a/test/wordfilter_test.rb b/test/wordfilter_test.rb index 5902603..fbfdae0 100644 --- a/test/wordfilter_test.rb +++ b/test/wordfilter_test.rb @@ -5,39 +5,39 @@ class WordfilterTest < Test::Unit::TestCase - def setup - super - Wordfilter.init - 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") - 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.") - 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.") - 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.") - 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 + def setup + super + Wordfilter.init + 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") + 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.") + 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.") + 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.") + 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 From a45432cc8bafee7e73fc603ce58f6bfac96ecac2 Mon Sep 17 00:00:00 2001 From: Eli Brody Date: Sun, 31 Jan 2016 10:36:11 +0200 Subject: [PATCH 7/7] Add cool gem badge to README.md --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 666b113..ad79691 100644 --- a/README.md +++ b/README.md @@ -40,6 +40,8 @@ wordfilter.blacklisted('this string has zebra in it') # True Or with Ruby: Install with `gem install wordfilter`. +[![Gem Version](https://badge.fury.io/rb/wordfilter.svg)](https://badge.fury.io/rb/wordfilter) + ```ruby require 'wordfilter'