Coverage for rdgai/notes.py: 98.46%
65 statements
« prev ^ index » next coverage.py v7.17.3, created at 2026-11-24 23:53 +0000
« prev ^ index » next coverage.py v7.17.3, created at 2026-11-24 23:53 +0000
1"""
2Notes embedded in the text of readings.
4Some apparatuses (e.g. ECM Mark) put editorial notes inside the reading text in parentheses:
5``θεου (cf. 24b)``, ``om (cf. 27c)``, ``τα περι (cf. 27/4) αυτης παντα``, ``αυτον (κενον)``, ``(Is 40:4-8)``.
6These are split off at runtime so that they are never treated as reading text. ``om`` (Latin *omittit*)
7left on its own marks an omission.
9Cross-references such as ``cf. 8b`` (same verse, word 8, reading b), ``cf. 3/4-6`` (verse 3, words 4-6) and
10lists such as ``cf. 38/14, 24`` or ``cf. 18a, c`` are resolved against unit ids of the form
11``<book>.<chapter>.<verse>.<start>[-<end>]``. Anything else is left unresolved.
12"""
13import re
14from dataclasses import dataclass
15from typing import Iterable
17NOTE = re.compile(r"\s*\(([^()]*)\)")
18OMISSION = re.compile(r"^om\.?$")
19UNIT_ID = re.compile(r"^(?P<prefix>.*\.)(?P<verse>\d+)\.(?P<start>\d+)(?:-(?P<end>\d+))?$")
20POSITION_ITEM = re.compile(r"^(?:(?P<verse>\d+)/)?(?P<start>\d+)(?:-(?P<end>\d+))?(?P<reading>[a-z]+)?$")
21READING_ITEM = re.compile(r"^(?P<reading>[a-z]+)$")
24def parse_reading_notes(text:str) -> tuple[str, list[str]]:
25 """ Splits a reading's text into the text proper and the notes given in parentheses. """
26 notes = [note.strip() for note in NOTE.findall(text)]
27 if not notes:
28 # leave text without notes exactly as it is (apart from a lone 'om')
29 return ("" if OMISSION.match(text.strip()) else text), []
30 cleaned = " ".join(NOTE.sub(" ", text).split())
31 if OMISSION.match(cleaned):
32 cleaned = ""
33 return cleaned, notes
36@dataclass
37class NoteReference:
38 label: str
39 unit_id: str | None
40 reading: str | None
43def _unit_range(unit_id:str) -> tuple[str, int, int, int] | None:
44 match = UNIT_ID.match(unit_id)
45 if not match:
46 return None
47 start = int(match["start"])
48 end = int(match["end"]) if match["end"] else start
49 return match["prefix"], int(match["verse"]), start, end
52def _find_unit(prefix:str, verse:int, start:int, end:int|None, unit_ids:list[str], unit_id_set:set[str]) -> str|None:
53 exact = f"{prefix}{verse}.{start}" + (f"-{end}" if end is not None else "")
54 if exact in unit_id_set:
55 return exact
56 for candidate in unit_ids:
57 parsed = _unit_range(candidate)
58 if parsed and parsed[0] == prefix and parsed[1] == verse and parsed[2] <= start <= parsed[3]:
59 return candidate
60 return None
63def resolve_references(note:str, unit_id:str, unit_ids:Iterable[str]) -> list[NoteReference]:
64 """
65 Returns the cross-references in a note, resolved to unit ids where possible.
66 A note that is not entirely a list of references (e.g. 'κενον', 'Is 40:4-8') gives an empty list.
67 """
68 body = note.strip()
69 if body.startswith("cf."):
70 body = body[3:].strip()
71 items = [item.strip() for item in body.split(",")]
72 if not items or not items[0] or not POSITION_ITEM.match(items[0]):
73 return []
74 if not all(POSITION_ITEM.match(item) or READING_ITEM.match(item) for item in items):
75 return []
77 unit_ids = list(unit_ids)
78 unit_id_set = set(unit_ids)
79 current = _unit_range(unit_id)
80 verse = current[1] if current else None
82 references = []
83 previous_unit = None
84 for item in items:
85 position = POSITION_ITEM.match(item)
86 if position:
87 if position["verse"]:
88 verse = int(position["verse"])
89 target = None
90 if current is not None and verse is not None:
91 end = int(position["end"]) if position["end"] else None
92 target = _find_unit(current[0], verse, int(position["start"]), end, unit_ids, unit_id_set)
93 references.append(NoteReference(label=item, unit_id=target, reading=position["reading"]))
94 previous_unit = target
95 else:
96 references.append(NoteReference(label=item, unit_id=previous_unit, reading=READING_ITEM.match(item)["reading"]))
97 return references