From a1822e2222c442944e27f167a10c567b245e1d02 Mon Sep 17 00:00:00 2001 From: Codex Date: Sat, 26 Sep 2026 22:11:24 +0530 Subject: [PATCH] Normalize rewrite punctuation --- backlink_intelligence/placement.py | 1 + tests/test_placement.py | 13 +++++++++++++ 2 files changed, 14 insertions(+) diff --git a/backlink_intelligence/placement.py b/backlink_intelligence/placement.py index b10d29a..29474c4 100644 --- a/backlink_intelligence/placement.py +++ b/backlink_intelligence/placement.py @@ -184,6 +184,7 @@ def _rewrite_candidate( stripped = sentence.rstrip() terminal = stripped[-1] if stripped and stripped[-1] in ".!?" else "." sentence_body = stripped[:-1] if stripped and stripped[-1] in ".!?" else stripped + sentence_body = sentence_body.rstrip() placed_anchor = _fallback_anchor_case(anchor) linked_phrase = _anchor_with_article(placed_anchor) diff --git a/tests/test_placement.py b/tests/test_placement.py index 1d4e09d..19c6ad1 100644 --- a/tests/test_placement.py +++ b/tests/test_placement.py @@ -245,6 +245,19 @@ def test_generated_rewrite_is_deterministic(self): second = rank_placements(source, target, "ai in finance course", "https://t.com", top_n=1)[0] self.assertEqual(first.to_dict(), second.to_dict()) + def test_rewrite_normalizes_space_before_terminal_punctuation(self): + source = parse_page( + """

Financial agents monitor transactions and support continuous accounting across connected global finance operations . Teams review exceptions before reports are finalized.

""", + requested_url="https://s.com", final_url="https://s.com", status_code=200, + ) + target = parse_page( + """AI in Finance Course

Finance AI Applications

Study financial agents, continuous accounting, and transaction monitoring.

""", + requested_url="https://t.com", final_url="https://t.com", status_code=200, + ) + item = rank_placements(source, target, "ai in finance course", "https://t.com", top_n=1)[0] + self.assertNotIn(" ,", item.after_text) + self.assertIn("operations, one of the practical applications", item.after_text) + if __name__ == "__main__":