From 527370f6004c11538d4b3dc0e7f92dc5a2c3dd10 Mon Sep 17 00:00:00 2001 From: AC Date: Wed, 7 Oct 2026 18:27:08 +0800 Subject: [PATCH] fix: treat subtitle correction values as literal text --- tests/tools/test_subtitle_corrections.py | 29 ++++++++++++++++++++++++ tools/subtitle/subtitle_gen.py | 2 +- 2 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 tests/tools/test_subtitle_corrections.py diff --git a/tests/tools/test_subtitle_corrections.py b/tests/tools/test_subtitle_corrections.py new file mode 100644 index 0000000000..d4cf8c01be --- /dev/null +++ b/tests/tools/test_subtitle_corrections.py @@ -0,0 +1,29 @@ +"""Correction values are literal text, not regular-expression replacements.""" + +import pytest + +from tools.subtitle.subtitle_gen import SubtitleGen + + +@pytest.mark.parametrize("replacement", [r"C:\Users\speaker", r"\1", r"line\ntext"]) +def test_segment_corrections_preserve_backslashes(tmp_path, replacement): + segments = [{"text": "Use folder.", "start": 0, "end": 1}] + output = tmp_path / "captions.srt" + result = SubtitleGen().execute({ + "segments": segments, + "corrections": {"folder": replacement}, + "output_path": str(output), + }) + assert result.success, result.error + assert f"Use {replacement}." in output.read_text() + assert segments[0]["text"] == "Use folder." + + +def test_segment_and_word_corrections_agree(): + replacement = r"C:\Users\speaker" + segments = [ + {"text": "FOLDER", "start": 0, "end": 1}, + {"words": [{"word": "FOLDER", "start": 1, "end": 2}]}, + ] + corrected = SubtitleGen._apply_corrections(segments, {"folder": replacement}) + assert corrected[0]["text"] == corrected[1]["words"][0]["word"] == replacement diff --git a/tools/subtitle/subtitle_gen.py b/tools/subtitle/subtitle_gen.py index a22f67d828..4ecd527a7a 100644 --- a/tools/subtitle/subtitle_gen.py +++ b/tools/subtitle/subtitle_gen.py @@ -158,7 +158,7 @@ def _apply_corrections( import re as _re seg["text"] = _re.sub( r"\b" + _re.escape(wrong) + r"\b", - right, + lambda match, replacement=right: replacement, seg["text"], flags=_re.IGNORECASE, )