Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 23 additions & 5 deletions nemo_curator/stages/text/modifiers/string/markdown_remover.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,35 @@ class MarkdownRemover(DocumentModifier):
Removes Markdown formatting in a document including bold, italic, underline, and URL text.
"""

def __init__(self):
def __init__(
self,
markdown_bold_regex: str = MARKDOWN_BOLD_REGEX,
markdown_italic_regex: str = MARKDOWN_ITALIC_REGEX,
markdown_underline_regex: str = MARKDOWN_UNDERLINE_REGEX,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Default still strips word underscores

This revision restores MARKDOWN_UNDERLINE_REGEX to _(.*?)_, so calling MarkdownRemover() still changes max_new_tokens to maxnewtokens. Only callers supplying their own pattern get the behavior promised by this PR; existing cleaning jobs keep losing underscores in names and file paths.

Keep a word-aware default and restore the regression test that calls MarkdownRemover() without arguments.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is intentional.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Understood — keeping MARKDOWN_UNDERLINE_REGEX unchanged is intentional to preserve MarkdownRemover()’s existing behavior. The custom parameter provides an opt-in stricter pattern for callers that need to protect underscores in identifiers or email addresses, so this is not an issue.

markdown_link_regex: str = MARKDOWN_LINK_REGEX,
):
"""
Args:
markdown_bold_regex (str): Pattern for bold text. Each match is replaced with its first group.
markdown_italic_regex (str): Pattern for italic text. Each match is replaced with its first group.
markdown_underline_regex (str): Pattern for underlined text. Each match is replaced with its
first group.
markdown_link_regex (str): Pattern for links. Each match is replaced with its first group.
"""
super().__init__()
self.markdown_bold_regex = markdown_bold_regex
self.markdown_italic_regex = markdown_italic_regex
self.markdown_underline_regex = markdown_underline_regex
self.markdown_link_regex = markdown_link_regex

def modify_document(self, text: str) -> 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
line = re.sub(MARKDOWN_UNDERLINE_REGEX, r"\1", line) # _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)
15 changes: 15 additions & 0 deletions tests/stages/text/modules/test_modifiers.py
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,21 @@ def test_underline_removal(self) -> None:
expected = "This is underlined text."
assert result == expected

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"(?<!\w)_(\S.*?)_(?!\w)")
result = remover.modify_document(text)
expected = "Set max_new_tokens in generation_config.json and email first_last@example.com about this."
assert result == expected

def test_custom_regexes_replace_the_defaults(self) -> None:
text = "~~struck~~ and <https://example.com>"
remover = MarkdownRemover(markdown_bold_regex=r"~~(.*?)~~", markdown_link_regex=r"<(.*?)>")
result = remover.modify_document(text)
expected = "struck and https://example.com"
assert result == expected

def test_link_removal(self) -> None:
text = "Link: [Google](https://google.com)"
remover = MarkdownRemover()
Expand Down
Loading