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
2 changes: 2 additions & 0 deletions SeforimApp/src/commonMain/composeResources/files/ABOUT.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

אני מודה ל־JetBrains על יצירת Kotlin, ל־Google על מסגרת העבודה Compose, ול־JetBrains על התאמתה לסביבת הדסקטופ. אני מודה גם לקרן Apache על Lucene, שאפשרה לי לבנות מנוע חיפוש עוצמתי במיוחד, וכן לכל מפתחי הספריות שבהן עושה התוכנה שימוש. בלעדיהם, דבר מכל זה לא היה מתאפשר.

אייקון היד הכותבת: Emoji One, ברישיון CC BY 4.0.

בסיס הנתונים של זית נשען על העבודה המרשימה של פרויקטי Sefaria ו־אוצריא, ואני מודה להם בכנות על האיכות והפתיחות של מאגרי הנתונים שלהם. מתוך מקורות אלו, בניתי מחדש ועיצבתי מחדש את בסיס הנתונים באופן מלא, כך שיהיה יעיל הרבה יותר וקריא באמת עבור מחשב. הוא תוכנן לא כאוסף טקסטים פשוט, אלא כמבנה המותאם לחיפוש, לניווט מהיר וללימוד מעמיק.

בבסיס זה השתמשתי בטכנולוגיות החזקות ביותר הזמינות כיום, כדי לבנות מה שלדעתי הוא מנוע חיפוש יעיל במיוחד ללימוד התורה.
Expand Down
12 changes: 3 additions & 9 deletions SeforimApp/src/commonMain/composeResources/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -287,10 +287,9 @@
<string name="please_select_a_book">אנא בחר ספר</string>

<!-- Home Page View -->
<string name="search_mode_reference">ספר</string>
<string name="search_mode_text">טקסט</string>
<string name="search_mode_reference_explicit">חיפוש לפי מקור</string>
<string name="search_mode_text_explicit">חיפוש בטקסט</string>
<string name="search_text_row">חיפוש במאגר: %1$s</string>
<string name="author_books_count">%1$d ספרים</string>
<string name="search_text_row_in_book">חיפוש בתוך %2$s: %1$s</string>
<string name="search_by_category_or_book">חפש לפי קטגוריה או ספר</string>
<string name="remove_selected_book">הסר ספר שנבחר</string>

Expand All @@ -302,11 +301,6 @@
<string name="reference_hint_5">תהילים פרק כג פסוק ד</string>

<!-- Book names only (for reference-mode first field) -->
<string name="reference_book_hint_1">משנת יסודות התורה</string>
<string name="reference_book_hint_2">שוע יוד</string>
<string name="reference_book_hint_3">תהילים</string>
<string name="reference_book_hint_4">ברכות</string>
<string name="reference_book_hint_5">שוע אורח חיים</string>

<!-- TOC examples (for second field when no book yet) -->
<string name="reference_toc_hint_1">פרק א</string>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,6 @@ fun TabsContent() {
HomeSearchCallbacks(
onReferenceQueryChanged = searchHomeViewModel::onReferenceQueryChanged,
onTocQueryChanged = searchHomeViewModel::onTocQueryChanged,
onFilterChange = searchHomeViewModel::onFilterChange,
onGlobalExtendedChange = searchHomeViewModel::onGlobalExtendedChange,
onSubmitTextSearch = { query ->
val tabId = latestCurrentTabId ?: return@HomeSearchCallbacks
Expand All @@ -148,6 +147,8 @@ fun TabsContent() {
onPickCategory = searchHomeViewModel::onPickCategory,
onPickBook = searchHomeViewModel::onPickBook,
onPickToc = searchHomeViewModel::onPickToc,
onPickAuthor = searchHomeViewModel::onPickAuthor,
onClearAuthor = searchHomeViewModel::onClearAuthor,
onOpenJump = { jump ->
val tabId = latestCurrentTabId ?: return@HomeSearchCallbacks
scope.launch { searchHomeViewModel.openJump(jump, tabId) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ fun BookContentPanel(
HomeSearchCallbacks(
onReferenceQueryChanged = {},
onTocQueryChanged = {},
onFilterChange = {},
onGlobalExtendedChange = {},
onSubmitTextSearch = {},
onOpenReference = {},
Expand Down

Large diffs are not rendered by default.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ import io.github.kdroidfilter.seforim.tabs.TabsDestination
import io.github.kdroidfilter.seforimapp.core.coroutines.runSuspendCatching
import io.github.kdroidfilter.seforimapp.core.deeplink.parseZayitDeepLink
import io.github.kdroidfilter.seforimapp.core.settings.AppSettings
import io.github.kdroidfilter.seforimapp.features.search.domain.reference.ReferenceParser
import io.github.kdroidfilter.seforimapp.features.search.domain.reference.ReferenceResolver
import io.github.kdroidfilter.seforimapp.features.search.domain.reference.RepositoryReferenceSource
import io.github.kdroidfilter.seforimapp.features.search.domain.reference.ResolvedReference
import io.github.kdroidfilter.seforimapp.framework.search.LuceneLookupSearchService
import io.github.kdroidfilter.seforimapp.framework.search.LuceneLookupSearchService.AuthorHit
import io.github.kdroidfilter.seforimapp.framework.search.MIN_BOOK_QUERY_LENGTH
import io.github.kdroidfilter.seforimapp.framework.session.SearchPersistedState
import io.github.kdroidfilter.seforimapp.framework.session.TabPersistedStateStore
Expand Down Expand Up @@ -95,14 +97,17 @@ data class TocSuggestionDto(

@Immutable
data class SearchHomeUiState(
val selectedFilter: SearchFilter = SearchFilter.TEXT,
val globalExtended: Boolean = false,
val suggestionsVisible: Boolean = false,
val isReferenceLoading: Boolean = false,
val categorySuggestions: List<CategorySuggestionDto> = emptyList(),
val bookSuggestions: List<BookSuggestionDto> = emptyList(),
/** Places the typed text points to (`חולין יב:`), listed above the book suggestions. */
val jumpSuggestions: List<ResolvedReference> = emptyList(),
/** Authors the typed text may name, listed before the books. */
val authorSuggestions: List<AuthorHit> = emptyList(),
/** The author picked: the books listed are then theirs. */
val selectedScopeAuthor: AuthorHit? = null,
val tocSuggestionsVisible: Boolean = false,
val isTocLoading: Boolean = false,
val tocSuggestions: List<TocSuggestionDto> = emptyList(),
Expand Down Expand Up @@ -131,6 +136,9 @@ class SearchHomeViewModel(

private val referenceResolver = ReferenceResolver(RepositoryReferenceSource(repository))

// The books of the picked author, filtered by what is typed next
private var authorBooks: List<BookSuggestionDto> = emptyList()

private val referenceQuery = MutableStateFlow("")
private val tocQuery = MutableStateFlow("")

Expand Down Expand Up @@ -212,13 +220,21 @@ class SearchHomeViewModel(
.distinctUntilChanged()
.collectLatest { qRaw ->
val q = qRaw.trim()
if (q.isBlank()) {
if (_uiState.value.selectedScopeAuthor != null) {
_uiState.value =
_uiState.value.copy(
isReferenceLoading = false,
bookSuggestions = authorBooks.filter { it.titleContains(q) },
suggestionsVisible = true,
)
} else if (q.isBlank()) {
_uiState.value =
_uiState.value.copy(
isReferenceLoading = false,
categorySuggestions = emptyList(),
bookSuggestions = emptyList(),
jumpSuggestions = emptyList(),
authorSuggestions = emptyList(),
suggestionsVisible = false,
)
} else {
Expand Down Expand Up @@ -292,22 +308,26 @@ class SearchHomeViewModel(
}
}

val authorsDeferred =
async(Dispatchers.Default) { lookup.suggestAuthors(q, limit = MAX_AUTHOR_SUGGESTIONS) }

val jumpsDeferred =
async(Dispatchers.IO) {
runSuspendCatching { referenceResolver.resolve(q) }.getOrDefault(emptyList())
}

Triple(catsDeferred.await(), booksDeferred.await(), jumpsDeferred.await())
Suggestions(catsDeferred.await(), booksDeferred.await(), jumpsDeferred.await(), authorsDeferred.await())
}
}

val (catSuggestions, bookSuggestions, jumpSuggestions) = result
val (catSuggestions, bookSuggestions, jumpSuggestions, authorSuggestions) = result
_uiState.value =
_uiState.value.copy(
isReferenceLoading = false,
categorySuggestions = catSuggestions,
bookSuggestions = bookSuggestions,
jumpSuggestions = jumpSuggestions,
authorSuggestions = authorSuggestions,
suggestionsVisible = true,
)
}
Expand Down Expand Up @@ -406,10 +426,44 @@ class SearchHomeViewModel(
)
}

/** Lists the books of [author], to pick one of them next. */
fun onPickAuthor(author: AuthorHit) {
_uiState.value =
_uiState.value.copy(
selectedScopeAuthor = author,
authorSuggestions = emptyList(),
jumpSuggestions = emptyList(),
bookSuggestions = emptyList(),
isReferenceLoading = true,
suggestionsVisible = true,
)
viewModelScope.launch {
authorBooks =
withContext(Dispatchers.IO) {
runSuspendCatching { repository.searchBooksByAuthor(author.name) }
.getOrDefault(emptyList())
// The query matches the name anywhere: keep this author's books only
.filter { book -> book.authors.any { it.id == author.id } }
.sortedWith(compareByDescending<Book> { it.isBaseBook }.thenBy { it.order })
.map { book -> BookSuggestionDto(book, buildCategoryPathTitlesCached(book.categoryId) + book.title) }
}
if (_uiState.value.selectedScopeAuthor?.id == author.id) {
_uiState.value = _uiState.value.copy(bookSuggestions = authorBooks, isReferenceLoading = false)
}
}
}

fun onClearAuthor() {
authorBooks = emptyList()
_uiState.value = _uiState.value.copy(selectedScopeAuthor = null, bookSuggestions = emptyList(), suggestionsVisible = false)
}

fun onPickBook(book: Book) {
authorBooks = emptyList()
// Update synchronously first
_uiState.value =
_uiState.value.copy(
selectedScopeAuthor = null,
selectedScopeCategory = null,
selectedScopeBook = book,
selectedScopeToc = null,
Expand Down Expand Up @@ -502,10 +556,6 @@ class SearchHomeViewModel(
)
}

fun onFilterChange(filter: SearchFilter) {
_uiState.value = _uiState.value.copy(selectedFilter = filter)
}

fun onGlobalExtendedChange(extended: Boolean) {
_uiState.value = _uiState.value.copy(globalExtended = extended)
}
Expand Down Expand Up @@ -545,8 +595,10 @@ class SearchHomeViewModel(
val selected = _uiState.value
val scope =
when {
selected.selectedScopeToc != null ->
// An alternative-TOC entry has no main-TOC scope: search its whole book
selected.selectedScopeToc != null && !selected.selectedScopeToc.isAltTocEntry() ->
SearchScope.Toc(bookId = selected.selectedScopeToc.bookId, tocId = selected.selectedScopeToc.id)
selected.selectedScopeToc != null -> SearchScope.Book(selected.selectedScopeToc.bookId)
selected.selectedScopeBook != null -> SearchScope.Book(selected.selectedScopeBook.id)
selected.selectedScopeCategory != null -> SearchScope.Category(selected.selectedScopeCategory.id)
else -> SearchScope.Global
Expand Down Expand Up @@ -726,3 +778,16 @@ private val ALT_TOC_LABELS =
"Daf" to "דפים",
"Contents" to "תוכן",
)

private const val MAX_AUTHOR_SUGGESTIONS = 4

// Typed text against a title, both without nikud or quotes (שו"ת / שו״ת)
private fun BookSuggestionDto.titleContains(typed: String): Boolean =
typed.isBlank() || ReferenceParser.normalizeName(book.title).contains(ReferenceParser.normalizeName(typed))

private data class Suggestions(
val categories: List<CategorySuggestionDto>,
val books: List<BookSuggestionDto>,
val jumps: List<ResolvedReference>,
val authors: List<AuthorHit>,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package io.github.kdroidfilter.seforimapp.features.search.domain

/**
* How an author's name is shown: respectfully, never bare (`יוסף קארו` → `הרב יוסף קארו`).
*
* The database keeps the upstream (Sefaria, Otzaria) names untouched: the honorific is display only.
* Acronyms (`רמב"ם`, `מהר"ם פדובה`: a quote in the first word) and names already carrying a title
* (`רבי…`, `חכם…`) stay as they are; every other name gets `הרב`. Names stored the catalog way
* (`ליבשיץ, יחזקאל`) are put back in reading order; nikud, bidi marks and extra spaces go.
*/
object AuthorNames {
private val NIKUD = Regex("[֑-ׇ]")
private val BIDI_MARKS = Regex("[\u200E\u200F\u202A-\u202E]")
private val QUOTES = Regex("[\"״]")
private val TITLES =
listOf(
"רבי",
"רב",
"רבן",
"רבנו",
"רבינו",
"הרב",
"ר'",
"ר׳",
"חכם",
"החכם",
"הגאון",
"מרן",
"מורנו",
"האדמו",
)
private const val HONORIFIC = "הרב"

fun display(raw: String): String {
val name =
readingOrder(
raw
.replace(NIKUD, "")
.replace(BIDI_MARKS, "")
.replace(Regex("\\s+"), " ")
.trim(),
)
if (name.isEmpty() || isAcronym(name) || hasTitle(name)) return name
return "$HONORIFIC $name"
}

// `רמב"ם`, `מהר"ם פדובה`; but not `יוסף בן משה באב"ד`, whose quote is in a later word
private fun isAcronym(name: String): Boolean = QUOTES.containsMatchIn(name.substringBefore(' '))

private fun hasTitle(name: String): Boolean =
TITLES.any { title ->
name == title ||
name.startsWith("$title ") ||
name.startsWith(title + "\"")
}

// `ליבשיץ, יחזקאל בן הילל` → `יחזקאל בן הילל ליבשיץ`: a short family name before a single comma
private fun readingOrder(name: String): String {
val parts = name.split(", ")
if (parts.size != 2 || parts[0].split(' ').size > 2 || parts[1].isBlank()) return name
return "${parts[1]} ${parts[0]}"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ class LuceneLookupSearchService(
)
}

private val authorSort = Sort(SortField("book_count", SortField.Type.LONG, true))

private val bookSort =
Sort(
SortField("is_base_book", SortField.Type.INT, true),
Expand All @@ -69,6 +71,13 @@ class LuceneLookupSearchService(
val score: Float,
)

/** An author of the lookup index, with the number of its books. */
data class AuthorHit(
val id: Long,
val name: String,
val bookCount: Int,
)

data class BookHit(
val id: Long,
val categoryId: Long,
Expand Down Expand Up @@ -141,6 +150,48 @@ class LuceneLookupSearchService(
}
}

/**
* Authors whose name or alias (רעק״א, ראב״ע...) starts with the typed words, those with the
* most books first. Empty below [MIN_BOOK_QUERY_LENGTH] characters, or with an index built
* before authors were indexed.
*/
fun suggestAuthors(
raw: String,
limit: Int,
): List<AuthorHit> {
val q = normalizeHebrew(raw)
if (q.length < MIN_BOOK_QUERY_LENGTH) return emptyList()
// Aliases are indexed whole (רעקא) and split at their gershayim (רעק + א): the split reading
// only when the whole one finds nobody, as its one-letter pieces match many names
val whole = authorsMatching(q, limit)
if (whole.isNotEmpty()) return whole
val split = normalizeHebrew(raw.replace(Regex("[\"'״׳]"), " "))
return if (split == q) whole else authorsMatching(split, limit)
}

private fun authorsMatching(
normalized: String,
limit: Int,
): List<AuthorHit> {
val tokens = normalized.split("\\s+".toRegex()).filter { it.isNotEmpty() }
if (tokens.isEmpty()) return emptyList()
return withSearcher { searcher ->
val query = BooleanQuery.Builder()
query.add(TermQuery(Term("type", "author")), BooleanClause.Occur.FILTER)
tokens.forEach { t -> query.add(PrefixQuery(Term("q", t)), BooleanClause.Occur.MUST) }
val top = searcher.search(query.build(), limit, authorSort)
val stored = searcher.storedFields()
top.scoreDocs.map { sd ->
val doc = stored.document(sd.doc)
AuthorHit(
id = doc.getField("author_id").numericValue().toLong(),
name = doc.getField("author_name").stringValue(),
bookCount = doc.getField("book_count").numericValue().toInt(),
)
}
}
}

fun searchTocPrefix(
raw: String,
limit: Int = 20,
Expand Down
Loading
Loading