Swift corpus packs¶
gutenberg_kg.export_swift
¶
Build the on-device corpus packs the native app searches locally.
Phase 0 of analysis/APP_ARCHITECTURE.md: turn bundles/gutenberg-all/
— a 2.9 GB DocKG graph.sqlite plus a 1.1 GB vector store plus four diary
KGs — into three SQLite files small enough to sit on a phone, carrying exactly
what the query path reads and nothing else.
Layout produced¶
::
<out>/
manifest.json pack versions, checksums, the embedder the packs require
core.pack catalog: genres, books, the Browse entry points
gutenberg.pack passages + FTS5 for the 241 books
gutenberg.vectors their embeddings, row-major, memory-mappable
diaries.pack the same, for the four diaries, with timestamps
diaries.vectors
golden.json reference top-k per query — the Swift parity gate
What is dropped, and why it is safe¶
The served query path (serve/handler.py:_semantic_search) is a dense kNN
plus an FTS5/BM25 query plus RRF over kind IN ('chunk','section'), with
content hydrated from SQLite afterwards. It never hops the graph. So the packs
carry chunk and section nodes only: the 324 K topic/entity/keyword nodes, every
edge, and the structured embed-text duplicate of each passage all stay behind.
Vectors are re-encoded from fp32 to int8, which is where most of the remaining
size goes.
Why the vectors sit beside the pack, not inside it¶
A vec0 virtual table cannot be read without the sqlite-vec C extension
compiled into the reader, and iOS ships stock SQLite — so a pack built that way
would not open on the device it was built for. Vendoring ten thousand lines of C
would buy nothing either: vec0's search is exhaustive, and so is the dot product
the app does instead over a memory-mapped file, with SIMD and no allocation.
The sidecar is therefore a 32-byte header (magic, dtype, dim, count) followed by
row-major vectors. passages.vector_index is a dense row number into it, so
a passage whose vector the source store lacks leaves no hole in the file. The
header lets a reader reject a truncated download instead of interpreting
whatever bytes it finds as embeddings.
Schema notes¶
Two title columns are deliberate: title is the work's title, which the
hit cards show, while node_title is the node's own — a section's chapter
name, which the Browse tab lists. Collapsing them loses the chapter list.
The FTS5 index is rebuilt here over clean passage text rather than copied,
because the worker's nodes_fts indexes the embed-text form. Lexical results
will therefore not match the worker's token for token — which is why
golden.json is generated from the pack itself. It is the contract the Swift
engine must reproduce, not a record of what the worker happened to return.
Reading the source¶
Column sets differ across the fleet's stores (a diary carries timestamp, a
book carries chapter), and they change as KGs are rebuilt, so every read
here is driven by PRAGMA table_info rather than a hardcoded SELECT.
Missing columns become NULL in the pack; they never become a crash three
hundred thousand rows into an export.
Usage¶
::
gutenkg export-swift # bundles/gutenberg-all → bundles/gutenberg-all/swift
gutenkg export-swift --dtype float # fp32 vectors, ~3x larger, exact
gutenkg export-swift --verify # measure int8 recall against the source
BundlePaths(root, graph, catalog, vectors, diaries=())
dataclass
¶
Everything the export reads, resolved and checked to exist.
DiarySource(slug, graph, vectors)
dataclass
¶
One DiaryKG inside the bundle.
ExportError
¶
Bases: RuntimeError
A bundle could not be read, or a required dependency is missing.
ExportOptions(bundle=DEFAULT_BUNDLE, out=None, dtype='int8', max_chars=0, include_diaries=True, with_vectors=True, golden=True, golden_k=10, verify=False, force=False, catalog_keys=None, diary_dirs=None, golden_queries=None, product_name=None, product_version=None)
dataclass
¶
Inputs to :func:export_swift.
catalog_keys, diary_dirs, golden_queries, product_name,
and product_version are the selective-bundle-export additions
(analysis/SELECTIVE_BUNDLE_EXPORT_PLAN.md phase 2). Every one of them
defaults to None, and None reproduces the export's original,
unfiltered behaviour exactly — an existing caller that never sets them
is unaffected.
resolved_out()
¶
Output directory; defaults to <bundle>/swift.
Source code in src/gutenberg_kg/export_swift.py
1815 1816 1817 | |
ExportReport(out, packs=list(), golden=None, verification=None, elapsed_s=0.0)
dataclass
¶
What an export produced.
PackStats(path, passages=0, vectors=0, missing_vectors=0, bytes=0, sha256='', sidecar=None, sidecar_bytes=0, sidecar_sha256='')
dataclass
¶
What a finished pack contains.
total_bytes
property
¶
Pack plus sidecar — what actually has to reach the device.
build_core_pack(dest, *, bundle, catalog, diary_count, catalog_keys=None)
¶
Write core.pack — the catalog the Browse tab and the scope picker read.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dest
|
Path
|
Output path. |
required |
bundle
|
BundlePaths
|
Resolved bundle paths. |
required |
catalog
|
dict[str, dict]
|
Parsed |
required |
diary_count
|
int
|
Diaries included in this export. |
required |
catalog_keys
|
frozenset[str] | None
|
Restrict to these |
None
|
Returns:
| Type | Description |
|---|---|
PackStats
|
Stats for the finished pack. |
Source code in src/gutenberg_kg/export_swift.py
780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 | |
build_golden(packs, *, k, dtype, queries=GOLDEN_QUERIES, progress=None)
¶
Record each golden query's top-k, as the packs themselves answer it.
The Swift engine is correct when it reproduces this file: same node ids in roughly the same order, same scores to two decimal places. Divergence localises to one of two places — the WordPiece tokenizer in front of the Core ML embedder, or the int8 quantisation — which is what makes the gate worth having.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
packs
|
dict[str, Path]
|
|
required |
k
|
int
|
Hits to record per query. |
required |
dtype
|
str
|
The packs' vector dtype. |
required |
queries
|
Sequence[str]
|
Queries to record. |
GOLDEN_QUERIES
|
progress
|
Optional status callable. |
None
|
Returns:
| Type | Description |
|---|---|
dict
|
The golden document, ready to serialise. |
Source code in src/gutenberg_kg/export_swift.py
1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 | |
build_passage_pack(dest, sources, *, catalog, dtype='int8', max_chars=0, with_vectors=True, browse_window=BROWSE_WINDOW_TRIGGER_CHARS, progress=None)
¶
Write a passage pack: content, an FTS5 index over it, and the vectors.
Passages are written first so SQLite assigns their rowids; vectors are then
streamed in against those same rowids. A passage whose vector is missing
from the source store is kept — it stays findable lexically and readable in
Browse — and counted in missing_vectors so a partial store is visible
rather than silent.
The pack id is <kg_name>:<node_id>, not the source node id verbatim.
Diary node ids (chunk:entry_0000_chunk_0.md:0000) name the entry file
within a book but not the book, and every diary numbers its entries from
entry_0000 — so merging four diaries into one table keyed by the raw
id silently dropped every later diary's colliding rows (4,601 of 27,462
passages, found 2026-09-01). The prefix restores uniqueness while keeping
the id an opaque string, which is all the search code here and the Swift
engine ever treat it as. The insert is a plain INSERT, so any residual
collision fails the export instead of shrinking the pack.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dest
|
Path
|
Output path. |
required |
sources
|
Sequence[_PassageSource]
|
KGs to merge into this pack (one for books, four for diaries). |
required |
catalog
|
dict[str, dict]
|
Parsed |
required |
dtype
|
str
|
|
'int8'
|
max_chars
|
int
|
Per-passage truncation; 0 keeps passages whole. |
0
|
with_vectors
|
bool
|
Skip the vector stage entirely when False. |
True
|
progress
|
Optional |
None
|
Returns:
| Type | Description |
|---|---|
PackStats
|
Stats for the finished pack. |
Source code in src/gutenberg_kg/export_swift.py
1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 | |
encode_vector(vector, dtype)
¶
Encode one vector for a vec0 column.
Vectors are L2-normalised before int8 quantisation. Cosine distance is
scale-invariant, so this changes no ranking — but it does guarantee the
* 127 scaling lands inside int8 range instead of clipping whatever the
embedder happened to emit.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vector
|
A numpy float32 array. |
required | |
dtype
|
str
|
|
required |
Returns:
| Type | Description |
|---|---|
bytes
|
The packed bytes for the column. |
Source code in src/gutenberg_kg/export_swift.py
679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 | |
export_swift(options, *, progress=None)
¶
Build the on-device packs from a bundle.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
options
|
ExportOptions
|
Export inputs. |
required |
progress
|
Optional |
None
|
Returns:
| Type | Description |
|---|---|
ExportReport
|
A report naming every file written. |
Raises:
| Type | Description |
|---|---|
ExportError
|
On a missing bundle, an unreadable store, or an
existing output directory without |
Source code in src/gutenberg_kg/export_swift.py
1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 | |
fts_match_expression(query)
¶
The any-term FTS5 expression — the recall fallback behind the phrase.
Every term is quoted and the terms are OR-ed, so an apostrophe or a question mark in the query cannot become FTS5 syntax — and a query is never rejected wholesale for one odd character.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query
|
str
|
The user's query text. |
required |
Returns:
| Type | Description |
|---|---|
str
|
An FTS5 MATCH expression, or |
Source code in src/gutenberg_kg/export_swift.py
1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 | |
fts_phrase_expression(query)
¶
The exact-phrase FTS5 expression: every term, adjacent and in order.
Tried before :func:fts_match_expression, because a rare phrase diluted
in a long chunk is exactly what the dense channel buries — "pillar of salt"
ranks the Lot's-wife verse 604th on cosine alone, and only a phrase match
recovers it. doc_kg.store.GraphStore.search_lexical does the same two
steps in the same order; this is the pack's copy of that behaviour.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query
|
str
|
The user's query text. |
required |
Returns:
| Type | Description |
|---|---|
str
|
A quoted phrase expression, or |
Source code in src/gutenberg_kg/export_swift.py
1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 | |
fts_terms(query)
¶
Tokenise a query into bare alphanumeric terms for FTS5.
Strips apostrophes, quotes and punctuation that would otherwise be read as
FTS5 query syntax, so Lot's becomes lot, s. Mirrors
doc_kg.store._fts_terms.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query
|
str
|
The user's query text. |
required |
Returns:
| Type | Description |
|---|---|
list[str]
|
The searchable terms, in order; empty when nothing is searchable. |
Source code in src/gutenberg_kg/export_swift.py
1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 | |
iter_source_passages(graph, *, max_chars=0, allowed_keys=None)
¶
Yield searchable passages from a graph.sqlite.
Applies the worker's own scope — kind IN ('chunk','section'), and never
a book's reference.md metadata sheet — so the pack holds the same set
the served query ranks.
Empty chunks are dropped. Empty sections are not: they carry no prose but
they are the chapter markers get_chapters lists and get_chapter
slices between, so dropping them would take the Browse tab with them.
:yields: Row dicts keyed by the columns in :data:_WANTED_NODE_COLUMNS,
with absent columns set to None.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
graph
|
Path
|
Path to a DocKG/DiaryKG |
required |
max_chars
|
int
|
Truncate |
0
|
allowed_keys
|
frozenset[str] | None
|
Restrict to nodes whose |
None
|
Source code in src/gutenberg_kg/export_swift.py
395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 | |
iter_source_vectors(vectors, *, allowed_keys=None)
¶
Stream (node_id, vector) from the bundle's sqlite-vec store.
Streams rather than materialising: the consolidated store holds 688 K vectors, and only the ~364 K searchable ones end up in the pack.
:yields: (id, numpy float32 array) pairs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vectors
|
Path | None
|
The |
required |
allowed_keys
|
frozenset[str] | None
|
Restrict to |
None
|
Raises:
| Type | Description |
|---|---|
ExportError
|
If there is no store. |
Source code in src/gutenberg_kg/export_swift.py
634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 | |
load_catalog(path)
¶
Load catalog.json: "<genre>/<book>" -> {genre, title, author, ...}.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path | None
|
Path to the catalog, or None. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, dict]
|
The catalog, or |
Source code in src/gutenberg_kg/export_swift.py
475 476 477 478 479 480 481 482 483 484 485 486 487 488 | |
locate_bundle(root)
¶
Resolve a bundle directory into the files the export reads.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
root
|
Path
|
Bundle root, e.g. |
required |
Returns:
| Type | Description |
|---|---|
BundlePaths
|
Resolved paths, with diaries discovered under |
Raises:
| Type | Description |
|---|---|
ExportError
|
If the root or its consolidated |
Source code in src/gutenberg_kg/export_swift.py
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 | |
read_vector_sidecar(sidecar)
¶
Memory-map a sidecar and return (matrix, dtype).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sidecar
|
Path
|
A |
required |
Returns:
| Type | Description |
|---|---|
|
An |
Raises:
| Type | Description |
|---|---|
ExportError
|
If the magic, dimension or length do not agree — a truncated download is the failure this catches. |
Source code in src/gutenberg_kg/export_swift.py
1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 | |
resolve_catalog_selectors(*, books, genres, catalog)
¶
Resolve --book/--genre selectors against a built bundle's catalog.
A sibling to bundle_spec.resolve_selection, deliberately not shared
with it: that resolver reads corpus/<genre>/<book>/reference.md from
the source tree used to build a bundle, while export-swift --book
filters a bundle that may be all that is present — a downloaded bundle
with no corpus/ beside it still has to work. Both apply the same
three rules in the same order (explicit key, ebook_id, unique bare name),
just against different data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
books
|
Sequence[str]
|
Raw |
required |
genres
|
Sequence[str]
|
Raw |
required |
catalog
|
dict[str, dict]
|
Parsed |
required |
Returns:
| Type | Description |
|---|---|
tuple[frozenset[str], tuple[str, ...]]
|
|
Source code in src/gutenberg_kg/export_swift.py
491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 | |
rrf_fuse(dense_ids, lexical_ids, k)
¶
Blend two rank lists with reciprocal rank fusion.
Byte-for-byte the arithmetic in handler._rrf_fuse, at the same
:data:RRF_K. A hit ranked well by either channel floats up; one ranked by
both wins.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dense_ids
|
Sequence[str]
|
Ids best-first by cosine. |
required |
lexical_ids
|
Sequence[str]
|
Ids best-first by BM25. |
required |
k
|
int
|
How many fused ids to return. |
required |
Returns:
| Type | Description |
|---|---|
list[str]
|
Fused ids, best-first. |
Source code in src/gutenberg_kg/export_swift.py
1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 | |
search_pack(pack, query_vector, query_text, *, k=10, genre=None, min_score=0.0, lexical=True)
¶
Run the packed corpus's own hybrid search — dense + BM25, fused by RRF.
:func:search_pack_with_provenance without the rescued set; see it for the
parameters.
Source code in src/gutenberg_kg/export_swift.py
1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 | |
search_pack_with_provenance(pack, query_vector, query_text, *, k=10, genre=None, min_score=0.0, lexical=True)
¶
Run the packed corpus's own hybrid search — dense + BM25, fused by RRF.
A direct translation of handler._semantic_search onto the pack: same
k * 3 oversampling in both channels, same :data:RRF_K, same
round(1 - distance, 4) score, and the same hydrate-missing-cosine step
so every fused hit carries an honest score.
The dense channel is a dot product over the memory-mapped sidecar rather than a vec0 query, which is what the Swift engine does with Accelerate. Both are exhaustive, so this is the same ranking by a simpler route.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pack
|
Path
|
A passage pack (its sidecar sits beside it). |
required |
query_vector
|
The query embedding (numpy float32, 384-d). |
required | |
query_text
|
str
|
The raw query, for the lexical channel. |
required |
k
|
int
|
Hits to return. |
10
|
genre
|
str | None
|
Restrict to one genre, as the worker's |
None
|
min_score
|
float
|
Drop hits below this cosine score. |
0.0
|
lexical
|
bool
|
Include the BM25 channel. False gives the dense channel
alone, which is what :func: |
True
|
Returns:
| Type | Description |
|---|---|
tuple[list[dict], set[str]]
|
Hit dicts in the worker's shape, best-first, and the node ids
among them the lexical channel rescued -- found by BM25 outside the
dense top |
Source code in src/gutenberg_kg/export_swift.py
1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 | |
split_source_path(file_path)
¶
Split a node file_path into (genre, book).
Paths are "<genre>/<book>/<document>.md". This is the same split
handler._enrich_catalog does to key into the catalog.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_path
|
str | None
|
A node's file path, or None. |
required |
Returns:
| Type | Description |
|---|---|
tuple[str | None, str | None]
|
|
Source code in src/gutenberg_kg/export_swift.py
540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 | |
table_columns(con, table)
¶
Column names present on table.
Every read in this module is shaped by this rather than by a hardcoded column list: DocKG and DiaryKG differ, and both change as they are rebuilt.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
con
|
Connection
|
An open connection. |
required |
table
|
str
|
Table name. |
required |
Returns:
| Type | Description |
|---|---|
set[str]
|
The column names, or an empty set when the table is absent. |
Source code in src/gutenberg_kg/export_swift.py
365 366 367 368 369 370 371 372 373 374 375 376 377 378 | |
verify_pack(pack, source, *, k, dtype, queries=GOLDEN_QUERIES, progress=None)
¶
Measure what quantisation cost, against exact fp32 ground truth.
Computes the true top-k by brute force over the source vectors — the same
method benchmarks/bench_sqlite_vec.py uses — and compares it with what
the pack's dense channel returns (search_pack(..., lexical=False); the
BM25 channel is deliberately off, because ground truth here is dense-only
and fusing lexical hits into one side reported the divergence as recall
loss — measured at 0.567 apparent vs 0.958 actual on 2026-09-01). This is
a build-time gate: an int8 recall that has quietly fallen is something to
see here, not in a user's answer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pack
|
Path
|
The finished pack. |
required |
source
|
_PassageSource
|
The KG whose source vectors are the ground truth. |
required |
k
|
int
|
Depth to compare at. |
required |
dtype
|
str
|
The pack's vector dtype. |
required |
queries
|
Sequence[str]
|
Queries to check. |
GOLDEN_QUERIES
|
progress
|
Optional status callable. |
None
|
Returns:
| Type | Description |
|---|---|
dict
|
|
Source code in src/gutenberg_kg/export_swift.py
1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 | |
window_oversized_sections(con, *, trigger_chars, part_chars=BROWSE_WINDOW_PART_CHARS)
¶
Split sections too long to read as one chapter into synthetic parts.
Browse lists kind='section' rows by char_start and slices the chunks
between consecutive markers, so a book whose whole text sits under one
heading renders as a single scroll — 373 K characters for Franklin's
Autobiography, 1.2 M for Boswell's Johnson. Works like those have no
divisions the converter can find, so the markers have to be manufactured
here.
Only sections past trigger_chars are touched, and those are divided into equal parts of roughly part_chars, cut at chunk boundaries. The two numbers have to differ: cutting everything down to part size fragments the real chapters of books that were already correct, which is a worse result than the monolith it fixes.
Measuring in characters rather than chunks keeps a part the same length to read across genres, since chunk sizes vary (a tenth are under 268 characters, a tenth over 510) and the verse chunker's are smaller again.
The extra rows carry no content and get no vector, which keeps this to the Browse tab: retrieval ranks chunks, and a marker with no text and no embedding can never be a hit. The parent section keeps its id, so anything holding one still resolves.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
con
|
Connection
|
Open connection to a passage pack. |
required |
trigger_chars
|
int
|
Only sections longer than this are split. Values <= 0 disable windowing. |
required |
part_chars
|
int
|
Length each part aims for. |
BROWSE_WINDOW_PART_CHARS
|
Returns:
| Type | Description |
|---|---|
int
|
Number of synthetic sections inserted. |
Source code in src/gutenberg_kg/export_swift.py
918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 | |
write_vector_sidecar(pack, sources, rowid_by_id, *, dtype, progress)
¶
Write <pack>.vectors and point each passage at its row.
The sidecar is a header plus row-major vectors — nothing else. The device
memory-maps it and multiplies straight out of the mapping, so a query never
allocates and the OS pages in only the part it touches. That is also why
the vectors do not live in the pack as a vec0 table: reading one needs
the sqlite-vec C extension compiled into the app, and vec0's search is
brute force regardless, so the extension would buy nothing.
passages.vector_index is dense (0..N-1) rather than the passage rowid,
because a passage whose vector the source store lacks must not leave a hole
in the file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pack
|
Path
|
The pack whose passages were just written. |
required |
sources
|
Sequence[_PassageSource]
|
KGs whose vector stores to stream. |
required |
rowid_by_id
|
dict[str, int]
|
Node id → the rowid its passage got. |
required |
dtype
|
str
|
|
required |
progress
|
Status callable. |
required |
Returns:
| Type | Description |
|---|---|
tuple[Path | None, int, int]
|
|
Source code in src/gutenberg_kg/export_swift.py
1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 | |