Download & ingest¶
gutenberg_kg.ingest
¶
ingest.py — Build per-book DocKGs for every book in gutenberg_kg, register them in the KGRAG registry, and add them to genre corpora and gutenberg-all.
Usage: python scripts/ingest.py [OPTIONS]
Options: --genre GENRE Process only this genre (repeatable; default: all) --force-build Rebuild even if .dockg already exists --force-register Re-register even if KG name already in registry --push git add + commit + push after each genre completes --dry-run Print actions without executing anything --registry PATH Override registry path
Examples: python scripts/ingest.py python scripts/ingest.py --genre shakespeare --genre ancient-classical python scripts/ingest.py --force-build --genre philosophy python scripts/ingest.py --push python scripts/ingest.py --dry-run
BookResult(name, genre, status, elapsed=0.0, nodes=0, edges=0)
dataclass
¶
Timing and outcome for one book.
GenreSummary(genre, results=list(), wall_elapsed=0.0)
dataclass
¶
Aggregated results for one genre.
built
property
¶
Number of books successfully built in this genre.
edges
property
¶
Total DocKG edges across all books in this genre.
elapsed
property
¶
Total wall-clock seconds across all books in this genre.
failed
property
¶
Number of books that failed to build.
nodes
property
¶
Total DocKG nodes across all books in this genre.
skipped
property
¶
Number of books skipped (already up-to-date).
total
property
¶
Total books processed in this genre.
IngestOptions(force_build=False, force_register=False, dry_run=False, push=False, quiet=False)
dataclass
¶
Flags controlling ingest behaviour.
add_to_corpus(corp_reg, corpus_name, kg_entry, dry_run=False)
¶
Add kg_entry to corpus_name (idempotent via add_kg's dedup).
Source code in src/gutenberg_kg/ingest.py
357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 | |
build_dockg(book_dir, dry_run=False, embedder=None, quiet=False)
¶
Build DocKG for book_dir in-process, reusing a shared embedder if provided.
Mirrors the three-step pipeline used by dockg build:
corpus → SQLite, SQLite → JSONL embedding cache, cache → sqlite-vec.
Programming errors (a bad import, a signature drift against doc_kg) are re-raised rather than reported per book: they fail identically for every book, so surfacing them once aborts the run instead of printing the same message 249 times. Per-book data failures are caught and reported.
Source code in src/gutenberg_kg/ingest.py
247 248 249 250 251 252 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 297 298 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 | |
dockg_defect(store_dir)
¶
Return why a .dockg store is unusable, or None if it is complete.
:meth:build_dockg writes graph.sqlite before it builds the vector
store, so a run that dies in between leaves a valid graph with no vectors.
Checking the graph alone would call that shape "already built": the book
would be skipped on the next run and then registered with no semantic
search, which fails silently at query time rather than at ingest time.
The vector row count comes from the vec_nodes_rowids shadow table rather
than vec_nodes itself, because the latter is a vec0 virtual table
and cannot be opened without the sqlite-vec extension loaded.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
store_dir
|
Path
|
The book's |
required |
Returns:
| Type | Description |
|---|---|
str | None
|
A short reason string, or |
Source code in src/gutenberg_kg/ingest.py
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 232 233 234 235 236 237 238 239 240 241 242 243 244 | |
ensure_corpus(corp_reg, name, description='', dry_run=False)
¶
Create corpus if it doesn't exist (idempotent).
Source code in src/gutenberg_kg/ingest.py
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 | |
fmt_duration(seconds)
¶
Human-readable duration string.
Source code in src/gutenberg_kg/ingest.py
163 164 165 166 167 168 169 170 171 | |
git_commit_push_genre(genre_dir, genre, dry_run=False)
¶
Stage genre_dir, commit, and push. Skips silently if nothing to commit.
Source code in src/gutenberg_kg/ingest.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 | |
ingest_diaries(registry_path, opts)
¶
Run the DiaryKG pipeline for the diaries genre and register the indices.
Diaries are not built through the standard DocKG path — they use a separate
pipeline: chunk-diaries (.md → .diary/) then build-diaries
(.diary/ → .diarykg/). This routes them so a single gutenkg ingest
/ rebuild-indices rebuilds prose and diaries. Runs before the Live
progress display to avoid nesting Rich renderers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
registry_path
|
Path
|
Resolved KGRAG registry database path. |
required |
opts
|
IngestOptions
|
Ingest option flags ( |
required |
Returns:
| Type | Description |
|---|---|
tuple[int, GenreSummary | None]
|
(0 on success else 1, a GenreSummary for the "diaries" genre so it
rolls into the same Totals/report as the standard DocKG genres — or
|
Source code in src/gutenberg_kg/ingest.py
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 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 | |
is_sqlite_valid(path)
¶
Return True if path is a readable, non-corrupt SQLite database.
Source code in src/gutenberg_kg/ingest.py
192 193 194 195 196 197 198 199 200 201 202 203 | |
print_summary(genre_summaries, opts, registry_path, wall_start, wall_elapsed, embed_model='', diary_rc=0)
¶
Print a rich job summary to stdout.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
diary_rc
|
int
|
Non-zero if the diary stage failed. Diaries build through a separate pipeline and contribute no GenreSummary when they fail, so without this the report would call a failed run a success. |
0
|
Source code in src/gutenberg_kg/ingest.py
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 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 | |
process_book(book_dir, genre, kg_reg, corp_reg, opts, embedder=None)
¶
Process one book directory. Returns a BookResult with timing and graph stats.
Source code in src/gutenberg_kg/ingest.py
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 | |
register_book(kg_reg, name, book_dir, dry_run=False)
¶
Register a book DocKG. Returns the KGEntry (new or existing). Returns None on dry-run or failure.
Source code in src/gutenberg_kg/ingest.py
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 354 | |
register_diary_book(kg_reg, name, diary_dir, dry_run=False)
¶
Register a diary's .diarykg/ index (DiaryKG, not standard .dockg/).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
kg_reg
|
KGRegistry
|
Open KGRAG registry. |
required |
name
|
str
|
KG name, e.g. |
required |
diary_dir
|
Path
|
Diary book directory under |
required |
dry_run
|
bool
|
Print the action without writing to the registry. |
False
|
Returns:
| Type | Description |
|---|---|
KGEntry | None
|
The registered :class: |
Source code in src/gutenberg_kg/ingest.py
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 | |
run_ingest(genres, opts, registry=None)
¶
Orchestrate DocKG builds, KGRAG registration, and corpus membership.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
genres
|
list[str]
|
Genre names to process (already validated against ALL_GENRES). |
required |
opts
|
IngestOptions
|
Ingest option flags. |
required |
registry
|
str | Path | None
|
Override path to the KGRAG registry database; |
None
|
Returns:
| Type | Description |
|---|---|
int
|
0 on full success, 1 if any book failed. |
Source code in src/gutenberg_kg/ingest.py
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 1046 1047 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 | |
run_reregister(genres, registry=None, dry_run=False)
¶
Re-register all built books from disk with the current KGKind without rebuilding DocKGs.
Walks each genre directory, finds book dirs that have a built .dockg/graph.sqlite, and calls register_book for each one. Idempotent — safe to run on any machine to fix stale kind values or populate a fresh registry after cloning.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
genres
|
list[str]
|
Genre names to process. |
required |
registry
|
str | Path | None
|
Override path to the KGRAG registry database. |
None
|
dry_run
|
bool
|
Print actions without writing to the registry. |
False
|
Returns:
| Type | Description |
|---|---|
int
|
0 on success. |
Source code in src/gutenberg_kg/ingest.py
1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 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 | |
save_summary(genre_summaries, opts, registry_path, wall_start, wall_elapsed, embed_model='', diary_rc=0)
¶
Write a Markdown ingest report to reports/ and return its path.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
diary_rc
|
int
|
Non-zero if the diary stage failed. See :func: |
0
|
Source code in src/gutenberg_kg/ingest.py
892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 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 | |
slugify(s)
¶
Lowercase and replace non-alphanumeric runs with hyphens.
Source code in src/gutenberg_kg/ingest.py
158 159 160 | |