Aug-12-2021, 04:25 PM
Sounds like you are trying to filter out bad words and bad words only. If that is the case, regex is probably the way to go. I made the following snippet (warning: may content offensive material, but kinda need to do it to illustrate my point):
The above example assumes phrases like 'hellwtf' constitutes bad words and needs to be filtered out, whereas 'hellwtfs' is okay.
import re string = ['assassin', 'title','titassembled', 'ass', 'tit', 'asstit', 'hello'] def find_bad_word(string): bad_word_list = ['ass', 'tit'] bad_iter = iter(bad_word_list) badword = next(bad_iter) cleaned = re.sub(badword, '', string) if len(cleaned) == 0 or cleaned == badword: return 'found bad word' while len(cleaned) != 0 or badword in cleaned: try: badword = next(bad_iter) cleaned = re.sub(badword, '', cleaned) if len(cleaned) == 0 or cleaned == badword: return 'found bad word' break except StopIteration: return string for i in string: print(find_bad_word(i))The above will print:
Output:assassin
title
titassembled
found bad word
found bad word
found bad word
hello
I am sure there is a more elegant way to do it but its past midnight now so yeah.....The above example assumes phrases like 'hellwtf' constitutes bad words and needs to be filtered out, whereas 'hellwtfs' is okay.