Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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<SeifLinkRow>): List<SeifMarker> {
val refByLine = LinkedHashMap<Long, Triple<Long, String, String>>() // lineIndex -> (lineId, key, seif)
val candidatesByLine = sortedMapOf<Long, MutableList<SeifLinkRow>>()
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<SeifMarker>()
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 —
Expand Down Expand Up @@ -363,7 +386,7 @@ internal fun readLinkedSeifimCandidateSnapshots(conn: Connection): List<SeifimBo
var declaredBaseLinkCount = 0
conn.prepareStatement(
"""
SELECT ml.id, ml.lineIndex, bl.heRef, k.sourceBookId
SELECT ml.id, ml.lineIndex, bl.heRef, k.sourceBookId, bl.lineIndex
FROM link k INDEXED BY idx_link_target_book
JOIN connection_type c ON c.id = k.connectionTypeId
JOIN line ml ON ml.id = k.targetLineId
Expand All @@ -379,7 +402,13 @@ internal fun readLinkedSeifimCandidateSnapshots(conn: Connection): List<SeifimBo
while (rs.next()) {
declaredBaseLinkCount++
if (rs.getLong(4) in shulchanAruchBaseIds) {
linkRows += SeifLinkRow(rs.getLong(1), rs.getLong(2), rs.getString(3))
linkRows += SeifLinkRow(
lineId = rs.getLong(1),
lineIndex = rs.getLong(2),
baseHeRef = rs.getString(3),
baseBookId = rs.getLong(4),
baseLineIndex = rs.getLong(5),
)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,90 @@ import kotlin.test.assertTrue

class ComputeSeifMarkersTest {

private fun row(lineIndex: Long, heRef: String, lineId: Long = lineIndex * 10) =
SeifLinkRow(lineId = lineId, lineIndex = lineIndex, baseHeRef = heRef)
// Base line order follows the heRef here: siman * 1000 + se'if, both as gematria.
private fun row(lineIndex: Long, heRef: String, lineId: Long = lineIndex * 10, baseBookId: Long = 1) =
SeifLinkRow(
lineId = lineId,
lineIndex = lineIndex,
baseHeRef = heRef,
baseBookId = baseBookId,
baseLineIndex = baseLineOf(heRef),
)

private fun baseLineOf(heRef: String): Long {
val segments = heRef.split(',').map { it.trim() }
if (segments.size < 2) return 0
val siman = gematria(segments[segments.size - 2].substringAfterLast(' '))
return siman * 1000 + gematria(segments.last())
}

private fun gematria(text: String): Long = text.sumOf { letter ->
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`() {
Expand All @@ -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, "שולחן ערוך, אורח חיים ב, א"),
Expand Down