Skip to content
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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions bridges/python/Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ name = "pypi"
requests = "==2.21.0"
pytube = "==9.2.2"
tinydb = "==3.9.0"
parsedatetime = "*"
Copy link
Member

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.


[dev-packages]

Expand Down
48 changes: 35 additions & 13 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

51 changes: 51 additions & 0 deletions packages/dateandtime/README.md
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
Copy link
Member

Choose a reason for hiding this comment

The 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
Empty file.
Empty file.
Empty file.
Empty file.
20 changes: 20 additions & 0 deletions packages/dateandtime/data/answers/en.json
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..."
]
}
}
Empty file.
Empty file.
27 changes: 27 additions & 0 deletions packages/dateandtime/data/expressions/en.json
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?"
]
}
36 changes: 36 additions & 0 deletions packages/dateandtime/guessdate.py
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
Copy link
Member

Choose a reason for hiding this comment

The 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()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should avoid abbreviations for variable names. So instead of cal, we could call it calendar.

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"))
Copy link
Member

Choose a reason for hiding this comment

The 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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should avoid useless comments.

44 changes: 44 additions & 0 deletions packages/dateandtime/saycurrenttime.py
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/
Copy link
Member

Choose a reason for hiding this comment

The 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
Copy link
Member

Choose a reason for hiding this comment

The 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) }))


13 changes: 13 additions & 0 deletions packages/dateandtime/test/guessdate.spec.js
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')
})
})