From d7eadb209f45b286f5894ee384bfd77c8a8aafa3 Mon Sep 17 00:00:00 2001 From: Alvaro Balbin <153404840+AlvaroBalbin@users.noreply.github.com> Date: Tue, 1 Dec 2026 02:05:26 +0000 Subject: [PATCH] Preserve nonmatching text when no common block qualifies I fixed text combination discarding both inputs when no common block qualifies. I changed the final append conditions to preserve remaining nonmatching text with the existing separator handling. --- src/hdx/utilities/matching.py | 4 ++-- .../utilities/test_matching_no_common_block.py | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) create mode 100644 tests/hdx/utilities/test_matching_no_common_block.py diff --git a/src/hdx/utilities/matching.py b/src/hdx/utilities/matching.py index 08e8e06..f1b12bd 100644 --- a/src/hdx/utilities/matching.py +++ b/src/hdx/utilities/matching.py @@ -529,10 +529,10 @@ def add_separator_if_needed(text_list): output.append(text_2) pos += len(text_2) text_2 = None - if text_1 and pos_1 == combined_len: + if text_1: add_separator_if_needed(output) output.append(text_1) - if text_2 and pos_2 == combined_len: + if text_2: add_separator_if_needed(output) output.append(text_2) a = "".join(output) diff --git a/tests/hdx/utilities/test_matching_no_common_block.py b/tests/hdx/utilities/test_matching_no_common_block.py new file mode 100644 index 0000000..536a463 --- /dev/null +++ b/tests/hdx/utilities/test_matching_no_common_block.py @@ -0,0 +1,17 @@ +"""AI-assisted regression for text loss when no matching block qualifies.""" + +import pytest + +from hdx.utilities.matching import get_matching_then_nonmatching_text + + +@pytest.mark.parametrize("separator", ["", "\n\n"]) +def test_preserves_both_texts_without_qualifying_match(separator: str) -> None: + texts = [ + "Apples grow in the northern orchard.", + "Buses depart every evening from town.", + ] + + result = get_matching_then_nonmatching_text(texts, separator=separator) + + assert result == texts[0] + separator + texts[1]