Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
* fix MycroftAI/issues/20

* last/past month

* more tests

* more tests

authored-by: jarbasai <[email protected]>
  • Loading branch information
NeonJarbas authored Apr 11, 2022
1 parent 500f8e0 commit 3fa67da
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
26 changes: 24 additions & 2 deletions lingua_franca/lang/parse_en.py
Original file line number Diff line number Diff line change
Expand Up @@ -707,6 +707,7 @@ def date_found():
'sept', 'oct', 'nov', 'dec']
year_multiples = ["decade", "century", "millennium"]
day_multiples = ["weeks", "months", "years"]
past_markers = ["was", "last", "past"]

words = clean_string(text)

Expand Down Expand Up @@ -895,12 +896,25 @@ def date_found():
hasYear = True
else:
hasYear = False

# if no date indicators found, it may not be the month of May
# may "i/we" ...
# "... may be"
elif word == 'may' and wordNext in ['i', 'we', 'be']:
datestr = ""
# when was MONTH
elif not hasYear and wordPrev in past_markers:
if anchorDate.month > m:
datestr += f" {anchorDate.year}"
else:
datestr += f" {anchorDate.year - 1}"
hasYear = True
# when is MONTH
elif not hasYear:
if anchorDate.month > m:
datestr += f" {anchorDate.year + 1}"
else:
datestr += f" {anchorDate.year}"
hasYear = True

# parse 5 days from tomorrow, 10 weeks from next thursday,
# 2 months from July
Expand Down Expand Up @@ -1364,7 +1378,15 @@ def date_found():
temp = datetime.strptime(datestr, "%B %d")
except ValueError:
# Try again, allowing the year
temp = datetime.strptime(datestr, "%B %d %Y")
try:
temp = datetime.strptime(datestr, "%B %d %Y")
except ValueError:
# Try again, without day
try:
temp = datetime.strptime(datestr, "%B %Y")
except ValueError:
# Try again, with only month
temp = datetime.strptime(datestr, "%B")
extractedDate = extractedDate.replace(hour=0, minute=0, second=0)
if not hasYear:
temp = temp.replace(year=extractedDate.year,
Expand Down
28 changes: 28 additions & 0 deletions test/unittests/test_parse_en.py
Original file line number Diff line number Diff line change
Expand Up @@ -750,6 +750,34 @@ def test_extract_date_years(self):
self.assertEqual(extract_datetime('in 2007', date)[0],
datetime(2007, 6, 27, tzinfo=date.tzinfo))

def test_extract_ambiguous_month_en(self):
dec = datetime(2017, 12, 27, 8, 1, 2)
jun = datetime(2017, 6, 27, 20, 1, 2)
self.assertEqual(
extract_datetime('when is september', jun)[0],
datetime(2017, 9, 1, tzinfo=default_timezone()))
self.assertEqual(
extract_datetime('when was september', dec)[0],
datetime(2017, 9, 1, tzinfo=default_timezone()))
self.assertEqual(
extract_datetime('when was september', jun)[0],
datetime(2016, 9, 1, tzinfo=default_timezone()))
self.assertEqual(
extract_datetime('when is september', dec)[0],
datetime(2018, 9, 1, tzinfo=default_timezone()))
self.assertEqual(
extract_datetime('i did the thing last september', jun)[0],
datetime(2016, 9, 1, tzinfo=default_timezone()))
self.assertEqual(
extract_datetime('past september the thing was done', dec)[0],
datetime(2017, 9, 1, tzinfo=default_timezone()))
self.assertEqual(
extract_datetime('i will do the thing in september', jun)[0],
datetime(2017, 9, 1, tzinfo=default_timezone()))
self.assertEqual(
extract_datetime('next september the thing will be done', dec)[0],
datetime(2018, 9, 1, tzinfo=default_timezone()))

def test_extract_ambiguous_time_en(self):
morning = datetime(2017, 6, 27, 8, 1, 2, tzinfo=default_timezone())
evening = datetime(2017, 6, 27, 20, 1, 2, tzinfo=default_timezone())
Expand Down

0 comments on commit 3fa67da

Please sign in to comment.