-
Notifications
You must be signed in to change notification settings - Fork 2
/
alignments-from-usfm-Larry.py
150 lines (138 loc) · 4.3 KB
/
alignments-from-usfm-Larry.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
import os, glob, re, sys, datetime
ignoreList = ["the", "a", "an", "and", "or", "but", "of", "to", "at", "from", "in", "his", "my", "according", "for", "by", "your", "toward", "is", "who", "that", "which", "he", "they", "them", "so", "him", "her", "this", "that", "she", "you", "as", "on", "are", "me", "it", "its", "have", "then", "be", "I", "than", "had", "their", "s", "o"]
filesLocation = "~/Desktop/uW projects/ULT - Home.nosync/en_ult/*.usfm"
booksToCheck = [
"GEN",
"EXO",
"LEV",
"NUM",
"DEU",
"JOS",
"JDG",
"RUT",
"1SA",
"2SA",
"1KI",
"2KI",
"1CH",
"2CH",
"EZR",
"NEH",
"EST",
"JOB",
"PSA",
"PRO",
"ECC",
"SNG",
"ISA",
"JER",
"LAM",
"EZK",
"DAN",
"HOS",
"JOL",
"AMO",
"OBA",
"JON",
"MIC",
"NAM",
"HAB",
"ZEP",
"HAG",
"ZEC",
"MAL",
"MAT",
"MRK",
"LUK",
"JHN",
"ACT",
"ROM",
"1CO",
"2CO",
"GAL",
"EPH",
"PHP",
"COL",
"1TH",
"2TH",
"1TI",
"2TI",
"TIT",
"PHM",
"HEB",
"JAS",
"1PE",
"2PE",
"1JN",
"2JN",
"3JN",
"JUD",
"REV"
]
reChapter = re.compile(r".*\\{1,2}c\s(\d{1,3})")
reVerse = re.compile(r".*\\{1,2}v\s(\d{1,3})")
reStrongs = re.compile(r'x-strong="([HG]\d{4,5})')
reWords = re.compile(r'\\w (\w+)|')
currentBook = ''
currentChapter = ''
currentVerse = ''
alignedWords = []
strongs = input("Enter strong # to check alignments: ").upper()
print()
possibleTranslations = {}
print("checking: " ,end = '')
for filename in glob.glob(filesLocation):
if filename[-11] == "A": continue
if filename[-8:-5] not in booksToCheck: continue
if strongs[0] == "H" and int(filename[-11]) > 4: continue
elif strongs[0] == "G" and int(filename[-11]) < 4: continue
currentBook = ''
currentChapter = ''
currentVerse = ''
alignedWords = []
with open(os.path.join(os.getcwd(), filename), 'r', encoding='utf8') as f:
book = f.read().replace('\n',' ')
booklines = book.split('\zaln-e\*')
currentBook = booklines[0][4:7]
print(currentBook + "…", end = '')
for line in booklines:
oldChapter = currentChapter
oldVerse = currentVerse
try:
currentChapter = re.search(reChapter, line).group(1)
except:
currentChapter = oldChapter
try:
currentVerse = re.search(reVerse, line).group(1)
except:
currentVerse = oldVerse
try:
if re.search(strongs, line):
alignedWords = re.findall(reWords, line)
location = currentBook + " " + currentChapter + ":" + currentVerse
#print(location, end = ' ')
for translation in alignedWords:
if len(translation) == 0: continue
translationLower = translation.lower()
if translationLower in ignoreList: continue
if translationLower not in possibleTranslations:
possibleTranslations.update({translationLower:[location]})
#print(translation, end=' ')
elif translationLower in possibleTranslations:
possibleTranslations[translationLower].append(location)
#print()
except: pass
now = datetime.datetime.now().strftime("%Y-%m-%d %H.%M")
resultFile = strongs + "_results at " + now + ".txt"
with open(os.path.join(sys.path[0], resultFile), "w") as f:
f.write("Strongs#: " + strongs + '\n')
#sorted_items = sorted(possibleTranslations.items(), key = lambda item : len(item[1]))
sorted_items = {key: value for key, value in sorted(possibleTranslations.items())}
sortedPossibleTranslations = dict(sorted_items)
for item in sortedPossibleTranslations.keys():
f.write('\n')
f.write(item.upper() + " : " + str(len(sortedPossibleTranslations[item])) + 'x in: ')
for values in sortedPossibleTranslations[item]:
f.write(values.upper())
f.write(', ')
print('\n' + str(os.path.join(sys.path[0], resultFile)) + " written.")