-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse_json_bible.py
70 lines (52 loc) · 2.17 KB
/
parse_json_bible.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
import json
import re
import copy
"""
Parses JSON Bibles and searches them with regex.
Verse dictionaries look like:
{
"chapter":1,
"verse":1,
"text":"In the beginning God created the heaven and the earth.",
"translation_id":"KJV",
"book_id":"Gen",
"book_name":"Genesis"
}
A JSON Bible should be an ordered list of the above.
"""
BIBLE_RESOURCES_DIR = "bibles"
ASV = "asv.json"
KJV = "kjv.json"
def parse_json_bible(filename):
with open(filename, "r") as bibleraw:
return json.loads(bibleraw.read())
KJV_JSON = parse_json_bible(BIBLE_RESOURCES_DIR + "/" + KJV)
ASV_JSON = parse_json_bible(BIBLE_RESOURCES_DIR + "/" + ASV)
def get_reference_text(verseDict):
return verseDict["book_name"] + " " + str(verseDict["chapter"]) + ":" + str(verseDict["verse"])
def regex_search_bibles(versionsToSearch, searchPattern):
"""
Returns the matching verses from the given Bible versions that match the regex search
:param versionsToSearch: A list of string filenames that represent the versions of the Bible to search
:param searchPattern: A regex string to search for matching verses with
:return: A list of matching verse dictionaries (see above for structure) including the re.Match object
as the value for the "match" entry in the dict
"""
results = []
print("Searching '" + searchPattern + "' in " + ", ".join(versionsToSearch))
for version in versionsToSearch:
bibleToSearch = None
strippedLoweredVersion = version.lower().strip()
if strippedLoweredVersion == "kjv":
bibleToSearch = KJV_JSON
elif strippedLoweredVersion == "asv":
bibleToSearch = ASV_JSON
for verseDict in bibleToSearch:
verseMatch = re.search(searchPattern, verseDict["text"])
if verseMatch:
print("Found match: " + get_reference_text(verseDict))
newResult = copy.deepcopy(verseDict)
newResult["match"] = verseMatch
newResult["reference"] = get_reference_text(verseDict)
results.append(newResult)
return results