Skip to content
Merged
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 @@ -7,6 +7,8 @@ import io.github.kdroidfilter.seforimlibrary.search.SearchEngine
import io.github.kdroidfilter.seforimlibrary.search.SearchFacets
import io.github.kdroidfilter.seforimlibrary.search.SearchPage
import io.github.kdroidfilter.seforimlibrary.search.SearchSession
import io.github.kdroidfilter.seforimlibrary.search.SnippetSources
import io.github.kdroidfilter.seforimlibrary.search.TextHighlights
import io.github.kdroidfilter.seforimlibrary.search.WARMUP_PAGE_SIZE
import io.github.kdroidfilter.seforimlibrary.search.WARMUP_QUERIES
import io.github.kdroidfilter.seforimlibrary.search.WARMUP_ROUNDS
Expand All @@ -16,8 +18,6 @@ import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import kotlinx.coroutines.withContext
import org.jsoup.Jsoup
import org.jsoup.safety.Safelist
import java.nio.file.Path

/**
Expand Down Expand Up @@ -93,19 +93,41 @@ internal class HybridSearchEngine(
near: Int,
): String = lexical.buildSnippet(rawText, query, near)

override fun findInBookCandidates(
override fun highlightRanges(
text: String,
query: String,
bookId: Long,
): LongArray? = lexical.findInBookCandidates(query, bookId)
): List<IntRange> = lexical.highlightRanges(text, query)

override suspend fun semanticSpan(
query: String,
override fun rangeSnippet(
text: String,
): String? {
val semantic = ensureSemantic() ?: return null
return withContext(Dispatchers.Default) { semantic.bestPassage(query, text) }
ranges: List<IntRange>,
): String = lexical.rangeSnippet(text, ranges)

override suspend fun highlights(
texts: List<String>,
query: String,
): List<TextHighlights> {
val words = texts.map { lexical.highlightRanges(it, query) }
// Lines without the query's words: the passage closest in meaning, all of them in one batched model run
val byMeaning = texts.indices.filter { words[it].isEmpty() && texts[it].isNotBlank() }
val semantic = if (byMeaning.isEmpty()) null else ensureSemantic()
if (semantic == null) return words.map { TextHighlights(it) }
val passages = withContext(Dispatchers.Default) { semantic.bestPassages(query, byMeaning.map { texts[it] }) }
val passageOf = byMeaning.indices.associate { byMeaning[it] to passages[it] }
return texts.mapIndexed { i, text ->
if (i !in passageOf) return@mapIndexed TextHighlights(words[i])
// A single clause is the passage itself
val passage = passageOf[i] ?: text.trim()
val at = text.indexOf(passage)
TextHighlights(if (at < 0) emptyList() else listOf(at until at + passage.length), byMeaning = true)
}
}

override fun findInBookCandidates(
query: String,
bookId: Long,
): LongArray? = lexical.findInBookCandidates(query, bookId)

override suspend fun denseReady(): Boolean = ensureSemantic() != null

override suspend fun attachSnippets(
Expand Down Expand Up @@ -133,10 +155,9 @@ internal class HybridSearchEngine(
limit: Int,
): List<Long> {
if (query.isBlank()) return emptyList()
val semantic = ensureSemantic() ?: return emptyList()
return withContext(Dispatchers.Default) {
semantic.search(query, limit, bookIds = listOf(bookId)).map { it.lineId }
}
// The search itself, scoped to the book: the lines holding the query's words and those closest in meaning,
// fused as the results page fuses them
return fuse(query, near = SMART_FIND_NEAR, bookIds = listOf(bookId), baseOnly = false).take(limit).map { it.lineId }
}

override fun computeFacets(
Expand Down Expand Up @@ -199,37 +220,6 @@ internal class HybridSearchEngine(
.map { (lineId, score) -> FusedEntry(lineId, score.toFloat(), lexById[lineId]) }
}

/**
* Snippets for dense-only hits: the lexical builder can't anchor (the query words aren't in the text), so bold the
* passage closest in meaning as one span inside a context window. All passages come from one batched model run;
* a null keeps the lexical snippet.
*/
private suspend fun semanticSnippets(
query: String,
rawTexts: List<String>,
near: Int,
): List<String?> {
val semantic = ensureSemantic() ?: return rawTexts.map { null }
// Jsoup.clean returns HTML-escaped text; substrings stay escaped, so <b> is spliced in directly
// (same convention as the lexical snippet builder)
val cleaned = rawTexts.map { Jsoup.clean(it, Safelist.none()) }
val passages = withContext(Dispatchers.Default) { semantic.bestPassages(query, cleaned) }
return cleaned.mapIndexed { i, clean ->
val passage = passages[i] ?: return@mapIndexed null
val idx = clean.indexOf(passage)
if (idx < 0) return@mapIndexed lexical.buildSnippet(rawTexts[i], passage, near)
val from = (idx - SNIPPET_CONTEXT).coerceAtLeast(0)
val to = (idx + passage.length + SNIPPET_CONTEXT).coerceAtMost(clean.length)
buildString {
if (from > 0) append("…")
append(clean, from, idx)
append("<b>").append(passage).append("</b>")
append(clean, idx + passage.length, to)
if (to < clean.length) append("…")
}
}
}

private inner class HybridSession(
private val query: String,
private val near: Int,
Expand Down Expand Up @@ -275,16 +265,22 @@ internal class HybridSearchEngine(
val lexicalIds = fused.orEmpty().mapNotNullTo(HashSet()) { entry -> entry.lexical?.lineId }
val denseOnly = hits.filter { it.lineId !in lexicalIds && it.rawText.isNotBlank() }
if (denseOnly.isEmpty()) return emptyMap()
val snippets = semanticSnippets(query, denseOnly.map { it.rawText }, near)
return buildMap { denseOnly.forEachIndexed { i, hit -> snippets[i]?.let { put(hit.lineId, it) } } }
// Snippet text, as the lexical snippets: a line the query's words show in keeps its lexical snippet
val texts = denseOnly.map { SnippetSources.clean(it.rawText) }
val highlights = highlights(texts, query)
return buildMap {
denseOnly.forEachIndexed { i, hit ->
if (highlights[i].byMeaning) put(hit.lineId, lexical.rangeSnippet(texts[i], highlights[i].ranges))
}
}
}

override fun close() {}
}

private companion object {
const val RRF_K = 60
const val SMART_FIND_NEAR = 5
const val CANDIDATES = 150
const val SNIPPET_CONTEXT = 90 // chars of context kept on each side of the passage
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,22 @@ fun highlightAnnotated(
return builder.toAnnotatedString()
}

/** A copy of this text with a [color] background over each of [ranges]. */
fun AnnotatedString.withBackground(
ranges: List<IntRange>,
color: Color,
): AnnotatedString {
if (ranges.isEmpty()) return this
val builder = AnnotatedString.Builder()
builder.append(this)
for (r in ranges) {
val start = r.first.coerceIn(0, length)
val end = (r.last + 1).coerceAtMost(length)
if (end > start) builder.addStyle(SpanStyle(background = color), start, end)
}
return builder.toAnnotatedString()
}

/**
* Like [highlightAnnotated], but emphasizes the [currentIndex]-th match (from 0) with a different
* color. A rank, unlike an offset, holds whatever diacritics are hidden.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ import io.github.kdroidfilter.seforimapp.core.presentation.text.drawNoteUnderlin
import io.github.kdroidfilter.seforimapp.core.presentation.text.findAllMatchesOriginal
import io.github.kdroidfilter.seforimapp.core.presentation.text.normalizeQueryForHebrew
import io.github.kdroidfilter.seforimapp.core.presentation.text.noteDisplayRanges
import io.github.kdroidfilter.seforimapp.core.presentation.text.withBackground
import io.github.kdroidfilter.seforimapp.core.presentation.typography.FontCatalog
import io.github.kdroidfilter.seforimapp.features.bookcontent.BookContentEvent
import io.github.kdroidfilter.seforimapp.features.bookcontent.state.LineConnectionsSnapshot
Expand Down Expand Up @@ -932,27 +933,8 @@ fun BookContentView(
val line = lazyPagingItems[index]

if (line != null) {
// Smart find: this line's passage closest in meaning to the query, computed
// on the DISPLAYED text (same HTML pipeline as rendering) so the span is
// always found back — short passages included. Falls back to the whole line.
// Smart find: the line is highlighted as a search result is (see LineItem)
val isSmartMatch = showFind && smartModeEnabled && line.id in semanticFindIds
val smartPassage by androidx.compose.runtime.produceState<String?>(
null,
line.id,
persistedFindQuery,
isSmartMatch,
) {
value =
if (!isSmartMatch || persistedFindQuery.length < 2) {
null
} else {
withContext(kotlinx.coroutines.Dispatchers.Default) {
val plain = buildAnnotatedFromHtml(line.content, textSize).text
runCatching { appGraph.searchEngine.semanticSpan(persistedFindQuery, plain) }
.getOrNull() ?: plain.trim().ifBlank { null }
}
}
}
val altHeadings = altHeadingsByLineId[line.id]
val isCurrentSelected = line.id in selectedLineIds
val useThickBar = shouldUseThickBar(line.id, primarySelectedLineId, isTocEntrySelection)
Expand Down Expand Up @@ -1048,15 +1030,9 @@ fun BookContentView(
baseTextSize = textSize,
lineHeight = lineHeight,
boldScale = boldScaleForPlatform,
highlightQuery =
when {
!showFind -> null
// Smart mode: highlight this line's own semantic
// passage (one contiguous span) if it's a match;
// simple mode: the literal query.
smartModeEnabled -> smartPassage
else -> findState.text.toString()
},
// Simple mode: the literal query; smart mode: the search's highlighting
highlightQuery = findState.text.toString().takeIf { showFind && !smartModeEnabled },
smartQuery = persistedFindQuery.takeIf { isSmartMatch && it.length >= 2 },
currentMatchOrdinal =
liveMatch?.takeIf { showFind && it.lineId == line.id }?.ordinal,
findMatchLocator =
Expand Down Expand Up @@ -1417,6 +1393,9 @@ private fun LineItem(
lineHeight: Float = 1.5f,
boldScale: Float = 1.0f,
highlightQuery: String? = null,
// Smart find: the line highlighted as a search result for this query (its words, else the passage closest in
// meaning), on the text as displayed
smartQuery: String? = null,
currentMatchOrdinal: Int? = null,
// Given to the line showing the current match only, which publishes where the match sits
findMatchLocator: FindMatchLocator? = null,
Expand Down Expand Up @@ -1516,6 +1495,14 @@ private fun LineItem(
buildAnnotatedFromHtml(lineContent, baseTextSize, boldScale = 1f).text
}

val searchEngine = LocalAppGraph.current.searchEngine
val smartRanges by produceState<List<IntRange>?>(null, annotated.text, smartQuery) {
value =
smartQuery?.let { query ->
runSuspendCatching { searchEngine.highlights(listOf(annotated.text), query).single().ranges }.getOrNull()
}
}

// Build highlighted text when a query is active (>= 2 chars)
val baseHl =
JewelTheme.globalColors.outlines.focused
Expand All @@ -1526,6 +1513,7 @@ private fun LineItem(
annotated,
highlightQuery,
currentMatchOrdinal,
smartRanges,
baseHl,
currentHl,
userHighlights,
Expand All @@ -1541,13 +1529,19 @@ private fun LineItem(
originalText = originalPlainText,
diacritics = diacritics,
)
io.github.kdroidfilter.seforimapp.core.presentation.text.highlightAnnotatedWithCurrent(
annotated = withUserHighlights,
query = highlightQuery,
currentIndex = currentMatchOrdinal,
baseColor = baseHl,
currentColor = currentHl,
)
val ranges = smartRanges
if (ranges != null) {
// The line is one smart match: all its spans take the current color when it is the current one
withUserHighlights.withBackground(ranges, if (currentMatchOrdinal != null) currentHl else baseHl)
} else {
io.github.kdroidfilter.seforimapp.core.presentation.text.highlightAnnotatedWithCurrent(
annotated = withUserHighlights,
query = highlightQuery,
currentIndex = currentMatchOrdinal,
baseColor = baseHl,
currentColor = currentHl,
)
}
}

// Dotted grey underline marking the noted ranges (drawn from the text layout so it supports
Expand All @@ -1561,10 +1555,9 @@ private fun LineItem(

if (findMatchLocator != null) {
val matchStart =
remember(displayText, highlightQuery, currentMatchOrdinal) {
highlightQuery
?.let { findAllMatchesOriginal(displayText.text, it) }
?.getOrNull(currentMatchOrdinal ?: 0)
remember(displayText, highlightQuery, smartRanges, currentMatchOrdinal) {
(smartRanges ?: highlightQuery?.let { findAllMatchesOriginal(displayText.text, it) })
?.getOrNull(if (smartRanges != null) 0 else currentMatchOrdinal ?: 0)
?.first ?: 0
}
SideEffect { findMatchLocator.matchStart = matchStart }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,13 @@ import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.compose.ui.zIndex
import io.github.kdroidfilter.seforim.htmlparser.buildAnnotatedFromHtml
import io.github.kdroidfilter.seforimapp.core.coroutines.runSuspendCatching
import io.github.kdroidfilter.seforimapp.core.presentation.components.FindInPageBar
import io.github.kdroidfilter.seforimapp.core.presentation.components.syncFindField
import io.github.kdroidfilter.seforimapp.core.presentation.tabs.LocalTabSelected
import io.github.kdroidfilter.seforimapp.core.presentation.text.DiacriticsMode
import io.github.kdroidfilter.seforimapp.core.presentation.text.highlightAnnotated
import io.github.kdroidfilter.seforimapp.core.presentation.text.withBackground
import io.github.kdroidfilter.seforimapp.core.presentation.typography.FontCatalog
import io.github.kdroidfilter.seforimapp.features.author.AUTHOR_ERAS
import io.github.kdroidfilter.seforimapp.features.author.authorYears
Expand Down Expand Up @@ -841,18 +843,7 @@ private fun PassagePreview(
val (categories, place) = remember(pieces, hit.bookTitle) { categoriesAndPlace(pieces, hit.bookTitle) }
val lines = preview.lines
val fontFamily = FontCatalog.familyFor(bookFontCode)
// The words the engine matched, as bold in the result's snippet (its variants: מן העין for מהעין),
// so the preview marks the same ones as the list
val words =
remember(hit.snippet, query) {
BOLD_SPAN
.findAll(hit.snippet)
.map { it.groupValues[1].replace(HTML_TAG, "").trim() }
.filter { it.length > 1 }
.distinct()
.toList()
.ifEmpty { query.split(Regex("\\s+")).filter { it.length > 1 } }
}
val searchEngine = LocalAppGraph.current.searchEngine
val highlight = accent.copy(alpha = 0.22f)
val scroll = rememberScrollState()
// The found line in view, once laid out
Expand Down Expand Up @@ -885,12 +876,21 @@ private fun PassagePreview(
) {
lines.forEach { line ->
val found = line.id == hit.lineId
val text =
remember(line.id, textSize, words, highlight) {
words.fold(buildAnnotatedFromHtml(line.content, textSize)) { acc, word ->
highlightAnnotated(acc, word, highlight)
}
val annotated = remember(line.id, textSize) { buildAnnotatedFromHtml(line.content, textSize) }
// The search's own highlighting: the query's words, and in the found line, when none is there,
// the passage closest in meaning (as its snippet)
val ranges by produceState(
searchEngine.highlightRanges(annotated.text, query),
annotated,
query,
found,
) {
if (found) {
runSuspendCatching { searchEngine.highlights(listOf(annotated.text), query).single().ranges }
.onSuccess { value = it }
}
}
val text = remember(annotated, ranges, highlight) { annotated.withBackground(ranges, highlight) }
val lineHover = remember { MutableInteractionSource() }
val lineHovered by lineHover.collectIsHoveredAsState()
Row(
Expand Down
Loading
Loading