Now uses regex censor in the title too

remotes/1693045480750635534/spooky-22
Yo Mama 2021-10-20 01:54:23 +02:00
parent 9c7878d7c9
commit 35e77adb26
3 changed files with 12 additions and 19 deletions

View File

@ -9,7 +9,7 @@ from sqlalchemy import *
from sqlalchemy.orm import relationship, deferred
from files.__main__ import Base
from files.helpers.const import SLURS, AUTOPOLLER_ACCOUNT
from files.helpers.const import AUTOPOLLER_ACCOUNT
from files.helpers.lazy import lazy
from .flags import Flag
from ..helpers.word_censor import censor_slurs
@ -365,8 +365,7 @@ class Submission(Base):
elif self.title_html: title = self.title_html
else: title = self.title
if not v or v.slurreplacer:
for s,r in SLURS.items(): title = title.replace(s, r)
title = censor_slurs(title, v)
return title
@ -375,8 +374,7 @@ class Submission(Base):
if self.club and not (v and v.paid_dues) and not (v and v.admin_level == 6): return 'COUNTRY CLUB MEMBERS ONLY'
else: title = self.title
if not v or v.slurreplacer:
for s,r in SLURS.items(): title = title.replace(s, r)
title = censor_slurs(title, v)
return title

View File

@ -43,7 +43,7 @@ def create_slur_regex() -> Pattern[str]:
"""Creates the regex that will find the slurs"""
single_words = "|".join([slur.lower() for slur in SLURS.keys()])
return re.compile(rf"(?i)(?<=\s|>)({single_words})([\s<,.])")
return re.compile(rf"(?i)(?<=\s|>)({single_words})(?=[\s<,.])")
def create_replace_map() -> Dict[str, str]:
@ -60,9 +60,9 @@ REPLACE_MAP = create_replace_map()
def sub_matcher(match: Match) -> str:
"""given a match returns the correct replacer string"""
found = match.group(1)
found = match.group(0)
# if it does not find the correct capitalization, it tries the all lower, or return the original word
return (REPLACE_MAP.get(found) or REPLACE_MAP.get(found.lower()) or found) + match.group(2)
return REPLACE_MAP.get(found) or REPLACE_MAP.get(found.lower()) or found
def censor_slurs(body: str, logged_user) -> str:

View File

@ -52,7 +52,7 @@ def test_get_permutations_slur_wiht_link_replacer():
"retard": "r-slur",
})
def test_create_slur_regex():
expected = r"(?i)(?<=\s|>)(kill yourself|faggot|nig|retard)([\s<,.])"
expected = r"(?i)(?<=\s|>)(kill yourself|faggot|nig|retard)(?=[\s<,.])"
assert_that(create_slur_regex()).is_equal_to(re.compile(expected))
@ -91,19 +91,19 @@ def test_create_replace_map():
@patch("files.helpers.word_censor.REPLACE_MAP", {'retard': 'r-slur', 'Faggot': 'Cute twink', 'NIG': '🏀'})
def test_sub_matcher():
regex = re.compile(r"(?i)(?<=\s|>)(kill yourself|retard|nig|faggot)([\s<,.])")
regex = re.compile(r"(?i)(?<=\s|>)(kill yourself|retard|nig|faggot)(?=[\s<,.])")
match = regex.search("<p>retard</p>")
assert_that(sub_matcher(match)).is_equal_to("r-slur<")
assert_that(sub_matcher(match)).is_equal_to("r-slur")
match = regex.search("<p>ReTaRd</p>")
assert_that(sub_matcher(match)).is_equal_to("r-slur<")
assert_that(sub_matcher(match)).is_equal_to("r-slur")
match = regex.search("<p>NIG</p>")
assert_that(sub_matcher(match)).is_equal_to("🏀<")
assert_that(sub_matcher(match)).is_equal_to("🏀")
match = regex.search("<p>Faggot </p>")
assert_that(sub_matcher(match)).is_equal_to("Cute twink ")
assert_that(sub_matcher(match)).is_equal_to("Cute twink")
@patch("files.helpers.word_censor.SLURS", {
@ -149,11 +149,6 @@ def test_censor_slurs():
assert_that(censor_slurs('... i Hate carp ...', None)).is_equal_to('... i love Carp ...')
assert_that(censor_slurs('... i hate a carp ...', None)).is_equal_to('... i hate a carp ...')
assert_that(censor_slurs("""<h1 id="post-title" class="card-title post-title text-left mb-md-3">
I had a dream about this site last night
</h1>""", None)).is_equal_to("""<h1 id="post-title" class="card-title post-title text-left mb-md-3">
I had a dream about this site last night
</h1>""")
assert_that(censor_slurs("<p>retard Manlet NIG</p>", None)).is_equal_to("<p>r-slur Little king 🏀</p>")