forked from openscriptures/morphhb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
morphhbXML-to-JSON.py
292 lines (235 loc) · 9.32 KB
/
morphhbXML-to-JSON.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
284
285
286
287
288
289
290
291
292
# !/usr/bin/python3
# encoding=utf8
import re
import copy
import sys
import getopt
import json
import os
from xml.etree import ElementTree as ET
bookNameData = {}
bookNameData['Gen'] = 'Genesis'
bookNameData['Exod'] = 'Exodus'
bookNameData['Lev'] = 'Leviticus'
bookNameData['Num'] = 'Numbers'
bookNameData['Deut'] = 'Deuteronomy'
bookNameData['Josh'] = 'Joshua'
bookNameData['Judg'] = 'Judges'
bookNameData['Ruth'] = 'Ruth'
bookNameData['1Sam'] = 'I Samuel'
bookNameData['2Sam'] = 'II Samuel'
bookNameData['1Kgs'] = 'I Kings'
bookNameData['2Kgs'] = 'II Kings'
bookNameData['1Chr'] = 'I Chronicles'
bookNameData['2Chr'] = 'II Chronicles'
bookNameData['Ezra'] = 'Ezra'
bookNameData['Neh'] = 'Nehemiah'
bookNameData['Esth'] = 'Esther'
bookNameData['Job'] = 'Job'
bookNameData['Ps'] = 'Psalms'
bookNameData['Prov'] = 'Proverbs'
bookNameData['Eccl'] = 'Ecclesiastes'
bookNameData['Song'] = 'Song of Solomon'
bookNameData['Isa'] = 'Isaiah'
bookNameData['Jer'] = 'Jeremiah'
bookNameData['Lam'] = 'Lamentations'
bookNameData['Ezek'] = 'Ezekiel'
bookNameData['Dan'] = 'Daniel'
bookNameData['Hos'] = 'Hosea'
bookNameData['Joel'] = 'Joel'
bookNameData['Amos'] = 'Amos'
bookNameData['Obad'] = 'Obadiah'
bookNameData['Jonah'] = 'Jonah'
bookNameData['Mic'] = 'Micah'
bookNameData['Nah'] = 'Nahum'
bookNameData['Hab'] = 'Habakkuk'
bookNameData['Zeph'] = 'Zephaniah'
bookNameData['Hag'] = 'Haggai'
bookNameData['Zech'] = 'Zechariah'
bookNameData['Mal'] = 'Malachi'
stripPointing = False
removeLemmaTypes = False
stripHFromMorph = False
prefixLemmasWithH = False
remapVerses = False
splitByBook = False
def getBookData(filename):
tree = ET.parse(filename)
namespaces = {
'osis': 'http://www.bibletechnologies.net/2003/OSIS/namespace'}
chapters = tree.getroot().findall('.//osis:chapter', namespaces)
bookData = []
for chapter in chapters:
verses = chapter.findall('.//osis:verse', namespaces)
verseArray = []
for verse in verses:
words = verse.findall('.//osis:w', namespaces)
wordArray = []
for word in words:
singleWordArray = []
lemma = word.attrib.get('lemma')
if removeLemmaTypes:
lemma = removeLemmaTypesFunc(lemma)
if prefixLemmasWithH:
# print(lemma)
lemma = prefixLemmasWithHFunc(lemma)
# print(lemma)
morph = word.attrib.get('morph')
if morph and stripHFromMorph:
morph = stripHFromMorphFunc(morph)
singleWordArray.append(word.text)
singleWordArray.append(lemma)
singleWordArray.append(morph)
wordArray.append(singleWordArray)
verseArray.append(wordArray)
bookData.append(verseArray)
return bookData
def prefixLemmasWithHFunc(lemmaString):
lemmaArray = lemmaString.split('/')
returnArray = []
for lemma in lemmaArray:
returnArray.append('H' + lemma)
returnString = '/'.join(returnArray)
return returnString
def stripHFromMorphFunc(string):
if string.find('H') == 0:
return string[1:]
return string
def removeLemmaTypesFunc(string):
return re.sub(r" [abcdef]|\+", "", string)
def stripPointingFunc(string):
return re.sub(r"[\u0591-\u05c7]", "", string)
def getCommandOptions(argv):
global stripPointing
global removeLemmaTypes
global stripHFromMorph
global prefixLemmasWithH
global remapVerses
global splitByBook
try:
opts, args = getopt.getopt(argv, "h:",
[
"stripPointing",
"removeLemmaTypes",
"stripHFromMorph",
"prefixLemmasWithH",
"remapVerses",
"splitByBook"
])
except getopt.GetoptError:
print('python3 morphhb.py --stripPointing --removeLemmaTypes --stripHFromMorph --prefixLemmasWithH --remapVerses --splitByBook')
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print('python3 morphhb.py --stripPointing --removeLemmaTypes --stripHFromMorph --prefixLemmasWithH --remapVerses --splitByBook')
sys.exit()
elif opt in ("--stripPointing"):
print('stripPointing')
stripPointing = True
elif opt in ("--removeLemmaTypes"):
print('removeLemmaTypes')
removeLemmaTypes = True
elif opt in ("--stripHFromMorph"):
print('stripHFromMorph')
stripHFromMorph = True
elif opt in ("--prefixLemmasWithH"):
print('prefixLemmasWithH')
prefixLemmasWithH = True
elif opt in ("--remapVerses"):
print('remapVerses')
remapVerses = True
elif opt in ("--splitByBook"):
print('splitByBook')
splitByBook = True
def main():
hebrew = {}
for bookNameShort in bookNameData.keys():
hebrew[bookNameData[bookNameShort]] = getBookData(
'wlc/' + bookNameShort + '.xml')
# map hebrew to english verses
filename = 'wlc/VerseMap.xml'
tree = ET.parse(filename)
namespaces = {'vm': 'http://www.APTBibleTools.com/namespace'}
books = tree.getroot().findall('.//vm:book', namespaces)
# must use deep copy here
remapped = copy.deepcopy(hebrew)
for book in books:
verses = book.findall('.//vm:verse', namespaces)
for verse in verses:
if verse.attrib['type'] == 'full':
# take the wlc verse and move it to the kjv position
wlcRef = verse.attrib['wlc']
wlcVerseArray = wlcRef.split('.')
kjvRef = verse.attrib['kjv']
kjvVerseArray = kjvRef.split('.')
kjvBook = bookNameData[kjvVerseArray[0]]
kjvChapter = int(kjvVerseArray[1]) - 1
kjvVerse = int(kjvVerseArray[2]) - 1
wlcBook = bookNameData[wlcVerseArray[0]]
wlcChapter = int(wlcVerseArray[1]) - 1
wlcVerse = int(wlcVerseArray[2]) - 1
wlcData = hebrew[wlcBook][wlcChapter][wlcVerse]
if kjvBook == 'Psalms' and kjvVerse == 0:
remapped[kjvBook][kjvChapter][kjvVerse] = hebrew[wlcBook][wlcChapter][wlcVerse - 1] + wlcData
remapped[wlcBook][wlcChapter][wlcVerse] = []
else:
if kjvChapter >= len(remapped[kjvBook]):
remapped[kjvBook].append([])
if kjvVerse >= len(remapped[kjvBook][kjvChapter]):
remapped[kjvBook][kjvChapter].append([])
remapped[kjvBook][kjvChapter][kjvVerse] = wlcData.copy()
if wlcChapter < kjvChapter or (wlcChapter == kjvChapter and wlcVerse > kjvVerse) or wlcChapter > kjvChapter:
remapped[wlcBook][wlcChapter][wlcVerse] = []
# delete those empty entries now
verses.reverse()
for verse in verses:
if verse.attrib['type'] == 'full':
wlcRef = verse.attrib['wlc']
wlcVerseArray = wlcRef.split('.')
wlcBook = bookNameData[wlcVerseArray[0]]
wlcChapter = int(wlcVerseArray[1]) - 1
wlcVerse = int(wlcVerseArray[2]) - 1
if len(remapped[wlcBook][wlcChapter][wlcVerse]) == 0:
del remapped[wlcBook][wlcChapter][wlcVerse]
# Fix partial verses manually
remapped['I Kings'][17][32] = hebrew['I Kings'][17][32] + \
hebrew['I Kings'][17][33]
remapped['I Kings'][17][32][19:43] = []
remapped['I Kings'][17][33][0:10] = []
remapped['I Kings'][19][1] = hebrew['I Kings'][19][1] + \
hebrew['I Kings'][19][2]
remapped['I Kings'][19][1][13:34] = []
remapped['I Kings'][19][2][0:6] = []
remapped['I Kings'][21][20][8:19] = []
remapped['I Kings'][21][21] = hebrew['I Kings'][21][20] + \
hebrew['I Kings'][21][21]
remapped['I Kings'][21][21][0:8] = []
remapped['I Kings'][21][42] = hebrew['I Kings'][21][42] + \
hebrew['I Kings'][21][43]
remapped['Isaiah'][63][0] = hebrew['Isaiah'][62][18].copy()
remapped['Isaiah'][62][18][8:23] = []
remapped['Isaiah'][63][0][0:8] = []
remapped['Psalms'][12][4] = hebrew['Psalms'][12][4].copy()
remapped['Psalms'][12].append([])
remapped['Psalms'][12][5] = hebrew['Psalms'][12][4].copy()
remapped['Psalms'][12][4][6:16] = []
remapped['Psalms'][12][5][0: 6] = []
if remapVerses:
final = remapped
else:
final = hebrew
name = 'remapped' if remapVerses else 'hebrew'
if splitByBook:
output_dir = os.path.join('./json', name)
if not os.path.exists(output_dir):
os.makedirs(output_dir)
for book in final:
target_file = os.path.join(output_dir, book.replace(" ", "").lower())
with open(target_file + '.json', 'w', encoding='utf8') as f:
json.dump(final[book], f, ensure_ascii=False)
else:
with open(name + '.json', 'w', encoding='utf8') as f:
json.dump(final, f, ensure_ascii=False)
if __name__ == "__main__":
getCommandOptions(sys.argv[1:])
main()