Worker corpus scope¶
gutenberg_kg.serve.scope
¶
Corpus scoping for the worker, kept free of the handler's import-time startup.
handler opens stores and loads the embedder when it is imported, so nothing
in it can be unit-tested cheaply (see :mod:gutenberg_kg.serve.fusion). The
rules that turn a request's corpus value into a search scope are pure, so
they live here and the handler calls in.
A corpus value is one of:
all,gutenbergordiary-- the whole corpus or one half of it;- a genre name -- every book under
<genre>/; - a
"<genre>/<book>"catalog key -- that one book; "diaries/<title>"-- that one diary, searched in its own DiaryKG.
Keys can contain apostrophes and non-ASCII characters (the diary titles carry an em dash). They are passed through unchanged, never normalized.
dense_scope_sql(prefix)
¶
The where fragment that scopes the dense channel to a path prefix.
Single quotes are doubled, since book directory names can contain
apostrophes. _ and % in the prefix still act as LIKE wildcards;
:func:keep_in_scope removes what they over-match.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prefix
|
str
|
A |
required |
Returns:
| Type | Description |
|---|---|
str
|
|
Source code in src/gutenberg_kg/serve/scope.py
94 95 96 97 98 99 100 101 102 103 104 105 | |
diary_slug_for_key(key)
¶
Resolve a "diaries/<title>" key to its DiaryKG slug.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
A corpus value, |
required |
Returns:
| Type | Description |
|---|---|
str | None
|
The |
Source code in src/gutenberg_kg/serve/scope.py
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | |
keep_in_scope(rows, prefix)
¶
Drop rows whose file_path does not start with the exact prefix.
Applied to the vector store's rows (which carry file_path) before
fusion, so the top-k is still filled from in-scope rows.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rows
|
list[dict]
|
Vector-store rows, each with a |
required |
prefix
|
str | None
|
A |
required |
Returns:
| Type | Description |
|---|---|
list[dict]
|
The rows inside the prefix, in their original order. |
Source code in src/gutenberg_kg/serve/scope.py
108 109 110 111 112 113 114 115 116 117 118 119 120 | |
resolve_corpus(corpus, catalog_keys, genres)
¶
Turn a request's corpus value into a search scope.
The diary check runs before the catalog check: the diary keys are also in the catalog, and a DocKG book filter on them would silently return nothing.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
corpus
|
str
|
The request's |
required |
catalog_keys
|
Collection[str]
|
Every |
required |
genres
|
Collection[str]
|
Every valid genre name. |
required |
Returns:
| Type | Description |
|---|---|
dict
|
|
Source code in src/gutenberg_kg/serve/scope.py
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | |
scope_prefix(genre_filter, book_filter)
¶
The file_path prefix a DocKG search is restricted to.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
genre_filter
|
str | None
|
A genre name, or |
required |
book_filter
|
str | None
|
A |
required |
Returns:
| Type | Description |
|---|---|
str | None
|
|
Source code in src/gutenberg_kg/serve/scope.py
80 81 82 83 84 85 86 87 88 89 90 91 | |