Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
112 changes: 73 additions & 39 deletions app/services/full_psalm_import_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from xml.etree import ElementTree as ET

from app.core.config import get_settings
from app.services import registry_service
from app.services import audit_service, registry_service

HEBREW_MARKS_RE = re.compile(r"[\u0591-\u05C7]")
OSIS_NS = {"osis": "http://www.bibletechnologies.net/2003/OSIS/namespace"}
Expand Down Expand Up @@ -463,14 +463,27 @@ def _load_oshb_tokens() -> dict[tuple[int, int], list[dict[str, Any]]]:
verse_number = int(verse.attrib["osisID"].split(".")[2])
tokens: list[dict[str, Any]] = []
word_index = 0
qere_words = 0
for child in verse:
if child.tag == f"{{{OSIS_NS['osis']}}}note":
# MACULA gives each qere word its own number after the ketiv, and nothing
# at the ketiv's; OSHB keeps the qere in a note after the ketiv word.
qere = child.findall("osis:rdg[@type='x-qere']/osis:w", OSIS_NS)
if qere and tokens:
ketiv_number = tokens[-1]["macula_word_number"]
tokens[-1]["qere_word_numbers"] = [
ketiv_number + offset for offset in range(1, len(qere) + 1)
]
qere_words += len(qere)
continue
if child.tag != f"{{{OSIS_NS['osis']}}}w":
continue
word_index += 1
morph_code = _parse_oshb_morph(child.attrib.get("morph"))
tokens.append(
{
"word_number": word_index,
"macula_word_number": word_index + qere_words,
"surface": _clean_oshb_surface("".join(child.itertext())),
"lemma": child.attrib.get("lemma"),
"strong": _parse_strong_number(child.attrib.get("lemma")),
Expand All @@ -484,7 +497,7 @@ def _load_oshb_tokens() -> dict[tuple[int, int], list[dict[str, Any]]]:
return tokens_by_verse


def _load_macula_groups() -> dict[tuple[int, int, int], dict[str, Any]]:
def _load_macula_words() -> dict[tuple[int, int, int], dict[str, Any]]:
grouped: dict[tuple[int, int, int], dict[str, Any]] = {}
for path in sorted((get_settings().raw_dir / "macula" / "lowfat").glob("19-Psa-*-lowfat.xml")):
root = ET.fromstring(path.read_text(encoding="utf-8"))
Expand Down Expand Up @@ -548,39 +561,42 @@ def _load_macula_groups() -> dict[tuple[int, int, int], dict[str, Any]]:
"strongnumberx": _collapse_whitespace(word.attrib.get("strongnumberx")),
}
)
normalized: dict[tuple[int, int, int], dict[str, Any]] = {}
for key, payload in grouped.items():
gloss_parts = [
part for part in (_clean_gloss_part(item) for item in payload["glosses"]) if part
]
word_sense = payload["glosses"][-1] if payload["glosses"] else None
display_gloss = _derive_display_gloss(payload["components"])
normalized[key] = {
"transliteration": _clean_transliteration(payload["transliterations"]),
"lemma": payload["lemmas"][-1] if payload["lemmas"] else None,
"syntax_role": payload["roles"][0] if payload["roles"] else None,
"semantic_role": None,
"referent": None,
"word_sense": word_sense,
"gloss_parts": gloss_parts,
"display_gloss": display_gloss,
"compiler_features": _derive_compiler_features(
payload["components"], word_sense, display_gloss
),
"part_of_speech": payload["pos"][-1] if payload["pos"] else None,
"stem": payload["stems"][-1] if payload["stems"] else None,
"greek": _collapse_whitespace(" ".join(payload["greek"])) or None,
"greek_strong": payload["greekstrong"][-1] if payload["greekstrong"] else None,
}
return normalized
return grouped


def _normalize_macula_words(words: list[dict[str, Any]]) -> dict[str, Any]:
"""Enrichment for one token from the MACULA word(s) it reads as, in order."""
payload = {field: [item for word in words for item in word[field]] for field in words[0]}
gloss_parts = [
part for part in (_clean_gloss_part(item) for item in payload["glosses"]) if part
]
word_sense = payload["glosses"][-1] if payload["glosses"] else None
display_gloss = _derive_display_gloss(payload["components"])
return {
"transliteration": _clean_transliteration(payload["transliterations"]),
"lemma": payload["lemmas"][-1] if payload["lemmas"] else None,
"syntax_role": payload["roles"][0] if payload["roles"] else None,
"semantic_role": None,
"referent": None,
"word_sense": word_sense,
"gloss_parts": gloss_parts,
"display_gloss": display_gloss,
"compiler_features": _derive_compiler_features(
payload["components"], word_sense, display_gloss
),
"part_of_speech": payload["pos"][-1] if payload["pos"] else None,
"stem": payload["stems"][-1] if payload["stems"] else None,
"greek": _collapse_whitespace(" ".join(payload["greek"])) or None,
"greek_strong": payload["greekstrong"][-1] if payload["greekstrong"] else None,
}


def _load_lxx_verses(
macula_groups: dict[tuple[int, int, int], dict[str, Any]],
macula_words: dict[tuple[int, int, int], dict[str, Any]],
) -> dict[tuple[int, int], str]:
verse_words: dict[tuple[int, int], list[tuple[int, str]]] = defaultdict(list)
for (psalm_number, verse_number, word_number), payload in macula_groups.items():
greek = _collapse_whitespace(payload.get("greek"))
for (psalm_number, verse_number, word_number), payload in macula_words.items():
greek = _collapse_whitespace(" ".join(payload["greek"]))
if not greek:
continue
verse_words[(psalm_number, verse_number)].append((word_number, greek))
Expand Down Expand Up @@ -645,7 +661,7 @@ def _build_unit(
verse_number: int,
source_hebrew: str,
oshb_tokens: list[dict[str, Any]],
macula_groups: dict[tuple[int, int, int], dict[str, Any]],
macula_words: dict[tuple[int, int, int], dict[str, Any]],
witness_verses: dict[str, dict[tuple[int, int], str]],
) -> dict[str, Any]:
unit_id = _unit_id(psalm_number, verse_number)
Expand All @@ -667,7 +683,16 @@ def _build_unit(
]
for oshb_token in oshb_tokens:
word_number = oshb_token["word_number"]
macula = macula_groups.get((psalm_number, verse_number, word_number), {})
# A ketiv has no MACULA word of its own; it reads as its qere.
numbers = oshb_token.get("qere_word_numbers") or [
oshb_token.get("macula_word_number", word_number)
]
words = [
macula_words[key]
for key in ((psalm_number, verse_number, number) for number in numbers)
if key in macula_words
]
macula = _normalize_macula_words(words) if words else {}
token = {
"token_id": _token_id(unit_id, word_number),
"ref": f"{ref}#{word_number}",
Expand Down Expand Up @@ -942,9 +967,9 @@ def import_vendored_psalms() -> list[dict[str, Any]]:

uxlc_verses = _load_uxlc_verses()
oshb_tokens = _load_oshb_tokens()
macula_groups = _load_macula_groups()
macula_words = _load_macula_words()
witness_verses = {source_id: _load_witness_verses(source_id) for source_id in WITNESS_SOURCES}
witness_verses["lxx"] = _load_lxx_verses(macula_groups)
witness_verses["lxx"] = _load_lxx_verses(macula_words)

units: list[dict[str, Any]] = []
by_psalm: dict[int, list[dict[str, Any]]] = defaultdict(list)
Expand All @@ -958,7 +983,7 @@ def import_vendored_psalms() -> list[dict[str, Any]]:
verse_number=verse_number,
source_hebrew=uxlc_verses[(psalm_number, verse_number)],
oshb_tokens=oshb_tokens.get((psalm_number, verse_number), []),
macula_groups=macula_groups,
macula_words=macula_words,
witness_verses=witness_verses,
)
units.append(unit)
Expand All @@ -967,11 +992,20 @@ def import_vendored_psalms() -> list[dict[str, Any]]:
_apply_occurrence_refs(units)

for unit in units:
registry_service.save_unit(
_merge_with_existing(
unit, registry_service.unit_path(unit["unit_id"]), IMPORTED_UNIT_FIELDS
path = registry_service.unit_path(unit["unit_id"])
before = registry_service.read_json(path) if path.exists() else None
merged = _merge_with_existing(unit, path, IMPORTED_UNIT_FIELDS)
if before is not None and merged != before:
audit_service.create_audit_record(
merged,
before_hash=registry_service.file_hash(before),
after_hash=registry_service.file_hash(merged),
summary="Refresh source text and tokens from the vendored corpus",
rationale="Re-import from UXLC, OSHB and MACULA changed fields the corpus owns.",
created_by="import-psalms",
change_type="import",
)
)
registry_service.save_unit(merged)
for psalm_number, psalm_units in by_psalm.items():
psalm_id = _psalm_id(psalm_number)
meta_path = registry_service.psalm_dir(psalm_id) / f"{psalm_id}.meta.json"
Expand Down
27 changes: 22 additions & 5 deletions content/psalms/ps002/ps002.v002.a.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
],
"audit_ids": [
"aud.ps002.v002.a.0001",
"aud.ps002.v002.a.0002"
"aud.ps002.v002.a.0002",
"aud.ps002.v002.a.0003"
],
"audit_records": [
{
Expand Down Expand Up @@ -39,6 +40,22 @@
"summary": "Create comparison assessment",
"triggered_by_issue": null,
"triggered_by_pr": null
},
{
"after_hash": "4f3094d494992947a033fd7aff57b5290943a3189a2d70612948e830c1fabf90",
"audit_id": "aud.ps002.v002.a.0003",
"before_hash": "631b66fa7c1dca6cf57c020412c31c7a3608c047bef689c7a20bd7c805d09b66",
"change_type": "import",
"checks": [],
"created_at": "2026-11-23T23:59:14.905608+00:00",
"created_by": "import-psalms",
"entity_id": "ps002.v002.a",
"entity_type": "unit",
"rationale": "Re-import from UXLC, OSHB and MACULA changed fields the corpus owns.",
"review_signoff": {},
"summary": "Refresh source text and tokens from the vendored corpus",
"triggered_by_issue": null,
"triggered_by_pr": null
}
],
"canonical_rendering_ids": [],
Expand Down Expand Up @@ -520,6 +537,7 @@
"Psalm 10:18",
"Psalm 12:7",
"Psalm 16:3",
"Psalm 17:11",
"Psalm 18:8",
"Psalm 19:5",
"Psalm 21:11",
Expand All @@ -536,8 +554,7 @@
"Psalm 37:3",
"Psalm 37:9",
"Psalm 37:11",
"Psalm 37:22",
"Psalm 37:29"
"Psalm 37:22"
],
"display_gloss": "of the earth",
"enrichment_sources": {
Expand Down Expand Up @@ -590,6 +607,7 @@
"Psalm 10:18",
"Psalm 12:7",
"Psalm 16:3",
"Psalm 17:11",
"Psalm 18:8",
"Psalm 19:5",
"Psalm 21:11",
Expand All @@ -606,8 +624,7 @@
"Psalm 37:3",
"Psalm 37:9",
"Psalm 37:11",
"Psalm 37:22",
"Psalm 37:29"
"Psalm 37:22"
],
"ref": "Psalm 2:2#3",
"referent": null,
Expand Down
27 changes: 22 additions & 5 deletions content/psalms/ps002/ps002.v008.a.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
],
"audit_ids": [
"aud.ps002.v008.a.0001",
"aud.ps002.v008.a.0002"
"aud.ps002.v008.a.0002",
"aud.ps002.v008.a.0003"
],
"audit_records": [
{
Expand Down Expand Up @@ -39,6 +40,22 @@
"summary": "Create comparison assessment",
"triggered_by_issue": null,
"triggered_by_pr": null
},
{
"after_hash": "cfa609ac52ab515f01c89ca0131375401c443620c6ab52ea74900cab931f2a69",
"audit_id": "aud.ps002.v008.a.0003",
"before_hash": "1a274decb5b4ea103e656ff04225eeef849586f4d378b36c3a41a6731e3fcafe",
"change_type": "import",
"checks": [],
"created_at": "2026-11-23T23:59:14.929944+00:00",
"created_by": "import-psalms",
"entity_id": "ps002.v008.a",
"entity_type": "unit",
"rationale": "Re-import from UXLC, OSHB and MACULA changed fields the corpus owns.",
"review_signoff": {},
"summary": "Refresh source text and tokens from the vendored corpus",
"triggered_by_issue": null,
"triggered_by_pr": null
}
],
"canonical_rendering_ids": [],
Expand Down Expand Up @@ -1194,6 +1211,7 @@
"Psalm 10:18",
"Psalm 12:7",
"Psalm 16:3",
"Psalm 17:11",
"Psalm 18:8",
"Psalm 19:5",
"Psalm 21:11",
Expand All @@ -1210,8 +1228,7 @@
"Psalm 37:3",
"Psalm 37:9",
"Psalm 37:11",
"Psalm 37:22",
"Psalm 37:29"
"Psalm 37:22"
],
"display_gloss": "of the earth",
"enrichment_sources": {
Expand Down Expand Up @@ -1264,6 +1281,7 @@
"Psalm 10:18",
"Psalm 12:7",
"Psalm 16:3",
"Psalm 17:11",
"Psalm 18:8",
"Psalm 19:5",
"Psalm 21:11",
Expand All @@ -1280,8 +1298,7 @@
"Psalm 37:3",
"Psalm 37:9",
"Psalm 37:11",
"Psalm 37:22",
"Psalm 37:29"
"Psalm 37:22"
],
"ref": "Psalm 2:8#8",
"referent": null,
Expand Down
27 changes: 22 additions & 5 deletions content/psalms/ps002/ps002.v010.a.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
],
"audit_ids": [
"aud.ps002.v010.a.0001",
"aud.ps002.v010.a.0002"
"aud.ps002.v010.a.0002",
"aud.ps002.v010.a.0003"
],
"audit_records": [
{
Expand Down Expand Up @@ -39,6 +40,22 @@
"summary": "Create comparison assessment",
"triggered_by_issue": null,
"triggered_by_pr": null
},
{
"after_hash": "719639de25d55b492f3a1c03579eca419d8bad2abb58b4cbc0e8d2f4d58b13ce",
"audit_id": "aud.ps002.v010.a.0003",
"before_hash": "c411dd487961e1515aabfde761df55fa2bc95f91e2224412f7ec74cf15c67850",
"change_type": "import",
"checks": [],
"created_at": "2026-11-23T23:59:14.939648+00:00",
"created_by": "import-psalms",
"entity_id": "ps002.v010.a",
"entity_type": "unit",
"rationale": "Re-import from UXLC, OSHB and MACULA changed fields the corpus owns.",
"review_signoff": {},
"summary": "Refresh source text and tokens from the vendored corpus",
"triggered_by_issue": null,
"triggered_by_pr": null
}
],
"canonical_rendering_ids": [],
Expand Down Expand Up @@ -825,6 +842,7 @@
"Psalm 10:18",
"Psalm 12:7",
"Psalm 16:3",
"Psalm 17:11",
"Psalm 18:8",
"Psalm 19:5",
"Psalm 21:11",
Expand All @@ -841,8 +859,7 @@
"Psalm 37:3",
"Psalm 37:9",
"Psalm 37:11",
"Psalm 37:22",
"Psalm 37:29"
"Psalm 37:22"
],
"display_gloss": "of the earth",
"enrichment_sources": {
Expand Down Expand Up @@ -895,6 +912,7 @@
"Psalm 10:18",
"Psalm 12:7",
"Psalm 16:3",
"Psalm 17:11",
"Psalm 18:8",
"Psalm 19:5",
"Psalm 21:11",
Expand All @@ -911,8 +929,7 @@
"Psalm 37:3",
"Psalm 37:9",
"Psalm 37:11",
"Psalm 37:22",
"Psalm 37:29"
"Psalm 37:22"
],
"ref": "Psalm 2:10#6",
"referent": null,
Expand Down
Loading
Loading