Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved runtime of has_bad_word #19

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions profanityfilter/profanityfilter.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import os
import re

import string
import inflection


Expand Down Expand Up @@ -66,7 +66,15 @@ def set_censor(self, character):

def has_bad_word(self, text):
"""Returns True if text contains profanity, False otherwise."""
return self.censor(text) != text
prof = {}
for word in self.get_profane_words():
prof[word.lower()] = True
for word in text.split():
lower = word.lower()
without_punc = ''.join(ch for ch in lower if ch not in set(string.punctuation))
if prof.get(lower, False) or prof.get(without_punc, False):
return True
return False

def get_custom_censor_list(self):
"""Returns the list of custom profane words."""
Expand Down