-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDate_parser.py
More file actions
65 lines (41 loc) · 2.24 KB
/
Date_parser.py
File metadata and controls
65 lines (41 loc) · 2.24 KB
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
############################## Identifying dates from a string ##############################
from natty import DateParser ##
#####################################################################
#######################This below function will parse a string and understand if any dates available or not .
def DateParsing(inp2):
dp = DateParser(inp2)
return dp.result()
##############Above function can handle many dates
## 1. test 1 : inp2= "i want to have yesterday "
test1=DateParsing(inp2)
## result1: [datetime.datetime(2018, 3, 14, 17, 6, 2)]
## 2. test2 : inp2 = "i want to have yesterday and todays date "
test2= DateParsing(inp2)
#### result2 : [datetime.datetime(2018, 3, 14, 17, 7, 34)]
## 3. test3 : inp2 ="i want to have day before yesterday date"
test3=DateParsing(inp2)
### result3 : [datetime.datetime(2018, 3, 13, 17, 8, 59)]
## 4. test4 :inp2 =="i want to have yesterday and tomorrow "
test4 =DateParsing(inp2)
## result4 : [datetime.datetime(2018, 3, 14, 17, 9, 56),
#datetime.datetime(2018, 3, 16, 17, 9, 56)]
#################################################################################################
################################Comparing with present date ###################################
##finding dates through out the user intent
dates_from_user_intent=DateParsing(inp2)[0].replace(hour=0,minute=0,second=0,microsecond=0) #### asked date
### I am comparing the asked date by the user with our current date , and let know , the date is a past date or
### future date .
###Todays date to compare with the asked date
import datetime
todays_date=datetime.datetime.now().replace(hour=0,minute=0,second=0,microsecond=0)## today's date
### This below function will decide the date asked by the user is past date or a future date .
def which_date(dates_from_intent,todays_date):
date_value=0
if (dates_from_intent<todays_date):
date_value="past_date"
elif(dates_from_intent<todays_date ) :
date_value="future_date"
elif(dates_from_intent==todays_date ):
date_value="present_date"
return date_value
########################################################################################################