-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextract_intents.py
643 lines (521 loc) · 21.1 KB
/
extract_intents.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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
import re
### These are the regex that we are going to use
set_alarm_regex = r"([Ss]et\s+[Aa]larm\s+([\D ]*)((\d{1,2}):?(\d{0,2}))?)"
call_number_regex = r"([Cc]all\s+\+?([0-9\s-]){3,})"
view_contact_regex = r"([Vv]iew\s+contact\s+([A-Z0-9]\w*[\s\.]?)+)"
call_contact_regex = r"([Cc]all\s+([A-Z0-9]\w*[\s\.]?)+)"
send_email_regex = r"([Ss]end\s+[Ee]mail\s+to\s+([a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+)\s?(\[.*\])?\s(\[.*\]))"
set_event_regex = r"([Ss]et\s+[Ee]vent\s+(\d{4}:\d{1,2}:\d{1,2}:?\d{0,2}:?\d{0,2})\s*(\d{4}:\d{1,2}:\d{1,2}:?\d{0,2}:?\d{0,2})?\s+(\[[\w ,_]*\])\s*(\[[\w ,_]*\])?\s*(\[[\w ,_]*\])?)"
message_contact_regex = r"([Ss]end\s+[Mm]essage\s+[Tt]o\s+(([A-Z0-9]\w*[\s\.]?)+)(\[.*\]))"
message_number_regex = r"([Ss]end\s+[Mm]essage\s+[Tt]o\s+\+?([0-9\s-]{3,})\s?(\[.*\]))"
start_timer_regex = r"([Ss]tart\s+(\d{1,2}):?(\d{0,2})\s+[Tt]imer)"
save_note_regex = r"([Ss]ave\s+[Nn]ote\s+(\[[\w ,_]*\])\s*(\[[\w ,_]*\])?)"
show_note_regex = r"([Ss]how\s+(.*)\s+[Nn]ote)"
edit_note_regex = r"([Ee]dit\s+[Nn]ote\s+(\[.*\])\s+(\[.*\]))"
delete_note_regex = r"([Dd]elete\s+[Nn]ote\s+(\[.*\]))"
show_movie_regex = r"([Ss]how\s+(.*)\s+[Mm]ovie\s*([Ii]nfo)?)"
show_trailer_regex = r"([Ss]how\s+(.*)\s+[Tt]railer)"
recommend_movie_regex = r"(([Rr]ecommend|[Ss]uggest)\s+(.*)\s+[Mm]ovie)"
show_weather_regex = r"([Ss]how\s+(.*)\s+[Ww]eather)"
regex_list = [set_alarm_regex, call_number_regex, view_contact_regex, call_contact_regex, send_email_regex,
set_event_regex, message_contact_regex, message_number_regex, start_timer_regex,
save_note_regex, edit_note_regex, delete_note_regex, show_movie_regex,
show_trailer_regex, show_weather_regex]
###
########################## set_alarm ##########################
def trim(text):
if len(text) <= 1:
return ""
ending_marks = "."
if text[-1] in ending_marks:
return trim(text[:-1].strip())
elif text[0] in ending_marks:
return trim(text[1:].strip())
else:
return text.strip()
# text ="""
# set alarm morning alarm for 10:30
# Set alarm 10:00
# Set alarm 12-20 it causes an error
# Set alarm 10
# Set alarm 1:1
# set alarm any_name
# """
def default(par, value):
if len(par) == 0:
return str(value)
else:
return par
def get_set_alarm(text):
match = re.search(set_alarm_regex, text)
output = []
if match:
matches = re.findall(set_alarm_regex, text)
for match in matches:
output.append(("set_alarm", "title(\'" + trim(match[1]) + "\')", \
"hour(\'" + default(match[3], 0) + "\')", \
"minute(\'" + default(match[4], 0) + "\')"))
return output
########################## call_number ##########################
# text = """
# call 011 27 55 70 54
# call 013 27 55 70 54
# call 2010-6-12-51-0-93
# call +2011 287. very soon
# call 1. please, call +01 it's urgent.
# """
def trim_number(text):
marks = "\t\n .-_"
output = ""
if text == "":
return text
for ch in text:
if ch in marks:
pass
else:
output += ch
return output
def get_call_number(text):
match = re.search(call_number_regex, text)
output = []
if match:
matches = re.findall(call_number_regex, text)
for match in matches:
num = trim_number(match[0][5:])
output.append(("call_number", "number(\'" + num + "\')"))
return output
########################## view_contact ##########################
# text = "please, View contact Mohamed Anwar and View contact Mo7amed Gamal. \
# Don't forget to view contact M.Mostafa Elafasay and view contact Amr 3ezzat \
# and View amr info, or view Amr Diab Info"
def get_view_contact(text):
match = re.search(view_contact_regex, text)
output = []
if match:
matches = re.findall(view_contact_regex, text)
for match in matches:
output.append(("view_contact", "contact_name(\'" + trim(match[0][13:]) + "\')"))
another_regex = r"([Vv]iew\s+(([A-Z0-9]\w*[\s\.]?)+)\s*[Ii]nfo)"
match = re.search(another_regex, text)
if match:
matches = re.findall(another_regex, text)
for match in matches:
output.append(("view_contact",
"contact_name(\'" + trim(match[1]) + "\')"))
return output
########################## call_contact ##########################
# text = "please, call Mohamed Anwar and Call Mo7amed Gamal.\
# Don't forget to call M.Mostafa Elafasay and call Amr 3ezzat"
def delete_white_space(text):
white_space = "\t\n "
output = ""
if text == "":
return text
for ch in text:
if ch in white_space:
pass
else:
output += ch
return output
def get_call_contact(text):
match = re.search(call_contact_regex, text)
output = []
if match:
matches = re.findall(call_contact_regex, text)
for match in matches:
temp = delete_white_space(match[0][5:])
if trim(temp).isdigit():
pass
else:
output.append(("call_contact", "contact_name(\'" + trim(match[0][5:]) + "\')"))
return output
########################## send_email ##########################
# text = """
# try to send email to [email protected] [job offer] [You are totally rejected]
# let's Send email to [email protected] [we are going to be OK [now, we are not], isA]
# send email [no] [noo]
# send email to [email protected] []
# """
def get_send_email(text):
match = re.search(send_email_regex, text)
output = []
if match:
matches = re.findall(send_email_regex, text)
for match in matches:
output.append(("send_email", "email(\'" + trim(match[1]) + "\')", \
"subject(\'" + trim(match[2][1:-1]) + "\')", \
"body(\'" + trim(match[3][1:-1]) + "\')"))
return output
########################## set_event ##########################
# text = """
# set event 2017:8:01:14:00 2017:8:01:14:00 [title] [des] [location]
# set event 2017:8:14:17:32 [title] [description]
# set event 2087:8:30 [location]
# set event 2117:8:1 []
# Set event 12:3:4 []
# Set event 2:10 10:11 [title]
# Set event 22:00 [title]
# """
def get_set_event(text):
match = re.search(set_event_regex, text)
output = []
if match:
matches = re.findall(set_event_regex, text)
for match in matches:
output.append(("set_event", \
"start_time(\'" + match[1] + "\')", \
"end_time(\'" + default(match[2], 0) + "\')", \
"title(\'" + trim(match[3][1:-1]) + "\')", \
"text(\'" + match[4][1:-1] + "\')", \
"location(\'" + trim(match[5][1:-1]) + "\')"))
another_regex = r"([Ss]et\s+[Ee]vent\s+(\d{0,2}:\d{0,2})\s*(\d{0,2}:\d{0,2})?\s+(\[[\w ,_]*\])\s*(\[[\w ,_]*\])?\s*(\[[\w ,_]*\])?)"
match = re.search(another_regex, text)
if match:
matches = re.findall(another_regex, text)
for match in matches:
output.append(("set_event", \
"start_time(\'" + match[1] + "\')", \
"end_time(\'" + default(match[2], 0) + "\')", \
"title(\'" + trim(match[3][1:-1]) + "\')", \
"text(\'" + match[4][1:-1] + "\')", \
"location(\'" + trim(match[5][1:-1]) + "\')"))
return output
########################## message_contact ##########################
# text = """
# send message to Amr 3zzat [How are you?]
# Send message to M.Anwar[How are you?]
# send message to m.Anwar [How are you?]
# Send message to Anwar[][]
# send message to 011 [How are you?]
# """
def get_message_contact(text):
match = re.search(message_contact_regex, text)
output = []
if match:
matches = re.findall(message_contact_regex, text)
for match in matches:
temp = delete_white_space(trim(match[1]))
if trim(temp).isdigit():
pass
else:
output.append(("message_contact", \
"contact_name(\'" + trim(match[1]) + "\')", \
"message(\'" + match[3][1:-1] + "\')"))
return output
########################## message_number ##########################
# text = """
# send message to 011 []
# send message to 011 27 55 70 54[][][][][
# Send message to 01127557066u98[please, call me back. It's urgent]
# send message to 3zzat [How are you?]
# Send message to 1 [][]
# Send message to 11 [][]
# """
def get_message_number(text):
match = re.search(message_number_regex, text)
output = []
if match:
matches = re.findall(message_number_regex, text)
for match in matches:
output.append(("message_number", \
"number(\'" + trim_number(match[1]) + "\')", \
"message(\'" + match[2][1:-1] + "\')"))
return output
########################## start_timer ##########################
# text ="""
# start timer morning alarm 10:30
# start timer 10:00
# start timer 12-20 it causes an error
# start 10 timer
# start 1:1 timer
# start timer now
# start now timer
# """
def get_start_timer(text):
match = re.search(start_timer_regex, text)
output = []
if match:
matches = re.findall(start_timer_regex, text)
for match in matches:
output.append(("start_timer", "minute(\'" + default(match[1], 0) + "\')", \
"second(\'" + default(match[2], 0) + "\')"))
return output
########################## save_note ##########################
# text = """
# try to Save note [job offer] [You are totally rejected]
# let's save note [we are going to be OK [now, we are not], isA]
# Save note [no] [noo]
# """
def get_save_note(text):
match = re.search(save_note_regex, text)
output = []
if match:
matches = re.findall(save_note_regex, text)
for match in matches:
output.append(("save_note", "title(\'" + trim(match[1][1:-1]) + "\')", \
"text(\'" + match[2][1:-1] + "\')"))
return output
########################## show_note ##########################
# text = """
# Please, try to show [job offer] note
# show Morning Routine note
# and don't forget to show note.
# and don't forget to show all note.
# Show Note [Hello]
# """
def get_show_note(text):
match = re.search(show_note_regex, text)
output = []
if match:
matches = re.findall(show_note_regex, text)
for match in matches:
if match[1] == "all":
pass
else:
output.append(("show_note", "title(\'" + match[1] + "\')"))
another_regex = r"([Ss]how\s+[Nn]ote\s+(\[[\w ,_]*\]))"
match = re.search(another_regex, text)
if match:
matches = re.findall(another_regex, text)
for match in matches:
output.append(("show_note", "title(\'" + trim(match[1][1:-1]) + "\')"))
return output
########################## edit_note ##########################
# text = """
# try to edit note [job offer] [You are totally rejected]
# let's edit note [we are going to be OK [now, we are not], isA]
# edit note [no] [noo]
# """
def get_edit_note(text):
match = re.search(edit_note_regex, text)
output = []
if match:
matches = re.findall(edit_note_regex, text)
for match in matches:
output.append(("edit_note", "title(\'" + trim(match[1][1:-1]) + "\')", \
"text(\'" + match[2][1:-1] + "\')"))
return output
########################## delete_note ##########################
# text = """
# try to delete note [job offer]
# delete hey, YO Note
# delete note
# """
def get_delete_note(text):
match = re.search(delete_note_regex, text)
output = []
if match:
matches = re.findall(delete_note_regex, text)
for match in matches:
output.append(("delete_note", "title(\'" + trim(match[1][1:-1]) + "\')"))
another_regex = show_note_regex = r"([Dd]elete\s+(.*)\s+[Nn]ote)"
match = re.search(another_regex, text)
if match:
matches = re.findall(another_regex, text)
for match in matches:
output.append(("delete_note", "title(\'" + trim(match[1]) + "\')"))
return output
########################## new_send ##########################
# text = """
# send i've done with you to 011 27 55 70 54, please
# send i've done with you to Mo7med 3adel, for God's sake
# Send i've done with you to [email protected], it's necessary
# Send message to 011 [hello]
# """
def get_new_send(text):
output = []
new_send_regex = r"([Ss]end\s+(.*)\s+to\s+(([A-Z0-9]\w*[\s\.]?)+))"
match = re.search(new_send_regex, text)
if match:
matches = re.findall(new_send_regex, text)
for match in matches:
temp = delete_white_space(trim(match[2]))
if trim(temp).isdigit() or match[1].lower() == "message":
pass
else:
output.append(("message_contact", \
"contact_name(\'" + trim(match[2]) + "\')", \
"message(\'" + match[1] + "\')"))
new_send_regex = r"([Ss]end\s+(.*)\s+to\s+([0-9\s-]{3,}))"
match = re.search(new_send_regex, text)
if match:
matches = re.findall(new_send_regex, text)
for match in matches:
if match[1].lower() == "message":
pass
else:
output.append(("message_number", \
"number(\'" + trim_number(match[2]) + "\')", \
"message(\'" + match[1] + "\')"))
new_send_regex = r"([Ss]end\s+(.*)\s+to\s+([a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+))"
match = re.search(new_send_regex, text)
if match:
matches = re.findall(new_send_regex, text)
for match in matches:
if match[1].lower() == "message":
pass
else:
output.append(("send_email", \
"email(\'" + trim(match[2]) + "\')", \
"body(\'" + match[1] + "\')"))
return output
########################## show_movie ##########################
text = """
show Me The incredibles movie
show me movie
please, show me The amazing spiderman movie
show movie [The dark knight ] info[]
show Me movie [Wag the DOG ][]
"""
def get_show_movie(text):
output = []
match = re.search(show_movie_regex, text)
if match:
matches = re.findall(show_movie_regex, text)
for match in matches:
movie_name = trim(match[1])
if movie_name.split()[0].lower() == "me":
movie_name = trim(' '.join(movie_name.split()[1:]))
if len(movie_name) != 0:
output.append(("show_movie", \
"movie_name(\'" + movie_name + "\')"))
another_regex = r"(([Ss]how|[Ss]how\s+[Mm]e)\s+[Mm]ovie\s+(\[[\w ,_]*\])\s*([Ii]nfo)?)"
match = re.search(another_regex, text)
if match:
matches = re.findall(another_regex, text)
for match in matches:
output.append(("show_movie", \
"movie_name(\'" + trim(match[2][1:-1]) + "\')"))
return output
# print get_show_movie(text), "\n\n"
########################## show_trailer ##########################
text = """
show The dark knight trailer
show Split trailer
please, show me The amazing spiderman trailer
show trailer [The dark knight ]
show me trailer [How i met you mother]
show Me trailer [Wag the DOG ][]
"""
def get_show_trailer(text):
output = []
match = re.search(show_trailer_regex, text)
if match:
matches = re.findall(show_trailer_regex, text)
for match in matches:
movie_name = trim(match[1])
if movie_name.split()[0].lower() == "me":
movie_name = trim(' '.join(movie_name.split()[1:]))
if len(movie_name) != 0:
output.append(("show_trailer", \
"movie_name(\'" + movie_name + "\')"))
another_regex = r"(([Ss]how|[Ss]how\s+[Mm]e)\s+[Tt]railer\s+(\[[\w ,_]*\]))"
match = re.search(another_regex, text)
if match:
matches = re.findall(another_regex, text)
for match in matches:
output.append(("show_trailer", \
"movie_name(\'" + trim(match[2][1:-1]) + "\')"))
return output
# print get_show_trailer(text), "\n\n"
########################## recommend_movie ##########################
# text = """
# recommend an action movie
# recommend [comedy] movie
# suggest (a romance) movie
# Suggest a drama movie
# """
def handle_marks(text):
marks = "(\"'[{"
try:
if text[0] in marks:
return handle_marks(text[1:-1])
else:
return text
except IndexError:
return text
def get_recommend_movie(text):
output = []
categories = ["action", "adventure", "animation", "comedy", "crime", "drama", "horror",
"mystery", "romance"]
match = re.search(recommend_movie_regex, text)
try:
if match:
matches = re.findall(recommend_movie_regex, text)
for match in matches:
category = handle_marks(trim(match[2]))
if category.split()[0].lower() == "a" or category.split()[0].lower() == "an":
category = trim(' '.join(category.split()[1:]))
if len(category) != 0 and category in categories:
output.append(("recommend_movie", \
"category(\'" + category + "\')"))
finally:
return output
# print get_recommend_movie(text), "\n\n"
########################## show_weather ##########################
def get_show_weather(text):
output = []
match = re.search(show_weather_regex, text)
if match:
matches = re.findall(show_weather_regex, text)
for match in matches:
city_name = trim(match[1])
if city_name.split()[0].lower() == "me":
city_name = trim(' '.join(city_name.split()[1:]))
if len(city_name) != 0:
output.append(("show_weather", \
"city_name(\'" + city_name + "\')"))
another_regex = r"(([Ss]how|[Ss]how\s+[Mm]e)\s+([Ww]eather)\s+(\[[\w ,_]*\]))"
match = re.search(another_regex, text)
if match:
matches = re.findall(another_regex, text)
for match in matches:
output.append(("show_weather", \
"city_name(\'" + trim(match[3][1:-1]) + "\')"))
return output
#######################################################################
# text = "show cairo weather"
# print get_show_weather(text), "\n\n"
#######################################################################
def extract_intents(text):
whole_outputs = []
whole_outputs.append(get_set_alarm(text))
whole_outputs.append(get_call_number(text))
whole_outputs.append(get_view_contact(text))
whole_outputs.append(get_call_contact(text))
whole_outputs.append(get_send_email(text))
whole_outputs.append(get_new_send(text))
whole_outputs.append(get_set_event(text))
whole_outputs.append(get_message_contact(text))
whole_outputs.append(get_message_number(text))
whole_outputs.append(get_start_timer(text))
whole_outputs.append(get_save_note(text))
whole_outputs.append(get_edit_note(text))
whole_outputs.append(get_show_note(text))
whole_outputs.append(get_delete_note(text))
whole_outputs.append(get_show_movie(text))
whole_outputs.append(get_show_trailer(text))
whole_outputs.append(get_recommend_movie(text))
whole_outputs.append(get_show_weather(text))
if len(tuple(sum(whole_outputs, []))) == 0:
return [("message",)]
return sum(whole_outputs, [])
def crop_intents(text):
for regex in regex_list:
match = re.compile(regex).search(text)
if match is not None:
text = re.sub(match.group(), '', text)
return text
# text = """
# set alarm morning alarm 10:30 and please, call 011 27 55 70 54. View contact Mo7amed 3zzat call
# M.Mostafa Elafasay. Try to send email to [email protected] [job offer] [You are totally rejected]
# and send message to Amr 3zzat [How are you?]. Call 011
#
# I'm in great mode, so let's know what is the day today and Save note [Great Moooode] [Yes].
# And finally, show all notes out there :) Set alarm 10:00
# set event 2017:8:14:17:32 [title] [description] [location]
# show all notes. And in the last, Send i've done with you to [email protected], it's necessary!!
# ANd delete kza note
# """
#
# print extract_intents(text)