Datasets:
File size: 8,392 Bytes
7f096ee | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 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 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 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 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 | #!/usr/bin/env python3
"""
composition.py -- Generate per-collection composition breakdown for CatholicCorpus.
Produces stats/composition.md with: file count, byte size, token count (if available),
primary language(s), file formats present, and proportion of text-extractable content.
Usage:
python3 stats/composition.py
Reads from master_manifest.json and (optionally) stats/token_counts.json.
"""
from __future__ import annotations
import json
from pathlib import Path
SCRIPT_DIR = Path(__file__).resolve().parent
CORPUS_ROOT = SCRIPT_DIR.parent
MANIFEST = CORPUS_ROOT / "master_manifest.json"
TOKEN_COUNTS = SCRIPT_DIR / "token_counts.json"
OUTPUT = SCRIPT_DIR / "composition.md"
# Known metadata per collection
COLLECTION_META = {
"01_git_repos": {
"label": "Git Repos (CSEL, Aquinas, LXX, Byzantine Text, eBible)",
"languages": "Latin, Greek, English, Multilingual",
"era": "Mixed (Patristic through Modern)",
},
"02_corpus_corporum": {
"label": "Corpus Corporum (Patrologia Latina + 29 corpora)",
"languages": "Latin (primary), Greek",
"era": "Patristic, Medieval, Early Modern",
},
"03_gutenberg": {
"label": "Project Gutenberg (Catholic classics in English)",
"languages": "English",
"era": "Mixed",
},
"04_direct_downloads": {
"label": "Direct Downloads (Vulgate, SBLGNT, Canon Law, Roman Catechism)",
"languages": "Latin, Greek, English",
"era": "Mixed",
},
"05_ccel": {
"label": "CCEL (Ante-Nicene/Nicene Fathers, Summa, devotional)",
"languages": "English",
"era": "Patristic (in translation)",
},
"06_catholic_encyclopedia": {
"label": "Catholic Encyclopedia (1913, 15 volumes)",
"languages": "English",
"era": "Modern (reference, 1913)",
},
"07_latin_and_franciscan": {
"label": "Latin and Franciscan (Anselm, Lombard, Bonaventure, Scotus, Ockham)",
"languages": "Latin",
"era": "Medieval/Scholastic",
},
"08_liturgical_and_hymns": {
"label": "Liturgical and Hymns (Divine Office, hymns, encyclicals)",
"languages": "Latin (primary), English",
"era": "Mixed (liturgical)",
},
"09_archive_and_misc": {
"label": "Archive and Misc (Trent, Golden Legend, Butler's Lives, Albert)",
"languages": "Latin, English",
"era": "Medieval, Counter-Reformation",
},
"11_catholic_bible": {
"label": "Catholic Bible (Douay-Rheims, Haydock, Lapide)",
"languages": "English, Latin",
"era": "Mixed (scripture)",
},
"12_english_catholic_thinkers": {
"label": "English Catholic Thinkers (Newman, Chesterton, Belloc)",
"languages": "English",
"era": "Modern (19th-20th century)",
},
"13_mystics": {
"label": "Mystics (Therese, Julian, Catherine, Ignatius)",
"languages": "English (translations)",
"era": "Counter-Reformation, Medieval",
},
"14_liturgical_expansion": {
"label": "Liturgical Expansion (Liber Usualis, Missale, Rituale, Breviarium)",
"languages": "Latin",
"era": "Mixed (liturgical, Tridentine)",
},
"15_late_scholastics": {
"label": "Late Scholastics (Suarez, Bellarmine, Cajetan, a Lapide, Banez, Molina)",
"languages": "Latin",
"era": "Counter-Reformation/Early Modern",
},
"16_patrologia_graeca": {
"label": "Patrologia Graeca (159 of 161 Migne PG volumes)",
"languages": "Greek, Latin",
"era": "Patristic",
},
"17_modern_magisterium": {
"label": "Modern Magisterium (Vatican II, CCC, Canon Law, encyclicals)",
"languages": "English, Latin",
"era": "Modern (20th-21st century)",
},
}
# Extensions considered text-extractable
TEXT_EXTRACTABLE = {".txt", ".xml", ".html", ".htm", ".epub"}
def human_bytes(n: int) -> str:
for unit in ("B", "KB", "MB", "GB", "TB"):
if n < 1024 or unit == "TB":
return f"{n:.1f} {unit}" if unit != "B" else f"{n} B"
n /= 1024
return f"{n:.1f} TB"
def main():
if not MANIFEST.exists():
print("Error: master_manifest.json not found. Run build_manifest.py first.")
return
manifest = json.loads(MANIFEST.read_text())
# Load token counts if available
tokens_available = False
token_data = {}
if TOKEN_COUNTS.exists():
tc = json.loads(TOKEN_COUNTS.read_text())
token_data = tc.get("by_collection", {})
tokens_available = True
# Build composition table
md = []
md.append("# CatholicCorpus Composition Breakdown\n")
md.append(f"Generated from `master_manifest.json` ({manifest['generated_at_iso']})")
if tokens_available:
md.append(f" and `token_counts.json`")
md.append("\n")
# Summary
totals = manifest["totals"]
md.append("## Summary\n")
md.append(f"- **Total files:** {totals['file_count']:,}")
md.append(f"- **Content files:** {totals['content_count']:,}")
md.append(f"- **Total size:** {totals['total_human']}")
if tokens_available:
total_tokens = sum(v.get("tokens", 0) for v in token_data.values())
md.append(f"- **Total tokens (GPT-2):** {total_tokens:,}")
md.append("")
# Per-collection table
md.append("## Per-Collection Breakdown\n")
header = "| Collection | Files | Content | Size | Formats | Languages | Era |"
sep = "|------------|------:|--------:|-----:|---------|-----------|-----|"
if tokens_available:
header = "| Collection | Files | Content | Size | Tokens | Formats | Languages | Era |"
sep = "|------------|------:|--------:|-----:|-------:|---------|-----------|-----|"
md.append(header)
md.append(sep)
for task_name in sorted(manifest["tasks"].keys()):
task = manifest["tasks"][task_name]
meta = COLLECTION_META.get(task_name, {})
# Determine formats present
exts = set()
text_extractable_count = 0
for f in task.get("files", []):
ext = f.get("ext", "")
if ext:
exts.add(ext)
if ext in TEXT_EXTRACTABLE:
text_extractable_count += 1
formats_str = ", ".join(sorted(exts)[:5]) # Top 5 extensions
if len(exts) > 5:
formats_str += f" (+{len(exts)-5})"
langs = meta.get("languages", "Unknown")
era = meta.get("era", "Unknown")
if tokens_available and task_name in token_data:
tokens = token_data[task_name].get("tokens", 0)
md.append(
f"| {task_name} | {task['file_count']:,} | {task['content_count']:,} | "
f"{task['total_human']} | {tokens:,} | {formats_str} | {langs} | {era} |"
)
else:
md.append(
f"| {task_name} | {task['file_count']:,} | {task['content_count']:,} | "
f"{task['total_human']} | {formats_str} | {langs} | {era} |"
)
md.append("")
# Text-extractable proportion
md.append("## Text-Extractable Content\n")
md.append("Files with extensions that can be directly converted to plain text (.txt, .xml, .html, .htm, .epub) vs. files requiring OCR (.pdf) or other processing:\n")
total_content = totals["content_count"]
md.append("| Category | Files | Percentage |")
md.append("|----------|------:|-----------:|")
# Count across all tasks
extractable = 0
pdf_count = 0
other_count = 0
for task in manifest["tasks"].values():
for f in task.get("files", []):
if not f.get("is_content"):
continue
ext = f.get("ext", "")
if ext in TEXT_EXTRACTABLE:
extractable += 1
elif ext == ".pdf":
pdf_count += 1
else:
other_count += 1
md.append(f"| Directly extractable (.txt, .xml, .html, .htm, .epub) | {extractable:,} | {extractable/total_content*100:.1f}% |")
md.append(f"| Requires text extraction (.pdf) | {pdf_count:,} | {pdf_count/total_content*100:.1f}% |")
md.append(f"| Other formats | {other_count:,} | {other_count/total_content*100:.1f}% |")
md.append("")
OUTPUT.write_text("\n".join(md))
print(f"Wrote {OUTPUT}")
if __name__ == "__main__":
main()
|