chore: add README

fix: pad symbols in string
feat: add `tomorrow` as a special keyword
This commit is contained in:
Mahdi Dibaiee 2018-01-14 14:52:12 +03:30
parent bfb50a7bf5
commit 981d7eeda0
3 changed files with 31 additions and 5 deletions

View File

@ -0,0 +1,17 @@
humandate
---------
Parse human-readable dates in Python.
Usage
-----
```
from humandate import parse_date
print(parse_date('next month'))
print(parse_date('I was there an hour ago!'))
print(parse_date('Our event will be held at 12 tomorrow!'))
```
For more examples see [tests.py](humandate/tests.py)

View File

@ -40,17 +40,24 @@ direction = {
'past': -1, 'past': -1,
} }
transformations = dict(zip((x + 's' for x in multipliers.keys()), multipliers.keys())) symbols = ['!', ',', '@', '#', '$', '%', '^', '&', '*', '?']
transformations.update({ 'next': '1', 'upcoming': '1', 'following': '1', 'a': '1', 'an': '1' })
transformations.update(dict(zip(direction.keys(), (x + ' ' + '1' for x in direction.keys())))) transformations = [dict(zip((x + 's' for x in multipliers.keys()), multipliers.keys()))]
transformations.append(dict(zip(direction.keys(), (x + ' ' + '1' for x in direction.keys()))))
transformations.append({ 'tomorrow': 'next day' })
transformations.append({ 'next': '1', 'upcoming': '1', 'following': '1', 'a': '1', 'an': '1' })
keywords = list(multipliers.keys()) + months + months_short + years + days + days_short + list(direction.keys()) + [time_prefix] + time_postfix keywords = list(multipliers.keys()) + months + months_short + years + days + days_short + list(direction.keys()) + [time_prefix] + time_postfix
text = '12 jan 2019 at 12 will be amazing' text = '12 jan 2019 at 12 will be amazing'
def tokenize(string): def tokenize(string):
pattern = re.compile(r'\b(' + '|'.join(transformations.keys()) + r')\b') sym_pattern = re.compile(r'([!,@#$%^&*?\'"])')
string = pattern.sub(lambda x: transformations[x.group()], string) string = sym_pattern.sub(lambda x: ' {} '.format(x.group()), string)
for ts in transformations:
pattern = re.compile(r'\b(' + '|'.join(ts.keys()) + r')\b')
string = pattern.sub(lambda x: ts[x.group()], string)
words = string.lower().split(' ') words = string.lower().split(' ')
n = len(words) n = len(words)

View File

@ -70,6 +70,7 @@ class HumanDateTests(unittest.TestCase):
'this week': self.today, 'this week': self.today,
'this month': self.today, 'this month': self.today,
'this year': self.today, 'this year': self.today,
'tomorrow!': self.today + timedelta(days=1),
} }
for (k, date) in tests.items(): for (k, date) in tests.items():
@ -109,6 +110,7 @@ class HumanDateTests(unittest.TestCase):
'2 seconds ago': (self.now.hour, self.now.minute, self.now.second - 2), '2 seconds ago': (self.now.hour, self.now.minute, self.now.second - 2),
'past 3 minute': (self.now.hour, self.now.minute - 3, self.now.second), 'past 3 minute': (self.now.hour, self.now.minute - 3, self.now.second),
'last 10 hours': ((self.now.hour - 10) % 24, self.now.minute, self.now.second), 'last 10 hours': ((self.now.hour - 10) % 24, self.now.minute, self.now.second),
'an hour ago': ((self.now.hour - 1), self.now.minute, self.now.second),
} }
for (k, t) in tests.items(): for (k, t) in tests.items():