-
Notifications
You must be signed in to change notification settings - Fork 2
/
create index of alignments.py
226 lines (202 loc) · 7.66 KB
/
create index of alignments.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
from operator import contains
import os, glob, re, datetime, time
ULTfilesLocation = "C:/Users/benja/Documents/GitHub/en_ult/*.usfm"
USTfilesLocation = "C:/Users/benja/Documents/GitHub/en_ust/*.usfm"
outputlocation = "C:/Users/benja/Documents/GitHub/scriptoutput/"
def findStrongsAlignments(strongs, filesLocation):
# strongs = strongs.upper()
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", 'we', 'has', 'been', 'being']
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})')
reLemma = re.compile(r'x-lemma="(.*)"')
reWords = re.compile(r'\\w (\w+)|')
csStrongsLemmaTranslationRef = []
currentBook = ''
currentChapter = ''
currentVerse = ''
alignedWords = []
possibleTranslations = {}
print("checking: " ,end = '')
for filename in glob.glob(filesLocation):
if filename[-11] == "A": continue
if filename[-8:-5] not in booksToCheck: 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
if contains(line, '\\c'):
currentChapter = re.search(reChapter, line).group(1)
else:
currentChapter = oldChapter
if contains(line, '\\v'):
currentVerse = re.search(reVerse, line).group(1)
else:
currentVerse = oldVerse
if contains(line, "x-strong"):
alignedWords = re.findall(reWords, line)
# fix up the ordering
if len(currentVerse) == 1:
currentVerse = f'0{currentVerse}' if currentBook != 'PSA' else f'00{currentVerse}'
elif len(currentVerse) == 2 and currentBook == 'PSA':
currentVerse = f'0{currentVerse}'
if len(currentChapter) == 1:
currentChapter = f'0{currentChapter}' if currentBook != 'PSA' else f'00{currentChapter}'
elif len(currentChapter) == 2 and currentBook == 'PSA':
currentChapter = f'0{currentChapter}'
location = currentBook + " " + currentChapter + ":" + currentVerse
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]})
elif translationLower in possibleTranslations:
possibleTranslations[translationLower].append(location)
sorted_items = {key: value for key, value in sorted(possibleTranslations.items())}
sortedPossibleTranslations = dict(sorted_items)
return sortedPossibleTranslations
def sortByVerse(possibleTranslations):
possibleTranslationsByRef = {}
for k, v in possibleTranslations.items():
for x in v:
possibleTranslationsByRef.setdefault(x,[]).append(k)
sorted_items = {key: value for key, value in sorted(possibleTranslationsByRef.items())}
sortedPossibleTranslations = dict(sorted_items)
return sortedPossibleTranslations
def trimRef(ref):
trimmedRef = ref.replace(' 0', ' ')
trimmedRef = trimmedRef.replace(' 0', ' ')
trimmedRef = trimmedRef.replace(':0', ':')
trimmedRef = trimmedRef.replace(':0', ':')
return trimmedRef
def makeResultByWordFile(file, possibleTranslations):
for key in possibleTranslations.keys():
f.write('\n')
f.write(key.lower() + " : " + str(len(possibleTranslations[key])) + 'x in: ')
for value in possibleTranslations[key]:
trimmedValue = trimRef(value)
f.write(trimmedValue.lower())
f.write(', ')
# Main
# snum = input("Enter strong # to check alignments: ").upper()
start_time = time.time()
print('\nULT ')
ULTpossibletranslations = findStrongsAlignments(ULTfilesLocation)
print('\nUST ')
USTpossibletranslations = findStrongsAlignments(USTfilesLocation)
ULTptSortedbyVerse = sortByVerse(ULTpossibletranslations)
USTptSortedbyVerse = sortByVerse(USTpossibletranslations)
topWord = ''
x= 0
for k, v in ULTpossibletranslations.items():
if len(v) > x:
x = len(v)
topWord = k.lower()
now = datetime.datetime.now().strftime("%Y-%m-%d %H.%M")
resultFileByRef = f"{snum}={topWord}_result sorted by verse at {now}.tsv"
resultFileByWord = f"{snum}={topWord}_result sorted by ULT rendering at {now}.txt"
with open(os.path.join(outputlocation, resultFileByRef), "w") as f:
f.write("Reference\tULT\tUST")
for key in ULTptSortedbyVerse.keys():
f.write('\n')
trimmedKey = trimRef(key)
f.write(f'{trimmedKey}\t')
for value in ULTptSortedbyVerse[key]:
f.write(value.lower())
if len(ULTpossibletranslations[value]) > 1:
f.write(f' {str(len(ULTpossibletranslations[value]))}x')
f.write(', ')
f.write('\t')
if key in USTptSortedbyVerse:
for value in USTptSortedbyVerse[key]:
f.write(value.lower())
if len(USTpossibletranslations[value]) > 1:
f.write(f' {str(len(USTpossibletranslations[value]))}x')
f.write(', ')
print('\n' + str(os.path.join(outputlocation, resultFileByRef)) + " written.\n")
with open(os.path.join(outputlocation, resultFileByWord), "w") as f:
f.write(f'Strongs#: {snum}\n')
f.write('ULT:\n')
makeResultByWordFile(f, ULTpossibletranslations)
f.write('\n\nUST:\n')
makeResultByWordFile(f, USTpossibletranslations)
print('\n' + str(os.path.join(outputlocation, resultFileByRef)) + " written.\n")
duration = time.time() - start_time
print(f"Duration {duration} seconds")