diff --git a/generator/sefariasqlite/src/jvmMain/kotlin/io/github/kdroidfilter/seforimlibrary/sefariasqlite/SynthesizeSeifimAltTocPostProcess.kt b/generator/sefariasqlite/src/jvmMain/kotlin/io/github/kdroidfilter/seforimlibrary/sefariasqlite/SynthesizeSeifimAltTocPostProcess.kt index 8feeab38..a125510a 100644 --- a/generator/sefariasqlite/src/jvmMain/kotlin/io/github/kdroidfilter/seforimlibrary/sefariasqlite/SynthesizeSeifimAltTocPostProcess.kt +++ b/generator/sefariasqlite/src/jvmMain/kotlin/io/github/kdroidfilter/seforimlibrary/sefariasqlite/SynthesizeSeifimAltTocPostProcess.kt @@ -128,11 +128,16 @@ internal data class SeifimHeading( internal data class SeifMarker(val lineId: Long, val lineIndex: Long, val label: String) -/** One COMMENTARY link row: the book line it lands on and the SA source ref. */ +/** + * One COMMENTARY link row: the book line it lands on, and the SA source line + * (its book, its position in that book, its heRef). + */ internal data class SeifLinkRow( val lineId: Long, val lineIndex: Long, val baseHeRef: String, + val baseBookId: Long, + val baseLineIndex: Long, ) internal data class SeifimSynthesisResult(val structures: Int, val leaves: Int) @@ -272,35 +277,53 @@ internal fun synthesizeSeifimAltTocs( /** * Derives the group-opening markers from COMMENTARY link rows. * - * [rows] must be ordered by `(lineIndex, base lineIndex)` so the first link - * of a multi-linked line decides its se'if. The group key is the base line's - * full heRef (siman+se'if) — not the se'if letter alone — so a new siman - * whose first commented se'if carries the same letter still opens a group. - * The label is the se'if alone: the trailing comma-separated heRef segment. + * [rows] must be ordered by `(lineIndex, base lineIndex)`. The group key is + * the base line's full heRef (siman+se'if) — not the se'if letter alone — so a + * new siman whose first commented se'if carries the same letter still opens a + * group. The label is the se'if alone: the trailing comma-separated heRef + * segment. + * + * A ס"ק that bears on several se'ifim is linked from each of them, so a + * multi-linked line may name an earlier se'if besides its own. Such a line + * stays in the current group when one of its links is that group's se'if; + * otherwise it opens the nearest se'if after it in the same base book. Only a + * line with no such link falls back to its first link — even backwards, since + * then nothing else says where it belongs. */ internal fun computeSeifMarkers(rows: List): List { - val refByLine = LinkedHashMap>() // lineIndex -> (lineId, key, seif) + val candidatesByLine = sortedMapOf>() for (row in rows) { - if (refByLine.containsKey(row.lineIndex)) continue - val comma = row.baseHeRef.lastIndexOf(',') - if (comma < 0) continue - val seif = row.baseHeRef.substring(comma + 1).trim() - if (seif.isEmpty()) continue - refByLine[row.lineIndex] = Triple(row.lineId, row.baseHeRef, seif) + if (seifOfHeRef(row.baseHeRef).isEmpty()) continue + candidatesByLine.getOrPut(row.lineIndex) { mutableListOf() } += row } val markers = mutableListOf() - var previousKey: String? = null - for (lineIndex in refByLine.keys.sorted()) { - val (lineId, key, seif) = refByLine.getValue(lineIndex) - if (key != previousKey) { - markers += SeifMarker(lineId = lineId, lineIndex = lineIndex, label = "סעיף $seif") - previousKey = key + var current: SeifLinkRow? = null + for ((lineIndex, candidates) in candidatesByLine) { + val group = current + if (group != null && candidates.any { it.baseHeRef == group.baseHeRef }) continue + val nextInGroupBook = group?.let { g -> + candidates + .filter { it.baseBookId == g.baseBookId && it.baseLineIndex > g.baseLineIndex } + .minByOrNull { it.baseLineIndex } } + val chosen = nextInGroupBook ?: candidates.first() + markers += SeifMarker( + lineId = chosen.lineId, + lineIndex = lineIndex, + label = "סעיף ${seifOfHeRef(chosen.baseHeRef)}", + ) + current = chosen } return markers } +/** The se'if segment of an SA heRef — the text after its last comma — or "". */ +private fun seifOfHeRef(heRef: String): String { + val comma = heRef.lastIndexOf(',') + return if (comma < 0) "" else heRef.substring(comma + 1).trim() +} + /** * Reads every candidate book — the link-derived group first (declared base = * שולחן ערוך, no existing Seifim structure), then the heRef-derived group — @@ -363,7 +386,7 @@ internal fun readLinkedSeifimCandidateSnapshots(conn: Connection): List + val i = "אבגדהוזחטיכלמנסעפצקרשת".indexOf(letter) + when { + i < 0 -> 0L + i < 10 -> (i + 1).toLong() + i < 18 -> ((i - 8) * 10).toLong() + else -> ((i - 17) * 100).toLong() + } + } + + private fun magenAvraham447(lineIndex: Long, vararg seifim: String) = + seifim.map { row(lineIndex, "שולחן ערוך, אורח חיים, תמז, $it") } + + @Test + fun `a link to an earlier seif does not reopen it, like Magen Avraham siman 447`() { + // Otzaria/otzaria#5503: the multi-linked lines of siman 447 in the 2026-11 library. + val rows = listOf( + magenAvraham447(4774, "א", "ב"), magenAvraham447(4777, "א"), + magenAvraham447(4778, "ב", "ה"), magenAvraham447(4780, "ג", "ד"), + magenAvraham447(4783, "ב", "ד"), magenAvraham447(4784, "ד", "ט"), + magenAvraham447(4788, "ג", "ה"), magenAvraham447(4803, "ב", "ה"), + magenAvraham447(4805, "ו"), magenAvraham447(4806, "ז", "ח"), + magenAvraham447(4811, "ד", "ה", "ח"), magenAvraham447(4813, "ט"), + magenAvraham447(4814, "י"), magenAvraham447(4815, "ד", "יא", "יב"), + magenAvraham447(4816, "יא"), magenAvraham447(4818, "א", "יא"), + magenAvraham447(4819, "יא", "יב"), + ).flatten() + + assertEquals( + listOf( + 4774L to "סעיף א", 4778L to "סעיף ב", 4780L to "סעיף ג", 4783L to "סעיף ד", + 4788L to "סעיף ה", 4805L to "סעיף ו", 4806L to "סעיף ז", 4811L to "סעיף ח", + 4813L to "סעיף ט", 4814L to "סעיף י", 4815L to "סעיף יא", + ), + computeSeifMarkers(rows).map { it.lineIndex to it.label }, + ) + } + + @Test + fun `a line linked only to an earlier seif still opens it`() { + val markers = computeSeifMarkers( + listOf( + row(1, "שולחן ערוך, יורה דעה, קמב, ה"), + row(2, "שולחן ערוך, יורה דעה, קמב, ו"), + row(3, "שולחן ערוך, יורה דעה, קמב, ה"), + ), + ) + assertEquals( + listOf(1L to "סעיף ה", 2L to "סעיף ו", 3L to "סעיף ה"), + markers.map { it.lineIndex to it.label }, + ) + } + + @Test + fun `forward choice stays within the current base book`() { + val markers = computeSeifMarkers( + listOf( + row(1, "שולחן ערוך, אורח חיים, ב, ג", baseBookId = 1), + row(2, "שולחן ערוך, אורח חיים, ב, א", baseBookId = 1), + row(2, "שולחן ערוך הרב, אורח חיים, ב, ה", baseBookId = 2), + ), + ) + assertEquals( + listOf(1L to "סעיף ג", 2L to "סעיף א"), + markers.map { it.lineIndex to it.label }, + ) + } @Test fun `marker opens each seif group, like Mishnah Berurah siman 1`() { @@ -49,7 +131,7 @@ class ComputeSeifMarkersTest { } @Test - fun `first link decides a multi-linked line`() { + fun `first link decides a multi-linked line with no current group`() { val markers = computeSeifMarkers( listOf( row(10, "שולחן ערוך, אורח חיים ב, א"),