Aligning Parallel Texts Is Harder Than It Looks

Two texts with the same number of stanzas are not necessarily the same poem, stanza for stanza. A twelve-line original and its transliteration proved it, and the fix was comparing shape, not count.

Aligning Parallel Texts Is Harder Than It Looks

The bug report was, in effect, "the fifth line is missing." It wasn't missing. It was paired against the wrong stanza, one row down, because the code that lines up an original against its transliteration and translation had been checking the wrong thing: that the two texts had the same number of stanzas, not that the stanzas had the same shape.

Equal stanza counts are not equal stanza shapes
Equal stanza counts are not equal stanza shapes

Original: 5+4+3 lines. Transliteration: one block, sub-grouped into 4+4+4. Three stanzas both ways — wrongly paired.

The failure, concretely

The commit message states it more precisely than I can summarize:

A twelve-line original written as stanzas of five, four and three, against a transliteration typed as one unbroken block, both come out as three stanzas: a long block is sub-grouped into fours for reading rhythm. Three equalled three, so they paired, and the fifth line of the original sat against nothing while its transliteration sat against the next stanza — the precise misalignment the previous commit claimed to avoid.

That last clause is worth sitting with. This wasn't a fresh bug; it was a regression of something a previous fix explicitly claimed to have solved, caught only because someone tested the claim instead of trusting the commit message that made it.

The fix: compare shape, not count

core/templatetags/qasida_extras.py gained a _shape() helper that returns, per stanza, how many lines it holds — and the pairing check now requires the sequence of line-counts to match, not just its length:

def _shape(blocks):
    """How many lines each stanza holds."""
    return [len(_lines(block)) for block in blocks]


def _paired_layer(original_blocks, layer_text):
    if not layer_text or not layer_text.strip():
        return None
    blocks = _display_stanzas(layer_text)
    if blocks and _shape(blocks) == _shape(original_blocks):
        return blocks
    return _regrouped_like(original_blocks, layer_text)

[5, 4, 3] == [5, 4, 3] is a real match. [5, 4, 3] == [4, 4, 4] — same length, same sum, wrong shape — correctly fails now, and falls through to _regrouped_like, which tries the looser but still-safe fallback: cutting the layer into the original's shape when the two hold the exact same total line count, which does mean something even when stanza breaks disagree (Arabic often sets two hemistichs per line where a transliteration gives each hemistich its own line).

What happens when nothing lines up

There's a third tier, and it's the one that matters most for correctness: if neither the stanza marks nor the raw line counts agree, the layer is never force-fit against the wrong verse. It's shown as a whole passage instead, with a note on the page saying the layers don't correspond line by line. An earlier version of this feature had briefly regressed to putting mismatched layers into separate sections outright — restoring the interleaved reading and keeping the honest fallback both mattered, per a sibling commit ("one layout for every qasida"): a layer is never dropped and never set against the wrong verse.

The test that would have caught the regression the first time

def test_equal_stanza_counts_are_not_enough_on_their_own(self):
    original = '\n'.join(f'A{n}' for n in range(1, 6)) + '\n\n' \
               + '\n'.join(f'B{n}' for n in range(1, 5)) + '\n\n' \
               + '\n'.join(f'C{n}' for n in range(1, 4))
    qasida = make_qasida(
        lyrics=original,
        transliteration='\n'.join(f'L{n}' for n in range(1, 13)))

    rows = stanza_rows(qasida)
    for row in rows:
        self.assertEqual(len(row['original'].splitlines()),
                         len(row['latin'].splitlines()),
                         'a stanza was paired with the wrong number of lines')

Pairing bugs are seductive precisely because a plausible-looking metric (stanza count) agrees often enough to pass casual review. The lesson that generalizes: when two sequences "match," ask whether you compared their length or their shape — those are very different claims, and only one of them is actually about correspondence.

Series: Qasida App.