yum/FastTextPager
Compressed text paging over OSC.
git clone https://git.yummers.dev/yum/FastTextPager
7fb9c57
master
1#!/usr/bin/env python3 2 3class ProfanityFilter : 4def __init__ (self ,en_path :str ): 5self .en_path = en_path 6self .en_profanity = set () 7 8def load (self ): 9with open (self .en_path ,'r' )as f : 10for line in f : 11self .en_profanity .add (line .strip ()) 12 13def filter (self ,line :str ,language_code :str = "en" )-> str : 14filtered = "" 15 16if language_code not in {"en" }: 17raise ValueError (f"Language code \" { language_code } \" is " + 18"unsupported by the profanity filter" ) 19 20# Translation table converting vowels to asterisks. 21vowel_to_asterisk = str .maketrans ('aeiouAEIOU' ,'**********' ) 22 23result = [] 24for word in line .split (): 25word_clean = word .lower () 26# Filter out non-alphabet characters from the word. 27word_clean = '' .join ([char for char in word_clean if char .isalpha ()]) 28if word_clean in self .en_profanity : 29result .append (word .translate (vowel_to_asterisk )) 30else : 31result .append (word ) 32 33return " " .join (result ) 34 35if __name__ == "__main__" : 36en_path = "/mnt/d/vrc/TaSTT/GUI/Profanity/Profanity/en" 37p = ProfanityFilter (en_path ) 38p .load () 39assert (p .filter ("fuck" )== "f*ck" ) 40assert (p .filter ("fuck!" )== "f*ck!" ) 41assert (p .filter ("fuck shit" )== "f*ck sh*t" ) 42assert (p .filter ("fuck shit this should not be filtered" )== "f*ck sh*t this should not be filtered" ) 43assert (p .filter ("ASS" )== "*SS" )