projects
/
python_utils.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Make the new cmd_showing_output select and display data from stderr in
[python_utils.git]
/
profanity_filter.py
diff --git
a/profanity_filter.py
b/profanity_filter.py
index 5621cef94489f6b5446a9e786777a8cb93e68be4..db014e1704742c7cab01bc6e7ca1f6ca7f874de5 100755
(executable)
--- a/
profanity_filter.py
+++ b/
profanity_filter.py
@@
-347,6
+347,7
@@
class ProfanityFilter(object):
'poop chute',
'poopchute',
'porn',
'poop chute',
'poopchute',
'porn',
+ 'pron',
'pornhub',
'porno',
'pornographi',
'pornhub',
'porno',
'pornographi',
@@
-469,8
+470,25
@@
class ProfanityFilter(object):
self.stemmer = PorterStemmer()
def _normalize(self, text: str) -> str:
self.stemmer = PorterStemmer()
def _normalize(self, text: str) -> str:
+ """Normalize text.
+
+ >>> _normalize('Tittie5')
+ 'titties'
+
+ >>> _normalize('Suck a Dick!')
+ 'suck a dick'
+
+ >>> _normalize('fucking a whore')
+ 'fuck a whore'
+
+ """
result = text.lower()
result = result.replace("_", " ")
result = text.lower()
result = result.replace("_", " ")
+ result = result.replace('0', 'o')
+ result = result.replace('1', 'l')
+ result = result.replace('4', 'a')
+ result = result.replace('5', 's')
+ result = result.replace('3', 'e')
for x in string.punctuation:
result = result.replace(x, "")
chunks = [
for x in string.punctuation:
result = result.replace(x, "")
chunks = [
@@
-479,6
+497,19
@@
class ProfanityFilter(object):
return ' '.join(chunks)
def contains_bad_word(self, text: str) -> bool:
return ' '.join(chunks)
def contains_bad_word(self, text: str) -> bool:
+ """Returns True if text contains a bad word (or more than one)
+ and False if no bad words were detected.
+
+ >>> contains_bad_word('fuck you')
+ True
+
+ >>> contains_bad_word('FucK u')
+ True
+
+ >>> contains_bad_word('FuK U')
+ False
+
+ """
words = nltk.word_tokenize(text)
for word in words:
if self.is_bad_word(word):
words = nltk.word_tokenize(text)
for word in words:
if self.is_bad_word(word):
@@
-507,7
+538,10
@@
class ProfanityFilter(object):
)
def obscure_bad_words(self, text: str) -> str:
)
def obscure_bad_words(self, text: str) -> str:
+ """Obscure bad words that are detected by inserting random punctuation
+ characters.
+ """
def obscure(word: str):
out = ''
last = ''
def obscure(word: str):
out = ''
last = ''
@@
-550,6
+584,8
@@
class ProfanityFilter(object):
def main() -> None:
def main() -> None:
+ import doctest
+ doctest.testmod()
pf = ProfanityFilter()
phrase = ' '.join(sys.argv[1:])
print(pf.contains_bad_word(phrase))
pf = ProfanityFilter()
phrase = ' '.join(sys.argv[1:])
print(pf.contains_bad_word(phrase))