Support "last/next/this <weekday>" (fixes #573) - #1357
Conversation
bd74217 to
ee81dc6
Compare
This is what Opus is telling me, thoughts? |
Parse relative weekday expressions like "next friday", "last monday" and "this sunday", which previously returned None (scrapinghub#573, scrapinghub#1177, scrapinghub#635). Semantics use the nearest occurrence in the modifier's direction, which needs no notion of a calendar week or its start day: * "next <weekday>": first occurrence strictly after the base date. * "last <weekday>": most recent occurrence strictly before. * "this <weekday>": the coming occurrence, with the base's own weekday mapping to itself. A weekday modifier overrides PREFER_DATES_FROM for that string. On the base date's own weekday, "last"/"next" are exactly -/+7 days. The weekday relative phrases already exist in CLDR but were filtered out on import. get_cldr_data.py now maps each locale's phrase (e.g. es "el próximo martes", de "nächsten Dienstag") to a canonical English target ("next tuesday"), so the feature works across ~all locales rather than English only. Phrases that collide with a bare weekday name (nb "torsdag") or with an existing relative-type entry (sr "prošle nedelje" = "last week") are skipped. "this <weekday>" and the "past" synonym, which CLDR does not provide cleanly, are supplied for English via supplementary data. A modifier that is not immediately followed by a weekday is not treated as a date, so "the last three commits" and "last updated ..." do not yield false positives in search_dates. Doubled or trailing modifiers ("next next friday", "friday next") are rejected. Tests cover a 7 base-weekday x 7 target-weekday x {last,next,this} matrix, the base-equals-target case, PREFER_DATES_FROM override, malformed inputs, and search_dates positive/negative cases.
ee81dc6 to
b4552de
Compare
|
Can you review your own changes and let me know if you see any issues with them? |
"this friday" worked but "this fri" and "this f" returned None, while the CLDR-sourced "last"/"next" accepted their short and narrow forms. The supplementary "this <weekday>" entries only listed the full names. Add the short and narrow forms (mirroring CLDR's "last"/"next" output) so "this fri"/"this f" resolve like "next fri"/"next f". These behave exactly as the existing "next"/"last" abbreviations, including in search_dates.
|
Went back through it properly. One thing I think needs your call before it's mergeable, one thing I already fixed, and a few notes on stuff I checked so you don't have to re-verify. The one worth deciding on: Already fixed (last commit): Things I checked that look fine:
And one non-issue for completeness: |
|
Closing. I prefer that this complex a change has a human in the loop. |
Reworked per the design discussion in #573. Thanks to @AdrianAtZyte for the detailed review; this replaces the earlier adjacent-week approach.
What
Parse relative weekday expressions like
next friday,last monday,this sunday, which previously returnedNone. Fixes #573; also covers #1177 and #635.Semantics (nearest occurrence)
Agreed in #573. No calendar-week concept, no week-start dependency, no new setting:
next <weekday>: first occurrence strictly after the base date.last <weekday>: most recent occurrence strictly before the base date.this <weekday>: the coming occurrence, with the base's own weekday mapping to itself.A modifier overrides
PREFER_DATES_FROMfor that string. On the base date's own weekday,last/nextare exactly -/+7 days (so "last Wednesday"/"next Wednesday" said on a Wednesday are unambiguous).All locales, not English-only
The weekday relative phrases already exist in CLDR but were filtered out on import.
get_cldr_data.pynow maps each locale's phrase (el próximo martes,nächsten Dienstag,mardi prochain, ...) to a canonical English target (next tuesday), which the parser then resolves. This reuses the existingrelative-typetranslation mechanism.Collisions are handled:
torsdag= "Thursday" and "this Thursday") is skipped.prošle nedelje= "last week" and "last Sunday") is skipped so existing behavior wins.this <weekday>(identical to the bare weekday in many locales) and the Englishpastsynonym are supplied via supplementary data instead of CLDR.No search_dates false positives
A modifier is only a date component when immediately followed by a weekday, so
the last three commits,this April, andlast updated ...do not match. Doubled/trailing modifiers (next next friday,friday next) are rejected.Tests & docs
PREFER_DATES_FROMoverride, malformed-input rejection, andsearch_datespositive/negative cases.calendarsandlangdetectextras;pre-commitclean.get_cldr_data.py+write_complete_data.py(CLDR 44.1.0).Addressed from the review
PREFER_WEEKsetting removed._weekday_modifierinitialized in__init__; the bare-weekday branch is extracted into a helper rather than re-indented.