From 981d7eeda0072e6786a5cee7e48731a6ece24957 Mon Sep 17 00:00:00 2001 From: Mahdi Dibaiee Date: Sun, 14 Jan 2018 14:52:12 +0330 Subject: [PATCH] chore: add README fix: pad symbols in string feat: add `tomorrow` as a special keyword --- README.md | 17 +++++++++++++++++ humandate/index.py | 17 ++++++++++++----- humandate/tests.py | 2 ++ 3 files changed, 31 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index e69de29..96b1715 100644 --- a/README.md +++ b/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) diff --git a/humandate/index.py b/humandate/index.py index 894aa78..4ede3c3 100644 --- a/humandate/index.py +++ b/humandate/index.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) diff --git a/humandate/tests.py b/humandate/tests.py index e927b41..b1bb75a 100644 --- a/humandate/tests.py +++ b/humandate/tests.py @@ -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():