import os import json import re class Wordfilter: def __init__(self): # json is in same directory as this class, given by __location__. self.blocklist = set() __location__ = os.path.realpath( os.path.join(os.getcwd(), os.path.dirname(__file__))) with open(os.path.join(__location__, 'badwords.json')) as f: self.add_words(json.loads(f.read())) def blocked(self, text): words = set(re.split(r'\W+', text)) return any(word.casefold() in self.blocklist for word in words) def add_words(self, words): self.blocklist |= set(word.casefold() for word in words)