-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathstrongsImport.py
executable file
·131 lines (111 loc) · 4.51 KB
/
strongsImport.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
#!/usr/bin/python
from xml.dom.minidom import parse
import sqlite3
sourceXML = '/Users/kerrishotts/SkyDrive/gib/strongsgreek.xml'
targetDB = '/Users/kerrishotts/SkyDrive/gib/bibleContent'
print " Strong's Greek Dictionary Importer v 1.1 "
print "=========================================="
print ""
print "The Strong's Dictionary comes from https://github.com/morphgnt/strongs-dictionary-xml/downloads"
print ""
print " Source: ", sourceXML
print " Target: ", targetDB
print ""
print "... Opening XML and Parsing..."
dom = parse(sourceXML)
print "... Opening target database..."
dbConn = sqlite3.connect(targetDB)
print "... ... Dropping Existing Table..."
dbConn.execute("DROP TABLE [strongsgr]")
print "... ... Recreating Table..."
dbConn.execute("create table [strongsgr] ([key] VARCHAR (10) PRIMARY KEY NOT NULL, [lemma] VARCHAR (512), [pronunciation] VARCHAR (512), [definition] VARCHAR (4096))")
strongsLookup = {}
def strongsref(node):
#
# returns a strong's reference, like so:
# G25 (agapo)
#
prefix = ""
theValueToPrint = ""
if node.getAttribute('language') == 'GREEK':
prefix += 'G'
else:
prefix += 'H'
theIndex = node.getAttribute('strongs').strip()
theValueToPrint = prefix + theIndex
if node.getAttribute('language') == 'GREEK':
theValueToPrint += ' (' + strongsLookup[node.getAttribute('strongs').strip()] + ') '
return theValueToPrint
def renderNode(node):
#
# prints a node. The node may contain <strongs/> references,
# <greek/> text, and simple text.
#
theValueToPrint = ''
for eachNode in node.childNodes:
if eachNode.nodeType == 3:
theValueToPrint += eachNode.nodeValue.strip() + ' '
else:
if eachNode.nodeName == 'strongsref':
theValueToPrint = strongsref(eachNode)
if eachNode.nodeName == 'greek':
theValueToPrint += eachNode.getAttribute('unicode').strip() + ' '
return theValueToPrint
def textContent(node):
if node.nodeName in ["pronunciation", "see", "strongs"]:
return ""
if node.nodeName in ["greek"]:
return node.getAttribute("unicode").strip()
if node.nodeName in ["strongsref"]:
return strongsref(node)
if node.nodeType in (node.TEXT_NODE, node.CDATA_SECTION_NODE):
return node.nodeValue
return ''.join(textContent(n) for n in node.childNodes)
print "... Creating Greek Index..."
for entry in dom.getElementsByTagName("entry"):
index = entry.getElementsByTagName("strongs")[0].childNodes[0].nodeValue.strip()
if entry.getElementsByTagName("greek").length > 0:
lemma = entry.getElementsByTagName("greek")[0].getAttribute("unicode").strip()
strongsLookup[index] = lemma
print "... Processing Greek Entries..."
i = 0
for entry in dom.getElementsByTagName("entry"):
i = i + 1
index = entry.getElementsByTagName("strongs")[0].childNodes[0].nodeValue.strip()
lemma = ""
pronunciation = ""
definition = ""
if i % 1000 == 0:
print '... ... ', i, 'entries processed...'
if entry.getElementsByTagName("greek").length > 0:
lemma = entry.getElementsByTagName("greek")[0].getAttribute("unicode").strip()
pronunciation = entry.getElementsByTagName("pronunciation")[0].getAttribute("strongs").strip()
definition = textContent(entry)
# for node in entry.childNodes:
# if node.nodeType == 3:
# definition += node.nodeValue.strip() + ' '
# else:
# if node.nodeName == 'strongs_derivation' or \
# node.nodeName == 'strongs_def' or \
# node.nodeName == 'kjv_def' or \
# node.nodeName == 'see':
# definition += renderNode(node).strip() + ' '
# if node.nodeName == 'strongsref':
# definition += strongsref(node) + ' '
# if node.nodeName == 'greek':
# definition += node.getAttribute('unicode').strip() + ' '
definition = definition.replace("\n", "").replace("\t", " ")
while ' ' in definition:
definition = definition.replace(' ', ' ')
dbConn.execute("insert into [strongsgr] values ( ?, ?, ?, ? )",
('G' + index,
lemma.strip(),
pronunciation.strip(),
definition.strip().replace("\n", "").replace("\t", " "), ))
print "... Committing..."
dbConn.commit()
print "... Closing..."
dbConn.close()
print ""
print "Complete."
print ""