-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
datetime #53
base: master
Are you sure you want to change the base?
datetime #53
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# Date Time Package | ||
|
||
The Date Time package contains multiple functions to deal with date and time. | ||
|
||
## Modules | ||
|
||
### Guess Date | ||
|
||
ask for future, current or past date | ||
|
||
#### Usage | ||
|
||
``` | ||
(en-US) "What day is it?" | ||
(en-US) "What day is today?" | ||
(en-US) "What day is tomorrow?" | ||
(en-US) "What day was yesterday?" | ||
(en-US) "What day it will be 2 days from tomorrow?" | ||
(en-US) "What day it will be 5 days from yesterday?" | ||
... | ||
``` | ||
|
||
### Say Current Time | ||
|
||
ask for current time | ||
|
||
#### Usage | ||
|
||
``` | ||
(en-US) "Could you tell me the time, please?" | ||
(en-US) "can I get the time?" | ||
(en-US) "Do you have the time?" | ||
(en-US) "Do you know what time it is?" | ||
(en-US) "Have you got time?" | ||
(en-US) "Have you got the time on you?" | ||
(en-US) "may I know what time it is right now?" | ||
(en-US) "What time do you have?" | ||
(en-US) "What time do you make it?" | ||
(en-US) "What time is it now?" | ||
(en-US) "what time is it, please?" | ||
(en-US) "What time is it?" | ||
(en-US) "What’s the time?" | ||
(en-US) "What time have we got?" | ||
(en-US) "You got the time?" | ||
... | ||
``` | ||
|
||
## Todo | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should not use "Todo" inside markdown files, instead it would be GitHub issues or Trello Card. What do you think? |
||
|
||
- translate to as many languages as possible | ||
- answer date/time for different timezone |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{ | ||
"saycurrenttime": { | ||
"time": [ | ||
"It's %time%." | ||
], | ||
"error": [ | ||
"Couldn't get the current time, maybe you should try again.", | ||
"I am having a hard time getting the current time, try again..." | ||
] | ||
}, | ||
"guessdate": { | ||
"guess": [ | ||
"%guess%." | ||
], | ||
"error": [ | ||
"Couldn't guess the date, maybe you should try again.", | ||
"I am having a hard time guessing the date, try again..." | ||
] | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
{ | ||
"saycurrenttime": [ | ||
"Could you tell me the time, please?", | ||
"can I get the time?", | ||
"Do you have the time?", | ||
"Do you know what time it is?", | ||
"Have you got time?", | ||
"Have you got the time on you?", | ||
"may I know what time it is right now?", | ||
"What time do you have?", | ||
"What time do you make it?", | ||
"What time is it now?", | ||
"what time is it, please?", | ||
"What time is it?", | ||
"What’s the time?", | ||
"What time have we got?", | ||
"You got the time?" | ||
] | ||
,"guessdate": [ | ||
"What day was yesterday", | ||
"What day is it?", | ||
"What day is it today?", | ||
"What’s the date today?", | ||
"What date is it?", | ||
"What’s today’s date?" | ||
] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#!/usr/bin/env python | ||
# -*- coding:utf-8 -*- | ||
|
||
import requests | ||
import utils | ||
import datetime | ||
import parsedatetime | ||
# import dateparser | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why there is this comment ? |
||
from random import randint | ||
|
||
def guessdate(word): | ||
|
||
word = word.lstrip() | ||
if word.startswith('the'): | ||
word = word.replace('the', 'one', 1) | ||
|
||
cal = parsedatetime.Calendar() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should avoid abbreviations for variable names. So instead of |
||
time_struct, parse_status = cal.parse(word) | ||
guess = datetime.datetime(*time_struct[:6]) | ||
day = guess.day | ||
time = datetime.datetime.time(guess) | ||
random = randint(0, 2) | ||
|
||
# https://stackoverflow.com/questions/9647202/ordinal-numbers-replacement | ||
suf = lambda n: "%d%s"%(n,{1:"st",2:"nd",3:"rd"}.get(n if n<20 else n%10,"th")) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is quite hard to understand, what this is trying to do, maybe we could refactor this in multiple functions/method or with variables etc. |
||
|
||
if random == 0: | ||
return utils.output('end', 'guess', utils.translate('guess', { 'guess': "The " + suf(day) + ' of ' + '{:%B}'.format(guess) + ', {:%Y}'.format(guess)})) | ||
elif random == 1: | ||
return utils.output('end', 'guess', utils.translate('guess', { 'guess': '{:%B}'.format(guess) + ' the ' + suf(day) + ', {:%Y}'.format(guess)})) | ||
else: | ||
return utils.output('end', 'guess', utils.translate('guess', { 'guess': suf(day) + ' of ' '{:%B}'.format(guess) + ', {:%Y}'.format(guess)})) | ||
|
||
|
||
#trying to use dateparser lib | ||
# return utils.output('end', 'guess', utils.translate('guess', { 'guess': dateparser.parse(word) })) | ||
Comment on lines
+35
to
+36
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should avoid useless comments. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
#!/usr/bin/env python | ||
# -*- coding:utf-8 -*- | ||
|
||
import requests | ||
import utils | ||
import datetime | ||
from random import randint | ||
|
||
def saycurrenttime(string): | ||
|
||
# https://sukhbinder.wordpress.com/2013/12/29/time-in-words-with-python/ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess we don't need this link. |
||
time = datetime.datetime.now() | ||
|
||
if randint(0, 1) != 0: | ||
words = [ | ||
"one", "two", "three", "four", "five", "six", "seven", "eight","nine", | ||
"ten", "eleven", "twelve", "thirteen", "fourteen", "quarter", "sixteen", | ||
"seventeen", "eighteen", "nineteen", "twenty", "twenty one", | ||
"twenty two", "twenty three", "twenty four", "twenty five", | ||
"twenty six", "twenty seven", "twenty eight", "twenty nine", "half" | ||
] | ||
|
||
hrs = time.hour | ||
mins = time.minute | ||
msg = "" | ||
Comment on lines
+23
to
+25
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As I previously said, we should avoid abbreviations for variable names. |
||
|
||
if (hrs > 12): | ||
hrs = hrs-12 | ||
if (mins == 0): | ||
hr = words[hrs-1] | ||
msg = hr + " o'clock." | ||
elif (mins < 31): | ||
hr = words[hrs-1] | ||
mn = words[mins-1] | ||
msg = mn + " past " + hr + "." | ||
else: | ||
hr = words[hrs] | ||
mn = words[(60 - mins-1)] | ||
msg = mn + " to " + hr + "." | ||
return utils.output('end', 'time', utils.translate('time', { 'time': msg })) | ||
|
||
return utils.output('end', 'time', utils.translate('time', { 'time': 'exactly ' + '{:%H:%M:%S}'.format(time) })) | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
'use strict' | ||
|
||
describe('dateandtime:guessdate', async () => { | ||
test('detects valid date guess', async () => { | ||
global.nlu.brain.execute = jest.fn() | ||
global.nlu.process('what day is today?') | ||
|
||
const [obj] = global.nlu.brain.execute.mock.calls | ||
await global.brain.execute(obj[0]) | ||
|
||
expect(global.brain.finalOutput.code).toBe('guess') | ||
}) | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think, it is great to use "*" for the version number because things could break between releases, so it would be better to have an exact version number.