Skip to content

WIP: implement missing info - #729

Closed
noviluni wants to merge 1 commit into
scrapinghub:masterfrom
noviluni:add_missing_info
Closed

noviluni wants to merge 1 commit into
scrapinghub:masterfrom
noviluni:add_missing_info

Conversation

@noviluni

Copy link
Copy Markdown
Contributor

I did a proof of concept to add the missing parts ("year", "month" or "day") to the result of DateDataParser.get_date_data().

This could be a fix for #424, #553 and #700.

Of course tests are not passing, it's just a PoC to discuss it.


Precedents:

Sometimes there is a part of a date that it's missing: "22 may", "1994", etc. and there are people interested in knowing the missing part/s to be able to print / handle the result. Examples coming from the issues:

When doing:

>>> from dateparser import parse
>>> print(parse('2017'))
2017-06-18 00:00:00

to be able to print just:

2017

and when doing

>>> print(parse('2017 Jan'))
2017-01-18 00:00:00

to be able to print:

2017-01

Feasibility

As dateparser.parse() returns a datetime object, and it's not possible to return those parts with zero (example from the mentioned issues: datetime.datetime(2015, 12, 0, 0, 0)), we can't just do it.

However, as we can use directly the DateDataParser.get_date_data() method and it returns more information (locale, period, etc.), we could add another field with the missing parts (if applicable).

I added an extra field containing the missing field. We could probably simplify this, adding the missing field directly.

Code snippets with the implementation from this PR:

This:

from dateparser.date import DateDataParser
p = DateDataParser()

print(p.get_date_data("22 may"))

will print:

{'date_obj': datetime.datetime(2020, 5, 22, 0, 0), 'period': 'day', 'extra': {'missing': ['year']}, 'locale': 'en'}

And this:

print(p.get_date_data("1999"))

will print:

{'date_obj': datetime.datetime(1999, 6, 26, 0, 0), 'period': 'year', 'extra': {'missing': ['day', 'month']}, 'locale': 'en'}

With this, we could just do something like:

from dateparser.date import DateDataParser
p = DateDataParser()

def print_date_obj(result):
    missing = result.get('extra', {}).get('missing', [])
    if not missing:
        date_format = '%Y-%m-%d'
    elif 'day' and 'month' in missing:
        date_format = '%Y'
    elif 'day' in missing:
        date_format = '%Y-%m'
    elif 'year' in missing:
        date_format = '%m-%d'

   ...

    print(result.get('date_obj').strftime(date_format))


result = p.get_date_data("22 may")

if result['date_obj']:
    print_date_obj(result)

Feedback

This PR can't be merged as-is, we should work on the tests and add more scenarios. Apart from that, it should be merged after merging this: #715 (and we should fix the conflicts)

Let me know what you think about this approach and if you see any blocker.

@Gallaecio

Copy link
Copy Markdown
Contributor

I think having a function that returns all this data is necessary for some use cases.

My first instinct would be to return an object, though, instead of a dictionary. We could create a custom class (e.g. DateMatch, ParsedDate), and return an instance of it. That way, we have room for deprecation logic in the future, as well as lazy loading where needed.

@rmedaer

rmedaer commented Sep 4, 2020

Copy link
Copy Markdown

It might be also useful to add time as possible missing field:

        self.missing = [field for field in ('day', 'month', 'year', 'time')
                   if not getattr(self, field)]

So you know if the time is coming from parsed hour/minute. For instance, both 04/09/2020 12AM and 04/09/2020 return the same and we don't know if a time did match or not..

@noviluni noviluni mentioned this pull request Sep 7, 2020
@noviluni noviluni added this to the v1.0.0 milestone Sep 7, 2020
@noviluni

noviluni commented Sep 8, 2020

Copy link
Copy Markdown
Contributor Author

I added the DateData class (#778), so I will wait until it's merged before proceeding with this

@noviluni noviluni modified the milestones: v1.0.0, 1.1.0 Sep 28, 2020
@rezaghazeli

Copy link
Copy Markdown

Hi looks like (#778) got merged, is there any chance to have this change merged?

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.

4 participants