-
Notifications
You must be signed in to change notification settings - Fork 1
/
findDates.py
293 lines (249 loc) · 9.85 KB
/
findDates.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
import re
import xml.etree.ElementTree as ET
from pyluach.dates import HebrewDate, GregorianDate
idDate = 0
hebToGreg = {}
gregToHeb = {}
datesWithId = {}
def HebrewFormat():
# search for Hebrew dates
months = "(בתשרי|בחשון|בכסלו|בטבת|בשבט|באדר|בניסן|באייר|בסיון|בתמוז|באב|באלול)"
days1 = "[אבגדהוזחטיכל]" + "."
x1 = "י"
x2 = "כ"
x3 = "ל"
days2 = x1 + "\"" + "[אבגדזחט]"
days3 = x2 + "\"" + "[אבגדהוזחט]"
days4 = x3 + "\"" + "[אבגדהוזחט]"
days5 = "ט" + "\"" + "[וז]"
days = "(" + days1 + "|" + days2 + "|" + days3 + "|" + days4 + "|" + days5 + ")"
year = "([\u0590-\u05fe]+" + "\"" + "[\u0590-\u05fe])"
HebrewDates = "(?:" + days + " " + months + " " + year + ")"
return HebrewDates
def GregorianFormat():
# search for gregorian dates
months2 = "(בינואר|בפברואר|במרץ|באפריל|במאי|ביוני|ביולי|באוגוסט|בספטמבר|באוקטובר|בנובמבר|בדצמבר)"
date_reg2 = "(?:" + "(3[01]|[12][0-9]|0?[1-9])" + " " + months2 + ",? " + "([0-9]{4})" + ")"
return date_reg2
def dates(txt):
if txt is not None:
HebrewDates = HebrewFormat()
y = re.findall(HebrewDates, txt)
date_reg2 = GregorianFormat()
z = re.findall(date_reg2, txt)
hebrewDatesArr = changeHebrewDate(y)
gregDatesArr = changeGregDates(z)
for date in hebrewDatesArr:
Id = increaseId()
heb = HebrewDate(date[2], date[1], date[0])
datesWithId[str(heb)] = Id
gregFormat = heb.to_greg()
hebToGreg[gregFormat] = Id
for date1 in gregDatesArr:
Id = increaseId()
greg = GregorianDate(date1[2], date1[1], date1[0])
datesWithId[str(greg)] = Id
hebFormat = greg.to_heb()
gregToHeb[hebFormat] = Id
def dateAsString(date):
if date[0] > 10:
if date[1] > 10:
return str(date[0]) + "-" + str(date[1]) + "-" + str(date[2])
else:
return str(date[0]) + "-0" + str(date[1]) + "-" + str(date[2])
else:
if date[1] > 10:
return "0" + str(date[0]) + "-" + str(date[1]) + "-" + str(date[2])
else:
return "0" + str(date[0]) + "-0" + str(date[1]) + "-" + str(date[2])
def addTagsText(node, txt):
if node.tag == "date":
return
if txt is not None:
HebrewDates = HebrewFormat()
date_reg2 = GregorianFormat()
allFormats = "(?:" + HebrewDates + "|" + date_reg2 + ")"
datesArr = re.findall(allFormats, txt)
if datesArr:
date = removeBlankCells(datesArr[0])
node.text = txt[0: txt.index(" ".join(date))]
i = 0
for date in datesArr:
date = removeBlankCells(date)
a = " ".join(date)
if len(re.findall(HebrewDates, a)) > 0:
numFormat = changeHebrewDate([date])
numFormat = numFormat[0]
numFormat = HebrewDate(numFormat[2], numFormat[1], numFormat[0])
Id = datesWithId.get(str(numFormat))
alterDate = gregToHeb.get(numFormat, None)
else:
numFormat = changeGregDates([date])
numFormat = numFormat[0]
numFormat = GregorianDate(numFormat[2], numFormat[1], numFormat[0])
Id = datesWithId.get(str(numFormat))
alterDate = hebToGreg.get(numFormat, None)
if alterDate is None:
newTag = ET.Element('date', attrib={'date eId': Id, 'date': numFormat})
else:
newTag = ET.Element('date', attrib={'date eId': Id, 'alternativeTo': alterDate, 'date': numFormat})
newTag.text = " ".join(date)
txt = txt[txt.index(" ".join(date)) + len(" ".join(date)):]
newTag.tail = txt
node.insert(i, newTag)
i = i + 1
def addTagsTail(node, txt):
if node.tag == "date":
return
if txt is not None:
HebrewDates = HebrewFormat()
date_reg2 = GregorianFormat()
allFormats = "(?:" + HebrewDates + "|" + date_reg2 + ")"
datesArr = re.findall(allFormats, txt)
if datesArr:
date = removeBlankCells(datesArr[0])
node.tail = txt[0: txt.index(" ".join(date))]
i = 0
for date in datesArr:
date = removeBlankCells(date)
a = " ".join(date)
if len(re.findall(HebrewDates, a)) > 0:
numFormat = changeHebrewDate([date])
numFormat = numFormat[0]
numFormat = HebrewDate(numFormat[2], numFormat[1], numFormat[0])
Id = datesWithId.get(str(numFormat))
alterDate = gregToHeb.get(numFormat, None)
else:
numFormat = changeGregDates([date])
numFormat = numFormat[0]
numFormat = GregorianDate(numFormat[2], numFormat[1], numFormat[0])
Id = datesWithId.get(str(numFormat))
alterDate = hebToGreg.get(numFormat, None)
if alterDate is None:
newTag = ET.Element('date', attrib={'eId': Id, 'date': numFormat})
else:
newTag = ET.Element('date', attrib={'eId': Id, 'alternativeTo': alterDate, 'date': numFormat})
newTag.text = " ".join(date)
txt = txt[txt.index(" ".join(date)) + len(" ".join(date)):]
if i < len(datesArr)-1:
next = removeBlankCells(datesArr[i+1])
next = " ".join(next)
newTag.tail = txt[0: txt.index(next)]
else:
newTag.tail = txt
node.append(newTag)
i = i+1
def removeBlankCells(date):
HebrewDates = HebrewFormat()
a = " ".join(date)
if len(re.findall(HebrewDates, a)) > 0:
clean = [date[0], date[1], date[2]]
else:
clean = [date[3], date[4], date[5]]
return clean
def increaseId():
global idDate
idDate = idDate + 1
return str(idDate)
def changeGregDates(arr):
output = []
i = 0
for date in arr:
newDate = [int(date[0])]
if date[1] == "בינואר":
newDate.append(1)
if date[1] == "בפברואר":
newDate.append(2)
if date[1] == "במרץ":
newDate.append(3)
if date[1] == "באפריל":
newDate.append(4)
if date[1] == "במאי":
newDate.append(5)
if date[1] == "ביוני":
newDate.append(6)
if date[1] == "ביולי":
newDate.append(7)
if date[1] == "באוגוסט":
newDate.append(8)
if date[1] == "בספטמבר":
newDate.append(9)
if date[1] == "באוקטובר":
newDate.append(10)
if date[1] == "בנובמבר":
newDate.append(11)
if date[1] == "בדצמבר":
newDate.append(12)
newDate.append(int(date[2]))
output.append(newDate)
i = i + 1
return output
def changeHebrewDate(arr):
output = []
i = 0
for date in arr:
newDate = []
# handle day
day = date[0]
dayInNumber = 0
if day.find('"') == -1:
dayInNumber += hebrewLettersValue.get(day[0])
else:
day = day.replace('"', '')
for i in range(len(day)):
dayInNumber += hebrewLettersValue.get(day[i - 1])
newDate.append(dayInNumber)
# handle month
if date[1] == "בניסן":
newDate.append(1)
if date[1] == "באייר":
newDate.append(2)
if date[1] == "בסיון":
newDate.append(3)
if date[1] == "בתמוז":
newDate.append(4)
if date[1] == "באב":
newDate.append(5)
if date[1] == "באלול":
newDate.append(6)
if date[1] == "בתשרי":
newDate.append(7)
if date[1] == "בחשון":
newDate.append(8)
if date[1] == "בכסלו":
newDate.append(9)
if date[1] == "בטבת":
newDate.append(10)
if date[1] == "בשבט":
newDate.append(11)
if date[1] == "באדר":
newDate.append(12)
# handle year
year = date[2]
year = year.replace('"', '')
yearInNumber = 5000
for i in range(len(year)-1):
yearInNumber += hebrewLettersValue.get(year[i+1])
newDate.append(yearInNumber)
output.append(newDate)
i = i + 1
return output
hebrewLettersValue = {'א': 1, 'ב': 2, 'ג': 3, 'ד': 4, 'ה': 5, 'ו': 6, 'ז': 7, 'ח': 8, 'ט': 9, 'י': 10, 'כ': 20, 'ל': 30,
'מ': 40, 'נ': 50, 'ס': 60, 'ע': 70, 'פ': 80, 'צ': 90, 'ק': 100, 'ר': 200, 'ש': 300, 'ת': 400}
def main():
file = input("Insert Path: \n")
tree = ET.parse(file) # saves the XML file -> tree
root = tree.getroot()
dates(root.text)
dates(root.tail)
for child in root.iter():
dates(child.text)
dates(child.tail)
addTagsText(root, root.text)
addTagsTail(root, root.tail)
for child in root.iter():
addTagsText(child, child.text)
addTagsTail(child, child.tail)
tree.write('withDates.xml', 'utf-8')
if __name__ == '__main__':
main()