From 8a7dfc0ec5986fa5e0f8b3391a5fedc31fd14fa9 Mon Sep 17 00:00:00 2001 From: Yo Mama Date: Wed, 20 Oct 2021 01:32:28 +0200 Subject: [PATCH] Final try to make it all work --- files/helpers/word_censor.py | 8 ++++---- test/files/helpers/test_word_censor.py | 20 +++++++++++++------- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/files/helpers/word_censor.py b/files/helpers/word_censor.py index 7399352b8a..369164fda7 100644 --- a/files/helpers/word_censor.py +++ b/files/helpers/word_censor.py @@ -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: diff --git a/test/files/helpers/test_word_censor.py b/test/files/helpers/test_word_censor.py index 79a2b0c85d..00bf4fc2f5 100644 --- a/test/files/helpers/test_word_censor.py +++ b/test/files/helpers/test_word_censor.py @@ -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("

retard

") - assert_that(sub_matcher(match)).is_equal_to("r-slur") + assert_that(sub_matcher(match)).is_equal_to("r-slur<") match = regex.search("

ReTaRd

") - assert_that(sub_matcher(match)).is_equal_to("r-slur") + assert_that(sub_matcher(match)).is_equal_to("r-slur<") match = regex.search("

NIG

") - assert_that(sub_matcher(match)).is_equal_to("🏀") + assert_that(sub_matcher(match)).is_equal_to("🏀<") match = regex.search("

Faggot

") - 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("""

+I had a dream about this site last night +

""", None)).is_equal_to("""

+I had a dream about this site last night +

""") assert_that(censor_slurs("

retard Manlet NIG

", None)).is_equal_to("

r-slur Little king 🏀

") @@ -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()