forked from chaira19/Hindi-DateTime-Parser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.py
297 lines (181 loc) · 6.57 KB
/
parser.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
import data
from data import *
import functions
from functions import *
def get_parsed(Final={}):
Answer = {'Date':[], 'Time':[]}
for date in Final['Date']:
fulldate = []
y=0
mt=0
w=0
d=0
x = set(date).intersection(DateTime.keys())
yx = set(date).intersection(Numbers.keys())
if len(x)>0:
if len(yx)>0:
if ('din' in date) or ('divas' in date) or ('dino' in date):
d = Numbers[list(yx)[0]][0] * DateTime[list(x)[0]][0]
#why not if ('ghanta' or 'ghante') in time:
if ('mahine' in date) or ('mahina' in date):
mt = Numbers[list(yx)[0]][0] * DateTime[list(x)[0]][0]
if ('saal' in date) or ('varsh' in date):
y = Numbers[list(yx)[0]][0] * DateTime[list(x)[0]][0]
if ('hafta' in date) or ('hafte' in date):
w = Numbers[list(yx)[0]][0] * DateTime[list(x)[0]][0]
else:
## eleiminate this step by use of above step (putting value = 1)
if ('din' in date) or ('divas' in date) or ('dino' in date):
d = DateTime[list(x)[0]][0]
#why not if ('ghanta' or 'ghante') in time:
if ('mahine' in date) or ('mahina' in date):
mt = DateTime[list(x)[0]][0]
if ('saal' in date) or ('varsh' in date):
y = DateTime[list(x)[0]][0]
if ('hafta' in date) or ('hafte' in date):
w = DateTime[list(x)[0]][0]
### Instead of using [0] use previous word for (aaj agle mahine pichle saal)
# for word in date:
#if Date[word][1] == 'weekday':
# prev_word = date[date.index(word)-1]
# if prev_word == 'agle':
# print ("")
# elif prev_word == 'pichle':
# print ("")
# elif prev_word == 'is':
# print ("")
## pass in format of WE(+1) where WE is not string
Answer['Date'].append(change_datetime("date",y,mt,w,d))
else:
if len(set(date).intersection(Numbers.keys()))>0:
for word in date:
if word in Numbers.keys():
next_word = date[date.index(word)+1]
if (next_word == 'mahina') or (next_word == 'mahine'): ## check if next_word == 'mahina' or 'mahine' works or not
mt = Numbers[word][0]
elif (next_word == 'saal') or (next_word == 'varsh'):
y = Numbers[word][0]
elif (next_word == 'tarikh') or (next_word == 'din'):
d = Numbers[word][0]
elif (next_word.isdigit()) and (len(next_word)==4):
mt = Numbers[word][0]
elif Date[next_word][1] == 'month':
d = Numbers[word][0]
#fulldate.append(set_datetime(y,mt,d))
## Do something like if month in date then d=number , mt = month[0]
for word in date:
if (word in Date) and (Date[word][1] == 'month'):
mt = Date[word][0]
next_word = date[date.index(word)+1]
if next_word in Numbers:
d = Numbers[next_word][0]
#obviously word is in Date
if (word.isdigit()) and (len(word)==4):
y = int(word)
if len(date) == date.index(word)+1:
fulldate.append(set_datetime(y, mt, d))
if word == 'aaj':
fulldate.append(change_datetime("date",0,0,0,0))
if word == 'kal':
fulldate.append(change_datetime("date",0,0,0,1))
if word == 'parson':
fulldate.append(change_datetime("date",0,0,0,2))
if word == 'narson':
fulldate.append(change_datetime("date",0,0,0,3))
## Above 4 can be transformed into 1 just like done in above 4
#if (word in Date) and (Date[word][1] == 'weekday'):
# Difficult to implement without DateTime words i.e. to set the day according to month and year
## pseudocode
## if (word in Date) and (Date[word][1] == 'weekday'):
#if (y!=0) and (mt!=0):
#print ("")
## call weekday function passing month and year to get the date
Answer['Date'].append(fulldate)
for time in Final['Time']:
h=0
m=0
s=0
c=0
x = set(time).intersection(DateTime.keys())
y = set(time).intersection(Numbers.keys())
if len(y) == 0:
y.add("1")
#print (x)
#print (y)
if len(x)>0:
if len(y)>0:
if ('ghanta' in time) or ('ghante' in time) or ('ghanton' in time):
h = Numbers[list(y)[0]][0] * DateTime[list(x)[0]][0]
#why not if ('ghanta' or 'ghante') in time:
if ('minute' in time) or ('minutes' in time):
m = Numbers[list(y)[0]][0] * DateTime[list(x)[0]][0]
if ('second' in time) or ('seconds' in time):
s = Numbers[list(y)[0]][0] * DateTime[list(x)[0]][0]
else:
print ("waited for this example, thanks !!")
# if pichle (ghante then , minute then, second then) else agle (same)
Answer['Time'].append(change_datetime("time",0,0,0,0,h,m,s))
else:
if len(set(time).intersection(Numbers.keys()))>0:
#print ("Number Exist")
for word in time:
if word in Numbers.keys():
next_word = time[time.index(word)+1]
if (next_word == 'baj') or (next_word == 'baje'): ## baje is not working
h = Numbers[word][0]
print (h, "h")
elif (next_word == 'minute') or (next_word == 'minutes') or (next_word == 'min'): ## or ke baad wale main dikkar hai
m = Numbers[word][0]
print (m, "m")
elif (next_word == 'second') or (next_word == 'seconds') or (next_word == 'sec'):
s = Numbers[word][0]
print (s, "s")
if 'subah' in time:
c = "AM"
elif 'dopahar' in time:
c = "PM"
elif 'shaam' or 'sandhya' in time: ## sandhya to aa hi nahi raha
c = "PM"
elif ('raat' in time) and (0 <= h < 6):
c = "AM"
elif ('raat' in time) and (6 <= h < 12):
c = "PM"
print (c, "c")
Answer['Time'].append(set_datetime(0,0,0,h,m,s,c))
else:
#print ("Number doesn't Exist")
duration = []
for word in time:
if word == 'abhi':
duration.append(change_datetime("time"))
if word == 'subah':
duration.append("5 - 12 AM")
elif word == 'dopahar':
duration.append("12 - 5 PM")
elif word == ('shaam' or 'sandhya'):
duration.append("5 - 12 PM")
elif word == 'raat':
duration.append("10-12 PM & 12 - 5 AM")
Answer['Time'].append(duration)
return Answer
def get_emails_parsed(words):
emails = []
for word in words:
if ('.' in word) and ('@' in word):
emails.append(word)
return emails
def get_numbers_parsed(words):
phonenumbers = []
for word in words:
if (len(word) == 10) and (word.isdigit()):
phonenumbers.append(word)
if (len(word) == 12) and (word.isdigit()) and (word[0] == '9') and (word[1] == '1'):
new_word = word[2:]
phonenumbers.append(new_word)
if (len(word) == 13) and (word[:3] == '+91'):
new_word = word[3:]
phonenumbers.append(new_word)
if (len(word) == 11) and (word[0] == '0') and (word.isdigit()):
new_word = word[1:]
phonenumbers.append(new_word)
return phonenumbers