-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScriptureReference.py
More file actions
348 lines (330 loc) · 9.89 KB
/
ScriptureReference.py
File metadata and controls
348 lines (330 loc) · 9.89 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
import requests
from functools import cache
import re
import os
book_codes = {
'GEN': {
'codes': ['Gen', 'Gn', '1M']
},
'EXO': {
'codes': ['Ex', '2M']
},
'LEV': {
'codes': ['Lev', 'Lv', '3M']
},
'NUM': {
'codes': ['Nm', 'Nu', '4M']
},
'DEU': {
'codes': ['Deut', 'Dt', '5M']
},
'JOS': {
'codes': ['Josh', 'Jos']
},
'JDG': {
'codes': ['Jdg', 'Judg']
},
'RUT': {
'codes': ['Ru', 'Rth']
},
'1SA': {
'codes': ['1Sam', '1Sm']
},
'2SA': {
'codes': ['2Sam', '2Sm']
},
'1KI': {
'codes': ['1Kg', '1K']
},
'2KI': {
'codes': ['2Kg', '2K']
},
'1CH': {
'codes': ['1Ch']
},
'2CH': {
'codes': ['2Ch']
},
'EZR': {
'codes': ['Ezr']
},
'NEH': {
'codes': ['Neh']
},
'EST': {
'codes': ['Est']
},
'JOB': {
'codes': ['Jb', 'Job']
},
'PSA': {
'codes': ['Ps']
},
'PRO': {
'codes': ['Pr']
},
'ECC': {
'codes': ['Ec', 'Qoh']
},
'SNG': {
'codes': ['Sos', 'Song']
},
'ISA': {
'codes': ['Isa']
},
'JER': {
'codes': ['Jer', 'Jr']
},
'LAM': {
'codes': ['Lam', 'Lm']
},
'EZK': {
'codes': ['Ezek', 'Ezk']
},
'DAN': {
'codes': ['Dn', 'Dan']
},
'HOS': {
'codes': ['Hos', 'Hs']
},
'JOL': {
'codes': ['Joel', 'Jl']
},
'AMO': {
'codes': ['Am']
},
'OBA': {
'codes': ['Ob']
},
'JON': {
'codes': ['Jon']
},
'MIC': {
'codes': ['Mi', 'Mc']
},
'NAM': {
'codes': ['Na']
},
'HAB': {
'codes': ['Hab']
},
'ZEP': {
'codes': ['Zep', 'Zp']
},
'HAG': {
'codes': ['Hag', 'Hg']
},
'ZEC': {
'codes': ['Zc', 'Zec']
},
'MAL': {
'codes': ['Mal', 'Ml']
},
'MAT': {
'codes': ['Mt', 'Mat']
},
'MRK': {
'codes': ['Mk', 'Mar']
},
'LUK': {
'codes': ['Lk', 'Lu']
},
'JHN': {
'codes': ['Jn', 'Joh', 'Jhn']
},
'ACT': {
'codes': ['Ac']
},
'ROM': {
'codes': ['Ro', 'Rm']
},
'1CO': {
'codes': ['1Co']
},
'2CO': {
'codes': ['2Co']
},
'GAL': {
'codes': ['Gal', 'Gl']
},
'EPH': {
'codes': ['Ep']
},
'PHP': {
'codes': ['Php', 'Philip']
},
'COL': {
'codes': ['Col']
},
'1TH': {
'codes': ['1Th']
},
'2TH': {
'codes': ['2Th']
},
'1TI': {
'codes': ['1Ti', '1Tm']
},
'2TI': {
'codes': ['2Ti', '2Tm']
},
'TIT': {
'codes': ['Tit'],
},
'PHM': {
'codes': ['Phile', 'Phm'],
},
'HEB': {
'codes': ['Hb', 'Heb'],
},
'JAS': {
'codes': ['Ja', 'Jm'],
},
'1PE': {
'codes': ['1Pe', '2Pt'],
},
'2PE': {
'codes': ['2Pe', '2Pt'],
},
'1JN': {
'codes': ['1Jn', '1Jo', '1Jh'],
},
'2JN': {
'codes': ['2Jn', '2Jo', '2Jh'],
},
'3JN': {
'codes': ['3Jn', '3Jo', '3Jh'],
},
'JUD': {
'codes': ['Ju', 'Jd'],
},
'REV': {
'codes': ['Rev', 'Rv'],
}
}
class ScriptureReference:
# def __init__(self, start_ref, end_ref=None, bible_filename='eng-engwmbb'):
# self.start_ref = self.parse_scripture_reference(start_ref)
# self.end_ref = self.parse_scripture_reference(end_ref) if end_ref else self.start_ref
# self.bible_url = f"https://raw.githubusercontent.com/BibleNLP/ebible/main/corpus/{bible_filename}.txt"
# self.verses = self.get_verses_between_refs()
def __init__(self, start_ref, end_ref=None, bible_filename='eng-engwmbb', source_type='ebible', versification='eng'):
self.start_ref = self.parse_scripture_reference(start_ref)
self.end_ref = self.parse_scripture_reference(end_ref) if end_ref else self.start_ref
self.bible_filename = bible_filename
self.source_type = source_type
self.versification = versification
if source_type == 'ebible':
self.bible_url = f"https://raw.githubusercontent.com/BibleNLP/ebible/main/corpus/{bible_filename}.txt"
self.verses = self.get_verses_between_refs()
elif source_type == 'usfm':
self.verses = self.extract_verses_from_usfm()
@classmethod
def parse_scripture_reference(cls, input_ref):
normalized_input = re.sub(r"\s+", "", input_ref).upper()
regex = re.compile(r"^(\d)?(\D+)(\d+)?(?::(\d+))?(?:-(\d+)?(?::(\d+))?)?$")
match = regex.match(normalized_input)
if not match:
return None
bookPrefix, bookName, startChapter, startVerse, endChapter, endVerse = match.groups()
fullBookName = f"{bookPrefix or ''}{bookName}".upper()
bookCode = next((code for code, details in book_codes.items() if any(fullBookName.startswith(name.upper()) for name in details['codes'])), None)
if not bookCode:
return None
# Validate chapter and verse numbers by checking against the vref.txt data
startChapter = int(startChapter) if startChapter else 1
startVerse = int(startVerse) if startVerse else 1
endChapter = int(endChapter) if endChapter else startChapter
endVerse = int(endVerse) if endVerse else startVerse # Default to the same verse if not specified
return {
'bookCode': bookCode,
'startChapter': startChapter,
'startVerse': startVerse,
'endChapter': endChapter,
'endVerse': endVerse
}
@cache
def load_verses(self):
response = requests.get(f'https://raw.githubusercontent.com/BibleNLP/ebible/main/metadata/{self.versification}.vrs')
if response.status_code == 200:
lines = response.text.splitlines()
verses = []
start_processing = False
for line in lines:
if line.startswith('#') or not line.strip():
continue
if line.startswith('GEN'):
start_processing = True
if start_processing:
parts = line.split()
book = parts[0]
chapters = parts[1:]
for chapter in chapters:
chapter_verses = chapter.split(':')
if len(chapter_verses) != 2:
continue
chapter_number, verse_count = chapter_verses
try:
verse_count = int(verse_count)
except ValueError:
continue
for verse in range(1, verse_count + 1):
verses.append(f"{book} {chapter_number}:{verse}")
if line.startswith('REV'):
break
# print first and last 10 verses
print(verses[:10])
print(verses[-10:])
return verses
else:
return []
def load_bible_text(self):
response = requests.get(self.bible_url)
if response.status_code == 200:
return response.text.splitlines()
else:
return []
def get_verses_between_refs(self):
verses = self.load_verses()
bible_text = self.load_bible_text()
start_index = verses.index(f"{self.start_ref['bookCode']} {self.start_ref['startChapter']}:{self.start_ref['startVerse']}")
end_index = verses.index(f"{self.end_ref['bookCode']} {self.end_ref['endChapter']}:{self.end_ref['endVerse']}")
return [[f"{verses[i]}".replace(' ', '_'), f"{bible_text[i]}"] for i in range(start_index, end_index + 1)]
def extract_verses_from_usfm(self):
input_directory = self.bible_filename # Assuming bible_filename is now a directory path for USFM files
verses = []
files = [f for f in os.listdir(input_directory) if f.endswith('.SFM')]
for file in files:
input_path = os.path.join(input_directory, file)
with open(input_path, 'r', encoding='utf-8') as infile:
book = None
chapter = None
for line in infile:
if re.match(r'\\id (\w+)', line):
book = line.split()[1]
if re.match(r'\\c (\d+)', line):
chapter = line.split()[1]
if re.match(r'\\v (\d+)', line):
verse_number = line.split()[1]
verse_text = re.sub(r'^\\v \d+ ', '', line)
verse_text = re.sub(r'\\(\w+) .*?\\\1\*', '', verse_text) # Remove tags
verse_text = re.sub(r'[a-zA-Z\\]+', '', verse_text) # Remove remaining Roman characters and backslashes
formatted_verse = f"{book} {chapter}:{verse_number} {verse_text.strip()}"
formatted_verse = [f"{book}_{chapter}:{verse_number}", verse_text.strip()]
verses.append(formatted_verse)
return verses
# Example usage:
# scripture_ref = ScriptureReference('lev 5:20', 'lev 5:26', "eng-engkjvcpb")
# print("Verses between references:")
# for verse in scripture_ref.verses:
# print(verse)
# scripture_ref = ScriptureReference("rev22:20", "rev22:21", 'C:/Users/caleb/Bible Translation Project/No code/Tamazight/text', 'usfm')
# print("Verses from USFM:")
# for i, verse in enumerate(scripture_ref.verses):
# if i < 10:
# print(verse)
# # Write the verses to a file
# output_path = 'C:/Users/caleb/Bible Translation Project/No code/Tamazight/text/output/verses.txt'
# with open(output_path, 'w', encoding='utf-8') as outfile:
# for verse in scripture_ref.verses:
# outfile.write(verse + '\n')