From 27c50a9d2b7d652130a72ac4b925b748d4f8eabe Mon Sep 17 00:00:00 2001 From: usmanmateen <74776275+usmanmateen@users.noreply.github.com> Date: Sat, 10 Oct 2026 01:21:55 +0100 Subject: [PATCH 1/5] fix(text): keep underscores inside words in MarkdownRemover The underline pattern paired any two underscores on a line, so snake_case identifiers, file names, e-mail addresses and URL paths lost their underscores (max_new_tokens -> maxnewtokens). As in CommonMark, only treat underscores at word boundaries as emphasis. Closes #2494 Signed-off-by: usmanmateen <74776275+usmanmateen@users.noreply.github.com> --- .../text/modifiers/string/markdown_remover.py | 6 ++++-- tests/stages/text/modules/test_modifiers.py | 13 +++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/nemo_curator/stages/text/modifiers/string/markdown_remover.py b/nemo_curator/stages/text/modifiers/string/markdown_remover.py index b144ddbbab..dc748b1061 100644 --- a/nemo_curator/stages/text/modifiers/string/markdown_remover.py +++ b/nemo_curator/stages/text/modifiers/string/markdown_remover.py @@ -18,7 +18,9 @@ MARKDOWN_BOLD_REGEX = r"\*\*(.*?)\*\*" MARKDOWN_ITALIC_REGEX = r"\*(.*?)\*" -MARKDOWN_UNDERLINE_REGEX = r"_(.*?)_" +# As in CommonMark, an underscore inside a word (snake_case, file_name.py, first_last@example.com) +# can't open or close emphasis (_text_ or __text__) +MARKDOWN_UNDERLINE_REGEX = r"(? str: for line in lines: line = re.sub(MARKDOWN_BOLD_REGEX, r"\1", line) # **text** #noqa: PLW2901 line = re.sub(MARKDOWN_ITALIC_REGEX, r"\1", line) # *text* #noqa: PLW2901 - line = re.sub(MARKDOWN_UNDERLINE_REGEX, r"\1", line) # _text_ #noqa: PLW2901 + line = re.sub(MARKDOWN_UNDERLINE_REGEX, r"\2", line) # _text_ or __text__ #noqa: PLW2901 line = re.sub(MARKDOWN_LINK_REGEX, r"\1", line) # [text](url) #noqa: PLW2901 new_lines.append(line) diff --git a/tests/stages/text/modules/test_modifiers.py b/tests/stages/text/modules/test_modifiers.py index 17e097d4ef..dcbae0dcc2 100644 --- a/tests/stages/text/modules/test_modifiers.py +++ b/tests/stages/text/modules/test_modifiers.py @@ -384,6 +384,19 @@ def test_underline_removal(self) -> None: expected = "This is underlined text." assert result == expected + def test_underscores_inside_words_are_kept(self) -> None: + text = ( + "Set max_new_tokens and top_p in generation_config.json, " + "or email first_last@example.com about _this_ and __that__." + ) + remover = MarkdownRemover() + result = remover.modify_document(text) + expected = ( + "Set max_new_tokens and top_p in generation_config.json, " + "or email first_last@example.com about this and that." + ) + assert result == expected + def test_link_removal(self) -> None: text = "Link: [Google](https://google.com)" remover = MarkdownRemover() From 93551529cfef4276a4ab60a5e676b95b6c144259 Mon Sep 17 00:00:00 2001 From: usmanmateen <74776275+usmanmateen@users.noreply.github.com> Date: Sat, 10 Oct 2026 01:31:47 +0100 Subject: [PATCH 2/5] fix(text): scan MarkdownRemover underscores once and remove nested emphasis The text between two underscore delimiters can no longer contain another opening underscore, so a long line of unmatched underscores is scanned once instead of once per underscore (120k chars: 14 s -> 0.01 s). A second pass removes emphasis nested in the first (___text___, __bold _italic_ bold__), as before this change. Signed-off-by: usmanmateen <74776275+usmanmateen@users.noreply.github.com> --- .../text/modifiers/string/markdown_remover.py | 9 ++++++--- tests/stages/text/modules/test_modifiers.py | 18 ++++++++++++++++++ 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/nemo_curator/stages/text/modifiers/string/markdown_remover.py b/nemo_curator/stages/text/modifiers/string/markdown_remover.py index dc748b1061..b99063597f 100644 --- a/nemo_curator/stages/text/modifiers/string/markdown_remover.py +++ b/nemo_curator/stages/text/modifiers/string/markdown_remover.py @@ -19,8 +19,9 @@ MARKDOWN_BOLD_REGEX = r"\*\*(.*?)\*\*" MARKDOWN_ITALIC_REGEX = r"\*(.*?)\*" # As in CommonMark, an underscore inside a word (snake_case, file_name.py, first_last@example.com) -# can't open or close emphasis (_text_ or __text__) -MARKDOWN_UNDERLINE_REGEX = r"(? str: for line in lines: line = re.sub(MARKDOWN_BOLD_REGEX, r"\1", line) # **text** #noqa: PLW2901 line = re.sub(MARKDOWN_ITALIC_REGEX, r"\1", line) # *text* #noqa: PLW2901 - line = re.sub(MARKDOWN_UNDERLINE_REGEX, r"\2", line) # _text_ or __text__ #noqa: PLW2901 + # The second pass removes emphasis nested in the first (___text___, __bold _italic_ bold__) + for _ in range(2): + line = re.sub(MARKDOWN_UNDERLINE_REGEX, r"\2", line) # _text_ or __text__ #noqa: PLW2901 line = re.sub(MARKDOWN_LINK_REGEX, r"\1", line) # [text](url) #noqa: PLW2901 new_lines.append(line) diff --git a/tests/stages/text/modules/test_modifiers.py b/tests/stages/text/modules/test_modifiers.py index dcbae0dcc2..406c750e9c 100644 --- a/tests/stages/text/modules/test_modifiers.py +++ b/tests/stages/text/modules/test_modifiers.py @@ -12,6 +12,8 @@ # See the License for the specific language governing permissions and # limitations under the License. +import time + import pandas as pd import pytest @@ -397,6 +399,22 @@ def test_underscores_inside_words_are_kept(self) -> None: ) assert result == expected + def test_nested_underscore_emphasis(self) -> None: + text = "___both___ and __bold _and italic_ bold__ and _a __b__ c_" + remover = MarkdownRemover() + result = remover.modify_document(text) + expected = "both and bold and italic bold and a b c" + assert result == expected + + def test_long_line_of_unmatched_underscores(self) -> None: + # Each underscore opens but none closes; this must not take quadratic time + text = "_word self._x " * 10_000 + remover = MarkdownRemover() + start = time.perf_counter() + result = remover.modify_document(text) + assert time.perf_counter() - start < 1 + assert result == text + def test_link_removal(self) -> None: text = "Link: [Google](https://google.com)" remover = MarkdownRemover() From 7004b91a461ba5bb0ac758b3ad8670d9a6ccfac4 Mon Sep 17 00:00:00 2001 From: usmanmateen <74776275+usmanmateen@users.noreply.github.com> Date: Sat, 10 Oct 2026 01:38:12 +0100 Subject: [PATCH 3/5] fix(text): let MarkdownRemover emphasis contain an underscore that can't open An underscore followed by whitespace can't open emphasis, so it no longer stops the emphasized text: "_Use _ as a separator_" -> "Use _ as a separator". Only underscores that could open emphasis end the scan, so long unmatched lines are still scanned once. Signed-off-by: usmanmateen <74776275+usmanmateen@users.noreply.github.com> --- .../stages/text/modifiers/string/markdown_remover.py | 5 +++-- tests/stages/text/modules/test_modifiers.py | 8 ++++++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/nemo_curator/stages/text/modifiers/string/markdown_remover.py b/nemo_curator/stages/text/modifiers/string/markdown_remover.py index b99063597f..bcd121d886 100644 --- a/nemo_curator/stages/text/modifiers/string/markdown_remover.py +++ b/nemo_curator/stages/text/modifiers/string/markdown_remover.py @@ -20,8 +20,9 @@ MARKDOWN_ITALIC_REGEX = r"\*(.*?)\*" # As in CommonMark, an underscore inside a word (snake_case, file_name.py, first_last@example.com) # can't open or close emphasis (_text_ or __text__). The emphasized text can't contain another -# opening underscore, so a long line of unmatched ones is scanned once, not once per underscore. -MARKDOWN_UNDERLINE_REGEX = r"(? None: expected = "both and bold and italic bold and a b c" assert result == expected + def test_literal_underscore_inside_emphasis(self) -> None: + # An underscore followed by a space can't open emphasis, so it stays inside the emphasized text + text = "_Use _ as a separator_ in names." + remover = MarkdownRemover() + result = remover.modify_document(text) + expected = "Use _ as a separator in names." + assert result == expected + def test_long_line_of_unmatched_underscores(self) -> None: # Each underscore opens but none closes; this must not take quadratic time text = "_word self._x " * 10_000 From c313b484b8b2d50912951ef8d41077f71179b7f5 Mon Sep 17 00:00:00 2001 From: usmanmateen <74776275+usmanmateen@users.noreply.github.com> Date: Sat, 10 Oct 2026 02:35:25 +0100 Subject: [PATCH 4/5] fix(text): make MarkdownRemover's emphasis alternatives mutually exclusive An underscore after a word character and before whitespace matched both (?<=\w)_ and _(?!\S), so a failed "__" match retried every way of splitting such underscores: "__" + "a_ " * 22 + "!" (69 chars) took about a second, growing exponentially. Limit the second alternative to underscores after a non-word character. The set of underscores allowed inside emphasis is unchanged. Signed-off-by: usmanmateen <74776275+usmanmateen@users.noreply.github.com> --- .../stages/text/modifiers/string/markdown_remover.py | 5 +++-- tests/stages/text/modules/test_modifiers.py | 9 +++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/nemo_curator/stages/text/modifiers/string/markdown_remover.py b/nemo_curator/stages/text/modifiers/string/markdown_remover.py index bcd121d886..e0e8bba53d 100644 --- a/nemo_curator/stages/text/modifiers/string/markdown_remover.py +++ b/nemo_curator/stages/text/modifiers/string/markdown_remover.py @@ -21,8 +21,9 @@ # As in CommonMark, an underscore inside a word (snake_case, file_name.py, first_last@example.com) # can't open or close emphasis (_text_ or __text__). The emphasized text can't contain another # underscore that could open emphasis (one after a non-word character and before a non-space), so a -# long line of unmatched ones is scanned once, not once per underscore. -MARKDOWN_UNDERLINE_REGEX = r"(? None: assert time.perf_counter() - start < 1 assert result == text + def test_failed_double_underscore_match_does_not_backtrack(self) -> None: + # "a_ " underscores can't open emphasis; with overlapping alternatives, the failed "__" match + # retried every way of splitting them, which took seconds on this 78-character line + text = "__" + "a_ " * 25 + "!" + remover = MarkdownRemover() + start = time.perf_counter() + remover.modify_document(text) + assert time.perf_counter() - start < 1 + def test_link_removal(self) -> None: text = "Link: [Google](https://google.com)" remover = MarkdownRemover() From 4068c461c2fd993e1d707e409893b33a691868e0 Mon Sep 17 00:00:00 2001 From: usmanmateen <74776275+usmanmateen@users.noreply.github.com> Date: Wed, 21 Oct 2026 23:49:02 +0100 Subject: [PATCH 5/5] fix(text): make the MarkdownRemover patterns parameters Keep the original patterns as defaults and let callers pass their own bold, italic, underline and link patterns instead of changing the default underline pattern. Each match is still replaced with its first group. Signed-off-by: usmanmateen <74776275+usmanmateen@users.noreply.github.com> --- .../text/modifiers/string/markdown_remover.py | 37 +++++++++----- tests/stages/text/modules/test_modifiers.py | 51 ++++--------------- 2 files changed, 33 insertions(+), 55 deletions(-) diff --git a/nemo_curator/stages/text/modifiers/string/markdown_remover.py b/nemo_curator/stages/text/modifiers/string/markdown_remover.py index e0e8bba53d..9055153d44 100644 --- a/nemo_curator/stages/text/modifiers/string/markdown_remover.py +++ b/nemo_curator/stages/text/modifiers/string/markdown_remover.py @@ -18,12 +18,7 @@ MARKDOWN_BOLD_REGEX = r"\*\*(.*?)\*\*" MARKDOWN_ITALIC_REGEX = r"\*(.*?)\*" -# As in CommonMark, an underscore inside a word (snake_case, file_name.py, first_last@example.com) -# can't open or close emphasis (_text_ or __text__). The emphasized text can't contain another -# underscore that could open emphasis (one after a non-word character and before a non-space), so a -# long line of unmatched ones is scanned once, not once per underscore. The alternatives for the -# emphasized text are mutually exclusive, so a failed match can't backtrack exponentially. -MARKDOWN_UNDERLINE_REGEX = r"(? str: lines = text.split("\n") new_lines = [] for line in lines: - line = re.sub(MARKDOWN_BOLD_REGEX, r"\1", line) # **text** #noqa: PLW2901 - line = re.sub(MARKDOWN_ITALIC_REGEX, r"\1", line) # *text* #noqa: PLW2901 - # The second pass removes emphasis nested in the first (___text___, __bold _italic_ bold__) - for _ in range(2): - line = re.sub(MARKDOWN_UNDERLINE_REGEX, r"\2", line) # _text_ or __text__ #noqa: PLW2901 - line = re.sub(MARKDOWN_LINK_REGEX, r"\1", line) # [text](url) #noqa: PLW2901 + line = re.sub(self.markdown_bold_regex, r"\1", line) # **text** #noqa: PLW2901 + line = re.sub(self.markdown_italic_regex, r"\1", line) # *text* #noqa: PLW2901 + line = re.sub(self.markdown_underline_regex, r"\1", line) # _text_ #noqa: PLW2901 + line = re.sub(self.markdown_link_regex, r"\1", line) # [text](url) #noqa: PLW2901 new_lines.append(line) return "\n".join(new_lines) diff --git a/tests/stages/text/modules/test_modifiers.py b/tests/stages/text/modules/test_modifiers.py index 46b592e952..43fe95db8a 100644 --- a/tests/stages/text/modules/test_modifiers.py +++ b/tests/stages/text/modules/test_modifiers.py @@ -12,8 +12,6 @@ # See the License for the specific language governing permissions and # limitations under the License. -import time - import pandas as pd import pytest @@ -386,52 +384,21 @@ def test_underline_removal(self) -> None: expected = "This is underlined text." assert result == expected - def test_underscores_inside_words_are_kept(self) -> None: - text = ( - "Set max_new_tokens and top_p in generation_config.json, " - "or email first_last@example.com about _this_ and __that__." - ) - remover = MarkdownRemover() + def test_custom_underline_regex(self) -> None: + # The default pattern also matches underscores inside words; a caller can pass a stricter one + text = "Set max_new_tokens in generation_config.json and email first_last@example.com about _this_." + remover = MarkdownRemover(markdown_underline_regex=r"(? None: - text = "___both___ and __bold _and italic_ bold__ and _a __b__ c_" - remover = MarkdownRemover() + def test_custom_regexes_replace_the_defaults(self) -> None: + text = "~~struck~~ and " + remover = MarkdownRemover(markdown_bold_regex=r"~~(.*?)~~", markdown_link_regex=r"<(.*?)>") result = remover.modify_document(text) - expected = "both and bold and italic bold and a b c" + expected = "struck and https://example.com" assert result == expected - def test_literal_underscore_inside_emphasis(self) -> None: - # An underscore followed by a space can't open emphasis, so it stays inside the emphasized text - text = "_Use _ as a separator_ in names." - remover = MarkdownRemover() - result = remover.modify_document(text) - expected = "Use _ as a separator in names." - assert result == expected - - def test_long_line_of_unmatched_underscores(self) -> None: - # Each underscore opens but none closes; this must not take quadratic time - text = "_word self._x " * 10_000 - remover = MarkdownRemover() - start = time.perf_counter() - result = remover.modify_document(text) - assert time.perf_counter() - start < 1 - assert result == text - - def test_failed_double_underscore_match_does_not_backtrack(self) -> None: - # "a_ " underscores can't open emphasis; with overlapping alternatives, the failed "__" match - # retried every way of splitting them, which took seconds on this 78-character line - text = "__" + "a_ " * 25 + "!" - remover = MarkdownRemover() - start = time.perf_counter() - remover.modify_document(text) - assert time.perf_counter() - start < 1 - def test_link_removal(self) -> None: text = "Link: [Google](https://google.com)" remover = MarkdownRemover()