Skip to content

Commit

Permalink
Merge pull request #109 from ecatkins/strict_fix
Browse files Browse the repository at this point in the history
Improve strict=True
  • Loading branch information
akoumjian authored Jul 31, 2022
2 parents fcb70b8 + 8bb67e4 commit f2ab941
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
6 changes: 6 additions & 0 deletions datefinder/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ def extract_date_strings_inner(self, text, text_start=0, strict=False):
# digits_modifiers = captures.get('digits_modifiers')
# days = captures.get('days')
months = captures.get("months")
years = captures.get("years")
# timezones = captures.get('timezones')
# delimiters = captures.get('delimiters')
# time_periods = captures.get('time_periods')
Expand All @@ -190,6 +191,11 @@ def extract_date_strings_inner(self, text, text_start=0, strict=False):
len(digits) == 2
): # 19 February 2013 year 09:10
complete = True
elif (len(years)==1) and (len(digits)==2): #09/06/2018
complete = True

elif (len(years)==1) and (len(months)==1) and (len(digits)==1): # '19th day of May, 2015'
complete = True

if not complete:
continue
Expand Down
2 changes: 1 addition & 1 deletion tests/test_extract_date_strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def test_extract_date_strings(date_string, expected_match_date_string):
['the Friday after next Tuesday the 20th', ''], # no matches
['This Tuesday March 2015 in the evening', ''], # no matches
['They said it was on 01-03-2015', 'on 01-03-2015'], # 3 digits strict match
['May 20th 2015 is nowhere near the other date', 'May 20 2015'], # one month two digit match
['May 20 2015 is nowhere near the other date', 'May 20 2015'], # one month two digit match
])
def test_extract_date_strings_with_strict_option(date_string, expected_match_date_string):
"""
Expand Down
32 changes: 32 additions & 0 deletions tests/test_find_dates_strict.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import pytest
import datefinder
from datetime import datetime
import pytz
import sys
import logging
logging.basicConfig(level=logging.DEBUG, stream=sys.stdout)
logger = logging.getLogger(__name__)

today = datetime.today()


@pytest.mark.parametrize('input_text, expected_date', [
('June 2018', []),
('09/06/18', datetime(2018, 9, 6)),
('09/06/2018', datetime(2018, 9, 6)),
('recorded: 03/14/2008', datetime(2008, 3, 14)),
('19th day of May, 2015', datetime(2015, 5, 19)),
('19th day of May', [])
])
def test_find_date_strings_strict(input_text, expected_date):
if isinstance(expected_date,list):
matches = list(datefinder.find_dates(input_text, strict=True))
assert matches == expected_date

else:
return_date = None
for return_date in datefinder.find_dates(input_text, strict=True):
assert return_date == expected_date
assert return_date is not None, 'Did not find date for test line: "{}"'.format(input_text) # handles dates

0 comments on commit f2ab941

Please sign in to comment.