From 3f5f2feff87786f878e886332a72af109925b80d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gerard=20Gasc=C3=B3n?= <52170489+GerardGascon@users.noreply.github.com> Date: Wed, 23 Apr 2025 16:24:22 +0200 Subject: [PATCH] feat: better profanity filtering --- requirements.txt | 1 - server/filters.py | 97 +++ server/views.py | 15 +- server/wordlist.txt | 1479 ++++++++++++++++++++++++++++++++++++ torretadeldrac/settings.py | 3 +- 5 files changed, 1586 insertions(+), 9 deletions(-) create mode 100644 server/filters.py create mode 100644 server/wordlist.txt diff --git a/requirements.txt b/requirements.txt index 3961a51..4662eb1 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,6 +1,5 @@ asgiref==3.8.1 Django==5.1.7 -django-profanity-filter==0.2.1 inflection==0.5.1 python-dotenv==1.0.1 sqlparse==0.5.3 diff --git a/server/filters.py b/server/filters.py new file mode 100644 index 0000000..613e4cd --- /dev/null +++ b/server/filters.py @@ -0,0 +1,97 @@ +import re +import os + +import inflection + + +class ProfanityFilter: + def __init__(self, **kwargs): + + # If defined, use this instead of _censor_list + self._custom_censor_list = kwargs.get('custom_censor_list', []) + + # Words to be used in conjunction with _censor_list + self._extra_censor_list = kwargs.get('extra_censor_list', []) + + # What to be censored -- should not be modified by user + self._censor_list = [] + + # What to censor the words with + self._censor_char = "*" + + # Where to find the censored words + self._BASE_DIR = os.path.abspath(os.path.dirname(__file__)) + self._words_file = os.path.join(self._BASE_DIR, 'wordlist.txt') + + self._load_words() + + def _load_words(self): + """ Loads the list of profane words from file. """ + with open(self._words_file, 'r') as f: + self._censor_list = [line.strip() for line in f.readlines()] + + def define_words(self, word_list): + """ Define a custom list of profane words. """ + self._custom_censor_list = word_list + + def append_words(self, word_list): + """ Extends the profane word list with word_list """ + self._extra_censor_list.extend(word_list) + + def set_censor(self, character): + """ Replaces the original censor character '*' with character """ + if isinstance(character, int): + character = str(character) + self._censor_char = character + + def has_bad_word(self, text): + """ Returns True if text contains profanity, False otherwise """ + return self.censor(text) != text + + def get_custom_censor_list(self): + """ Returns the list of custom profane words """ + return self._custom_censor_list + + def get_extra_censor_list(self): + """ Returns the list of custom, additional, profane words """ + return self._extra_censor_list + + def get_profane_words(self): + """ Gets all profane words """ + profane_words = [] + + if self._custom_censor_list: + profane_words = [w for w in self._custom_censor_list] # Previous versions of Python don't have list.copy() + else: + profane_words = [w for w in self._censor_list] + + profane_words.extend(self._extra_censor_list) + profane_words.extend([inflection.pluralize(word) for word in profane_words]) + profane_words = list(set(profane_words)) + + return profane_words + + def restore_words(self): + """ Clears all custom censor lists """ + self._custom_censor_list = [] + self._extra_censor_list = [] + + def censor(self, input_text): + """ Returns input_text with any profane words censored """ + bad_words = self.get_profane_words() + res = input_text + + for word in bad_words: + word = r'\b%s\b' % word # Apply word boundaries to the bad word + regex = re.compile(word, re.IGNORECASE) + res = regex.sub(self._censor_char * (len(word) - 4), res) + + return res + + def is_clean(self, input_text): + """ Returns True if input_text doesn't contain any profane words, False otherwise. """ + return not self.has_bad_word(input_text) + + def is_profane(self, input_text): + """ Returns True if input_text contains any profane words, False otherwise. """ + return self.has_bad_word(input_text) \ No newline at end of file diff --git a/server/views.py b/server/views.py index 070bb67..b188dd0 100644 --- a/server/views.py +++ b/server/views.py @@ -1,13 +1,20 @@ import json import random -import profanity.templatetags.profanity from django.db import IntegrityError from django.http import HttpResponse, Http404 +from server.filters import ProfanityFilter from server.models import Dragon +pf = ProfanityFilter() + + +def is_profane(value): + return pf.is_profane(value) + + def add(request, origin, name, color, shirt, hat, decor): origin = origin.strip() name = name.strip() @@ -16,11 +23,7 @@ def add(request, origin, name, color, shirt, hat, decor): hat = int(hat.strip()) decor = int(decor.strip()) - if profanity.templatetags.profanity.is_profane(name): - print("profanity detected") - return HttpResponse("profanity detected") - - if profanity.templatetags.profanity.is_profane(origin): + if is_profane(name) or is_profane(origin): print("profanity detected") return HttpResponse("profanity detected") diff --git a/server/wordlist.txt b/server/wordlist.txt new file mode 100644 index 0000000..83c5c7d --- /dev/null +++ b/server/wordlist.txt @@ -0,0 +1,1479 @@ +assfuck +cuntfucker +niggers +niggerhole +nigger +balllicker +nlgger +porchmonkey +Porch-monkey +cunt +asswhore +fuck +assjockey +Dothead +blacks +cumqueen +fatfucker +Jigaboo +jiggabo +nlggor +snownigger +Spearchucker +Timber-nigger +shitnigger +asslick +shithead +asshole +cuntlicker +kunt +spaghettinigger +Towel-head +Chernozhopy +asslicker +Bluegum +twat +ABCD +bitchslap +bulldyke +choad +cumshot +fatass +jigger +kyke +cumskin +asian +asscowboy +assmuncher +banging +Burrhead +Camel-Jockey +coon +crotchrot +cumfest +dicklicker +fag +fagot +felatio +fatfuck +goldenshower +hore +jackoff +jigg +jigga +jizjuice +jizm +jiz +jizzim +kumming +kunilingus +Moolinyan +motherfucking +motherfuckings +phuk +Sheboon +shitforbrains +slanteye +spick +fuuck +antinigger +aperest +Americoon +ABC +Aunt-Jemima +queer +anal +asspirate +addict +bitch +ass +Buddhahead +chode +phuking +phukking +bastard +bulldike +dripdick +assassination +A-rab +Buckra +bootycall +assholes +assbagger +cheesedick +cooter +cum +cumquat +cunnilingus +datnigga +deepthroat +dick +dickforbrains +dickbrain +dickless +dike +diddle +dixiedyke +Eskimo +fannyfucker +fatso +fckcum +Golliwog +Goyim +homobangers +hooters +Indognesial +Indonesial +jew +jijjiboo +knockers +kummer +mothafucka +mooncricket +Moon-Cricket +Oven-Dodger +Peckerwood +phuked +piccaninny +picaninny +phuq +Polock +poorwhitetrash +prick +pu55y +Pshek +slut +jizzum +cunteyed +Spic +Swamp-Guinea +stupidfucker +stupidfuck +titfuck +Twinkie +cock +Abeed +analannie +asshore +Beaner +Bootlip +Burr-head +buttfucker +butt-fucker +Uncle-Tom +cocksmoker +Africoon +AmeriKKKunt +antifaggot +assklown +asspuppies +blackman +jism +blumpkin +retard +Gringo +douchebag +Piefke +areola +backdoorman +Abbie +bigbutt +buttface +cumbubble +cumming +Dego +dong +doggystyle +doggiestyle +erection +feces +goddamned +gonzagas +Greaser +Greaseball +handjob +Half-breed +horney +jihad +kumquat +Lebo +Moskal +Mountain-Turk +nofuckingway +orgies +orgy +pecker +poontang +poon +Polentone +pu55i +shitfuck +shiteater +shitdick +sluts +slutt +Mangal +Hymie +stiffy +titfucker +twink +asspacker +barelylegal +beaner +Bozgor +bumfuck +shit for brains +butchdyke +butt-fuckers +buttpirate +cameljockey +Carcamano +Chankoro +Choc-ice +Chug +Ciapaty-or-ciapak +Cina +cocksucer +crackwhore +Bougnoule +unfuckable +Africoon-Americoon +Africoonia +Americunt +apesault +Assburgers +fucktardedness +sheepfucker +Wuhan-virus +Wetback +Aseng +bumblefuck +fastfuck +itch +nizzle +Oriental +cisgender +ballsack +penis +zigabo +Bule +breastman +bountybar +Bounty-bar +bondage +bombing +bullshit +asses +cancer +cunilingus +cummer +dicklick +ejaculation +faeces +fairy +hoes +idiot +Laowai +Leb +muff +muffdive +Oreo +orgasm +orgasim +osama +peepshow +Petrol-sniffer +perv +prickhead +shitfit +spermbag +suckmytit +suckmydick +suckmyass +suckme +suckdick +Yuon +motherfucker +groe +Ali Baba +retarded +assfucker +assmunch +assranger +Ayrab +assclown +buttfuck +butt-fuck +buttman +Chink +cocksucker +cooly +Coon-ass +crotchmonkey +Bohunk +cockcowboy +cocksmith +catfucker +fucktardedly +trans-testicle +Wigger +whiskeydick +aboriginal +asskisser +whitelist +Latinx +yambag +boob +beef curtains +clunge +af +wokeness +bitchez +Iceberg Fuckers +Zhyd +bellend +arsehole +tatas +assassinate +boonga +booby +bullcrap +defecate +Dhoti +dope +hobo +bigass +hussy +illegal +ky +moneyshot +molestor +nooner +nookie +nookey +Paleface +pansy +peehole +phonesex +period +pornking +pornflick +porn +pooper +sexwhore +shitface +shit +slav +slimeball +sniggers +snowback +spermherder +spankthemonkey +spitter +strapon +Tacohead +suckoff +titbitnipply +Turco-Albanian +tranny +trannie +zhidovka +zhid +Bakra +Afro engineering +Ah Chah +alligatorbait +arabs +Arabush +Ashke-Nazi +assblaster +assmonkey +badfuck +bazongas +beatoff +bazooms +Balija +bunghole +butchdike +buttfuckers +Boche +buttbang +butt-bang +buttmunch +Charlie +chav +Chinaman +coloured +boong +butchbabes +clit +cockknob +cocksucking +cocktease +Cokin +anchor-baby +cumsock +fisting +fuck-you +Fritzie +transgendered +White-trash +whitetrash +whop +wtf +Vatnik +welfare queen +assman +black +Gyopo +goddam +minge +punani +douche +doofus +munter +moron +ballgag +femsplaining +asslover +looney +Boonga +fat +homosexual +turd +zhydovka +effing +minger +dullard +buggery +brea5t +addicted +demon +devilworshipper +deth +destroy +doo-doo +doodoo +escort +farting +fairies +husky +incest +Hunky +jiggy +laid +molester +Mzungu +nigglings +niggling +niggles +pee-pee +pi55 +phungky +porno +pooping +prostitute +pros +sexslave +sextogo +shag +shithappens +shithapens +shitfull +shitcan +shinola +slavedriver +sleezeball +spermhearder +swastika +shits +trots +trisexual +twobitwhore +Munt +gangsta +Abo +addicts +Alligator bait +analsex +Redskin +Gypsy +Ang mo +Ape +arab +Aravush +Armo +arse +asswipe +Beaney +beatyourmeat +bigbastard +bitches +Bogtrotter +bung +beaver +bestial +bogan +Cabbage-Eater +carpetmuncher +carruth +cocklover +cockrider +cornhole +bollock +Bog-Irish +chinamen +clamdigger +clamdiver +dwarf +cakewalk +ftw +fml +handicapped +cawk +carpet-muncher +fuzzy-headed +full-blood +fuckity-bye +frogess +Norte +troid +willy +pud +pubiclice +whitewashing +Brit +a55 +anus +ar5e +arrse +a$$ +ar$e +ass-fucker +assfukka +asswhole +a_s_s +b!tch +b00bs +b17ch +b1tch +ballbag +balls +beastial +beastiality +bestiality +bi\+ch +biatch +bitcher +bitchers +bitchin +bitching +bloody +blow job +blowjob +blowjobs +boiolas +bollok +boner +boobs +booobs +boooobs +booooobs +booooooobs +breasts +buceta +bugger +bunny fucker +butthole +buttmuch +buttplug +c0ck +c0cksucker +carpet muncher +chink +cipa +cl1t +clitoris +clits +cnut +cock-sucker +cock sucker +cockface +cockhead +cockmunch +cockmuncher +cocks +cocksuck +cocksucked +cocksucks +cocksuka +cocksukka +cok +cokmuncher +coksucka +cox +crap +cummies +cummiez +cums +cunillingus +cuntlick +cuntlicking +cunts +cunt-muncher +cuntmuncher +cuntmunch +cyalis +cyberfuc +cyberfuck +cyberfucked +cyberfucker +cyberfuckers +cyberfucking +d1ck +damn +dickhead +dildo +dildos +dink +dinks +dirsa +dlck +dog-fucker +doggin +dogging +donkeyribber +doosh +duche +dyke +ejaculate +ejaculated +ejaculates +ejaculating +ejaculatings +ejakulate +f u c k +f u c k e r +f4nny +fagging +faggitt +faggot +faggs +fagots +fags +fanny +fannyflaps +fanyy +fcuk +fcuker +fcuking +feck +fecker +felching +fellate +fellatio +fingerfuck +fingerfucked +fingerfucker +fingerfuckers +fingerfucking +fingerfucks +fistfuck +fistfucked +fistfucker +fistfuckers +fistfucking +fistfuckings +fistfucks +flange +fook +fooker +fucka +fucked +fucker +fuckers +fuckhead +fuckheads +fuckin +fucking +fcking +fck +fuckings +fuckingshitmotherfucker +fuckme +fucks +fuckwhit +fuckwit +fudge packer +fudgepacker +fuk +fuker +fukker +fukkin +fuks +fukwhit +fukwit +fux +fux0r +f_u_c_k +gangbang +gangbanged +gangbangs +gaylord +gaysex +goatse +God +god-dam +god-damned +goddamn +hardcoresex +hell +heshe +hoar +hoare +hoer +homo +horniest +horny +hotsex +jack-off +jap +jerk-off +jizz +kawk +knob +knobead +knobed +knobend +knobhead +knobjocky +knobjokey +kock +kondum +kondums +kum +kums +l3i\+ch +l3itch +labia +lmfao +lust +lusting +m0f0 +m0fo +m45terbate +ma5terb8 +ma5terbate +masochist +master-bate +masterb8 +masterbat +masterbat3 +masterbate +masterbation +masterbations +masturbate +mo-fo +mof0 +mofo +mothafuck +mothafuckas +mothafuckaz +mothafucked +mothafucker +mothafuckers +mothafuckin +mothafucking +mothafuckings +mothafucks +motherfuck +motherfucked +motherfuckers +motherfuckin +motherfuckka +motherfucks +mutha +muthafecker +muthafuckker +muther +mutherfucker +n1gga +n1gger +nazi +nigg3r +nigg4h +nigga +niggah +nigguh +niggas +niggaz +niggerz +nob +nobhead +nobjocky +nobjokey +numbnuts +nutsack +orgasims +orgasms +p0rn +pawn +penisfucker +phuck +phukked +phuks +pigfucker +pimpis +piss +pissed +pisser +pissers +pisses +pissflaps +pissin +pissing +pissoff +poop +pornography +pornos +pron +pube +pusse +pussi +pussies +pussy +rectum +rimjaw +rimming +s.o.b. +sadist +schlong +screwing +scroat +scrote +scrotum +semen +sex +sh!\+ +sh!t +sh1t +shagger +shaggin +shagging +shemale +shi\+ +shite +shited +shitey +shiting +shitted +shitter +shitting +shitty +skank +smegma +smut +snatch +son-of-a-bitch +spac +spunk +s_h_i_t +t1tt1e5 +t1tties +teets +teez +testical +testicle +tit +tits +titt +tittie5 +tittiefucker +titties +tittyfuck +tittywank +titwank +tosser +tw4t +twathead +twatty +twunt +twunter +v14gra +v1gra +vagina +viagra +vulva +w00se +wang +wank +wanker +wanky +whoar +whore +willies +xrated +xxx +puta +cabron +c4bron +cabrOn +abanto +abrazafarolas +acabada +adefesio +adufe +afilar +agarraderas +aguayón +alcornoque +alfeñique +andá +andorga +andurriasmo +aniz +argolla +arrastracueros +arrecho +artabán +Asesinato +asno +atarre +avergallón +ayotes +baboso +balurde +barrabás +barriobajero +bastardo +bebecharcos +bellaco +belloto +berzotas +besugo +bizcocho +bobalicón +bocabuzón +bocachancla +bocallanta +bolas +Bollera +bollito +bollo +boludo +bombín +boniato +boquimuelle +borondanga +borraja +borrico +bostazo +botarate +brisco +brizna +brocha +bruta +bucha +bufarrón +bujarra +bujarrón +burondanga +butifarra +cabecipelao +cabestro +cabezaalberca +cabezabuque +cabro +cabron +Cabron +cabrón +Cabrón +Caca +cacha +cachapera +cachar +cacheteo +cachibache +cachilo +cachimba +cachufleta +cafre +cagada +cagado +cagalindes +cagar +cagarruta +cagarse +cajeta +calabazo +calambuco +calamidad +caldúo +calientahielos +caliente +calzamonas +cansalmas +cantamañanas +cantimplora +capullo +caracaballo +caracartón +caraculo +caraflema +carajaula +carajote +carapapa +carapijo +casquete +cazurro +cebollino +cenizo +cenutrio +ceporro +cernícalo +cerote +chacón +chancay +chancla +chapero +chaqueta +charrán +chiche +chichi +chifar +chimar +chinga a madre +chinga tu madre +chingar +chingado +chiquilicuatre +chirimbaina +chocho +chucha +chupacables +Chupada +Chupapollas +chuparla +chupasangre +chuperson +Chupetón +chupóptero +cierrabares +cipote +clavar +cocheche +cocones +cocos +coger +cogollo +cogote +cojido +cojones +cola +colgados +colgazón +colipato +colisón +collofe +colloma +colón +comadres +come +comebolsas +comechapas +comeflores +comemierda +comer +comer bistec +comestacas +compañero +compañeros +concha +Concha de tu madre +conchudo +conejo +conejos +coño +copa +copetón +Coprofagía +coquimbanos +corneta +cornudo +coronta +corota +corotas +corozo +correa +corrida +coso +cotorra +coyoles +coyones +coyos +cretino +crica +cuacha +cuaresmeño +cuates +cuca +cuchara +cuchillo +cucona +cueco +cuerpoescombro +culamen +culeado +culear +culero +Culo +culopollo +curtir +cuyones +descerebrado +desgarracalzas +desgraciarse +dondiego +donnadie +echacantos +ejarramantas +empiernarse +emputado +emputecido +energúmeno +ensartar +esbaratabailes +escolimoso +escornacabras +estaca +estafiate +estulto +estupido +exprimir +fachar +fanfosquero +fantoche +fariseo +fiesta de salchichas +fifar +filiberto +filimincias +fleto +foligoso +follador +follar +forrar +forro +fulastre +fundillo +gañán +ganapán +ganapio +gandúl +garchar +garompa +gaznápiro +gilipichis +gilipollas +gilipuertas +giraesquinas +gomas +gorrino +gorrumino +guaranga +güebo +guitarro +gurriato +gustarle +habahelá +hacer una mamada +hacer una paja +haciendo el amor +heroína +hija de puta +hijo de mil putas +hijo de puta +hueco +huelegateras +huevo +huevón +huevos +idiota +imbécil +impermeable +infierno +jilipollas +joder +kapullo +la puta que te pario +ladilla +lamecharcos +lameculos +lameplatos +leche +lechuguino +lerdo +letrín +lloramigas +lolas +longanizas +lumbreras +maciza +macizorra +maganto +majadero +malasangre +malasombra +maldito +maletón +malparido +mama huevos +mamada +mamaguevo +mamar +mamarla +mameluco +mamey +mamon +mamplora +mamporrero +manchas +manegueta +mangarrán +mangurrián +maraco +marica +maricon +maricón +mariconazo +mariposa +mariposón +marricon +martillo +marujo +mastuerzo +masturbación +matacandiles +mayate +meapilas +mecos +melón +melones +mendrugo +mentecato +mequetrefe +merluzo +metemuertos +metijaco +mico +mierda +mindundi +minga +mojar +mojar el bizcocho +mojar el chingolo +montar +morfarse +morlaco +morroestufa +morrongo +muerdesartenes +nabo +naco +Ñema +ninfo +Ñorro +ojete +orate +orina +orto +ovejo +pagafantas +paja +pájaro +pajeado +pajearse +pajero +palo +palurdo +pamplinas +panarra +panocha +panoli +papafrita +papallona +papanatas +papaya +papayona +papirote +paquete +pardillo +parguela +pasmarote +pasmasuegras +pata +pataliebre +patán +pato +pavitonto +pazguato +pecholata +pechugas +pedazo +pedo +pedorro +peinabombillas +peinaovejas +pelagallos +pelagambas +pelagatos +pelatigres +pelazarzas +pelele +pelma +pelotas +pelotudo +pendejo +pepa +percebe +perrocostra +perroflauta +pervertido +peterete +petimetre +pezón +picapleitos +picha +pichabrava +pico +pija +pijo +pillavispas +piltrafa +pinchauvas +pinche +pinga +pintamonas +piojoso +pipe +pipían +pis +pisar +pistola +pitañoso +pito +pitofloro +playo +plomo +pocasluces +polla +pollopera +polvo +popa +poronga +poto +prostituta +puchero +puta +put4 +put0 +puto +quebracho +quebrachón +quitahipos +racista +raja +ramera +rastrapajo +reata +rebañasandías +remame +repisas +retazo +revientabaules +riata +ríeleches +rifle +robaperas +rulacho +sabandija +sacamuelas +sádico +sanguijuela +semen +seno +senos +sexo +sexo oral +sinentraero +singar +sinsustancia +sobársela +sobo +sonajas +sonso +sopladores +soplagaitas +soplaguindas +soplanucas +soplapollas +sorete +sosco +tagarote +tajarse +tanate +tarado +tarugo +tereso +tetas +tetas grandes +tía buena +tiralevitas +tirar +tirarse +tocapelotas +tocho +tolai +tolete +tompeates +tonga +tontaco +tontucio +tordo +tortillera +traga +tragaldabas +tragasables +transar +travesti +trincar +trio +trola +trolo +tuercebotas +tunante +vejiga +verga +vergallito +vergallo +vete +vete a la mierda +volar +volarse +voltearse la rosca +voltearse la tortilla +vulva +yegua +zamacuco +zambombo +zampabollos +zamugo +zángano +zarrapastroso +zascandil +zopenco +zoquete +zorra +zote +zullenco +zurcefrenillos \ No newline at end of file diff --git a/torretadeldrac/settings.py b/torretadeldrac/settings.py index e7d4104..08b07d8 100644 --- a/torretadeldrac/settings.py +++ b/torretadeldrac/settings.py @@ -46,8 +46,7 @@ INSTALLED_APPS = [ 'django.contrib.messages', 'django.contrib.staticfiles', - 'server', - 'profanity' + 'server' ] MIDDLEWARE = [