-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparse_kjv.py
284 lines (256 loc) · 8.39 KB
/
parse_kjv.py
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
import json
import re
import regex as rep
book_titles = [
"The First Book of Moses: Called Genesis",
"The Second Book of Moses: Called Exodus",
"The Third Book of Moses: Called Leviticus",
"The Fourth Book of Moses: Called Numbers",
"The Fifth Book of Moses: Called Deuteronomy",
"The Book of Joshua",
"The Book of Judges",
"The Book of Ruth",
"The First Book of Samuel",
"The Second Book of Samuel",
"The First Book of the Kings",
"The Second Book of the Kings",
"The First Book of the Chronicles",
"The Second Book of the Chronicles",
"Ezra",
"The Book of Nehemiah",
"The Book of Esther",
"The Book of Job",
"The Book of Psalms",
"The Proverbs",
"Ecclesiastes",
"The Song of Solomon",
"The Book of the Prophet Isaiah",
"The Book of the Prophet Jeremiah",
"The Lamentations of Jeremiah",
"The Book of the Prophet Ezekiel",
"The Book of Daniel",
"Hosea",
"Joel",
"Amos",
"Obadiah",
"Jonah",
"Micah",
"Nahum",
"Habakkuk",
"Zephaniah",
"Haggai",
"Zechariah",
"Malachi",
"The Gospel According to Saint Matthew",
"The Gospel According to Saint Mark",
"The Gospel According to Saint Luke",
"The Gospel According to Saint John",
"The Acts of the Apostles",
"The Epistle of Paul the Apostle to the Romans",
"The First Epistle of Paul the Apostle to the Corinthians",
"The Second Epistle of Paul the Apostle to the Corinthians",
"The Epistle of Paul the Apostle to the Galatians",
"The Epistle of Paul the Apostle to the Ephesians",
"The Epistle of Paul the Apostle to the Philippians",
"The Epistle of Paul the Apostle to the Colossians",
"The First Epistle of Paul the Apostle to the Thessalonians",
"The Second Epistle of Paul the Apostle to the Thessalonians",
"The First Epistle of Paul the Apostle to Timothy",
"The Second Epistle of Paul the Apostle to Timothy",
"The Epistle of Paul the Apostle to Titus",
"The Epistle of Paul the Apostle to Philemon",
"The Epistle of Paul the Apostle to the Hebrews",
"The General Epistle of James",
"The First Epistle General of Peter",
"The Second General Epistle of Peter",
"The First Epistle General of John",
"The Second Epistle General of John",
"The Third Epistle General of John",
"The General Epistle of Jude",
"The Revelation of Saint John the Divine",
]
books = [
"Genesis",
"Exodus",
"Leviticus",
"Numbers",
"Deuteronomy",
"Joshua",
"Judges",
"Ruth",
"1 Samuel",
"2 Samuel",
"1 Kings",
"2 Kings",
"1 Chronicles",
"2 Chronicles",
"Ezra",
"Nehemiah",
"Esther",
"Job",
"Psalms",
"Proverbs",
"Ecclesiastes",
"Song of Solomon",
"Isaiah",
"Jeremiah",
"Lamentations",
"Ezekiel",
"Daniel",
"Hosea",
"Joel",
"Amos",
"Obadiah",
"Jonah",
"Micah",
"Nahum",
"Habakkuk",
"Zephaniah",
"Haggai",
"Zechariah",
"Malachi",
"Matthew",
"Mark",
"Luke",
"John",
"Acts",
"Romans",
"1 Corinthians",
"2 Corinthians",
"Galatians",
"Ephesians",
"Philippians",
"Colossians",
"1 Thessalonians",
"2 Thessalonians",
"1 Timothy",
"2 Timothy",
"Titus",
"Philemon",
"Hebrews",
"James",
"1 Peter",
"2 Peter",
"1 John",
"2 John",
"3 John",
"Jude",
"Revelation",
]
def parse_bible_file(file_path):
bible_data = []
current_book = {}
current_chapter = {}
current_paragraph = {}
transl_table = dict( [ (ord(x), ord(y)) for x,y in zip( u"‘’´“”–-", u"'''\"\"--") ] )
with open(file_path, "r", encoding="utf-8-sig") as file:
content = file.read()
lines = content.split("\n")
for line in lines:
line = line.strip().translate(transl_table)
if line in book_titles:
# Save the last paragraph and chapter of the previous book
if current_book:
if current_paragraph:
current_chapter["paragraphs"].append(current_paragraph)
current_paragraph = {}
if current_chapter:
current_book["chapters"].append(current_chapter)
current_chapter = {}
bible_data.append(current_book)
# Start a new book
book_description = line
print(f"book {line} {books[len(bible_data)]}")
current_book = {
"name": books[len(bible_data)],
"description": book_description,
"chapters": [],
}
current_chapter = {}
current_paragraph = {}
elif re.match(r"\d+:\d+", line):
# Parse chapter and verse numbers
chapter_number, verse_rest = line.split(":", 1)
verse_number, verse_text = verse_rest.strip().split(" ", 1)
chapter_number = int(chapter_number)
verse_number = int(verse_number)
# Save the current paragraph before starting a new one
if current_paragraph:
current_chapter["paragraphs"].append(current_paragraph)
current_paragraph = {}
# Check if the chapter number has changed
if (
current_chapter
and "number" in current_chapter
and chapter_number != current_chapter["number"]
):
current_book["chapters"].append(current_chapter)
current_chapter = {
"number": chapter_number,
"paragraphs": [],
}
elif not current_chapter:
current_chapter = {
"number": chapter_number,
"paragraphs": [],
}
# Start a new paragraph
current_paragraph = {
"startingVerse": verse_number,
"text": verse_text,
}
else:
if current_paragraph:
current_paragraph["text"] += " " + line
# After the loop, save any remaining data
if current_paragraph:
current_chapter["paragraphs"].append(current_paragraph)
if current_chapter:
current_book["chapters"].append(current_chapter)
if current_book:
bible_data.append(current_book)
return bible_data
num_of_matched_phrases = 0
def parse_jesus_words():
lower_matched = set()
total_matches = 0
all_lowered_words = []
jesus_words = None
jesus_pattern = None
def find_book(books, book_name):
for book in books:
if book.get('name', '').lower() == book_name.lower():
return book
return None
def wrap_jesus_words(match):
global num_of_matched_phrases
num_of_matched_phrases += 1
lower_matched.add(match.group(0).lower())
return f"<JESUS>{match.group(0)}</JESUS>"
with open('jesus.json', 'r', encoding='utf-8') as f:
jesus_words_list = json.load(f)
for book_name in jesus_words_list.keys():
jesus_words = sorted(set(jesus_words_list[book_name]), key=len, reverse=True)
all_lowered_words += [w.lower() for w in jesus_words]
total_matches += len(jesus_words)
jesus_pattern = rep.compile(r"\L<words>", words=jesus_words, flags=re.IGNORECASE)
book = find_book(bible_data, book_name)
if book:
for chapter in book.get('chapters', []):
for paragraph in chapter.get('paragraphs', []):
paragraph["text"] = jesus_pattern.sub(wrap_jesus_words, paragraph["text"])
print(f"Total: {total_matches}")
print(f"Matched {num_of_matched_phrases}")
for j in all_lowered_words:
if j not in lower_matched:
print(j)
for i in lower_matched:
if i not in all_lowered_words:
print(i)
def save_to_json(bible_data, output_file):
with open(output_file, "w", encoding="utf-8") as file:
json.dump(bible_data, file, ensure_ascii=False, indent=4)
bible_file = "kjv.txt"
bible_data = parse_bible_file(bible_file)
parse_jesus_words()
save_to_json(bible_data, "swiftbible/Text/bible.json")