-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnertk.py
79 lines (65 loc) · 1.84 KB
/
nertk.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
import spacy
from spacy import displacy
from collections import Counter
import en_core_web_sm
import nltk
from nltk.tokenize import word_tokenize
from nltk.tag import pos_tag
from dateutil import parser
import datetime
import calendar
import json
nlp = en_core_web_sm.load()
today = datetime.date.today()
tomorrow = datetime.date.today() + datetime.timedelta(days=1)
nn = datetime.datetime.now()
def nltoschedule(sent):
doc = nlp(sent)
def preprocess(sent):
sent = nltk.word_tokenize(sent)
sent = nltk.pos_tag(sent)
return sent
time = ""
date = ""
fdate = ""
ftime = ""
minlen = []
color = ""
for sen in doc.ents:
if(sen.label_ == "CARDINAL"):
time = sen.text
minlen.append(sen.start_char)
if(sen.label_ == "DATE"):
date = sen.text
if(date.lower()=="today"):
date = today.strftime("%d %B %Y")
elif(date.lower()=="tomorrow"):
date = tomorrow.strftime("%d %B %Y")
minlen.append(sen.start_char)
for l in preprocess(sent):
if(l[1]=="NN" or l[1]=="JJ"):
color = l[0]
if(l[1]=="CD"):
if(":" in l[0] and time==""):
time = l[0]
if minlen!=[]:
newsent = sent[:min(minlen)]
else:
newsent = sent
prepos = preprocess(newsent)
prepos = prepos[::-1]
i = 0
l = []
for x in prepos:
if(i==0 and x[1]=='IN'):
continue
else:
l.append(x[0])
i+=1
l = ' '.join(l[::-1])
if(date+" "+time!=" "):
whole = parser.parse(date+" "+time)
fdate = str(whole.strftime("%d %a %b %Y"))
ftime = str(whole.strftime("%I:%M %p"))
return l,fdate,ftime,color
print(nltoschedule("Classes on Instrumental Science Today at 12:56 in blue"))