X-Git-Url: https://wannabe.guru.org/gitweb/?a=blobdiff_plain;f=profanity_filter.py;h=c64a2ec4cc04664efe5b7656c8eee08549494ac0;hb=1ae196e65426615856e81dcbac47eb9c473c49e0;hp=22de26395bcf188a3300ecfdeec193246d233ded;hpb=c901f3eb1acf78fd4933d8faeedc517ccafe627e;p=python_utils.git diff --git a/profanity_filter.py b/profanity_filter.py index 22de263..c64a2ec 100755 --- a/profanity_filter.py +++ b/profanity_filter.py @@ -1,5 +1,7 @@ #!/usr/bin/env python3 +"""A helper to identify and optionally obscure some bad words.""" + import logging import random import re @@ -12,12 +14,13 @@ from nltk.stem import PorterStemmer import decorator_utils import string_utils - logger = logging.getLogger(__name__) @decorator_utils.singleton class ProfanityFilter(object): + """A helper to identify and optionally obscure some bad words.""" + def __init__(self): self.bad_words = set( [ @@ -238,6 +241,13 @@ class ProfanityFilter(object): 'girl gone wild', 'girl on top', 'girl on', + 'give head', + 'giving head', + 'gave head', + 'gave you head', + 'gave him head', + 'gave them head', + 'gave us head', 'goatcx', 'goatse', 'goddamn', @@ -497,7 +507,8 @@ class ProfanityFilter(object): chunks = [self.stemmer.stem(word) for word in nltk.word_tokenize(result)] return ' '.join(chunks) - def tokenize(self, text: str): + @staticmethod + def tokenize(text: str): for x in nltk.word_tokenize(text): for y in re.split(r'\W+', x): yield y @@ -516,24 +527,24 @@ class ProfanityFilter(object): False """ - words = [word for word in self.tokenize(text)] + words = list(self.tokenize(text)) for word in words: if self.is_bad_word(word): - logger.debug(f'"{word}" is profanity') + logger.debug('"%s" is profanity', word) return True if len(words) > 1: for bigram in string_utils.ngrams_presplit(words, 2): bigram = ' '.join(bigram) if self.is_bad_word(bigram): - logger.debug(f'"{bigram}" is profanity') + logger.debug('"%s" is profanity', bigram) return True if len(words) > 2: for trigram in string_utils.ngrams_presplit(words, 3): trigram = ' '.join(trigram) if self.is_bad_word(trigram): - logger.debug(f'"{trigram}" is profanity') + logger.debug('"%s" is profanity', trigram) return True return False @@ -561,7 +572,7 @@ class ProfanityFilter(object): break return out - words = self.tokenize(text) + words = list(self.tokenize(text)) words.append('') words.append('') words.append('')