-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathold_osis_parser.py
More file actions
376 lines (301 loc) · 11.2 KB
/
old_osis_parser.py
File metadata and controls
376 lines (301 loc) · 11.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
"""Contains the parser for OSIS format files."""
from __future__ import annotations
import ast
from functools import lru_cache
from pathlib import Path
from typing import Any
from defusedxml import ElementTree
from pythonbible import Book
from pythonbible import InvalidVerseError
from pythonbible import Version
from pythonbible import get_book_chapter_verse
from pythonbible import get_verse_id
from pythonbible_parser.bible_parser import BibleParser
from pythonbible_parser.bible_parser import sort_paragraphs
from pythonbible_parser.osis.constants import BOOK_IDS
from pythonbible_parser.osis.osis_utilities import OSISID
from pythonbible_parser.osis.osis_utilities import get_element_tail
from pythonbible_parser.osis.osis_utilities import get_element_text
from pythonbible_parser.osis.osis_utilities import get_element_text_and_tail
from pythonbible_parser.osis.osis_utilities import get_namespace
from pythonbible_parser.osis.osis_utilities import parse_osis_id
from pythonbible_parser.osis.osis_utilities import strip_namespace_from_tag
XML_FOLDER: Path = Path(__file__).parent / "versions"
XPATH_BOOK: str = ".//xmlns:div[@osisID='{}']"
XPATH_BOOK_TITLE: str = f"{XPATH_BOOK}/xmlns:title"
XPATH_VERSE: str = ".//xmlns:verse[@osisID='{}.{}.{}']"
XPATH_VERSE_PARENT: str = f"{XPATH_VERSE}/.."
class OldOSISParser(BibleParser):
"""Parse files containing scripture text in the OSIS format.
OSISParser extends BibleParser and contains all the functionality necessary
to parse XML files that are in the OSIS format.
"""
def __init__(self: OldOSISParser, version: Version) -> None:
"""Initialize the OSIS parser.
Set the version, the element tree from the appropriate version XML file,
and the namespaces.
:param version:
"""
super().__init__(version)
self.tree: ElementTree = ElementTree.parse(
XML_FOLDER / f"{self.version.value.lower()}.xml",
)
self.namespaces: dict[str, str] = {
"xmlns": get_namespace(self.tree.getroot().tag),
}
@lru_cache()
def get_book_title(self: OldOSISParser, book: Book) -> str:
"""Given a book, return the full title for that book from the XML file.
:param book:
:return: the full title string
"""
book_title_element = self._get_book_title_element(book)
return book_title_element.text or ""
@lru_cache()
def get_short_book_title(self: OldOSISParser, book: Book) -> str:
"""Given a book, return the short title for that book from the XML file.
:param book:
:return: the short title string
"""
book_title_element = self._get_book_title_element(book)
return book_title_element.get("short") or ""
def get_scripture_passage_text(
self: OldOSISParser,
verse_ids: list[int],
**kwargs: Any | None,
) -> dict[Book, dict[int, list[str]]]:
"""Get the scripture passage for the given verse ids.
Given a list of verse ids, return the structured scripture text passage
organized by book, chapter, and paragraph.
If the include_verse_number keyword argument is True, include the verse
numbers in the scripture passage; otherwise, do not include them.
:param verse_ids:
:param kwargs
:return: the scripture passage text in a dictionary of books to
dictionary of chapter numbers to lists of paragraph strings
"""
if verse_ids is None or not verse_ids:
return {}
# Sort the verse ids and the convert it into a tuple so it's hashable
verse_ids.sort()
verse_ids_tuple: tuple[int, ...] = tuple(verse_ids)
# keyword arguments
include_verse_number: bool = ast.literal_eval(
str(kwargs.get("include_verse_number", True))
)
return self._get_scripture_passage_text_memoized(
verse_ids_tuple,
include_verse_number,
)
def verse_text(
self: OldOSISParser,
verse_id: int,
**kwargs: Any | None,
) -> str:
"""Get the scripture text for the given verse id.
Given a verse id, return the string scripture text passage for that verse.
If the include_verse_number keyword argument is True, include the verse
numbers in the scripture passage; otherwise, do not include them.
:param verse_id:
:param kwargs:
:return:
"""
if verse_id is None:
msg = "Verse id cannot be None."
raise InvalidVerseError(msg)
# keyword arguments
include_verse_number: bool = ast.literal_eval(
str(kwargs.get("include_verse_number", True))
)
return self._get_verse_text_memoized(verse_id, include_verse_number)
@lru_cache()
def _get_book_title_element(self: OldOSISParser, book: Book) -> Any:
xpath: str = XPATH_BOOK_TITLE.format(BOOK_IDS.get(book))
return self.tree.find(xpath, namespaces=self.namespaces)
@lru_cache()
def _get_scripture_passage_text_memoized(
self: OldOSISParser,
verse_ids: tuple[int],
include_verse_number: bool,
) -> dict[Book, dict[int, list[str]]]:
paragraphs: dict[Book, dict[int, list[str]]] = _get_paragraphs(
self.tree,
self.namespaces,
verse_ids,
include_verse_number,
)
return sort_paragraphs(paragraphs)
@lru_cache()
def _get_verse_text_memoized(
self: OldOSISParser,
verse_id: int,
include_verse_number: bool,
) -> str:
verse_ids = (verse_id,)
paragraphs: dict[Book, dict[int, list[str]]] = _get_paragraphs(
self.tree,
self.namespaces,
verse_ids,
include_verse_number,
)
verse_text: str = ""
for chapters in paragraphs.values():
for chapter_paragraphs in chapters.values():
verse_text = chapter_paragraphs[0]
break
return verse_text
def _get_paragraphs(
tree: ElementTree,
namespaces: dict[str, str],
verse_ids: tuple[int, ...],
include_verse_number: bool,
) -> dict[Book, dict[int, list[str]]]:
current_verse_id: int = verse_ids[0]
book: Book
chapter: int
verse: int
book, chapter, verse = get_book_chapter_verse(current_verse_id)
paragraph_element = tree.find(
XPATH_VERSE_PARENT.format(BOOK_IDS.get(book), chapter, verse),
namespaces,
)
paragraph: str
paragraph, current_verse_id = _get_paragraph_from_element(
paragraph_element,
verse_ids,
current_verse_id,
include_verse_number,
)
current_verse_index: int = verse_ids.index(current_verse_id) + 1
paragraph_dictionary: dict[Book, dict[int, list[str]]] = {}
if current_verse_index < len(verse_ids):
paragraph_dictionary = _get_paragraphs(
tree,
namespaces,
verse_ids[current_verse_index:],
include_verse_number,
)
book_dictionary: dict[int, list[str]] = paragraph_dictionary.get(book, {})
chapter_list: list[str] = book_dictionary.get(int(chapter), [])
chapter_list.insert(0, paragraph)
book_dictionary[int(chapter)] = chapter_list
paragraph_dictionary[book] = book_dictionary
return paragraph_dictionary
@lru_cache()
def _get_paragraph_from_element(
paragraph_element: Any,
verse_ids: tuple[int, ...],
current_verse_id: int,
include_verse_number: bool,
) -> tuple[str, int]:
new_current_verse_id: int = current_verse_id
paragraph: str = ""
skip_till_next_verse: bool = False
child_paragraph: str
for child_element in list(paragraph_element):
(
child_paragraph,
skip_till_next_verse,
new_current_verse_id,
) = _handle_child_element(
child_element,
verse_ids,
skip_till_next_verse,
new_current_verse_id,
include_verse_number,
)
if not child_paragraph:
continue
if paragraph and not paragraph.endswith(" "):
paragraph += " "
paragraph += child_paragraph
return clean_paragraph(paragraph), new_current_verse_id
@lru_cache()
def _handle_child_element(
child_element: Any,
verse_ids: tuple[int, ...],
skip_till_next_verse: bool,
current_verse_id: int,
include_verse_number: bool,
inside_note: bool = False,
) -> tuple[str, bool, int]:
tag: str = strip_namespace_from_tag(child_element.tag)
if tag == "verse":
return _handle_verse_tag(
child_element,
verse_ids,
skip_till_next_verse,
current_verse_id,
include_verse_number,
)
if skip_till_next_verse:
return "", skip_till_next_verse, current_verse_id
if tag == "rdg":
return (
get_element_text(child_element),
skip_till_next_verse,
current_verse_id,
)
# If we are inside a note tag, only allow the "rdg" text to be included
if inside_note:
return "", skip_till_next_verse, current_verse_id
if tag in {"w", "transChange"}:
return (
get_element_text_and_tail(child_element),
skip_till_next_verse,
current_verse_id,
)
paragraph: str = ""
if tag == "q":
paragraph += get_element_text_and_tail(child_element)
new_current_verse_id: int = current_verse_id
for grandchild_element in list(child_element):
(
grandchild_paragraph,
skip_till_next_verse,
new_current_verse_id,
) = _handle_child_element(
grandchild_element,
verse_ids,
skip_till_next_verse,
current_verse_id,
include_verse_number,
tag == "note",
)
paragraph += grandchild_paragraph
if tag == "seg":
paragraph += get_element_tail(child_element)
return clean_paragraph(paragraph), skip_till_next_verse, new_current_verse_id
@lru_cache()
def _handle_verse_tag(
child_element: Any,
verse_ids: tuple[int, ...],
skip_till_next_verse: bool,
current_verse_id: int,
include_verse_number: bool,
) -> tuple[str, bool, int]:
paragraph: str = ""
osis_id_str: str = child_element.get("osisID") or ".."
if osis_id_str == "..":
return paragraph, skip_till_next_verse, current_verse_id
osis_id: OSISID = parse_osis_id(osis_id_str)
verse_id: int = get_verse_id(
Book(osis_id.book.value),
int(osis_id.chapter),
int(osis_id.verse),
)
if verse_id in verse_ids:
if skip_till_next_verse:
skip_till_next_verse = False
if current_verse_id is not None and verse_id > current_verse_id + 1:
paragraph += "... "
if include_verse_number:
paragraph += f"{osis_id.verse}. "
paragraph += get_element_text_and_tail(child_element)
return paragraph, skip_till_next_verse, verse_id
skip_till_next_verse = True
return paragraph, skip_till_next_verse, current_verse_id
@lru_cache()
def clean_paragraph(paragraph: str) -> str:
cleaned_paragraph: str = paragraph.replace("¶", "").replace(" ", " ")
return cleaned_paragraph.strip()