Internet Archive source¶
gutenberg_kg.ia
¶
Download books from Internet Archive as structured Markdown.
Public API (used by the Click CLI in gutenberg_kg.cli.cmd_ia): download_book() — fetch one item by IA identifier run_catalog() — download all items from a catalog file run_survey() — scan corpus and print download/ingest status search_ia() — search the IA full-text search API format_search_results() — print search results as a table
Entry point:
gutenkg ia
Output structure (per book):
corpus/
clean_ocr(text, report=None)
¶
Clean common OCR artifacts from Internet Archive DjVu text.
Steps (in order):
1. Strip trailing whitespace from every line. The Archive's Tesseract
text ends each line with a space, which kept step 3 from ever joining
a hyphenated word.
2. Normalize unicode ligatures and common smart-quote variants
3. Join hyphenated line-breaks (OCR word-wrap artifact)
4. Strip bare page-number lines, including "4,047" / "4.047"
5. Strip the back-of-book index section (must happen before running-header
removal so the threshold is measured against the original body length)
6. Remove running headers/footers: lines that repeat 4+ times, and
running titles that repeat 4+ times with a changing page number
7. Remove figure/illustration markers
8. Blank lines of OCR debris (:func:is_ocr_debris): drawings and
rotated text read as characters. Blanked rather than deleted, so the
text either side of a figure is not run together.
9. Drop marks standing alone at the edge of a line of text
10. Collapse excessive blank lines
Nothing restores what the scan itself lacks: a word cut off at the page edge stays cut off.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
DjVu text as the Archive serves it. |
required |
report
|
dict | None
|
Optional dict that receives a count per step:
|
None
|
Returns:
| Type | Description |
|---|---|
str
|
The cleaned text. |
Source code in src/gutenberg_kg/ia.py
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 | |
download_book(identifier, title=None, genre=None, force=False, dry_run=False)
¶
Download an IA item, convert to Markdown, write reference.md.
Returns path to the .md file on success, None on failure.
Source code in src/gutenberg_kg/ia.py
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 | |
export_draft_catalog(query, results, path)
¶
Write search results to a catalog file as commented-out draft entries.
The curation step between "search returned 25 things" and "download these
six". Every line is written commented out, so an un-reviewed draft downloads
nothing if handed straight to gutenkg ia catalog -- IA search is noisy
and returns modern reprints alongside period scans, which is exactly what
needs a human pass before anything is fetched.
Appending several searches into one file before curating is the intended
workflow; see docs/IA_CATALOG_WORKFLOW.md.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query
|
str
|
The search query, recorded in the file header for provenance. |
required |
results
|
list[dict]
|
Search result dicts from :func: |
required |
path
|
str | Path
|
Destination catalog file; parent directories are created. |
required |
Returns:
| Type | Description |
|---|---|
int
|
Number of entries written. |
Source code in src/gutenberg_kg/ia.py
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 | |
fetch_ia_metadata(identifier)
¶
Fetch item metadata from the Internet Archive JSON API.
Returns a flat dict with keys: identifier, title, author, publisher, date, subjects, language, description, volume, series, edition, rights, plus '_files' (raw file list for text discovery).
Source code in src/gutenberg_kg/ia.py
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 | |
fetch_text(identifier, files)
¶
Download the best available text for an IA item. Returns raw text or None.
Source code in src/gutenberg_kg/ia.py
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 | |
fetch_url(url, retries=3, backoff=2.0)
¶
Fetch URL with retries and exponential backoff.
Source code in src/gutenberg_kg/ia.py
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 | |
find_text_file(identifier, files)
¶
Find the best available plain-text file in an IA item.
Priority: DjVu Text (_djvu.txt) > plain .txt Returns (filename, format_label) or None.
Source code in src/gutenberg_kg/ia.py
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 | |
format_search_results(results)
¶
Print IA search results as a formatted table.
Source code in src/gutenberg_kg/ia.py
1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 | |
is_ocr_debris(line, vocab=None)
¶
Whether a line of OCR text is debris rather than text.
Drawings, rotated or mirrored captions, and specks come out of OCR as lines like "s Cs ve ig" or "—t | x . Y \N —". A line is debris when fewer than half its characters are letters or digits, or fewer than half its tokens read as words, numbers or abbreviations. Dot leaders, pipes, dashes and underscores are ignored, so a table row such as "Buff limestone........ 40-60 20" survives.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
line
|
str
|
One line, any whitespace. |
required |
vocab
|
frozenset[str] | None
|
Words known to recur in the text, from :func: |
None
|
Returns:
| Type | Description |
|---|---|
bool
|
True for debris; False for text and for a blank line. |
Source code in src/gutenberg_kg/ia.py
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 | |
parse_catalog(catalog_path)
¶
Parse an IA catalog file. Each line: <identifier>[\t<title>].
The same shape as a Gutenberg catalog, with a string identifier where that
one has a numeric ID. Genres are either Gutenberg or IA and never both, so
which parser reads a given scripts/catalogs/<genre>.txt is unambiguous.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
catalog_path
|
str | Path
|
Path to the catalog file. |
required |
Returns:
| Type | Description |
|---|---|
list[tuple[str, str | None]]
|
|
Source code in src/gutenberg_kg/ia.py
1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 | |
record_in_catalog(genre, identifier, title, dry_run=False)
¶
Record a downloaded IA item in its genre catalog, if not already there.
The IA counterpart of :func:gutenberg_kg.gutenberg.record_in_catalog. IA
genres had no catalogs at all, so ia download recorded nothing anywhere
and every catalog-facing check had to exempt them.
The title override matters more here than on the Gutenberg side: IA titles are uncurated and often unusable as directory names, so the override column is where the curation lives.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
genre
|
str | None
|
Genre whose catalog to write; |
required |
identifier
|
str
|
Internet Archive item identifier. |
required |
title
|
str
|
The item's directory name, written as the title override. |
required |
dry_run
|
bool
|
Report what would be written but write nothing. |
False
|
Returns:
| Type | Description |
|---|---|
bool
|
True when a line was appended, or would have been under |
Source code in src/gutenberg_kg/ia.py
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 | |
recurring_words(text, min_count=2)
¶
Lowercased words that recur in the text's prose.
OCR garbage ("Carmoog", "WZZLLLN", mirrored "UOTYPOSOUAIP") almost never repeats letter for letter, and real words, even technical ones, almost always do somewhere in a book. Only prose-like lines are counted (six or more tokens, nearly all well-formed, half of them lowercase words), because debris does repeat within debris: "eee", "sss" and "nnn" recur across a book's drawings, and so do capital pairs such as "SU" and "ES". It stands in for a dictionary, which would also reject the book's own jargon.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The whole text. |
required |
min_count
|
int
|
Occurrences in prose needed to count as a word. |
2
|
Returns:
| Type | Description |
|---|---|
frozenset[str] | None
|
The recurring words, of two or more letters, or None for a text
with fewer than :data: |
Source code in src/gutenberg_kg/ia.py
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 | |
run_catalog(catalog_file, genre=None, force=False, dry_run=False)
¶
Download all items from a catalog file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
catalog_file
|
str
|
Path to a tab-separated catalog file (identifier [title]). |
required |
genre
|
str | None
|
Genre subdirectory for all items; when omitted it is inferred
from the catalog's filename stem, since |
None
|
force
|
bool
|
Re-download even if already present. |
False
|
dry_run
|
bool
|
Print actions without writing files. |
False
|
Returns:
| Type | Description |
|---|---|
int
|
0 on full success, 1 if any item failed. |
Source code in src/gutenberg_kg/ia.py
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 | |
run_catalog_sync(genres, dry_run=False)
¶
Record every downloaded IA item in its genre catalog.
The IA half of gutenkg catalog-sync; see
:func:gutenberg_kg.gutenberg.run_catalog_sync. Keyed on the identifier in
each reference.md, so nothing is downloaded.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
genres
|
list[str]
|
Genres to process; non-IA genres are ignored. |
required |
dry_run
|
bool
|
Report what would be written without writing. |
False
|
Returns:
| Type | Description |
|---|---|
int
|
Number of catalog entries added (or that would be added). |
Source code in src/gutenberg_kg/ia.py
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 | |
run_survey(genre=None)
¶
Scan the IA corpus and print download/ingest status.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
genre
|
str | None
|
Optional genre filter; default scans all IA genres. |
None
|
Returns:
| Type | Description |
|---|---|
int
|
Always 0. |
Source code in src/gutenberg_kg/ia.py
1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 | |
search_ia(query, max_results=25)
¶
Search Internet Archive texts. Returns list of result dicts.
Source code in src/gutenberg_kg/ia.py
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 | |
slugify(title)
¶
Convert a title to a filesystem-friendly slug (underscores).
Source code in src/gutenberg_kg/ia.py
114 115 116 117 118 119 | |
text_notes(meta)
¶
The ## Text notes section of an IA reference.md.
Says where the text came from and what cleaning did to it, so a reader
who meets a garbled line knows it is the scan's OCR and what was already
taken out. Written only when meta carries the counts from
:func:clean_ocr.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
meta
|
dict
|
Item metadata, with |
required |
Returns:
| Type | Description |
|---|---|
list[str]
|
The section's lines, or none. |
Source code in src/gutenberg_kg/ia.py
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 | |
text_to_markdown(text, meta, report=None)
¶
Convert IA OCR text to structured Markdown.
Pipeline: 1. Clean OCR artifacts (ligatures, hyphenation, running headers, index) 2. Add title / author / publisher front matter 3. Skip table-of-contents region 4. Detect headings (CHAPTER, PART, SECTION, ALL-CAPS, Ques.) 5. For CHAPTER headings, absorb an ALL-CAPS subtitle on the next line 6. Preserve paragraph structure; suppress extra blank lines
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
DjVu text as the Archive serves it. |
required |
meta
|
dict
|
Item metadata, for the front matter. |
required |
report
|
dict | None
|
Optional dict that receives :func: |
None
|
Returns:
| Type | Description |
|---|---|
str
|
The Markdown. |
Source code in src/gutenberg_kg/ia.py
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 | |
write_reference(book_dir, meta)
¶
Write a reference.md sidecar with Internet Archive metadata.
Source code in src/gutenberg_kg/ia.py
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 890 891 892 893 | |