Skip to content

Fix incomplete dates when STRICT_PARSING is True - #515

Closed
marcosmadr wants to merge 9 commits into
scrapinghub:masterfrom
marcosmadr:ribeiro-fix-complete-dates
Closed

marcosmadr wants to merge 9 commits into
scrapinghub:masterfrom
marcosmadr:ribeiro-fix-complete-dates

Conversation

@marcosmadr

Copy link
Copy Markdown

Add check to only try to parse complete dates if the date string has at least 6 digits.
Fix cases when it's impossible to guess the date
Ex:
Date: 11117
Possible dates: 11/01/17 or 01/11/17 or 11/11/07

So I suggest a minimum length of 6 to parse complete dates.

Closes #356

@codecov

codecov Bot commented Apr 9, 2019 •

Copy link
Copy Markdown

Codecov Report

Merging #515 into master will decrease coverage by 0.07%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #515      +/-   ##
==========================================
- Coverage   95.01%   94.94%   -0.08%     
==========================================
  Files         302      302              
  Lines        2466     2471       +5     
==========================================
+ Hits         2343     2346       +3     
- Misses        123      125       +2
Impacted Files Coverage Δ
dateparser/parser.py 97.51% <100%> (-0.53%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 6322545...fb4b83d. Read the comment docs.

@Gallaecio

Copy link
Copy Markdown
Contributor

Looking at the removed tests, I think we would be removing support for some valid use cases.

I wonder if, with a more convoluted solution, we could drop support for unclear scenarios while maintaining support for other cases where less than 6 digits are used but there is only 1 possible date or 1 of the possible dates is the obvious choice.

@marcosmadr

marcosmadr commented Apr 21, 2019 •

Copy link
Copy Markdown
Author

Hi @Gallaecio , I investigated a bit more to find an alternative approach and I have a suggestion.

The thing is that _no_spaces_parser class tries so many date formats that it almost always get an complete date, even if it doesnt look like correct.

My best guess would be to limit _no_spaces_parser to parse only the given DATE_ORDER, currently it priorizes the order given but it tries all other combinations. For example:

dp.parse('2011', settings={'STRICT_PARSING': True, 'DATE_ORDER': 'DMY'})
values tried = ['%d%m%Y', '%d%m%y', '%d%m%Y%H%M%S.%f', '%d%m%Y%H%M%S', '%d%m%Y%H%M', '%d%m%Y%H', '%d%m%y%H%M%S.%f', '%d%m%y%H%M%S', '%d%m%y%H%M', '%d%m%y%H', '%Y%m%d', '%Y%d%m', '%m%Y%d', '%m%d%Y', '%d%Y%m', '%y%m%d', '%y%d%m', '%m%y%d', '%m%d%y', '%d%y%m', '%Y%m%d%H%M%S.%f', '%Y%m%d%H%M%S', '%Y%m%d%H%M', '%Y%m%d%H', '%Y%d%m%H%M%S.%f', '%Y%d%m%H%M%S', '%Y%d%m%H%M', '%Y%d%m%H', '%m%Y%d%H%M%S.%f', '%m%Y%d%H%M%S', '%m%Y%d%H%M', '%m%Y%d%H', '%m%d%Y%H%M%S.%f', '%m%d%Y%H%M%S', '%m%d%Y%H%M', '%m%d%Y%H', '%d%Y%m%H%M%S.%f', '%d%Y%m%H%M%S', '%d%Y%m%H%M', '%d%Y%m%H', '%y%m%d%H%M%S.%f', '%y%m%d%H%M%S', '%y%m%d%H%M', '%y%m%d%H', '%y%d%m%H%M%S.%f', '%y%d%m%H%M%S', '%y%d%m%H%M', '%y%d%m%H', '%m%y%d%H%M%S.%f', '%m%y%d%H%M%S', '%m%y%d%H%M', '%m%y%d%H', '%m%d%y%H%M%S.%f', '%m%d%y%H%M%S', '%m%d%y%H%M', '%m%d%y%H', '%d%y%m%H%M%S.%f', '%d%y%m%H%M%S', '%d%y%m%H%M', '%d%y%m%H', '%H%M%S.%f', '%H%M%S', '%H%M', '%H']

result = datetime.datetime(2020, 1, 1, 0, 0)

IMHO, if DATE_ORDER is specified, it should only try with the given order. For example, the command above would try:

dp.parse('2011', settings={'STRICT_PARSING': True, 'DATE_ORDER': 'DMY'})
values tried: ['%d%m%Y', '%d%m%y', '%d%m%Y%H%M%S.%f', '%d%m%Y%H%M%S', '%d%m%Y%H%M', '%d%m%Y%H', '%d%m%y%H%M%S.%f', '%d%m%y%H%M%S', '%d%m%y%H%M', '%d%m%y%H']

result = None

This would fix STRICT_PARSING bug if DATE_ORDER is specified, and I'd go for using the default order if STRICT_PARSING = True and no DATE_ORDER is specified.

@Gallaecio

Copy link
Copy Markdown
Contributor

Sounds good to me.

@codecov-io

Copy link
Copy Markdown

Codecov Report

Merging #515 into master will decrease coverage by 0.07%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #515      +/-   ##
==========================================
- Coverage   95.01%   94.94%   -0.08%     
==========================================
  Files         302      302              
  Lines        2466     2471       +5     
==========================================
+ Hits         2343     2346       +3     
- Misses        123      125       +2
Impacted Files Coverage Δ
dateparser/parser.py 97.51% <100%> (-0.53%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 6322545...e0215f5. Read the comment docs.

@codecov-io

codecov-io commented May 2, 2019 •

Copy link
Copy Markdown

Codecov Report

Merging #515 into master will increase coverage by 0.01%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #515      +/-   ##
==========================================
+ Coverage   95.01%   95.02%   +0.01%     
==========================================
  Files         302      302              
  Lines        2466     2471       +5     
==========================================
+ Hits         2343     2348       +5     
  Misses        123      123
Impacted Files Coverage Δ
dateparser/parser.py 98.06% <100%> (+0.02%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 6322545...b3325d9. Read the comment docs.

@marcosmadr marcosmadr changed the title Add minimum length to parse complete dates Fix incomplete dates when STRICT_PARSING is True May 2, 2019
@noviluni noviluni added this to the v1.0.0 milestone Jun 23, 2020
@noviluni

noviluni commented Jun 24, 2020 •

Copy link
Copy Markdown
Contributor

I was about to merge this, but after merging master it stops working... Could you check it?

I think that what is happening is that when trying with other locales that have another default order (i.e.: "zh" that has "YMD") it's parsed badly again:

>>> dateparser.parse('2017', settings={'STRICT_PARSING': True})
2020-01-07 00:00:00

>>> dateparser.parse('2017', settings={'STRICT_PARSING': True}, locales=['en'])
None

>>> dateparser.parse('2017', settings={'STRICT_PARSING': True}, locales=['zh'])
2020-01-07 00:00:00

@marcosmadr

Copy link
Copy Markdown
Author

I was about to merge this, but after merging master it stops working... Could you check it?

I think that what is happening is that when trying with other locales that have another default order (i.e.: "zh" that has "YMD") it's parsed badly again:

>>> dateparser.parse('2017', settings={'STRICT_PARSING': True})
2020-01-07 00:00:00

>>> dateparser.parse('2017', settings={'STRICT_PARSING': True}, locales=['en'])
None

>>> dateparser.parse('2017', settings={'STRICT_PARSING': True}, locales=['zh'])
2020-01-07 00:00:00

Yes, sure I'll check it

@marcosmadr

Copy link
Copy Markdown
Author

It seems that DATE_ORDER was fixed on master and now it updates according to the locates parameter.

For example, the result with ribeiro-fix-complete-dates branch:

>>> dateparser.parse('2017', settings={'STRICT_PARSING': True}, locales=['en'])
--> DATE_ORDER = %m%d%y
>>>
>>> dateparser.parse('2017', settings={'STRICT_PARSING': True}, locales=['zh'])
--> DATE_ORDER = %y%m%d
datetime.datetime(2020, 1, 7, 0, 0)

The strptime method allows months and days with no leading zero, so it can consider 2017 as datetime(2020, 1, 7, 0, 0) depending on DATE_ORDER value. There are some tests with 5 digits dates, so probably those 4 digits like 2017 should also be considered valid cases.

tests/test_parser.py

test_date_are_parsed_in_order_supplied
...
        param(
            date_string=u"18216",
            expected_date=datetime(2018, 2, 16),
            date_order='YMD',
            expected_period='day',
        ),


        param(
            date_string=u"14271",
            expected_date=datetime(2014, 1, 27),
            date_order='YDM',
            expected_period='day',
        ),


        param(
            date_string=u"21813",
            expected_date=datetime(2018, 2, 13),
            date_order='MYD',
            expected_period='day',
        ),

One of the issues on master is that it tries to parse non space dates with all possible date formats, including different date order, but prioritizing the date order provided as parameter. Fox example, if DATE_ORDER is set to'DMY, it will prioritize DMY dates, but will try also other combinations like MDY, YMD, etc... This might lead to undesired matches when the input is an incomplete date, as it tries all possible combinations of date formats, it almost always matches a date (even if its incorrect).

We can see on master, the same test with locales=['en'] parses 2017 as YMD even if DATE_ORDER = MDY

>>> dateparser.parse('2017', settings={'STRICT_PARSING': True}, locales=['en'])
--> DATE_ORDERr = '%m%d%y'
datetime.datetime(2020, 1, 7, 0, 0)

This PR implements a filter method that, when STRICT_PARSING is true, it filters the date formats attribute keeping only those that comply with DATE_ORDER. It helps with those overmatches, but does not solve all the cases, since there are cases that a 4 digit number could be a valid complete date.

Another option to handle cases like this could be a new parameter to define the minimum date length when STRICT_PARSING is True, in this case the user can change it according to their needs.

But yeah, by default I think probably it should keep parsing those 4 digits date. What do you think @noviluni ?

@noviluni noviluni removed this from the v1.0.0 milestone Sep 21, 2020
@Gallaecio

Gallaecio commented Nov 11, 2022 •

Copy link
Copy Markdown
Contributor

@serhii73 noticed #356 having been fixed might mean this issue is no longer relevant, and indeed looks like that to me.

>>> import dateparser
>>> dateparser.parse("2011", settings={"STRICT_PARSING": True})
>>> dateparser.parse("2017", settings={"STRICT_PARSING": True})
>>> 

@Gallaecio Gallaecio closed this Nov 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Dateparser parses years as a date when STRICT_PARSING = True

4 participants