forked from Freely-Given-org/BibleOrgSys
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BibleLexicon.py
executable file
·443 lines (352 loc) · 19.6 KB
/
BibleLexicon.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
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# BibleLexicon.py
#
# Module handling the combined Hebrew and Greek lexicons
#
# Copyright (C) 2014-2016 Robert Hunt
# Author: Robert Hunt <[email protected]>
# License: See gpl-3.0.txt
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
Module handling the OpenScriptures Hebrew and morphgnt Greek lexicons.
Hebrew has Strongs and BrDrBr
Greek has Strongs only.
"""
from gettext import gettext as _
LastModifiedDate = '2016-02-25' # by RJH
ShortProgName = "BibleLexicon"
ProgName = "Bible Lexicon format handler"
ProgVersion = '0.24'
ProgNameVersion = '{} v{}'.format( ShortProgName, ProgVersion )
ProgNameVersionDate = '{} {} {}'.format( ProgNameVersion, _("last modified"), LastModifiedDate )
debuggingThisModule = False
import logging
import BibleOrgSysGlobals
import HebrewLexicon, GreekLexicon
def exp( messageString ):
"""
Expands the message string in debug mode.
Prepends the module name to a error or warning message string
if we are in debug mode.
Returns the new string.
"""
try: nameBit, errorBit = messageString.split( ': ', 1 )
except ValueError: nameBit, errorBit = '', messageString
if BibleOrgSysGlobals.debugFlag or debuggingThisModule:
nameBit = '{}{}{}'.format( ShortProgName, '.' if nameBit else '', nameBit )
return '{}{}'.format( nameBit+': ' if nameBit else '', errorBit )
# end of exp
class BibleLexiconIndex:
"""
Class for handling a Bible Lexicon (Hebrew -- not applicable yet to Greek).
This class doesn't deal at all with XML, only with Python dictionaries, etc.
"""
def __init__( self, HebrewXMLFolder, GreekXMLFolder, preload=False ):
"""
Constructor: expects the filepath of the source XML file.
Loads (and crudely validates the XML file) into an element tree.
"""
if BibleOrgSysGlobals.debugFlag and debuggingThisModule:
print( exp("BibleLexiconIndex.__init__( {}, {} )").format( HebrewXMLFolder, GreekXMLFolder ) )
self.HebrewXMLFolder, self.GreekXMLFolder = HebrewXMLFolder, GreekXMLFolder
self.hIndex = None
if preload: self.load()
# end of BibleLexiconIndex.__init__
def load( self ):
self.hIndex = HebrewLexicon.HebrewLexiconIndex( self.HebrewXMLFolder ) # Create the object
# end of BibleLexiconIndex.load
def __str__( self ):
"""
This method returns the string representation of a Bible book code.
@return: the name of a Bible object formatted as a string
@rtype: string
"""
result = "Bible Lexicon Index object"
#if self.title: result += ('\n' if result else '') + self.title
#if self.version: result += ('\n' if result else '') + "Version: {} ".format( self.version )
#if self.date: result += ('\n' if result else '') + "Date: {}".format( self.date )
if self.hIndex is not None:
result += ('\n' if result else '') + " " + _("Number of augmented Hebrew Strong's index entries = {}").format( len(self.hIndex.IndexEntries1) )
result += ('\n' if result else '') + " " + _("Number of Hebrew lexical index entries = {}").format( len(self.hIndex.IndexEntries['heb']) )
result += ('\n' if result else '') + " " + _("Number of Aramaic lexical index entries = {}").format( len(self.hIndex.IndexEntries['arc']) )
return result
# end of BibleLexiconIndex.__str__
def getLexiconCodeFromStrongsNumber( self, key ):
"""
The key is a digit string like '172' (optional preceding H).
Returns a lexicon internal code like 'acd'.
"""
if self.hIndex is None: self.load()
if key.startswith( 'H' ): return self.hIndex.getLexiconCodeFromStrongsNumber( key )
# end of BibleLexiconIndex.getLexiconCodeFromStrongsNumber
def _getStrongsNumberFromLexiconCode1( self, key ):
"""
The key is a three letter code like 'aac'.
Returns a Hebrew Strong's number (but only the digits -- no preceding H)
"""
if self.hIndex is None: self.load()
if key.startswith( 'H' ): return self.hIndex._getStrongsNumberFromLexiconCode1( key )
# end of BibleLexiconIndex.getStrongsNumberFromLexiconCode1
def _getStrongsNumberFromLexiconCode2( self, key ):
"""
The key is a three letter code like 'aac'.
Returns a Hebrew Strong's number (but only the digits -- no preceding H)
"""
if self.hIndex is None: self.load()
if key.startswith( 'H' ): return self.hIndex._getStrongsNumberFromLexiconCode2( key )
# end of BibleLexiconIndex.getStrongsNumberFromLexiconCode2
def getStrongsNumberFromLexiconCode( self, key ):
"""
The key is a three letter code like 'aac'.
Returns a Hebrew Strong's number (but only the digits -- no preceding H)
"""
if self.hIndex is None: self.load()
if key.startswith( 'H' ): return self.hIndex.getStrongsNumberFromLexiconCode( key )
# end of BibleLexiconIndex.getStrongsNumberFromLexiconCode
def getBrDrBrCodeFromLexiconCode( self, key ):
"""
The key is a three letter code like 'aac'.
Returns a BrDrBr code, e.g., 'm.ba.aa'
"""
if self.hIndex is None: self.load()
if key.startswith( 'H' ): return self.hIndex.getBrDrBrCodeFromLexiconCode( key )
# end of BibleLexiconIndex.getBrDrBrCodeFromLexiconCode
def getTWOTCodeFromLexiconCode( self, key ):
"""
The key is a three letter code like 'aac'.
Returns a BrDrBr code, e.g., '4a'
"""
if self.hIndex is None: self.load()
if key.startswith( 'H' ): return self.hIndex.getTWOTCodeFromLexiconCode( key )
# end of BibleLexiconIndex.getTWOTCodeFromLexiconCode
# end of BibleLexiconIndex class
class BibleLexicon:
"""
Class for handling a Bible Lexicon (Hebrew and Greek)
This class doesn't deal at all with XML, only with Python dictionaries, etc.
"""
def __init__( self, HebrewXMLFolder, GreekXMLFolder, preload=False ):
"""
Constructor: expects the filepath of the source XML file.
Does not actually cause the XML to be loaded (very slow).
"""
if BibleOrgSysGlobals.debugFlag and debuggingThisModule:
print( exp("BibleLexicon.__init__( {}, {}, {} )").format( HebrewXMLFolder, GreekXMLFolder, preload ) )
self.HebrewXMLFolder, self.GreekXMLFolder = HebrewXMLFolder, GreekXMLFolder
fnfCount = 0
try: self.hLexicon = HebrewLexicon.HebrewLexicon( self.HebrewXMLFolder, preload ) # Create the object
except FileNotFoundError:
logging.critical( exp("BibleLexicon could not find Hebrew lexicon at {}").format( HebrewXMLFolder ) )
fnfCount += 1
self.hLexicon = None
try: self.gLexicon = GreekLexicon.GreekLexicon( self.GreekXMLFolder, preload ) # Create the object
except FileNotFoundError:
logging.critical( exp("BibleLexicon could not find Greek lexicon at {}").format( GreekXMLFolder ) )
fnfCount += 1
self.gLexicon = None
if fnfCount >= 2: raise FileNotFoundError
# end of BibleLexicon.__init__
def __str__( self ):
"""
This method returns the string representation of the Bible lexicon.
@return: the name of a Bible object formatted as a string
@rtype: string
"""
result = "Bible Lexicon object"
#if self.title: result += ('\n' if result else '') + self.title
#if self.version: result += ('\n' if result else '') + "Version: {} ".format( self.version )
#if self.date: result += ('\n' if result else '') + "Date: {}".format( self.date )
if self.hLexicon is not None:
if self.hLexicon.StrongsEntries is not None:
result += ('\n' if result else '') + " " + _("Number of Strong's Hebrew entries = {}").format( len(self.hLexicon.StrongsEntries) )
if self.hLexicon.BrownDriverBriggsEntries is not None:
result += ('\n' if result else '') + " " + _("Number of BrDrBr Hebrew entries = {}").format( len(self.hLexicon.BrownDriverBriggsEntries['heb']) )
result += ('\n' if result else '') + " " + _("Number of BrDrBr Aramaic entries = {}").format( len(self.hLexicon.BrownDriverBriggsEntries['arc']) )
if self.gLexicon.StrongsEntries is not None:
result += ('\n' if result else '') + " " + _("Number of Strong's Greek entries = {}").format( len(self.gLexicon.StrongsEntries) )
return result
# end of BibleLexicon.__str__
def getStrongsEntryData( self, key ):
"""
The key is a Hebrew Strong's number (string) like 'H1979'.
Returns an entry for the given key.
This is a dictionary containing fields, e.g., ['usage'] = 'company, going, walk, way.'
Returns None if the key is not found.
"""
if BibleOrgSysGlobals.debugFlag and debuggingThisModule:
print( exp("BibleLexicon.getStrongsEntryData( {} )").format( repr(key) ) )
if key.startswith( 'H' ): return self.hLexicon.getStrongsEntryData( key )
if key.startswith( 'G' ): return self.gLexicon.getStrongsEntryData( key )
# end of BibleLexicon.getStrongsEntryData
def getStrongsEntryField( self, key, fieldName ):
"""
The key is a Hebrew Strong's number (string) like 'H1979'.
The fieldName is a name (string) like 'usage'.
Returns a string for the given key and fieldName names.
Returns None if the key or fieldName is not found.
"""
if BibleOrgSysGlobals.debugFlag and debuggingThisModule:
print( exp("BibleLexicon.getStrongsEntryField( {}, {} )").format( repr(key), repr(fieldName) ) )
if key.startswith( 'H' ): return self.hLexicon.getStrongsEntryField( key, fieldName )
if key.startswith( 'G' ): return self.gLexicon.getStrongsEntryField( key, fieldName )
# end of BibleLexicon.getStrongsEntryField
def getStrongsEntryHTML( self, key ):
"""
The key is a Hebrew Strong's number (string) like 'H1979'.
Returns an HTML li entry for the given key.
Returns None if the key is not found.
e.g., for H1, returns:
<li value="1" id="ot:1"><i title="{awb}" xml:lang="hbo">אָב</i> a primitive word;
father, in a literal and immediate, or figurative and remote application):
<span class="kjv_def">chief, (fore-)father(-less), X patrimony, principal</span>.
Compare names in "Abi-".</li>
<li value="165" id="ot:165"><i title="{e-hee'}" xml:lang="hbo">אֱהִי</i> apparently an
orthographical variation for <a href="#ot:346"><i title="{ah-yay'}" xml:lang="hbo">אַיֵּה</i></a>;
where: <span class="kjv_def">I will be (Hos</span>. 13:10, 14) (which is often the rendering of
the same Hebrew form from <a href="#ot:1961"><i title="{haw-yaw}" xml:lang="hbo">הָיָה</i></a>).</li>
"""
if BibleOrgSysGlobals.debugFlag and debuggingThisModule:
print( exp("BibleLexicon.getStrongsEntryHTML( {} )").format( repr(key) ) )
if key.startswith( 'H' ): return self.hLexicon.getStrongsEntryHTML( key )
if key.startswith( 'G' ): return self.gLexicon.getStrongsEntryHTML( key )
# end of BibleLexicon.getStrongsEntryHTML
def getBrDrBrEntryData( self, key ):
"""
The key is a BrDrBr number (string) like 'a.ca.ab'.
Returns an entry for the given key.
This is a dictionary containing fields, e.g.,
Returns None if the key is not found.
"""
if BibleOrgSysGlobals.debugFlag and debuggingThisModule:
print( exp("BibleLexicon.getBrDrBrEntryData( {} )").format( repr(key) ) )
return self.hLexicon.getBrDrBrEntryData( key )
# end of BibleLexicon.getBrDrBrEntryData
def getBrDrBrEntryField( self, key, fieldName ):
"""
The key is a BrDrBr number (string) like 'ah.ba.aa'.
The fieldName is a name (string) like 'status'.
Returns a string for the given key and fieldName names.
Returns None if the key or fieldName is not found.
"""
if BibleOrgSysGlobals.debugFlag and debuggingThisModule:
print( exp("BibleLexicon.getBrDrBrEntryField( {}, {} )").format( repr(key), repr(fieldName) ) )
return self.hLexicon.getBrDrBrEntryField( key, fieldName )
# end of BibleLexicon.getBrDrBrEntryField
def getBrDrBrEntryHTML( self, key ):
"""
The key is a BrDrBr number (string) like 'ah.ba.aa'.
Returns an HTML entry for the given key.
Returns None if the key is not found.
"""
if BibleOrgSysGlobals.debugFlag and debuggingThisModule:
print( exp("BibleLexicon.getBrDrBrEntryHTML( {} )").format( repr(key) ) )
return self.hLexicon.getBrDrBrEntryHTML( key )
# end of BibleLexicon.getBrDrBrEntryHTML
def getEntryData( self, key ):
"""
The key can be a Strong's number (string) like 'H1979'.
Returns an entry for the given key.
This is a dictionary containing fields, e.g., ['usage'] = 'company, going, walk, way.'
Returns None if the key is not found.
"""
if BibleOrgSysGlobals.debugFlag and debuggingThisModule:
print( exp("BibleLexicon.getEntryData( {} )").format( repr(key) ) )
if not key: return
if key[0]=='H' and key[1:].isdigit(): return self.hLexicon.getStrongsEntryData( key )
if key[0]=='G' and key[1:].isdigit(): return self.gLexicon.getStrongsEntryData( key )
if '.' in key: return self.hLexicon.getBrDrBrEntryData( key )
# end of BibleLexicon.getEntryData
def getEntryField( self, key, fieldName ):
"""
The key can be a Hebrew Strong's number (string) like 'H1979'.
The fieldName is a name (string) like 'usage'.
Returns a string for the given key and fieldName names.
Returns None if the key or fieldName is not found.
"""
if BibleOrgSysGlobals.debugFlag and debuggingThisModule:
print( exp("BibleLexicon.getEntryField( {}, {} )").format( repr(key), repr(fieldName) ) )
if not key: return
if key[0]=='H' and key[1:].isdigit(): return self.hLexicon.getStrongsEntryField( key, fieldName )
if key[0]=='G' and key[1:].isdigit(): return self.gLexicon.getStrongsEntryField( key, fieldName )
if '.' in key: return self.hLexicon.getBrDrBrEntryField( key, fieldName )
# end of BibleLexicon.getEntryField
def getEntryHTML( self, key ):
"""
The key can be a Hebrew Strong's number (string) like 'H1979'.
Returns an HTML li entry for the given key.
Returns None if the key is not found.
e.g., for H1, returns:
<li value="1" id="ot:1"><i title="{awb}" xml:lang="hbo">אָב</i> a primitive word;
father, in a literal and immediate, or figurative and remote application):
<span class="kjv_def">chief, (fore-)father(-less), X patrimony, principal</span>.
Compare names in "Abi-".</li>
<li value="165" id="ot:165"><i title="{e-hee'}" xml:lang="hbo">אֱהִי</i> apparently an
orthographical variation for <a href="#ot:346"><i title="{ah-yay'}" xml:lang="hbo">אַיֵּה</i></a>;
where: <span class="kjv_def">I will be (Hos</span>. 13:10, 14) (which is often the rendering of
the same Hebrew form from <a href="#ot:1961"><i title="{haw-yaw}" xml:lang="hbo">הָיָה</i></a>).</li>
"""
if BibleOrgSysGlobals.debugFlag and debuggingThisModule:
print( exp("BibleLexicon.getEntryHTML( {} )").format( repr(key) ) )
if not key: return
if key[0]=='H' and key[1:].isdigit(): return self.hLexicon.getStrongsEntryHTML( key )
if key[0]=='G' and key[1:].isdigit(): return self.gLexicon.getStrongsEntryHTML( key )
if '.' in key: return self.hLexicon.getBrDrBrEntryHTML( key )
# end of BibleLexicon.getEntryHTML
# end of BibleLexicon class
def demo():
"""
Main program to handle command line parameters and then run what they want.
"""
if BibleOrgSysGlobals.verbosityLevel > 0: print( ProgNameVersion )
HebrewLexiconFolder = "../HebrewLexicon/" # Open Scriptures Hebrew lexicon folder
GreekLexiconFolder = "../../../ExternalPrograms/morphgnt/strongs-dictionary-xml/" # morphgnt Greek lexicon folder
if 0: # demonstrate the Bible Lexicon Index class
if BibleOrgSysGlobals.verbosityLevel > 1: print( "\nDemonstrating the Bible Lexicon Index class…" )
blix = BibleLexiconIndex( HebrewLexiconFolder, GreekLexiconFolder ) # Load and process the XML
print( blix ) # Just print a summary
print()
print( "Code for 2 is", blix.getLexiconCodeFromStrongsNumber( '2' ) )
print( "Code for H8674 is", blix.getLexiconCodeFromStrongsNumber( 'H8674' ) )
print( "Code for H8675 is", blix.getLexiconCodeFromStrongsNumber( 'H8675' ) )
print( "Codes for aac are", blix.getStrongsNumberFromLexiconCode('aac'), blix.getBrDrBrCodeFromLexiconCode('aac'), blix.getTWOTCodeFromLexiconCode('aac') )
print( "Codes for nyy are", blix.getStrongsNumberFromLexiconCode('nyy'), blix.getBrDrBrCodeFromLexiconCode('nyy'), blix.getTWOTCodeFromLexiconCode('nyy') )
print( "Codes for pdc are", blix.getStrongsNumberFromLexiconCode('pdc'), blix.getBrDrBrCodeFromLexiconCode('pdc'), blix.getTWOTCodeFromLexiconCode('pdc') )
print( "Codes for pdd are", blix.getStrongsNumberFromLexiconCode('pdd'), blix.getBrDrBrCodeFromLexiconCode('pdd'), blix.getTWOTCodeFromLexiconCode('pdd') )
if 1: # demonstrate the Bible Lexicon class
if BibleOrgSysGlobals.verbosityLevel > 1: print( "\nDemonstrating the Bible Lexicon class…" )
bl = BibleLexicon( HebrewLexiconFolder, GreekLexiconFolder ) # Load and process the XML
print( bl ) # Just print a summary
print()
for strongsKey in ('H1','H123','H165','H1732','H1979','H2011','H8674','H8675',
'G1','G123','G165','G1732','G1979','G2011','G5624','G5625',): # Last ones of each are invalid
print( '\n' + strongsKey )
print( " Data:", bl.getStrongsEntryData( strongsKey ) )
print( " Usage:", bl.getStrongsEntryField( strongsKey, 'usage' ) )
print( " HTML:", bl.getStrongsEntryHTML( strongsKey ) )
for BrDrBrKey in ('a.ab.ac','a.gq.ab','b.aa.aa','xw.ah.ah','xy.zz.zz',): # Last one is invalid
print( '\n' + BrDrBrKey )
print( " Data:", bl.getBrDrBrEntryData( BrDrBrKey ) )
print( " Status:", bl.getBrDrBrEntryField( BrDrBrKey, 'status' ) )
print( " HTML:", bl.getBrDrBrEntryHTML( BrDrBrKey ) )
# end of demo
if __name__ == '__main__':
# Configure basic set-up
parser = BibleOrgSysGlobals.setup( ProgName, ProgVersion )
BibleOrgSysGlobals.addStandardOptionsAndProcess( parser, exportAvailable=False )
demo()
BibleOrgSysGlobals.closedown( ProgName, ProgVersion )
# end of BibleLexicon.py