diff --git a/profanityfilter/profanityfilter.py b/profanityfilter/profanityfilter.py index 497e473..2009087 100644 --- a/profanityfilter/profanityfilter.py +++ b/profanityfilter/profanityfilter.py @@ -53,9 +53,30 @@ def append_words(self, word_list): """Define a custom list of profane words to be used in conjunction with the default list.""" self._extra_censor_list.extend(word_list) - def remove_word(self, word): - """Remove given word from censor list.""" - self._censor_list.remove(word) + def remove_word(self, word, anywhere=True): + """ + Remove given word from censor list. + - anywhere (bool): + attempt removal from both the extra censor list and the custom/default censor list + """ + if word in self._extra_censor_list and anywhere: + self._extra_censor_list.remove(word) + else: + if word in self._custom_censor_list: + self._custom_censor_list.remove(word) + else: + self._censor_list.remove(word) + + def remove_words(self, word_list, anywhere=True): + """ + Remove given list of words from censor list. + - anywhere (bool): + attempt removal from both the extra censor list and the custom/default censor list + - word_list (list): + the list of words to remove + """ + for word in word_list: + self.remove_word(word, anywhere=anywhere) def set_censor(self, character): """Replaces the original censor character '*' with ``character``.""" diff --git a/tests/test_profanity.py b/tests/test_profanity.py index 338f6dc..6929f3d 100644 --- a/tests/test_profanity.py +++ b/tests/test_profanity.py @@ -35,7 +35,7 @@ def test_censor(self): def test_define_words(self): # Testing pluralization here as well - pf.define_words(["unicorn", "chocolate"]) + pf.define_words(["unicorn", "chocolate", "centaur"]) update_censored() self.assertFalse("unicorns" in censored) self.assertFalse("chocolate" in censored) @@ -46,14 +46,29 @@ def test_append_words(self): update_censored() self.assertTrue("oranges" in censored) self.assertFalse("Hey" in censored) - self.assertFalse("like" in censored) self.assertFalse("Turd" in censored) def test_remove_word(self): self.assertTrue("Turd" in censored) + pf.append_words(["oranges", "potato"]) + update_censored() pf.remove_word("turd") + self.assertRaises(ValueError, pf.remove_word, "potato", anywhere=False) + pf.remove_word("oranges") update_censored() self.assertTrue("Turd" in censored) + self.assertFalse("oranges" in pf.get_profane_words()) + self.assertTrue("potato" in pf.get_profane_words()) + + def test_remove_words(self): + pf.define_words(["chocolate", "centaur"]) + pf.append_words(["potato", "racecar", "hey"]) + self.assertTrue("chocolate" in pf.get_profane_words()) + self.assertRaises(ValueError, pf.remove_words, ["chocolate", "racecar"], anywhere=False) + pf.remove_words(["centaur", "hey"]) + self.assertFalse("chocolate" in pf.get_profane_words()) + self.assertTrue("racecar" in pf.get_profane_words()) + self.assertFalse("centaur" in pf.get_profane_words()) def test_restore_words(self): pf.define_words(["cupcakes"])