Compact slurs dict and added support for word censor

This commit is contained in:
Yo Mama 2021-10-16 20:42:00 +02:00
parent 3fe170f012
commit 0de6608408
3 changed files with 91 additions and 150 deletions

View file

@ -7,10 +7,10 @@ from files.helpers.const import SLURS
def create_replace_map():
dicts = [{
slur: replacer,
slur.title(): replacer.title(),
slur.capitalize(): replacer.capitalize(),
slur.upper(): replacer.upper(),
slur.strip(): replacer,
slur.strip().title(): replacer.title(),
slur.strip().capitalize(): replacer.capitalize(),
slur.strip().upper(): replacer.upper(),
} for (slur, replacer) in SLURS.items()]
# flattens the list of dict to a single dict
@ -21,22 +21,33 @@ REPLACE_MAP = create_replace_map()
def create_variations_slur_regex(slur: str):
variations = [slur, slur.upper(), slur.capitalize()]
stripped = slur.strip()
variations = [stripped, stripped.upper(), stripped.capitalize()]
# capitalize multiple words if there are multiple words (just in case)
if " " in slur:
variations.append(slur.title())
if " " in stripped:
variations.append(stripped.title())
return [rf"(\s|>)({var})|({var})(\s|<)" for var in variations]
if slur.startswith(" ") and slur.endswith(" "):
return [rf"(\s|>)({var})(\s|<)" for var in variations]
else:
return [rf"(\s|>)({var})|({var})(\s|<)" for var in variations]
def sub_matcher(match: Match):
found = match.group(2) if (match.group(2) is not None) else match.group(3)
replacer = REPLACE_MAP[found]
return (match.group(1) or '') + replacer + (match.group(4) or '')
# special case when it should match exact word
if len(match.groups()) is 3:
found = match.group(2)
replacer = REPLACE_MAP[found]
return match.group(1) + replacer + match.group(3)
else: # normal case with prefix or suffix
found = match.group(2) if (match.group(2) is not None) else match.group(3)
replacer = REPLACE_MAP[found]
return (match.group(1) or '') + replacer + (match.group(4) or '')
def censor_slurs(v, body):
def censor_slurs(v, body: str):
if v and not v.slurreplacer:
return body
@ -44,7 +55,7 @@ def censor_slurs(v, body):
for variation in create_variations_slur_regex(slur):
try:
body = re.sub(variation, sub_matcher, body)
except:
pass
except Exception as e:
print(e)
return body