-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaibot_time.py
359 lines (341 loc) · 12.7 KB
/
aibot_time.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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
import datetime
import re
from aibot_date import convertStr2num, export_date
import numpy as np
from aibot_utils import cleaning, location_handler, mix_tdl, unique_without_sort
from adhanAPI import Adhan
from hazm import word_tokenize
from vocab import am_pm_dict, time_literals, minute_literals, perstr_to_num
from copy import copy
adhan = Adhan()
def fix_hour_ampm(st, hour):
ampm_list = []
for ampm in am_pm_dict.keys():
if " " + ampm + " " in st:
ampm_list.append(ampm)
elif re.findall("^{} ".format(ampm), st):
ampm_list.append(ampm)
elif re.findall(" {}$".format(ampm), st):
ampm_list.append(ampm)
if ampm_list:
if am_pm_dict[ampm_list[0]] == 1:
if hour < 12:
hour += 12
elif hour == 12:
hour = 0
elif ampm_list[0] == "بامداد" and hour == 12:
hour = 0
return hour
def hour_min_exporter(st, force_return=False):
# try ساعت + num
mtch = re.findall("ساعت \d+", st)
if mtch:
mtch_minute = st.find("دقیقه")
minute = 0
if mtch_minute == -1:
# try minute_literals
min_liter = []
for m in minute_literals.keys():
if m in st:
min_liter.append(m)
if min_liter:
minute = minute_literals[min_liter[0]]
else:
# try num + دقیقه
mtch_min = re.findall("\d+ دقیقه", st)
if mtch_min:
try:
minute = int(mtch_min[0].strip("دقیقه"))
except Exception:
pass
else:
# try writed number + دقیقه
probable_min = cleaning(st[:mtch_minute])
m_n = []
for w in word_tokenize(probable_min):
if w in perstr_to_num.keys():
m_n.append(w)
if m_n:
minute = convertStr2num(" ".join(m_n))
try:
hour = int(mtch[0].strip("ساعت"))
except Exception:
hour = 0
return fix_hour_ampm(st, hour), minute
# try ساعت + writed num
mtch = st.find("ساعت")
if mtch != -1:
h_n = []
probable_hour = st[mtch + len("ساعت"):]
for w in word_tokenize(probable_hour):
if w in perstr_to_num.keys():
h_n.append(w)
if h_n:
h_n.append("صفر")
mtch_minute = st.find("دقیقه")
if mtch_minute == -1:
minute = 0
hour = convertStr2num(" ".join(h_n))
# try minute literals
m_l = []
for m in minute_literals:
if m in st:
m_l.append(m)
if m_l:
minute = minute_literals[m_l[0]]
try:
if hour > 24:
raise Exception
return fix_hour_ampm(st, hour), minute
except Exception:
pass
# try num + دقیقه
mtch_m = re.findall("\d+ دقیقه", st)
if mtch_m:
try:
minute = int(mtch_m[0].strip("دقیقه"))
hour = convertStr2num(" ".join(h_n))
if hour > 24:
raise Exception
return fix_hour_ampm(st, hour), minute
except Exception:
pass
# maybe the writed numbers are for minute too!
if len(h_n) > 2:
try:
hour = convertStr2num(h_n[0])
minute = convertStr2num(" ".join(h_n[1:]))
if hour > 24 or minute > 60:
raise Exception
return fix_hour_ampm(st, hour), minute
except Exception:
try:
if len(h_n) >= 3:
h_n.append("صفر")
# hours are at maximum 2 numbers (<24)
hour = convertStr2num(" ".join(h_n[:2]))
minute = convertStr2num(" ".join(h_n[2:]))
if hour > 24:
raise Exception
return fix_hour_ampm(st, hour), minute
else:
raise Exception
except Exception:
try:
hour = convertStr2num(" ".join(h_n[:-2]))
minute = convertStr2num(" ".join(h_n[-2]))
if hour > 24:
raise Exception
return fix_hour_ampm(st, hour), minute
except Exception:
pass
else:
try:
hour = convertStr2num(" ".join(h_n[:-1]))
minute = 0
if hour > 24:
raise Exception
return fix_hour_ampm(hour), minute
except Exception:
pass
# try time literals
t_l = []
for tl in time_literals:
if tl in st:
t_l.append(tl)
if t_l:
# try num + tl
mtch = re.findall("\d+ {}".format(t_l[0]), st)
if mtch:
try:
h = int(mtch[0].strip(t_l[0]))
t = datetime.datetime.now() + datetime.timedelta(hours=h *
time_literals[t_l[0]])
hour, minute = t.hour, t.minute
return fix_hour_ampm(st, hour), minute
except Exception:
pass
# try writed number + tl
h_n = []
probable_number = st[:st.find(t_l[0])]
for n in perstr_to_num.keys():
if n in word_tokenize(probable_number):
h_n.append(n)
if h_n:
try:
h = convertStr2num(" ".join(h_n))
t = datetime.datetime.now() + datetime.timedelta(hours=h *
time_literals[t_l[0]])
hour, minute = t.hour, t.minute
return fix_hour_ampm(st, hour), minute
except Exception:
pass
# if none of the above return tl itself
try:
t = datetime.datetime.now() + \
datetime.timedelta(hours=time_literals[t_l[0]])
hour, minute = t.hour, t.minute
return fix_hour_ampm(st, hour), minute
except Exception:
pass
# let's just return a number as hour:
if force_return:
mtch = re.findall("\d+", st)
if mtch:
hour = int(mtch[0])
return fix_hour_ampm(st, hour), 0
return None, None
# check for adhan times
def adhan_handler(time_list, tokens, labels, question):
res = []
url = []
if time_list == None or len(time_list) == 1:
adhan_names = adhan.export_adhan_names(question)
if adhan_names:
exportdat = export_date(question, tokens, labels)
date_list = []
for d in exportdat:
if d[0] != None:
date_list.append(d[0])
date_list = unique_without_sort(date_list)
if not date_list:
date_list.append(datetime.datetime.today())
locs = location_handler(question, tokens, labels)
mixture = mix_tdl(adhan_names, date_list, locs)
if mixture:
for m in mixture:
t, u = adhan.get_city_adhan_time(m[2], m[1], m[0])
res.append(t)
url.append(u)
return res, url, adhan_names
else:
return None, None, None
else:
adhan_names = adhan.export_adhan_names(question)
# for t in time_list:
# a = adhan.export_adhan_names(str(t))
# if a:
# adhan_names.append(a[0])
a = len(adhan_names)
if a == 0:
return None, None, None
if a == len(time_list):
exportdat = export_date(question, tokens, labels)
date_list = []
for d in exportdat:
if d[0] != None:
date_list.append(d[0])
if not date_list:
date_list.append(datetime.datetime.today())
locs = location_handler(question, tokens, labels)
mixture = mix_tdl(adhan_names, date_list, locs)
if mixture:
for m in mixture:
t, u = adhan.get_city_adhan_time(m[2], m[1], m[0])
res.append(t)
url.append(u)
return res, url, adhan_names
if a < len(time_list):
exportdat = export_date(question, tokens, labels)
date_list = []
for d in exportdat:
if d[0] != None:
date_list.append(d[0])
if not date_list:
date_list.append(datetime.datetime.today())
locs = location_handler(question, tokens, labels)
mixture = mix_tdl(time_list, date_list, locs)
if mixture:
for m in mixture:
if m[0] in adhan_names:
t, u = adhan.get_city_adhan_time(m[2], m[1], m[0])
res.append(t)
url.append(u)
else:
res.append(m[0])
return res, url, adhan_names
def export_time_single(st_arr, st=None, force_return=False):
not_st = False
if not st:
st = cleaning("".join(st_arr).replace("-", ":").replace("/",
":").replace("و", ":").replace(",", ":"))
not_st = True
# try hh:mm format
mtch = re.findall("\d+[:]\d+", st)
if mtch:
t = mtch[0].split(":")
try:
hour = int(t[0])
minute = int(t[1])
if hour > 24:
temp = minute
minute = hour
hour = temp
return datetime.time(fix_hour_ampm(" ".join(st_arr), hour), minute)
except Exception:
pass
if not_st:
st = cleaning(" ".join(st_arr))
hour, minute = hour_min_exporter(st, force_return=force_return)
if hour == None:
# hour = datetime.datetime.now().hour
return None
if minute == None:
# minute = datetime.datetime.now().minute
return None
return datetime.time(hour, minute)
def export_time(question, tokens, labels):
labels = np.array(labels)
b_time = np.where(labels == "B_TIM")[0]
i_time = np.where(labels == "I_TIM")[0]
url = None
n = len(b_time)
if n == 0:
st_arr = word_tokenize(question)
t_ = export_time_single(st_arr, question)
if t_ == None:
res, url, adhan_names = adhan_handler(
None, tokens, labels, question)
if res != None:
return res, True, url, adhan_names
return [t_], False, None, None
elif n >= 2:
t_ = []
time_texts = []
for i in range(n):
st_arr = []
if i < n - 1:
ida = i_time[np.where(
(i_time > b_time[i]) & (i_time < b_time[i + 1]))]
else:
ida = i_time[np.where(i_time > b_time[i])]
for t in np.r_[b_time[i], ida]:
st_arr.append(tokens[int(t)])
time_texts.append(" ".join(st_arr))
t_.append(export_time_single(st_arr, force_return=True))
is_adhan_needed = False
new_t = copy(t_)
for i, t in enumerate(t_):
if t_[i] == None:
new_t[i] = time_texts[i]
is_adhan_needed = True
if is_adhan_needed:
res, url, adhan_names = adhan_handler(
new_t, tokens, labels, question)
if res != None:
if not None in res:
return res, True, url, adhan_names
return t_, False, None, None
else:
st_arr = []
for t in np.r_[b_time, i_time]:
st_arr.append(tokens[int(t)])
t_ = export_time_single(st_arr, force_return=True)
if t_ == None:
t_ = export_time_single(word_tokenize(question), question)
if t_ == None:
res, url, adhan_names = adhan_handler(
None, tokens, labels, question)
if res != None:
return res, True, url, adhan_names
return [t_], False, None, None