From 2fc5ef809c631d62ecf219f7c67a4b8a946b7da5 Mon Sep 17 00:00:00 2001 From: balerion7 Date: Mon, 4 Jan 2016 21:33:48 -0600 Subject: [PATCH 1/3] modified: README.md modified: profanity_filter.py new file: test_profanity_filter.py --- README.md | 33 ++++++++--- profanity_filter.py | 116 +++++++++++++++++++++++++++++++++------ test_profanity_filter.py | 62 +++++++++++++++++++++ 3 files changed, 188 insertions(+), 23 deletions(-) create mode 100644 test_profanity_filter.py diff --git a/README.md b/README.md index c4991fb..20895b4 100644 --- a/README.md +++ b/README.md @@ -3,10 +3,29 @@ profanity-filter Python module that replaces inappropriate words with something more PG rated. -Usage ------ -```python -f = Filter('badword and bad words', clean_word='unicorn') -safe_string = f.clean() -print safe_string -``` \ No newline at end of file +Uses a line separated file listing bad words as it's source +to check if a user submitted something inappropriate. + +Code modified from: https://github.com/jared-mess/profanity-filter + +Modified by: jjb +Date 1/2/2016 + +Example of Code in test_profanity_filter.py + +Example Use: +text = "Cassandra is a fuCking piece of shit_on_a_long_stick" +f=Filter(text, "HAPPY") +f.clean_anywhere() +f.clean_start() +f.clean_whole_word() + +Example Output--- +Original: +Cassandra is a fuCking piece of shit_on_a_long_stick +Output from clean_anywhere: +CHAPPYandra is a HAPPYing piece of HAPPY_on_a_long_stick +Output from clean_start: +Cassandra is a HAPPYing piece of HAPPY_on_a_long_stick +Output from clean whole word: +Cassandra is a HAPPY piece of shit_on_a_long_stick diff --git a/profanity_filter.py b/profanity_filter.py index a2c7887..05dfc42 100644 --- a/profanity_filter.py +++ b/profanity_filter.py @@ -1,30 +1,114 @@ """ Uses a line separated file listing bad words as it's source -to check if a user submitted something inappropriate +to check if a user submitted something inappropriate. +Code modified from: https://github.com/jared-mess/profanity-filter -f = Filter('slut', clean_word='unicorn') -word = f.clean() -print word ->>slut +Modified by: Jeremy Becnel +Date 1/2/2016 + +Example of Code in test_profanity_filter.py + +Example Output--- +Original: +Cassandra is a fuCking piece of shit_on_a_long_stick +Output from clean_anywhere: +CHAPPYandra is a HAPPYing piece of HAPPY_on_a_long_stick +Output from clean_start: +Cassandra is a HAPPYing piece of HAPPY_on_a_long_stick +Output from clean whole word: +Cassandra is a HAPPY piece of shit_on_a_long_stick """ import re +# bad word file location and and name +badwordfile = 'bad_words.txt' + + class Filter(object): """ - Replaces a bad word in a string with something more PG friendly - - Filter('you annoying prick', 'unicorn') + Class is desigend to take a string and clean it up by replacing instances of "bad" words + with a more acceptable word. """ - def __init__(self, original_string, clean_word='****'): + + # class variable containing all the bad words we are looking for + bad_words = set(line.strip('\n') for line in open(badwordfile)) + + def __init__(self, original_string, replacement_string='****'): - bad_words_file = open('bad_words.txt', 'r') + #bad_words_file = open('bad_words.txt', 'r') - self.bad_words = set(line.strip('\n') for line in open('bad_words.txt')) - self.original_string = original_string - self.clean_word = clean_word - def clean(self): - exp = '(%s)' %'|'.join(self.bad_words) + #cls.bad_words = ['ass','fuck', 'shit' ] # used for testing + self.original_string = original_string + self.replacement_string = replacement_string + self.profanity_found = None + self.__has_been_cleaned = False + self.clean_string = None + +#===================================INSTANCE METHODS + +#---------------------------standard get set methods with some error checking + + def get_original_string(self): + return self.original_string + + + + def get_replacement_string(self): + return self.replacement_string + + def is_profanity_found(self): + # check to see if a cleaning has been performed. + assert (self.__has_been_cleaned), "Word must be cleaned before this can be determined." + # after a clean is performed this method can be used to to determine if + # profanity was found + return self.profanity_found + + def get_clean_string(self): + # check to see if a cleaning has been performed. + assert (self.__has_been_cleaned), "Word must be cleaned before a clean string can be returned." + # after a clean is performed this method can be used to to determine if + # profanity was found + return self.clean_string + +#------------------------------cleaners + +# The methods below are instance methods that cleans the given string according +# to different rules. + + def __clean(self,exp): r = re.compile(exp, re.IGNORECASE) - return r.sub(self.clean_word, self.original_string) + # check for any profanity in the string + self.profanity_found = (r.search(self.original_string) != None) + # return the original string where the replacements string has been substituted for the profanity + self.clean_string = r.sub(self.replacement_string, self.original_string) + self.__has_been_cleaned = True + return self.clean_string + + # cleans profanity found anywhere in the word + # example with #cls.bad_words = ['ass','fuck', 'shit' ] # used for testing: + # cleans "Cassandra Fuck Off you shithead" + # as 'C****andra **** Off you ****head' + def clean_anywhere(self): + exp = '(%s)' %'|'.join(Filter.bad_words) + return self.__clean(exp) + + + # requires blank at beginning and end of word, i.e. word must start with profanity + # example with #cls.bad_words = ['ass','fuck', 'shit' ] # used for testing: + # cleans "Cassandra Fuck Off you shithead" + # as 'Cassandra **** Off you ****head' + def clean_start(self): + exp = '(\\b%s)' %'|\\b'.join(Filter.bad_words) + return self.__clean(exp) + + + # requires blank at beginning and end of word, i.e. will match whole word only + # example with #cls.bad_words = ['ass','fuck', 'shit' ] # used for testing: + # cleans "Cassandra Fuck Off you shithead" + # as 'Cassandra **** Off you shithead' + def clean_whole_word(self): + exp = '(\\b%s\\b)' %'\\b|\\b'.join(Filter.bad_words) + return self.__clean(exp) + diff --git a/test_profanity_filter.py b/test_profanity_filter.py new file mode 100644 index 0000000..9a253ab --- /dev/null +++ b/test_profanity_filter.py @@ -0,0 +1,62 @@ +from profanity_filter import Filter + +text = "Cassandra is a fuCking piece of shit_on_a_long_stick" + +print "We first test the three cleaning methods on the message:" +print text +print + +f=Filter(text, "HAPPY") +print "Output from clean_anywhere:" +print f.clean_anywhere() +print "Output from clean_start:" +print f.clean_start() +print "Output from clean whole word:" +print f.clean_whole_word() + + +print + +text = "Cassy is an asset to our company." +print "We now test the profanity check with the following:" +print text +print "Output from clean_anywhere:" + +f=Filter(text,'DADA') +f.clean_anywhere() +if not f.is_profanity_found(): + print "No profanity." +else: + print "Here is the clean anywhere:" + print f.get_clean_string() + + +print +#f=Filter(text,'DADA') +print "Output from clean_start:" +print f.clean_start() +if not f.is_profanity_found(): + print "No profanity." +else: + print "Here is the clean string:" + print f.get_clean_string() + +print +#f=Filter(text,'DADA') +print "Output from clean whole word:" +print f.clean_whole_word() +if not f.is_profanity_found(): + print "No profanity." +else: + print "Here is the clean string:" + print f.get_clean_string() + + + + + +print "We now make sure the error handling is working." +f = Filter(text, "SAD") +#print f.is_profanity_found() +print f.get_clean_string() + From 20d5a5a4607d160a4f4d9fd5aa3e207c6f44e45e Mon Sep 17 00:00:00 2001 From: balerion7 Date: Tue, 5 Jan 2016 09:51:47 -0600 Subject: [PATCH 2/3] modified: README.md modified: profanity_filter.py modified: test_profanity_filter.py --- README.md | 6 ++++-- profanity_filter.py | 9 ++------- test_profanity_filter.py | 10 ++++------ 3 files changed, 10 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 20895b4..476edcc 100644 --- a/README.md +++ b/README.md @@ -6,12 +6,14 @@ Python module that replaces inappropriate words with something more PG rated. Uses a line separated file listing bad words as it's source to check if a user submitted something inappropriate. -Code modified from: https://github.com/jared-mess/profanity-filter +Code modified from orginal by Jared Mess Modified by: jjb Date 1/2/2016 -Example of Code in test_profanity_filter.py +Example of code use in test_profanity_filter.py: +Run +$ python test_profnaity_filter.py Example Use: text = "Cassandra is a fuCking piece of shit_on_a_long_stick" diff --git a/profanity_filter.py b/profanity_filter.py index 05dfc42..0a7db64 100644 --- a/profanity_filter.py +++ b/profanity_filter.py @@ -26,8 +26,8 @@ class Filter(object): """ - Class is desigend to take a string and clean it up by replacing instances of "bad" words - with a more acceptable word. + Class is desigend to take a string and clean it up by replacing + instances of "bad" words with a more acceptable word. """ @@ -36,9 +36,6 @@ class Filter(object): def __init__(self, original_string, replacement_string='****'): - #bad_words_file = open('bad_words.txt', 'r') - - #cls.bad_words = ['ass','fuck', 'shit' ] # used for testing self.original_string = original_string self.replacement_string = replacement_string @@ -53,8 +50,6 @@ def __init__(self, original_string, replacement_string='****'): def get_original_string(self): return self.original_string - - def get_replacement_string(self): return self.replacement_string diff --git a/test_profanity_filter.py b/test_profanity_filter.py index 9a253ab..ae00a3c 100644 --- a/test_profanity_filter.py +++ b/test_profanity_filter.py @@ -22,7 +22,7 @@ print text print "Output from clean_anywhere:" -f=Filter(text,'DADA') +f=Filter(text,'HAPPY') f.clean_anywhere() if not f.is_profanity_found(): print "No profanity." @@ -32,7 +32,6 @@ print -#f=Filter(text,'DADA') print "Output from clean_start:" print f.clean_start() if not f.is_profanity_found(): @@ -42,7 +41,6 @@ print f.get_clean_string() print -#f=Filter(text,'DADA') print "Output from clean whole word:" print f.clean_whole_word() if not f.is_profanity_found(): @@ -55,8 +53,8 @@ -print "We now make sure the error handling is working." -f = Filter(text, "SAD") +#print "We now make sure the error handling is working. An error should occur" +#f = Filter(text, "SAD") #print f.is_profanity_found() -print f.get_clean_string() +#print f.get_clean_string() From f6b6371c53f306324b273e0afde9b0acc9bba783 Mon Sep 17 00:00:00 2001 From: balerion7 Date: Fri, 14 Apr 2023 10:59:15 -0500 Subject: [PATCH 3/3] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 476edcc..67b184b 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ profanity-filter ================ -Python module that replaces inappropriate words with something more PG rated. +Python module that replaces inappropriate words with something more PG rated. Used in project that allows individuals to text message to sign used in Xmas decorations. Uses a line separated file listing bad words as it's source to check if a user submitted something inappropriate.