#!/usr/bin/env python3 """Extract Perfect Questions Perfect Answers (1977) from other.html. The book has 3 intro segments (front-pages, introduction, about-the-author), 9 chapters, and a Concluding Words section. Output: lib/jsons/books/pqpa/intro.json — Book metadata lib/jsons/books/pqpa/intro/*.json — Intro segments lib/jsons/books/pqpa/chapters/*.json — Chapters + concluding words Usage: python3 scripts/pqpa_extract.py """ import json import os import re import sys from bs4 import BeautifulSoup, Tag # --------------------------------------------------------------------------- # ScaGoudy → IAST Unicode mapping # --------------------------------------------------------------------------- SCAGOUDY_TO_IAST = { "ä": "ā", "Ä": "Ā", "å": "ṛ", "Å": "Ṛ", "ñ": "ṣ", "Ñ": "Ṣ", "ö": "ṭ", "Ö": "Ṭ", "ë": "ṇ", "Ë": "Ṇ", "ò": "ḍ", "Ò": "Ḍ", "ç": "ś", "Ç": "Ś", "ì": "ṅ", "ï": "ñ", "ü": "ū", "Ü": "Ū", "é": "ī", "É": "Ī", "à": "ṁ", "ù": "ḥ", "è": "ṝ", "û": "ṁl", "ÿ": "ḷ", } def decode_scagoudy(text: str) -> str: """Replace ScaGoudy-encoded characters with proper IAST Unicode.""" for src, dst in SCAGOUDY_TO_IAST.items(): text = text.replace(src, dst) return text def decode_html_content(tag: Tag) -> str: """Extract inner HTML from a tag, decode ScaGoudy, preserve formatting tags.""" inner = "".join(str(child) for child in tag.children) inner = decode_scagoudy(inner) # Remove span tags with CSS classes (RTF conversion artifacts) inner = re.sub( r']*class="[^"]*"[^>]*>(.*?)', r"\1", inner, flags=re.DOTALL ) # Clean up Apple-tab-span leftovers (tab characters) inner = inner.replace("\t", " ") # Normalize whitespace inner = re.sub(r"\s+", " ", inner).strip() return inner def decode_text(tag: Tag) -> str: """Get plain text from a tag with ScaGoudy decoding.""" return decode_scagoudy(tag.get_text().strip()) def get_css_class(tag: Tag) -> str: """Get the first CSS class of a tag, or empty string.""" classes = tag.get("class", []) return classes[0] if classes else "" def is_empty(tag: Tag) -> bool: """Check if a tag is empty or whitespace-only.""" text = tag.get_text().strip() return not text or text == "\xa0" def is_dash_line(text: str) -> bool: """Return True if text is only dashes, spaces, and/or whitespace.""" cleaned = text.strip() if not cleaned: return False return all(c in "-—–\u2014\u2013 \t" for c in cleaned) # --------------------------------------------------------------------------- # CSS class categorization for PQPA # --------------------------------------------------------------------------- # Book title header class BOOK_HEADER_CLASS = "p12" # Bookmark class — used for section boundary detection BOOKMARK_CLASS = "p13" # Body paragraph classes (primary content) BODY_CLASSES = frozenset(["p6", "p48"]) # Dialogue line class — the main content class (714 occurrences) DIALOGUE_CLASS = "p92" # Sanskrit verse pāda classes (italic, centered) VERSE_PADA_CLASSES = frozenset(["p53", "p66"]) # Verse translations / body text TRANSLATION_CLASS = "p44" # Chapter heading (bold) CHAPTER_HEADING_CLASS = "p27" # Section heading (bold) SECTION_HEADING_CLASS = "p31" # Front pages text FRONT_PAGES_CLASS = "p18" # Attribution text ATTRIBUTION_CLASS = "p42" # Numbered items / verse quotes NUMBERED_CLASS = "p24" # Mantra text MANTRA_CLASS = "p19" # Contents heading CONTENTS_CLASS = "p69" # Title page text TITLE_PAGE_CLASS = "p3" # Misc classes to include MISC_CLASSES = frozenset(["p28", "p76", "p93", "p94", "p95", "p96"]) # Classes to skip entirely (empty/decorative) SKIP_CLASSES = frozenset(["p9", "p15", "p70"]) # --------------------------------------------------------------------------- # Extraction helpers # --------------------------------------------------------------------------- def find_pqpa_boundaries(all_paragraphs: list) -> tuple: """Find the start and end paragraph indices of the PQPA content. Start:
containing "Perfect Questions Perfect Answers" End: next
after start, or end of file
"""
start_idx = None
end_idx = len(all_paragraphs)
for i, p in enumerate(all_paragraphs):
cls = get_css_class(p)
if cls != BOOK_HEADER_CLASS:
continue
text = decode_text(p)
if "Perfect Questions Perfect Answers" in text:
start_idx = i
elif start_idx is not None:
# Next p12 after PQPA start = end of PQPA
end_idx = i
break
if start_idx is None:
print(
"ERROR: Could not find 'Perfect Questions Perfect Answers' header in other.html!"
)
sys.exit(1)
# Also check for Glossary marker as end boundary
for i in range(start_idx + 1, end_idx):
p = all_paragraphs[i]
cls = get_css_class(p)
if cls == TITLE_PAGE_CLASS:
text = p.get_text().strip()
if text == "Glossary":
end_idx = i
break
return start_idx, end_idx
def find_all_bookmarks(all_paragraphs: list, start_idx: int, end_idx: int) -> list:
"""Find all p13 bookmark tags in the PQPA range.
Returns list of (paragraph_index, bookmark_text).
The bookmark text is stripped of any span noise like .
"""
bookmarks = []
for i in range(start_idx, end_idx):
p = all_paragraphs[i]
cls = get_css_class(p)
if cls == BOOKMARK_CLASS:
# Get text, stripping span noise
text = decode_text(p).strip()
if text.startswith("PQPA"):
bookmarks.append((i, text))
return bookmarks
def parse_chapter_bookmark(text: str):
"""Parse a chapter bookmark like 'PQPA 1: Kṛṣṇa, the All-Attractive'.
Returns (chapter_number, title) or None if not a chapter bookmark.
"""
m = re.match(r"PQPA\s+(\d+):\s+(.+)", text.strip())
if m:
return int(m.group(1)), m.group(2).strip()
return None
def parse_intro_bookmark(text: str) -> bool:
"""Check if this is the Introduction bookmark."""
return text.strip() == "PQPA Introduction"
def parse_concluding_bookmark(text: str) -> bool:
"""Check if this is the Concluding Words bookmark."""
return text.strip() == "PQPA Concluding Words"
def extract_paragraphs(
all_paragraphs: list,
start_idx: int,
end_idx: int,
is_front_pages: bool = False,
) -> list:
"""Extract paragraphs for a section (chapter, intro segment, or conclusion).
Handles all CSS classes within the PQPA content.
Groups consecutive p53/p66 verse pādas into centered italic divs.
Detects verse patterns in p6/p92 (starts with , has
).
"""
result = []
verse_accumulator = []
def flush_verse():
if verse_accumulator:
joined = "
".join(verse_accumulator)
result.append(f'
stripped_html = html.strip()
if stripped_html.startswith("") and "
", "", stripped_html)
result.append(
f'
stripped_html = html.strip()
if stripped_html.startswith("") and "
", "", stripped_html)
result.append(
f'
", "", html)
result.append(
f'
(multi-line italic = verse)
stripped_html = html.strip()
if stripped_html.startswith("") and "
", "", stripped_html)
result.append(
f'
His Divine Grace A. C. Bhaktivedanta Swami Prabhupāda
about_author_pos = None for i in range(concluding_bookmark + 1, pqpa_end): p = all_paragraphs[i] cls = get_css_class(p) if cls == CHAPTER_HEADING_CLASS: text = decode_text(p) if "His Divine Grace" in text: about_author_pos = i break # Concluding Words: from bookmark to About the Author (or pqpa_end) cw_content_start = concluding_bookmark + 1 cw_content_end = about_author_pos if about_author_pos is not None else pqpa_end cw_paras = extract_paragraphs(all_paragraphs, cw_content_start, cw_content_end) concluding_words = { "book": "pqpa", "chapter": None, "title": "Concluding Words", "type": "conclusion", "slug": "concluding-words", "paragraphs": cw_paras, } # About the Author: from p27 heading to pqpa_end if about_author_pos is not None: ata_paras = extract_paragraphs(all_paragraphs, about_author_pos, pqpa_end) about_the_author = { "slug": "about-the-author", "title": "About the Author", "type": "intro-segment", "book": "pqpa", "order": 99, "paragraphs": ata_paras, } # Step 8: Build intro segments list intro_segments = [front_pages] if introduction: intro_segments.append(introduction) if about_the_author: intro_segments.append(about_the_author) # Step 9: Build chapter listing for intro.json chapter_listing = [ {"number": ch["chapter"], "title": ch["title"]} for ch in chapters ] return { "intro_segments": intro_segments, "chapters": chapters, "concluding_words": concluding_words, "chapter_listing": chapter_listing, } # --------------------------------------------------------------------------- # Output # --------------------------------------------------------------------------- def write_json(data, filepath): """Write data as formatted JSON to filepath, creating directories as needed.""" os.makedirs(os.path.dirname(filepath), exist_ok=True) with open(filepath, "w", encoding="utf-8") as f: json.dump(data, f, ensure_ascii=False, indent=2) def main(): base_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) html_path = os.path.join(base_dir, "public", "html", "other.html") output_base = os.path.join(base_dir, "lib", "jsons", "books", "pqpa") if not os.path.exists(html_path): print(f"ERROR: {html_path} not found!") sys.exit(1) print(f"Reading {html_path}...") with open(html_path, "r", encoding="utf-8") as f: soup = BeautifulSoup(f, "html.parser") print("Extracting Perfect Questions Perfect Answers (1977)...\n") result = extract_pqpa(soup) intro_segments = result["intro_segments"] chapters = result["chapters"] concluding_words = result["concluding_words"] chapter_listing = result["chapter_listing"] # --- Write intro.json --- intro_meta = { "book": "pqpa", "title": "Perfect Questions Perfect Answers", "author": "His Divine Grace A.C. Bhaktivedanta Swami Prabhupāda", "year": 1977, "description": "Conversations between Śrīla Prabhupāda and Bob Cohen, a Peace Corps worker in India.", "totalChapters": 9, "chapters": chapter_listing, } intro_path = os.path.join(output_base, "intro.json") write_json(intro_meta, intro_path) print(f"\n Written: {intro_path}") # --- Write intro segments --- print(f"\n--- Intro Segments ---") for seg in intro_segments: filepath = os.path.join(output_base, "intro", f"{seg['slug']}.json") write_json(seg, filepath) print( f" {seg['order']}. {seg['title']}: {len(seg['paragraphs'])} paragraphs → {filepath}" ) # --- Write chapters --- print(f"\n--- Chapters ---") for ch in chapters: ch_num_str = str(ch["chapter"]).zfill(2) filepath = os.path.join(output_base, "chapters", f"{ch_num_str}.json") write_json(ch, filepath) print( f" Ch {ch['chapter']:>2}: {len(ch['paragraphs']):>3} paragraphs — " f"{ch['title']}" ) # --- Write concluding words --- if concluding_words: filepath = os.path.join(output_base, "chapters", "concluding-words.json") write_json(concluding_words, filepath) print( f"\n Concluding Words: {len(concluding_words['paragraphs'])} paragraphs → {filepath}" ) # --- Summary --- total_intro_paras = sum(len(s["paragraphs"]) for s in intro_segments) total_chapter_paras = sum(len(ch["paragraphs"]) for ch in chapters) total_concluding_paras = ( len(concluding_words["paragraphs"]) if concluding_words else 0 ) total_paras = total_intro_paras + total_chapter_paras + total_concluding_paras print(f"\n{'=' * 60}") print(f" EXTRACTION SUMMARY") print(f"{'=' * 60}") print(f" Intro segments: {len(intro_segments)}") for seg in intro_segments: print( f" {seg['order']}. {seg['title']}: {len(seg['paragraphs'])} paragraphs" ) print(f" Chapters: {len(chapters)}") print(f" Concluding Words: {'Yes' if concluding_words else 'No'}") print(f" Total paragraphs: {total_paras}") print(f" Intro: {total_intro_paras}") print(f" Chapters: {total_chapter_paras}") print(f" Concluding: {total_concluding_paras}") if chapters: print(f" Avg paras/chapter: {total_chapter_paras / len(chapters):.1f}") print(f" Output directory: {output_base}/") # --- Samples --- print(f"\n--- Sample: Chapter 1 ---") if chapters: ch1 = chapters[0] for i, p in enumerate(ch1["paragraphs"][:4]): preview = re.sub(r"<[^>]+>", "", p)[:120] print(f" [{i}] {preview}...") if len(chapters) >= 9: print(f"\n--- Sample: Chapter 9 (last) ---") last = chapters[-1] for i, p in enumerate(last["paragraphs"][:3]): preview = re.sub(r"<[^>]+>", "", p)[:120] print(f" [{i}] {preview}...") if concluding_words: print(f"\n--- Sample: Concluding Words ---") for i, p in enumerate(concluding_words["paragraphs"][:3]): preview = re.sub(r"<[^>]+>", "", p)[:120] print(f" [{i}] {preview}...") # --- Quality Checks --- print(f"\n--- Quality Checks ---") # Check all 9 chapters present expected_chapters = set(range(1, 10)) actual_chapters = set(ch["chapter"] for ch in chapters) missing = expected_chapters - actual_chapters extra = actual_chapters - expected_chapters if missing: print(f" ⚠ Missing chapters: {sorted(missing)}") if extra: print(f" ⚠ Extra chapters: {sorted(extra)}") if not missing and not extra: print(f" ✅ All 9 chapters present") # Check intro segments expected_intros = {"front-pages", "introduction", "about-the-author"} actual_intros = {s["slug"] for s in intro_segments} missing_intros = expected_intros - actual_intros if missing_intros: print(f" ⚠ Missing intro segments: {missing_intros}") else: print(f" ✅ All 3 intro segments present") # Check concluding words if concluding_words: print(f" ✅ Concluding Words present") else: print(f" ⚠ Concluding Words NOT found") # Check empty chapters empty_chapters = [ch["chapter"] for ch in chapters if len(ch["paragraphs"]) == 0] if empty_chapters: print(f" ⚠ Empty chapters (0 paragraphs): {empty_chapters}") else: print(f" ✅ No empty chapters") # Check ScaGoudy decoding — look for common encoded characters encoded_chars = set("äåöëòçüéàùè") suspect_count = 0 all_sections = chapters + intro_segments if concluding_words: all_sections = all_sections + [concluding_words] for section in all_sections: for p in section["paragraphs"]: plain = re.sub(r"<[^>]+>", "", p) for c in encoded_chars: if c in plain: suspect_count += 1 break if suspect_count == 0: print(f" ✅ No suspected undecoded ScaGoudy characters found") else: print( f" ℹ {suspect_count} paragraphs contain characters that may be undecoded ScaGoudy" ) # Check verse blocks verse_block_count = 0 for ch in chapters: for p in ch["paragraphs"]: if '