You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
114 lines
2.7 KiB
Python
114 lines
2.7 KiB
Python
from ctparse import ctparse #Used for parsing time (parsetime), https://github.com/comtravo/ctparse
|
|
import parsedatetime #Used for parsing time (parsetime2), https://github.com/bear/parsedatetime
|
|
import HumanTime
|
|
import arrow
|
|
import natural_time
|
|
|
|
from datetime import datetime
|
|
import time
|
|
|
|
# if __name__ == '__main__':
|
|
from regexTimeParser import RegexTimeParser
|
|
# else:
|
|
# from skills.regexTimeParser import RegexTimeParser
|
|
|
|
"""
|
|
Reading Material:
|
|
|
|
https://github.com/nltk/nltk_contrib/blob/95d1806e2f4e89e960b76a685b1fba2eaa7d5142/nltk_contrib/timex.py#L29
|
|
This has a bunch of regex expressions for NLP of time, might be able to modify this + parsedatetime + ctparse into my
|
|
own custom implementation that does everything I want. Maybe if it works well enough I can post it to pypi as its own
|
|
module and github for contributions.
|
|
|
|
"""
|
|
|
|
|
|
def parsetime(phrase):
|
|
"""
|
|
Takes in natrual language time phrase, outputs datetime object
|
|
"""
|
|
|
|
ts = datetime.now()
|
|
p = ctparse(phrase, ts=ts)
|
|
# print(p)
|
|
if p is not None:
|
|
return p.resolution.dt
|
|
return p
|
|
|
|
# return ctparse(phrase, ts=ts)
|
|
|
|
|
|
def parsetime2(phrase):
|
|
"""
|
|
Takes in natrual language time phrase, outputs datetime object
|
|
Handles seconds better
|
|
Doesnt handle 'in the afternoon'
|
|
Does handle 'this afternoon'
|
|
"""
|
|
|
|
time_struct, parse_status = parsedatetime.Calendar().parse(phrase)
|
|
# print(time_struct, parse_status)
|
|
return datetime(*time_struct[:6])
|
|
|
|
|
|
def parsetime3(phrase):
|
|
return HumanTime.parseTime(phrase)
|
|
|
|
def parsetime4(phrase):
|
|
return arrow.utcnow().dehumanize(phrase)
|
|
|
|
def parsetime5(phrase):
|
|
return natural_time.natural_time(phrase)
|
|
|
|
|
|
time_parser = RegexTimeParser()
|
|
def parsetime6(phrase):
|
|
return time_parser.parse_time(phrase)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
# t = parsetime('May 5th in the afternoon')
|
|
# print(t)
|
|
|
|
# t55 = parsetime('set an alarm for 2:30 in the afternoon')
|
|
# print(t55)
|
|
|
|
# t66 = parsetime('147 in the afternoon')
|
|
# print(t66)
|
|
# t67 = parsetime2('147 in the afternoon')
|
|
# print(t67)
|
|
t68 = parsetime6('147 afternoon')
|
|
print(t68)
|
|
t88 = parsetime6('1 in the afternoon')
|
|
print(t88)
|
|
t89 = parsetime6('thursday at 9 pm')
|
|
print(t89)
|
|
# t89 = parsetime5('147 in the afternoon')
|
|
# print(t89)
|
|
# t90 = parsetime5('147 after noon')
|
|
# print(t90)
|
|
# print(time.mktime(t67.timetuple()))
|
|
|
|
# t5 = parsetime('in 5 minutes 30 seconds')
|
|
# print(t5)
|
|
|
|
# t2 = parsetime('15 seconds')
|
|
# # print(t2)
|
|
# # print(t2)
|
|
# if t2 is not None:
|
|
# print(t2.resolution)
|
|
|
|
|
|
# t2 = parsetime2('now')
|
|
# print(time.mktime(t2.timetuple()))
|
|
|
|
# t3 = parsetime2('in 5 minutes 30 seconds')
|
|
# print(time.mktime(t3.timetuple()))
|
|
|
|
# t4 = parsetime2('4 in the afternoon')
|
|
# print(time.mktime(t4.timetuple()))
|
|
# print(t4)
|
|
|
|
# print(time.strftime("%H:%M:%S", t3.timetuple()))
|
|
# for x in t:
|
|
# print(x) |