Final try to make it all work

remotes/1693045480750635534/spooky-22
Yo Mama 2021-10-20 01:32:28 +02:00
parent e5e4cc93ca
commit 8a7dfc0ec5
2 changed files with 17 additions and 11 deletions

View File

@ -41,9 +41,9 @@ def get_permutations_slur(slur: str, replacer: str = "_") -> Dict[str, str]:
def create_slur_regex() -> Pattern[str]:
"""Creates the regex that will find the slurs"""
single_words = "|".join([slur.strip().lower() for slur in SLURS.keys()])
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(0)
found = match.group(1)
# 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
return (REPLACE_MAP.get(found) or REPLACE_MAP.get(found.lower()) or found) + match.group(2)
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", {
@ -146,8 +146,14 @@ def test_censor_slurs():
assert_that(censor_slurs('... I Hate carp ...', None)).is_equal_to('... i love Carp ...')
assert_that(censor_slurs('... i Hate Carp ...', None)).is_equal_to('... i love Carp ...')
assert_that(censor_slurs('... i Hate carp ...', None)).is_equal_to('... i love Carp ...')
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>")
@ -159,7 +165,7 @@ def test_censor_slurs():
.is_equal_to('... https://sciencedirect.com/science/article/abs/pii/S016028960600033X ...')
@patch("files.helpers.word_censor.SLURS", {'retard': 'r-slur', 'manlet': 'little king', ' nig ': 'πŸ€'})
@patch("files.helpers.word_censor.SLURS", {'retard': 'r-slur', 'manlet': 'little king', 'nig': 'πŸ€'})
def test_censor_slurs_does_not_error_out_on_exception():
word_censor.REPLACE_MAP = create_replace_map()
word_censor.SLUR_REGEX = create_slur_regex()