Datasets:
Download stats/composition.py from CatholicCorpus/catholiccorpus: direct link, hf CLI and curl.
- Browser
- Download file 8.39 kB
-
https://huggingface.co/datasets/CatholicCorpus/catholiccorpus/resolve/main/stats/composition.py
- Command line
-
hf download hf://datasets/CatholicCorpus/catholiccorpus/stats/composition.py
-
curl -L -o composition.py https://huggingface.co/datasets/CatholicCorpus/catholiccorpus/resolve/main/stats/composition.py
8.39 kB
| #!/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() | |