Update web/core/scripture_normalizer.py
This commit is contained in:
+185
-193
@@ -1,233 +1,223 @@
|
|||||||
# core/scripture_normalizer.py
|
# core/scripture_normalizer.py
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
import re
|
import re
|
||||||
from typing import Dict, List, Tuple, Optional
|
from typing import Dict, List, Optional, Tuple
|
||||||
|
|
||||||
# =========================
|
# --------------------------
|
||||||
# Canonical book abbreviations
|
# Canonical book abbreviations
|
||||||
# =========================
|
# --------------------------
|
||||||
# House style (edit as you like). Keys are normalized to lower-case without periods.
|
|
||||||
BOOK_CANON: Dict[str, str] = {
|
BOOK_CANON: Dict[str, str] = {
|
||||||
# ----- OT -----
|
# OT
|
||||||
"genesis": "Gen.", "gen": "Gen.", "ge": "Gen.", "gn": "Gen.", "genesisesis": "Gen.",
|
"genesis": "Gen.", "gen": "Gen.", "genesisesis": "Gen.",
|
||||||
"exodus": "Ex.", "ex": "Ex.", "exo": "Ex.",
|
"exodus": "Ex.", "ex": "Ex.",
|
||||||
"leviticus": "Lev.", "lev": "Lev.", "le": "Lev.",
|
"leviticus": "Lev.", "lev": "Lev.",
|
||||||
"numbers": "Num.", "num": "Num.", "nu": "Num.", "nums": "Num.",
|
"numbers": "Num.", "num": "Num.", "nums": "Num.",
|
||||||
"deuteronomy": "Deut.", "deut": "Deut.", "deu": "Deut.", "dt": "Deut.", "deutronomy": "Deut.",
|
"deuteronomy": "Deut.", "deut": "Deut.", "deutronomy": "Deut.", "deu": "Deut.",
|
||||||
"joshua": "Josh.", "josh": "Josh.", "jos": "Josh.",
|
"joshua": "Josh.", "josh": "Josh.",
|
||||||
"judges": "Judg.", "judg": "Judg.", "jdg": "Judg.",
|
"judges": "Judg.", "judg": "Judg.",
|
||||||
"ruth": "Ruth", "ru": "Ruth",
|
"ruth": "Ruth",
|
||||||
"1 samuel": "1 Sam.", "i samuel": "1 Sam.", "1 sam": "1 Sam.", "1sam": "1 Sam.", "1sa": "1 Sam.",
|
"1 samuel": "1 Sam.", "i samuel": "1 Sam.", "1 sam": "1 Sam.", "1sam": "1 Sam.",
|
||||||
"2 samuel": "2 Sam.", "ii samuel": "2 Sam.", "2 sam": "2 Sam.", "2sam": "2 Sam.", "2sa": "2 Sam.",
|
"2 samuel": "2 Sam.", "ii samuel": "2 Sam.", "2 sam": "2 Sam.", "2sam": "2 Sam.",
|
||||||
"1 kings": "1 Ki.", "i kings": "1 Ki.", "1 ki": "1 Ki.", "1kgs": "1 Ki.", "1 kgs": "1 Ki.", "1kings": "1 Ki.", "1ki": "1 Ki.",
|
"1 kings": "1 Ki.", "i kings": "1 Ki.", "1 ki": "1 Ki.", "1kgs": "1 Ki.", "1 kgs": "1 Ki.",
|
||||||
"2 kings": "2 Ki.", "ii kings": "2 Ki.", "2 ki": "2 Ki.", "2kgs": "2 Ki.", "2 kgs": "2 Ki.", "2kings": "2 Ki.", "2ki": "2 Ki.",
|
"2 kings": "2 Ki.", "ii kings": "2 Ki.", "2 ki": "2 Ki.", "2kgs": "2 Ki.", "2 kgs": "2 Ki.",
|
||||||
"1 chronicles": "1 Chron.", "i chronicles": "1 Chron.", "1 chron": "1 Chron.", "1ch": "1 Chron.", "1chron": "1 Chron.",
|
"1 chronicles": "1 Chron.", "i chronicles": "1 Chron.", "1 chron": "1 Chron.",
|
||||||
"2 chronicles": "2 Chron.", "ii chronicles": "2 Chron.", "2 chron": "2 Chron.", "2ch": "2 Chron.", "2chron": "2 Chron.",
|
"2 chronicles": "2 Chron.", "ii chronicles": "2 Chron.", "2 chron": "2 Chron.",
|
||||||
"ezra": "Ezra", "ezr": "Ezra",
|
"ezra": "Ezra",
|
||||||
"nehemiah": "Neh.", "neh": "Neh.",
|
"nehemiah": "Neh.", "neh": "Neh.",
|
||||||
"esther": "Esth.", "esth": "Esth.", "es": "Esth.",
|
"esther": "Esth.", "esth": "Esth.",
|
||||||
"job": "Job",
|
"job": "Job",
|
||||||
"psalm": "Ps.", "psalms": "Ps.", "ps": "Ps.", "psm": "Ps.", "pss": "Ps.",
|
"psalms": "Ps.", "psalm": "Ps.", "ps": "Ps.", "pss": "Ps.",
|
||||||
"proverbs": "Prov.", "prov": "Prov.", "pr": "Prov.",
|
"proverbs": "Prov.", "prov": "Prov.",
|
||||||
"ecclesiastes": "Eccl.", "eccles": "Eccl.", "eccl": "Eccl.", "ecc": "Eccl.", "ec": "Eccl.",
|
"ecclesiastes": "Eccl.", "eccles": "Eccl.", "eccl": "Eccl.",
|
||||||
"song of solomon": "Song", "song of songs": "Song", "song": "Song", "so": "Song", "sos": "Song",
|
"song of solomon": "Song", "song of songs": "Song", "song": "Song",
|
||||||
"isaiah": "Isa.", "isa": "Isa.", "is": "Isa.",
|
"isaiah": "Isa.", "isa": "Isa.", # IMPORTANT: direct alias so 'Isa.' never becomes '1 Sam.'
|
||||||
"jeremiah": "Jer.", "jer": "Jer.", "je": "Jer.",
|
"jeremiah": "Jer.", "jer": "Jer.",
|
||||||
"lamentations": "Lam.", "lam": "Lam.", "la": "Lam.",
|
"lamentations": "Lam.", "lam": "Lam.",
|
||||||
"ezekiel": "Ezek.", "ezek": "Ezek.", "eze": "Ezek.", "ezk": "Ezek.",
|
"ezekiel": "Ezek.", "ezek": "Ezek.",
|
||||||
"daniel": "Dan.", "dan": "Dan.", "da": "Dan.",
|
"daniel": "Dan.", "dan": "Dan.",
|
||||||
"hosea": "Hos.", "hos": "Hos.", "ho": "Hos.",
|
"hosea": "Hos.", "hos": "Hos.",
|
||||||
"joel": "Joel", "joe": "Joel", "jl": "Joel",
|
"joel": "Joel",
|
||||||
"amos": "Amos", "am": "Amos",
|
"amos": "Amos",
|
||||||
"obadiah": "Obad.", "obad": "Obad.", "ob": "Obad.",
|
"obadiah": "Obad.", "obad": "Obad.",
|
||||||
"jonah": "Jon.", "jon": "Jon.",
|
"jonah": "Jon.", "jon": "Jon.",
|
||||||
"micah": "Mic.", "mic": "Mic.",
|
"micah": "Mic.", "mic": "Mic.",
|
||||||
"nahum": "Nah.", "nah": "Nah.",
|
"nahum": "Nah.", "nah": "Nah.",
|
||||||
"habakkuk": "Hab.", "hab": "Hab.",
|
"habakkuk": "Hab.", "hab": "Hab.",
|
||||||
"zephaniah": "Zeph.", "zeph": "Zeph.", "zep": "Zeph.",
|
"zephaniah": "Zeph.", "zeph": "Zeph.",
|
||||||
"haggai": "Hag.", "hag": "Hag.",
|
"haggai": "Hag.", "hag": "Hag.",
|
||||||
"zechariah": "Zech.", "zech": "Zech.", "zec": "Zech.",
|
"zechariah": "Zech.", "zech": "Zech.",
|
||||||
"malachi": "Mal.", "mal": "Mal.",
|
"malachi": "Mal.", "mal": "Mal.",
|
||||||
# ----- NT -----
|
# NT
|
||||||
"matthew": "Matt.", "matt": "Matt.", "mt": "Matt.", "mat": "Matt.",
|
"matthew": "Matt.", "matt": "Matt.", "mt": "Matt.",
|
||||||
"mark": "Mark", "mrk": "Mark", "mk": "Mark", "mr": "Mark",
|
"mark": "Mark", "mk": "Mark", "mrk": "Mark", "mr": "Mark",
|
||||||
"luke": "Luke", "lk": "Luke",
|
"luke": "Luke", "lk": "Luke",
|
||||||
"john": "John", "jn": "John", "jo": "John", "joh": "John",
|
"john": "John", "jn": "John", "joh": "John",
|
||||||
"acts": "Acts", "act": "Acts", "ac": "Acts",
|
"acts": "Acts",
|
||||||
"romans": "Rom.", "rom": "Rom.", "ro": "Rom.", "rm": "Rom.",
|
"romans": "Rom.", "rom": "Rom.",
|
||||||
"1 corinthians": "1 Cor.", "i corinthians": "1 Cor.", "1 cor": "1 Cor.", "1cor": "1 Cor.", "1co": "1 Cor.", "1 corinthians": "1 Cor.",
|
"1 corinthians": "1 Cor.", "i corinthians": "1 Cor.", "1 cor": "1 Cor.", "1co": "1 Cor.",
|
||||||
"2 corinthians": "2 Cor.", "ii corinthians": "2 Cor.", "2 cor": "2 Cor.", "2cor": "2 Cor.", "2co": "2 Cor.", "2 corinthians": "2 Cor.",
|
"2 corinthians": "2 Cor.", "ii corinthians": "2 Cor.", "2 cor": "2 Cor.", "2co": "2 Cor.",
|
||||||
"galatians": "Gal.", "gal": "Gal.", "ga": "Gal.",
|
"galatians": "Gal.", "gal": "Gal.",
|
||||||
"ephesians": "Eph.", "eph": "Eph.", "eph.": "Eph.",
|
"ephesians": "Eph.", "eph": "Eph.",
|
||||||
"philippians": "Phil.", "phil": "Phil.", "php": "Phil.", "phi": "Phil.", "philippians216": "Phil.",
|
"philippians": "Phil.", "phil": "Phil.", "philippians 216": "Phil.", # import glitch seen
|
||||||
"colossians": "Col.", "col": "Col.",
|
"colossians": "Col.", "col": "Col.",
|
||||||
"1 thessalonians": "1 Thess.", "i thessalonians": "1 Thess.", "1 thess": "1 Thess.", "1 th": "1 Thess.", "1thess": "1 Thess.",
|
"1 thessalonians": "1 Thess.", "i thessalonians": "1 Thess.", "1 thess": "1 Thess.",
|
||||||
"2 thessalonians": "2 Thess.", "ii thessalonians": "2 Thess.", "2 thess": "2 Thess.", "2 th": "2 Thess.", "2thess": "2 Thess.",
|
"2 thessalonians": "2 Thess.", "ii thessalonians": "2 Thess.", "2 thess": "2 Thess.",
|
||||||
"1 timothy": "1 Tim.", "i timothy": "1 Tim.", "1 tim": "1 Tim.", "1ti": "1 Tim.", "1 timothy": "1 Tim.",
|
"1 timothy": "1 Tim.", "i timothy": "1 Tim.", "1 tim": "1 Tim.", "1ti": "1 Tim.",
|
||||||
"2 timothy": "2 Tim.", "ii timothy": "2 Tim.", "2 tim": "2 Tim.", "2ti": "2 Tim.", "2 timothy": "2 Tim.",
|
"2 timothy": "2 Tim.", "ii timothy": "2 Tim.", "2 tim": "2 Tim.", "2ti": "2 Tim.",
|
||||||
"titus": "Titus", "tit": "Titus", "ti.": "Titus",
|
"titus": "Titus", "tit": "Titus",
|
||||||
"philemon": "Philem.", "philem": "Philem.", "phm": "Philem.",
|
"philemon": "Philem.", "philem": "Philem.",
|
||||||
"hebrews": "Heb.", "heb": "Heb.",
|
"hebrews": "Heb.", "heb": "Heb.",
|
||||||
"james": "Jas.", "jas": "Jas.", "jam": "Jas.", "jms": "Jas.",
|
"james": "Jas.", "jas": "Jas.",
|
||||||
"1 peter": "1 Pet.", "i peter": "1 Pet.", "1 pet": "1 Pet.", "1pe": "1 Pet.",
|
"1 peter": "1 Pet.", "i peter": "1 Pet.", "1 pet": "1 Pet.",
|
||||||
"2 peter": "2 Pet.", "ii peter": "2 Pet.", "2 pet": "2 Pet.", "2pe": "2 Pet.",
|
"2 peter": "2 Pet.", "ii peter": "2 Pet.", "2 pet": "2 Pet.",
|
||||||
"1 john": "1 John", "i john": "1 John", "1jn": "1 John", "1 jo": "1 John",
|
"1 john": "1 John", "i john": "1 John",
|
||||||
"2 john": "2 John", "ii john": "2 John", "2jn": "2 John", "2 jo": "2 John",
|
"2 john": "2 John", "ii john": "2 John",
|
||||||
"3 john": "3 John", "iii john": "3 John", "3jn": "3 John", "3 jo": "3 John",
|
"3 john": "3 John", "iii john": "3 John",
|
||||||
"jude": "Jude", "jud": "Jude",
|
"jude": "Jude",
|
||||||
"revelation": "Rev.", "rev": "Rev.", "re": "Rev.",
|
"revelation": "Rev.", "rev": "Rev.",
|
||||||
}
|
}
|
||||||
|
|
||||||
def _variants() -> Dict[str, str]:
|
# Short bare tokens that must *never* be treated as “roman numeral + book”
|
||||||
base = dict(BOOK_CANON)
|
# (prevents 'Isa.' => '1 Sam.' and similar).
|
||||||
numbered = [
|
NEVER_PREFIX_NUMERAL = {"isa", "isaiah", "job", "joel", "amos", "nah", "hag", "mal", "rom", "gal", "eph", "tit", "heb", "jas", "jude"}
|
||||||
("samuel", "Sam."), ("kings", "Ki."), ("chronicles", "Chron."),
|
|
||||||
("corinthians", "Cor."), ("thessalonians", "Thess."),
|
|
||||||
("timothy", "Tim."), ("peter", "Pet."), ("john", "John"),
|
|
||||||
]
|
|
||||||
for n, prefix in (("1", "1"), ("i", "1"), ("2", "2"), ("ii", "2"), ("3", "3"), ("iii", "3")):
|
|
||||||
for name, abbr in numbered:
|
|
||||||
base[f"{n} {name}"] = f"{prefix} {abbr}"
|
|
||||||
base[f"{n}{name}"] = f"{prefix} {abbr}"
|
|
||||||
base[f"{n}{name[:2]}"] = f"{prefix} {abbr}"
|
|
||||||
base["ps"] = "Ps."
|
|
||||||
base["prov"] = "Prov."
|
|
||||||
base["eccles"] = "Eccl."
|
|
||||||
base["deut."] = "Deut."
|
|
||||||
base["mt"] = "Matt."
|
|
||||||
base["mk"] = "Mark"
|
|
||||||
base["lk"] = "Luke"
|
|
||||||
base["jn"] = "John"
|
|
||||||
base["ti"] = "Tim."
|
|
||||||
base["co"] = "Cor."
|
|
||||||
return base
|
|
||||||
|
|
||||||
BOOK_MAP = _variants()
|
def _canon_key(s: str) -> str:
|
||||||
|
s = (s or "").strip().lower()
|
||||||
|
s = s.replace(".", " ")
|
||||||
|
s = re.sub(r"\s+", " ", s)
|
||||||
|
return s
|
||||||
|
|
||||||
|
def _direct_lookup(key: str) -> Optional[str]:
|
||||||
|
"""Try direct lookups with and without spaces (e.g., 'i samuel', '1co')."""
|
||||||
|
if key in BOOK_CANON:
|
||||||
|
return BOOK_CANON[key]
|
||||||
|
nospace = key.replace(" ", "")
|
||||||
|
if nospace in BOOK_CANON:
|
||||||
|
return BOOK_CANON[nospace]
|
||||||
|
return None
|
||||||
|
|
||||||
|
# Split helpers
|
||||||
CRUFT_RE = re.compile(r"\b(read|see|chap(?:ter)?|ch)\b\.?", re.I)
|
CRUFT_RE = re.compile(r"\b(read|see|chap(?:ter)?|ch)\b\.?", re.I)
|
||||||
|
|
||||||
BOOK_RE = re.compile(
|
|
||||||
r"""
|
|
||||||
^\s*
|
|
||||||
(?:
|
|
||||||
(?P<num>[1-3]|i{1,3})\s*
|
|
||||||
)?
|
|
||||||
\s*
|
|
||||||
(?P<book>[A-Za-z\.]+(?:\s+[A-Za-z\.]+){0,2})
|
|
||||||
""",
|
|
||||||
re.X,
|
|
||||||
)
|
|
||||||
|
|
||||||
C_V_RE = re.compile(
|
|
||||||
r"""
|
|
||||||
(?:
|
|
||||||
(?P<ch>\d+)
|
|
||||||
(?:
|
|
||||||
:(?P<vs>[\d,\-\u2013\u2014\s]+)
|
|
||||||
)?
|
|
||||||
)
|
|
||||||
""",
|
|
||||||
re.X,
|
|
||||||
)
|
|
||||||
|
|
||||||
def _clean_text(s: str) -> str:
|
def _clean_text(s: str) -> str:
|
||||||
s = s.replace("\xa0", " ").replace("\u2009", " ").replace("\u202f", " ")
|
s = s.replace("\xa0", " ").replace("\u2009", " ").replace("\u202f", " ")
|
||||||
s = CRUFT_RE.sub("", s)
|
s = CRUFT_RE.sub("", s)
|
||||||
s = s.replace("..", ".").replace("—", "-").replace("–", "-")
|
s = s.replace("—", "-").replace("–", "-")
|
||||||
s = re.sub(r"\s{2,}", " ", s)
|
s = re.sub(r"\s+", " ", s)
|
||||||
return s.strip(" ;,.\t\r\n ")
|
return s.strip(" ;,.\t\r\n ")
|
||||||
|
|
||||||
def _canon_key(raw: str) -> str:
|
BOOK_PREFIX_RE = re.compile(
|
||||||
key = raw.lower().strip()
|
r"""
|
||||||
key = key.replace(".", "")
|
^\s*
|
||||||
key = re.sub(r"\s+", " ", key)
|
(?:(?P<num>[1-3]|i{1,3})\s*)? # optional leading number (1/2/3 or i/ii/iii)
|
||||||
return key
|
(?P<book>[A-Za-z\.]+(?:\s+[A-Za-z\.]+){0,2}) # 1-3 words for the book
|
||||||
|
""",
|
||||||
|
re.X,
|
||||||
|
)
|
||||||
|
|
||||||
def _canon_book(book_raw: str, num: Optional[str] = None) -> Optional[str]:
|
C_V_RE = re.compile(r"(?P<ch>\d+)(?::(?P<vs>[\d,\-\s]+))?")
|
||||||
|
|
||||||
|
def _canon_book(book_raw: str, num: Optional[str]) -> Optional[str]:
|
||||||
"""
|
"""
|
||||||
Map a raw book token to our canonical abbreviation.
|
Decide the canonical book abbreviation.
|
||||||
Strategy:
|
Rules:
|
||||||
1) Try exact/alias matches (with dots removed) ✅
|
1) Always try direct alias matches first (with/without spaces).
|
||||||
2) Try the same again with ALL spaces removed ✅ (so 'i sa' -> 'isa')
|
2) If the *book* piece looks like a short token that could be misread as a roman
|
||||||
3) Only then, try the 'leading 1/2/3 or i/ii/iii + rest' heuristic.
|
numeral + another book (e.g., 'Isa'), *never* do numeral heuristics.
|
||||||
|
3) Otherwise, allow '1/2/3 + co/cor/ti/tim/pet/peter/jn/john' style heuristics.
|
||||||
"""
|
"""
|
||||||
key = _canon_key(book_raw) # lower, trimmed, dots removed, single spaces
|
key = _canon_key(book_raw)
|
||||||
key_nospace = key.replace(" ", "") # used to catch 'i sa' -> 'isa'
|
# Step 1: direct lookups
|
||||||
|
direct = _direct_lookup(key)
|
||||||
|
if direct:
|
||||||
|
return direct
|
||||||
|
|
||||||
# 1) direct lookup (e.g., 'isa', 'isaiah', 'ps', '1 corinthians', etc.)
|
# Guard: tokens like 'isa' must not be split into 'i' + 'sa'
|
||||||
if key in BOOK_MAP:
|
bare = key.replace(" ", "")
|
||||||
return BOOK_MAP[key]
|
if bare in NEVER_PREFIX_NUMERAL:
|
||||||
if key_nospace in BOOK_MAP:
|
return _direct_lookup(bare)
|
||||||
return BOOK_MAP[key_nospace]
|
|
||||||
|
|
||||||
# 2) special explicit “number + short key” combos
|
# Step 3: numeral + short token combos
|
||||||
if num and key in ("co", "cor"):
|
if num:
|
||||||
return f"{num} Cor."
|
n = {"i": "1", "ii": "2", "iii": "3"}.get(num.lower(), num)
|
||||||
if num and key in ("ti", "tim"):
|
short = key.replace(" ", "")
|
||||||
return f"{num} Tim."
|
# common short targets where number matters
|
||||||
|
map_num = {
|
||||||
|
"co": "Cor.", "cor": "Cor.",
|
||||||
|
"ti": "Tim.", "tim": "Tim.",
|
||||||
|
"pe": "Pet.", "pet": "Pet.", "peter": "Pet.",
|
||||||
|
"jo": "John", "jn": "John", "joh": "John", "john": "John",
|
||||||
|
"sa": "Sam.", "sam": "Sam.", "samuel": "Sam.",
|
||||||
|
"ki": "Ki.", "kgs": "Ki.", "kings": "Ki.",
|
||||||
|
"chronicles": "Chron.", "chron": "Chron.",
|
||||||
|
"thessalonians": "Thess.", "thess": "Thess.",
|
||||||
|
}
|
||||||
|
if short in map_num:
|
||||||
|
return f"{n} {map_num[short]}"
|
||||||
|
|
||||||
# 3) heuristic: split a leading number/roman from the rest (e.g., 'i john' or '2ki')
|
# Fallback
|
||||||
m = re.match(r"(?P<n>[1-3]|i{1,3})(?P<rest>[A-Za-z].*)$", key_nospace)
|
return None
|
||||||
if m:
|
|
||||||
n = {"i": "1", "ii": "2", "iii": "3"}.get(m.group("n"), m.group("n"))
|
|
||||||
rest = m.group("rest")
|
|
||||||
if rest in BOOK_MAP:
|
|
||||||
base = BOOK_MAP[rest]
|
|
||||||
# If base already begins with a number (rare in our map), replace it with n
|
|
||||||
if base[0] in "123":
|
|
||||||
return f"{n} {base.split(' ', 1)[1]}"
|
|
||||||
return f"{n} {base}"
|
|
||||||
|
|
||||||
# 4) last‑resort: try the raw key against canonical map
|
|
||||||
return BOOK_CANON.get(key_nospace) or BOOK_CANON.get(key)
|
|
||||||
|
|
||||||
def _parse_segment(seg: str, last_book: Optional[str]) -> Tuple[Optional[str], Optional[str], bool]:
|
def _parse_segment(seg: str, last_book: Optional[str]) -> Tuple[Optional[str], Optional[str], bool]:
|
||||||
original = seg.strip()
|
"""
|
||||||
s = _clean_text(seg)
|
Parse one semicolon-delimited piece.
|
||||||
if not s:
|
Returns (book_canon, cv, preserve_original_if_unparsed).
|
||||||
|
"""
|
||||||
|
raw = _clean_text(seg)
|
||||||
|
if not raw:
|
||||||
return (None, None, False)
|
return (None, None, False)
|
||||||
|
|
||||||
m = BOOK_RE.match(s)
|
m = BOOK_PREFIX_RE.match(raw)
|
||||||
book = None
|
book_part = None
|
||||||
rest = s
|
num = None
|
||||||
|
rest = raw
|
||||||
|
|
||||||
if m:
|
if m:
|
||||||
num = (m.group("num") or "").strip()
|
# What the regex thinks is a numeric prefix and a book word blob:
|
||||||
raw_book = (m.group("book") or "").strip()
|
num = (m.group("num") or "").strip() or None
|
||||||
raw_joined = f"{num} {raw_book}".strip()
|
book_text = (m.group("book") or "").strip()
|
||||||
canon = _canon_book(raw_joined or raw_book, num=num or None)
|
rest = raw[m.end():].strip(" :,;.")
|
||||||
if canon:
|
|
||||||
book = canon
|
|
||||||
rest = s[m.end():].strip(",;: .")
|
|
||||||
else:
|
|
||||||
return (None, original, True)
|
|
||||||
|
|
||||||
if not book:
|
# Try to resolve the book using robust rules
|
||||||
if last_book:
|
book_part = _canon_book(book_text if not num else f"{num} {book_text}", num=num)
|
||||||
book = last_book
|
|
||||||
else:
|
|
||||||
return (None, original, True)
|
|
||||||
|
|
||||||
rest = re.sub(r"\s+", "", rest)
|
# If regex split out a bogus 'i' from something like "Isa.", fix by trying full token directly.
|
||||||
rest = re.sub(r":\s*", ":", rest)
|
if not book_part:
|
||||||
|
whole = _canon_book(book_text, None)
|
||||||
|
if whole:
|
||||||
|
book_part = whole
|
||||||
|
rest = raw[m.end():].strip(" :,;.")
|
||||||
|
|
||||||
|
# no book found → inherit previous if we have verses; otherwise preserve as-is
|
||||||
|
if not book_part:
|
||||||
|
# If there's obvious chapter/verse, but no book, we cannot link → preserve as-is.
|
||||||
|
if C_V_RE.search(rest):
|
||||||
|
return (None, None, True)
|
||||||
|
# Or the whole thing might already be a known book alone:
|
||||||
|
whole = _canon_book(raw, None)
|
||||||
|
if whole:
|
||||||
|
return (whole, None, False)
|
||||||
|
return (None, None, True)
|
||||||
|
|
||||||
|
# normalize chapter/verse
|
||||||
|
rest = rest.replace(" ", "")
|
||||||
if not rest:
|
if not rest:
|
||||||
return (book, None, False)
|
return (book_part, None, False)
|
||||||
|
|
||||||
if C_V_RE.search(rest):
|
mcv = C_V_RE.search(rest)
|
||||||
cv = rest.replace(" ", "")
|
if mcv:
|
||||||
else:
|
cv = rest
|
||||||
m2 = re.search(r"\d+(?::[\d,\-]+)?", rest)
|
cv = cv.replace(" ", "")
|
||||||
cv = m2.group(0).replace(" ", "") if m2 else None
|
return (book_part, cv, False)
|
||||||
|
|
||||||
if not cv:
|
# not a recognizable cv → keep original piece untouched
|
||||||
return (book, None, False)
|
return (None, None, True)
|
||||||
|
|
||||||
cv = cv.replace("–", "-").replace("—", "-")
|
|
||||||
cv = re.sub(r",\s*", ",", cv)
|
|
||||||
|
|
||||||
return (book, cv, False)
|
|
||||||
|
|
||||||
def normalize_scripture_field(text: str) -> Tuple[str, List[str]]:
|
def normalize_scripture_field(text: str) -> Tuple[str, List[str]]:
|
||||||
|
"""
|
||||||
|
Normalize a whole scripture_raw string.
|
||||||
|
Returns (normalized_text, warnings).
|
||||||
|
Unknown/unparseable chunks are preserved verbatim.
|
||||||
|
"""
|
||||||
warnings: List[str] = []
|
warnings: List[str] = []
|
||||||
if not text:
|
if not text:
|
||||||
return ("", warnings)
|
return ("", warnings)
|
||||||
@@ -238,13 +228,10 @@ def normalize_scripture_field(text: str) -> Tuple[str, List[str]]:
|
|||||||
|
|
||||||
for piece in pieces:
|
for piece in pieces:
|
||||||
book, cv, preserve = _parse_segment(piece, last_book)
|
book, cv, preserve = _parse_segment(piece, last_book)
|
||||||
if preserve:
|
|
||||||
out.append(cv or piece.strip())
|
|
||||||
continue
|
|
||||||
|
|
||||||
if book and cv:
|
if preserve:
|
||||||
out.append(f"{book} {cv}")
|
# Keep the original chunk
|
||||||
last_book = book
|
out.append(_clean_text(piece))
|
||||||
continue
|
continue
|
||||||
|
|
||||||
if book and not cv:
|
if book and not cv:
|
||||||
@@ -252,9 +239,14 @@ def normalize_scripture_field(text: str) -> Tuple[str, List[str]]:
|
|||||||
last_book = book
|
last_book = book
|
||||||
continue
|
continue
|
||||||
|
|
||||||
if piece.strip():
|
if not book and cv:
|
||||||
out.append(piece.strip())
|
warnings.append(f"Missing book for '{piece.strip()}'")
|
||||||
|
continue
|
||||||
|
|
||||||
norm = "; ".join(x.strip() for x in out if x.strip())
|
if book and cv:
|
||||||
|
out.append(f"{book} {cv}")
|
||||||
|
last_book = book
|
||||||
|
|
||||||
|
norm = "; ".join(s for s in (o.strip(" ;,") for o in out) if s)
|
||||||
norm = re.sub(r"\s+", " ", norm).strip(" ;,")
|
norm = re.sub(r"\s+", " ", norm).strip(" ;,")
|
||||||
return (norm, warnings)
|
return (norm, warnings)
|
||||||
Reference in New Issue
Block a user