summaryrefslogtreecommitdiff
path: root/profanity_filter.py
diff options
context:
space:
mode:
authorScott <[email protected]>2022-01-12 20:58:40 -0800
committerScott <[email protected]>2022-01-12 20:58:40 -0800
commita38d345b8b9348bab10c3e359997aadad814a6a1 (patch)
tree4adef76153a56d8cf86f94cecf639eed72689e6d /profanity_filter.py
parentf33ea8b6b0f7dd862ae24f49a7e834c2bbb3d4dd (diff)
Adds doctests.
Diffstat (limited to 'profanity_filter.py')
-rwxr-xr-xprofanity_filter.py30
1 files changed, 30 insertions, 0 deletions
diff --git a/profanity_filter.py b/profanity_filter.py
index fe54221..db014e1 100755
--- a/profanity_filter.py
+++ b/profanity_filter.py
@@ -470,6 +470,18 @@ class ProfanityFilter(object):
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 = result.replace('0', 'o')
@@ -485,6 +497,19 @@ class ProfanityFilter(object):
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):
@@ -513,7 +538,10 @@ class ProfanityFilter(object):
)
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 = ''
@@ -556,6 +584,8 @@ class ProfanityFilter(object):
def main() -> None:
+ import doctest
+ doctest.testmod()
pf = ProfanityFilter()
phrase = ' '.join(sys.argv[1:])
print(pf.contains_bad_word(phrase))