Repository navigation
Parse "midi" as noon in French (#459) - #1536
Conversation
French had no entry for "midi", so a date string with it was not
understood: "mardi midi", "mardi à midi" and "demain à midi" returned
None, and search_dates dropped the time of "demain à midi".
Add a simplification that reads "midi" as 12:00, like English does for
"noon". "après-midi" (the afternoon) and "avant midi" (before noon) are
not noon, so they are excluded, also with an article in between ("après
le midi") and with any run of non-word characters between the words. A
hyphen counts as a word boundary, so without the exclusion a plain
"midi" would match inside "après-midi", and "demain après-midi" would be
read as a time zone offset of -12:00.
"<weekday> prochain" is not covered: it needs support for "next
<weekday>", which master lacks even in English (#573). The examples of
the issue that need it are strict xfails.
Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #1536 +/- ##
=======================================
Coverage 98.96% 98.96%
=======================================
Files 239 239
Lines 3592 3592
=======================================
Hits 3555 3555
Misses 37 37 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
- The comment above the new tests gave a wrong reason for using a Wednesday: the seven weekdays resolve to seven different dates whatever the current date. - The parse() rows with a non-breaking hyphen and a no-break space could not tell \W+ from [\s-], because sanitize_date normalises both before the simplifications run. Only search_dates sees the raw text, so the rows that pin \W+ (an en dash and a non-breaking hyphen) are search_dates rows now. - Nothing pinned that "après" and "avant" only exclude the "midi" that directly follows, or that "12 midi" keeps its day: add "après-demain à midi", "avant-hier midi" and "le 12 midi". - Rows inserted in the middle of test_translation and test_search_and_parse renumbered 390 existing test IDs. The new rows are in new test methods instead, so no existing test ID changes. - The strict xfails only tolerate an AssertionError, and say that the French entries are needed besides the support for "next <weekday>". Also move the rule to the end of the simplifications of fr.yaml. Their order does not matter for it, and this keeps it away from the place where the open #1441 inserts its own rules. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Two cases slipped past the exclusion of "après" and "avant", both found by comparing master and the branch on large corpora: - search_dates always strips the accents of the text before it simplifies it, whatever NORMALIZE says, but with NORMALIZE off the rules keep their accents, so the "après" of the lookbehind never matched. "Réunion demain après-midi" lost its "demain" and "Livraison après midi" became noon. Write the rule as apr[eè]s, which the normalised rule set turns into apr[ee]s. - Every rule is wrapped in a boundary that counts "_" as a separator, so "après_midi" is a standalone "midi", but \W+ did not cross the "_" and it became noon. Use [\W_]+, like the boundary. Add the rows that fail without these changes, and rows for what the exclusion must not do: "après-demain à midi" and "avant-hier midi" (the existing relative expressions that contain the words it keys on), and "2026-10-12 midi", next to "le 12 midi" for the missing "12 " prefix. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Should we then not mark this PR as fixing the issue, just as being part of the solution? |
There was a problem hiding this comment.
Please, also cut the tests down: drop the
prochainxfails and the “unchanged” regression block, and keep a handful of positive and negative rows, similar to #1519. The description could be a lot shorter too.
(feel free to ignore it and merge as is, "too many tests" is not an issue I think we need to prioritize at the moment 🙂 )
|
This is approved and CI is green. The branch is 5 commits behind master, but I merged current master locally: no conflicts, and the full test suite passes (24844 passed). Could a maintainer merge it when convenient? On Adrian's question above: this PR only fixes the |
Fixes #459.
mardi midi,demain à midiand a baremidibehave the same way. This fixes themidipart of the issue.lundi prochain, and so the title examplelundi prochain à midi, still does not parse: see Not included.Root cause
French has no entry for
midi, so the word is unknown and any string with it is not a date. Insearch_datesthe time was silently lost:On se voit demain à midi.returneddemain àwithout the time (2000-01-02 00:00withRELATIVE_BASE=datetime(2000, 1, 1)). English getsnoonfrom a simplification inen.yaml,(?:12\s+)?noon: '12:00'.What changed
One simplification at the end of the list in
fr.yaml, andfr.pyregenerated withpython -m dateparser_scripts.write_complete_data. Before the edit the generator reproduces all 205 committed language files byte for byte, sofr.pyis the only generated file that changes.The lookbehind is the exclusion the issue asks for:
après-midiis the afternoon andavant midiis before noon, not noon. A hyphen counts as a non-word character for the automatic word boundary, so a plainmidimatches insideaprès-midi:après-midiwould become noon today anddemain après-midia date with a UTC−12 offset (the12:00read as a time zone). With the exclusion they returnNoneexactly as on master. The details:[\W_]+and not[\s-]:parse()normalises spaces and dashes before the simplifications run, butsearch_datessimplifies the raw text, where a[\s-]lookbehind letsmidithrough after two spaces, an en dash or a non-breaking hyphen._is in there because the boundary every rule is wrapped in counts_as a separator.apr[eè]sbecausesearch_datesstrips the accents of the text before simplifying it, whateverNORMALIZEsays, while withNORMALIZEoff the rules keep theirs. Withaprèsthe exclusion never matched there:Réunion demain après-midilost itsdemain.lecoversaprès le midi, which otherwise became noon today.12prefix of the English pattern is not copied:12 midiis not French, and the prefix would drop the day ofle 12 midi.midi, about 4 ms for 64,000.Not included
<weekday> prochainneedsnext <weekday>, which master does not support even in English (parse("next friday")isNone, dateparser not able to parse things like next tuesday. #573). Parse last, this and next before a weekday #1401 adds it for English, but landing it is not enough: French also needs the entries. On this branch plus Parse last, this and next before a weekday #1401 plus sevenrelative-typeentries infr.yaml(next monday: [lundi prochain]…next sunday: [dimanche prochain], withfr.pyregenerated),lundi prochainis 2026-10-12,mardi prochain2026-10-13 andlundi prochain à midi2026-10-12 12:00, with and withoutlanguages(same Wednesday as above). The three examples are in the tests as strict xfails: they stay xfail when Parse last, this and next before a weekday #1401 lands, and flip (strict, so the test fails and the marker has to go) once the French entries are added. Parse last, this and next before a weekday #1401 is the PR that covers dateparser not able to parse things like next tuesday. #573, so after it and this PR are merged nothing open tracks that French follow-up.lundi prochain à midistill givesNoneinparse(), andsearch_datesnow returns two unrelated results for it,lundi(the past Monday) andà midi(today at noon), where it returned onlylundi.minuit,ce midi,vers midiand similar are unchanged (None).Known limitations
Making
midia word has the side effectsnoonhas in English:search_datesreads a standaloneMidias noon, proper nouns included:Le Canal du Midi est beau.returnsMidiat 12:00,Le Midi Libre du 12 octobre 2026 titrait en une.gainsLe Midi, andRendez-vous à la gare de Bruxelles-Midi demainloses itsdemain.languages=None, a short date phrase in another language that contains the loanwordMIDIcan be detected as French:Ich habe am 5. Mai 2020 ein MIDI-Keyboard gekauft.is German on master and French here. WithIGNORE_SURROUNDING_TEXT,parse("Midi Libre 12 octobre 2026")goes from 2026-10-12 toNone.midiin the same sentence:search_dates(strategy="ngram")reads n-grams on their own, soRendez-vous cet après midireturnsmidiat 12:00 (the default strategy returnsNone), and so does a line break betweenaprèsandmidi.midi et demiandmidi 30are not understood as 12:30:parse("midi et demi")isNoneas before,search_datesreadsIl est midi et demi.as 12:00, andparse("midi 30")reads the 30 as a day (2026-10-30 12:00).Tests
Nothing existing was touched, and no existing test ID changes (new test methods only): 5 files, +223 −0.
test_languages.py:test_french_midi_translation, 3 rows (midi,Mardi à midi,12 octobre 2026 à midi).test_date_parser.py:test_french_midi_is_noon: 20 rows with a fixedRELATIVE_BASE: the two forms of the issue (mardi midi,mardi à midi), the other six weekdays,midi,à midi,demain à midi,hier midi,après-demain à midi,avant-hier midi, two dates (le 12 midi,2026-10-12 midi), upper case,PREFER_DATES_FROM: futureandNORMALIZE: False.test_french_midi_is_noon_when_french_is_detected: 2 rows withoutlanguages.test_french_midi_after_apres_or_avant_is_not_noon: 14 negatives (après-midi,après midi,l'après-midi,après le midi,avant midi,avant-midi,demain après-midi,lundi après-midi,mardi avant midi, upper case, three with_,NORMALIZE: False), all stillNone.test_french_dates_that_were_parsed_before_midi_are_unchanged: 8 regression rows (lundi,mardi,mardi à 12:00,mardi 14:30,demain,après-demain,il y a 3 jours,18 octobre 2012 à 19 h 21 min).test_french_next_weekday:lundi prochain,mardi prochainandlundi prochain à midias strict xfails (raises=AssertionError).test_search.py:test_search_dates_with_french_midi, 11 rows: two withmidi, and nine checking thataprès-midi(hyphen, en dash, non-breaking hyphen,_,NORMALIZE: False, withle) andavant midiare not read as noon.parse()normalises dashes and spaces first, so the rows that tell[\W_]+from[\s-]have to go throughsearch_dates.On master source 27 of the new tests fail, exactly the rows that expect noon; the 31 others (negatives and regression rows) pass there as controls. I also mutated the pattern twelve ways (no exclusion, only
après, onlyavant,[\s-],[\s-]+, no_, an accent-sensitiveaprès, no optionalle, an over-broad.*lookbehind, the English12prefix, two other replacements): each mutant is caught by at least one new test.Verification
Full suite, same environment: 24820 passed, 17 skipped, 4 xfailed with this change, against 24762 passed, 17 skipped, 1 xfailed on master. That is the 58 new passing tests plus the 3 xfails.
tests/test_dateparser_data_integrity.py, which regenerates every language in memory and compares it with the committed files, passes.A differential run of master against this branch (about 750,000
parse/search_dates/translatecalls per tree, 29 settings) found no change for strings withoutmidiand none for the other 459 locales.pre-commit run --all-filesis clean.I have read the contributing guide and set up pre-commit.
🤖 Generated with Claude Code