Skip to content

Commit 76802e4

Browse files
authored
Merge pull request #116 from kdroidFilter/fix/search-highlighting
feat(search): one highlighting logic, word-exact and trustworthy
2 parents 2f6f4d3 + a9b8bf3 commit 76802e4

11 files changed

Lines changed: 799 additions & 648 deletions

File tree

‎cli/src/jvmMain/kotlin/io/github/kdroidfilter/seforimlibrary/cli/CliSnippetProvider.kt‎

Lines changed: 5 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -3,61 +3,23 @@ package io.github.kdroidfilter.seforimlibrary.cli
33
import io.github.kdroidfilter.seforimlibrary.dao.repository.SeforimRepository
44
import io.github.kdroidfilter.seforimlibrary.search.LineSnippetInfo
55
import io.github.kdroidfilter.seforimlibrary.search.SnippetProvider
6+
import io.github.kdroidfilter.seforimlibrary.search.SnippetSource
7+
import io.github.kdroidfilter.seforimlibrary.search.SnippetSources
68
import kotlinx.coroutines.Dispatchers
79
import kotlinx.coroutines.withContext
8-
import org.jsoup.Jsoup
9-
import org.jsoup.safety.Safelist
1010

1111
/**
1212
* CLI implementation of [SnippetProvider] that fetches line content from the database.
1313
*/
14-
private const val SNIPPET_NEIGHBOR_WINDOW = 4
15-
private const val SNIPPET_MIN_LENGTH = 280
16-
1714
class CliSnippetProvider(
1815
private val repository: SeforimRepository,
1916
) : SnippetProvider {
20-
override suspend fun getSnippetSources(lines: List<LineSnippetInfo>): Map<Long, String> {
17+
override suspend fun getSnippetSources(lines: List<LineSnippetInfo>): Map<Long, SnippetSource> {
2118
if (lines.isEmpty()) return emptyMap()
22-
2319
return withContext(Dispatchers.IO) {
24-
val byBook = lines.groupBy { it.bookId }
25-
val result = mutableMapOf<Long, String>()
26-
27-
for ((bookId, bookLines) in byBook) {
28-
val minIdx = bookLines.minOf { it.lineIndex }
29-
val maxIdx = bookLines.maxOf { it.lineIndex }
30-
val rangeStart = (minIdx - SNIPPET_NEIGHBOR_WINDOW).coerceAtLeast(0)
31-
val rangeEnd = maxIdx + SNIPPET_NEIGHBOR_WINDOW
32-
33-
val allLines = repository.getLines(bookId, rangeStart, rangeEnd)
34-
35-
val plainByIndex = allLines.associate { line ->
36-
line.lineIndex to cleanHtml(line.content)
37-
}
38-
39-
for (info in bookLines) {
40-
val basePlain = plainByIndex[info.lineIndex].orEmpty()
41-
val snippetSource = if (basePlain.length >= SNIPPET_MIN_LENGTH) {
42-
basePlain
43-
} else {
44-
val start = (info.lineIndex - SNIPPET_NEIGHBOR_WINDOW).coerceAtLeast(0)
45-
val end = info.lineIndex + SNIPPET_NEIGHBOR_WINDOW
46-
(start..end)
47-
.mapNotNull { plainByIndex[it] }
48-
.joinToString(" ")
49-
}
50-
result[info.lineId] = snippetSource
51-
}
20+
SnippetSources.build(lines) { bookId, from, to ->
21+
repository.getLines(bookId, from, to).associate { it.lineIndex to it.content }
5222
}
53-
54-
result
5523
}
5624
}
57-
58-
private fun cleanHtml(content: String): String =
59-
Jsoup
60-
.clean(content, Safelist.none())
61-
.replace("\\s+".toRegex(), " ")
62-
.trim()
6325
}

‎gradle/libs.versions.toml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ kotlin = "2.3.20"
55
agp = "9.1.1"
66
jvmToolchain = "25"
77
lucene = "10.5.2"
8-
seforimSemantic = "0.4.0"
8+
seforimSemantic = "0.4.1"
99
onnxruntime = "1.30.0"
1010
maven-publish = "0.36.0"
1111
kotlinx-coroutines = "1.10.2"

‎search/src/jvmMain/kotlin/io/github/kdroidfilter/seforimlibrary/search/HebrewTextUtils.kt‎

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,22 @@ object HebrewTextUtils {
5454
* @param text The input text
5555
* @return Text with final letters replaced
5656
*/
57-
fun replaceFinalsWithBase(text: String): String = text
58-
.replace('\u05DA', '\u05DB') // ך -> כ
59-
.replace('\u05DD', '\u05DE') // ם -> מ
60-
.replace('\u05DF', '\u05E0') // ן -> נ
61-
.replace('\u05E3', '\u05E4') // ף -> פ
62-
.replace('\u05E5', '\u05E6') // ץ -> צ
57+
fun replaceFinalsWithBase(text: String): String {
58+
if (text.none { it != baseLetter(it) }) return text
59+
val sb = StringBuilder(text.length)
60+
for (c in text) sb.append(baseLetter(c))
61+
return sb.toString()
62+
}
63+
64+
/** [c] with a final letter (sofit) as its base form: ך כ, ם מ, ן נ, ף פ, ץ צ. */
65+
fun baseLetter(c: Char): Char = when (c) {
66+
'\u05DA' -> '\u05DB'
67+
'\u05DD' -> '\u05DE'
68+
'\u05DF' -> '\u05E0'
69+
'\u05E3' -> '\u05E4'
70+
'\u05E5' -> '\u05E6'
71+
else -> c
72+
}
6373

6474
/**
6575
* Checks if a character is a Hebrew diacritic (nikud or teamim).
@@ -71,7 +81,10 @@ object HebrewTextUtils {
7181
val code = c.code
7282
return (code in 0x0591..0x05AF) || // teamim
7383
(code in 0x05B0..0x05BD) || // nikud + meteg
74-
(c == '\u05C1') || (c == '\u05C2') || (c == '\u05C7')
84+
(c == '\u05BF') || // rafe
85+
(c == '\u05C1') || (c == '\u05C2') || // shin and sin dots
86+
(c == '\u05C4') || (c == '\u05C5') || // upper and lower dots
87+
(c == '\u05C7')
7588
}
7689

7790
/**

0 commit comments

Comments
 (0)