Corpus build pipeline¶
gutenberg_kg.build_corpus
¶
build_corpus.py — Build a single consolidated DocKG over the whole Gutenberg
corpus (or a chosen subset of genres), written to bundles/<name>/.dockg/.
This differs from :mod:gutenberg_kg.ingest, which builds one DocKG per book
and federates them through the KGRAG registry. Here we walk corpus/ once and
produce a single graph.sqlite + vectors.sqlite (sqlite-vec) index — the
artifact that gets baked into the standalone "fat image" (Pepys-style: pull,
run, query directly).
Genre is recoverable for free at query time: with the walk rooted at corpus/,
every node's file_path is <genre>/<book>/<file>.md, so the genre is just
the first path segment — no schema change, no tagging pass.
Chunk strategies are applied per-genre group. Sacred texts (Bible KJV, Quran,
Torah, etc.) use the verse chunker which honours chapter:verse numbering.
All other genres default to semantic. Override via --strategy genre:strategy
or the strategy_overrides field on :class:BuildCorpusOptions.
DiaryKG indices (already built per-diary with temporal metadata) are not
re-ingested through DocKG. Instead, each corpus/diaries/*/.diarykg/ index
is copied verbatim into bundles/<name>/diaries/; the handler can register
both the DocKG and the DiaryKG indices through KGRAG.
Output layout::
bundles/
gutenberg-all/
.dockg/
graph.sqlite
vectors.sqlite (sqlite-vec; no lancedb/ — a stale one is purged)
catalog.json
diaries/
pepys-complete/.diarykg/
evelyn-vol1/.diarykg/
…
bundles/ is gitignored and already in [tool.dockg].exclude, so the
consolidated index is never re-ingested by a stray repo-root dockg build.
Author: Eric G. Suchanek, PhD License: Elastic 2.0
BuildCorpusOptions(output=None, similar_k=DEFAULT_SIMILAR_K, similar_max_degree=DEFAULT_SIMILAR_K, discover_similar=False, n_workers=4, embed_batch_size=64, embed_device='auto', wipe=True, update=False, dry_run=False, quiet=False, diaries_only=False, strategy_overrides=dict(), catalog_keys=None, diary_dirs=None, product_name=None, product_version=None, spec_path=None, force_overwrite_full=False)
dataclass
¶
Flags controlling a consolidated corpus build.
BuildError
¶
Bases: RuntimeError
A book-filtered build's selection could not be honoured as asked.
assert_selection(files, catalog_keys)
¶
Hard-error if the name-based exclude let an unselected book through.
Cheap — a set difference over the file list already walked — and it is
what makes :func:derive_exclude's basename-only exclusion safe to ship:
if two genres ever did share a book directory name, this is what turns
that into a build failure instead of a bundle silently carrying content
nobody asked for.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
files
|
list[Path]
|
Paths returned by |
required |
catalog_keys
|
frozenset[str]
|
The resolved |
required |
Raises:
| Type | Description |
|---|---|
BuildError
|
If any walked file lies outside the selection. |
Source code in src/gutenberg_kg/build_corpus.py
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 | |
build_catalog(genres, out_dir, *, catalog_keys=None)
¶
Write a catalog.json sidecar mapping <genre>/<book> → book metadata.
Parses each selected book's reference.md for author, title, and Gutenberg
ID. The key matches the <genre>/<book> prefix of every node's
file_path, so the handler can join author/title onto a hit at query time
— no node-schema change, and the file bakes into the image alongside the
index (it lives inside .dockg/).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
genres
|
list[str]
|
Selected genres. |
required |
out_dir
|
Path
|
The bundle's |
required |
catalog_keys
|
frozenset[str] | None
|
Restrict to these keys within |
None
|
Returns:
| Type | Description |
|---|---|
tuple[int, int]
|
|
Source code in src/gutenberg_kg/build_corpus.py
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 | |
bundle_diaries(out_dir, *, diary_dirs=None)
¶
Copy existing .diarykg/ indices from corpus/diaries/ into the bundle.
DiaryKG indices carry temporal metadata (YAML timestamps, diary-specific
chunks) that DocKG's semantic pipeline cannot reproduce. We copy them
verbatim so the handler can register both KGKind.GUTENBERG (the DocKG
index) and KGKind.DIARY (each DiaryKG) through KGRAG — without
re-ingesting the diaries.
Destination layout::
bundles/<name>/diaries/<diary-name>/.diarykg/
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
out_dir
|
Path
|
The bundle's |
required |
diary_dirs
|
tuple[str, ...] | None
|
Diary slugs to include (matching
|
None
|
Returns:
| Type | Description |
|---|---|
int
|
Number of diary indices copied. |
Source code in src/gutenberg_kg/build_corpus.py
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 | |
derive_exclude(genres, *, only_books=None)
¶
Return the set of directory names to prune from the corpus/ walk.
Excludes every genre not selected, the non-genre corpus dirs, and the
built-in DocKG SKIP_DIRS (.git, .dockg, .venv, …). Pruning
the unselected genres lets us keep the walk rooted at corpus/ so node
file_paths stay genre-prefixed regardless of the subset chosen.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
genres
|
list[str]
|
Selected genres. |
required |
only_books
|
set[str] | None
|
Book directory names to keep within the selected
genres; every sibling not in this set is added to the exclude set
too. |
None
|
Returns:
| Type | Description |
|---|---|
set[str]
|
Directory names to exclude at every level of the walk. This is a flat set of basenames, pruned wherever they occur in the
walk — it cannot tell one genre's "Republic" from another's. That is
a real gap in principle and a closed one in practice: every book
directory name in the corpus is unique across all 253 books today.
:func: |
Source code in src/gutenberg_kg/build_corpus.py
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 | |
derive_output_name(genres, override)
¶
Return the bundle directory name for a genre selection.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
genres
|
list[str]
|
Selected genres (sorted by caller; full set == all genres). |
required |
override
|
str | None
|
Explicit |
required |
Returns:
| Type | Description |
|---|---|
str
|
|
Source code in src/gutenberg_kg/build_corpus.py
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 | |
ensure_diaries_built(dry_run=False, quiet=False)
¶
Reconstruct any unbuilt diary indices so :func:bundle_diaries finds them.
Runs the DiaryKG pipeline — chunk-diaries (.md → .diary/) then
build-diaries (.diary/ → .diarykg/) — idempotently: diaries that
already have a .diary/ and .diarykg/ are skipped. This makes
gutenkg build-corpus self-sufficient on a clean clone instead of relying
on the Makefile to sequence build-diaries first.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dry_run
|
bool
|
Print the plan without executing. |
False
|
quiet
|
bool
|
Suppress per-stage DiaryKG build progress. |
False
|
Returns:
| Type | Description |
|---|---|
int
|
0 on success, 1 if chunking or building any diary failed. |
Source code in src/gutenberg_kg/build_corpus.py
356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 | |
fmt_duration(seconds)
¶
Human-readable duration string.
Source code in src/gutenberg_kg/build_corpus.py
387 388 389 390 391 392 393 394 395 | |
run_build_corpus(genres, opts)
¶
Build one consolidated DocKG over the selected genres.
Genres are grouped by their effective chunk strategy, then each group is
walked and parsed into the same graph.sqlite (first group wipes, the
rest append). A single embedding pass and a single vector index pass run
over the combined node table. Finally, any DiaryKG indices found under
corpus/diaries/ are copied verbatim into the bundle.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
genres
|
list[str]
|
Genre names to include (already validated; empty == all). |
required |
opts
|
BuildCorpusOptions
|
Build option flags. |
required |
Returns:
| Type | Description |
|---|---|
int
|
0 on success, 1 on failure. |
Source code in src/gutenberg_kg/build_corpus.py
488 489 490 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 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 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 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 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 850 851 852 853 854 855 856 857 | |
write_product_json(bundle_dir, *, name, version, catalog_keys, diary_dirs, materialize='rebuild', spec_path=None)
¶
Freeze a selective build's resolved selection and checksums to disk.
A rollback and audit record only — neither the worker nor the Swift
reader reads it. Written only for a book-filtered build; the plain
gutenberg-all build has no selection to freeze.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bundle_dir
|
Path
|
The bundle root, |
required |
name
|
str
|
Product name — the spec's |
required |
version
|
str | None
|
Product version, or |
required |
catalog_keys
|
frozenset[str]
|
The resolved |
required |
diary_dirs
|
tuple[str, ...]
|
The resolved diary slugs, possibly empty. |
required |
materialize
|
str
|
Always |
'rebuild'
|
spec_path
|
Path | None
|
The spec file used, if any; hashed into
|
None
|
Returns:
| Type | Description |
|---|---|
Path
|
Path to the written |
Source code in src/gutenberg_kg/build_corpus.py
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 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 | |