chore: add README
fix: pad symbols in string feat: add `tomorrow` as a special keyword
This commit is contained in:
parent
bfb50a7bf5
commit
981d7eeda0
17
README.md
17
README.md
@ -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)
|
@ -40,17 +40,24 @@ direction = {
|
||||
'past': -1,
|
||||
}
|
||||
|
||||
transformations = dict(zip((x + 's' for x in multipliers.keys()), multipliers.keys()))
|
||||
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()))))
|
||||
symbols = ['!', ',', '@', '#', '$', '%', '^', '&', '*', '?']
|
||||
|
||||
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
|
||||
|
||||
text = '12 jan 2019 at 12 will be amazing'
|
||||
|
||||
def tokenize(string):
|
||||
pattern = re.compile(r'\b(' + '|'.join(transformations.keys()) + r')\b')
|
||||
string = pattern.sub(lambda x: transformations[x.group()], string)
|
||||
sym_pattern = re.compile(r'([!,@#$%^&*?\'"])')
|
||||
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(' ')
|
||||
n = len(words)
|
||||
|
@ -70,6 +70,7 @@ class HumanDateTests(unittest.TestCase):
|
||||
'this week': self.today,
|
||||
'this month': self.today,
|
||||
'this year': self.today,
|
||||
'tomorrow!': self.today + timedelta(days=1),
|
||||
}
|
||||
|
||||
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),
|
||||
'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),
|
||||
'an hour ago': ((self.now.hour - 1), self.now.minute, self.now.second),
|
||||
}
|
||||
|
||||
for (k, t) in tests.items():
|
||||
|
Loading…
Reference in New Issue
Block a user