Repository navigation
Keep full stops that belong to a date inside the date in search_dates - #1577
Conversation
search_dates() splits its input into sentences before looking for dates, and
Locale._sentence_split treated every full stop as a sentence end. A date that
contains one was therefore cut in half before any parser saw it, even though
dateparser.parse() handles all of these strings on its own:
search_dates("15. november 2020") # lost the day
search_dates("10. jul. 2017")
search_dates("January 6, 2021 8:45 a.m.")
search_dates("Meeting at 8:45 p.m. sharp.")
Extend the existing abbreviation lookbehinds in _sentence_split so a full stop
does not end a sentence when it is part of a date:
- after an abbreviated month or weekday name ("Jan.", "Fri.", "déc."), derived
per locale from the locale's own translation data rather than hardcoded;
- after a one- or two-digit day number that is followed by a month name
("15. november"), which also keeps the year in "2014. July 13th" from being
mistaken for a day;
- inside "a.m." and "p.m.".
The splitter is now noticeably more expensive to build, so compile it once per
locale and cache it on the Locale instead of rebuilding it on every call.
Also add "a.m." / "p.m." simplifications to the English translation data,
ordered before the existing "a" -> "1" rule. Without them "a.m." was rewritten
to "1.m." (with "m" then read as the minute abbreviation), and "8:45 p.m." lost
its meridiem and parsed twelve hours off as 08:45.
Verified against the article-date corpus (dateparser_scripts/article_dates.py,
5000-row sample): no change on the parse() path, and 63 rows improved with no
regressions on the search_dates() path, across de/da/mk/sl/sk/et/is. The
lookbehinds are covered by a new linear-time test so they cannot regress into
quadratic behaviour on hostile input.
28c4acf to
8039232
Compare
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #1577 +/- ##
=======================================
Coverage 99.23% 99.24%
=======================================
Files 239 239
Lines 4203 4213 +10
=======================================
+ Hits 4171 4181 +10
Misses 32 32 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
serhii73
left a comment
There was a problem hiding this comment.
Thanks! Some regressions against master in search_dates:
- Full names that are shorter than another form of the same month or weekday are treated as abbreviations, so the sentence no longer splits:
Сейчас август. Завтра 5 мая 2020 года.(ru) returnsNone, master finds5 мая 2020 года; the same with ukлистопадand plstyczeń. - Short names that are also common words glue the previous word into the match:
So do we. 5 May 2020 is the date.returns'we. 5 May 2020'(also ensat, deso, frmer). - The day number + full stop + month rule applies to every language:
Chapter 5. November 2019 was cold.now gives 2019-11-05 (master: 2019-11-01), andStep 2. May the force be with yougives'2. May the'.
Keeping the full stop based on what follows it (a date token) rather than on the length of the name would avoid these.
Thanks — all three reproduced and fixed. The length heuristic is gone (a day All nine inputs match master exactly, with tests. Two notes: keying on what |
serhii73
left a comment
There was a problem hiding this comment.
Reviewed head 1546dd4. No urgent issue found, and CI is green. Non-blocking: the test for "Twenty five years after the Sept. 11, 2001, attacks" pins the output that its own comment says is not the date a reader would pick, so a later correct fix would fail that test.
search_dates()splits its input into sentences before looking for dates, andLocale._sentence_splittreats every full stop as a sentence end. A date containing one is cut in half before any parser sees it, even thoughdateparser.parse()handles the string fine on its own:"Jan. 5, 2020 was a holiday."Jan.("Jan. 5, 2020", 2020-01-05)"He left on 31 Jul. 1999 and came back."Jul.("on 31 Jul. 1999", 1999-07-31)"5 déc. 2020"(fr)déc.("5 déc. 2020", 2020-12-05)"10. jul. 2017"(de)("10. jul", …), ("2017", …)("10. jul. 2017", 2017-07-10)"January 6, 2021 8:45 a.m."("January 6", …), ("2021 8:45 a", …)("January 6, 2021 8:45 a.m", 2021-01-06 08:45)What changed
Two negative lookaheads join the lookbehind chain
_sentence_splitalready builds, so a full stop does not end a sentence when a month name and the rest of a date sit around it:<day> <month>. <year>—"31 Jul. 1999","5 déc. 2020", and"10. jul. 2017"in the locales where the day number already keeps its own full stop.<month>. <day>, <year>—"Jan. 5, 2020".Month names come from the locale's own translation data, so every language gets this. The rule deliberately does not try to decide which spellings are abbreviations: that is not recorded in the data and cannot be inferred from length, since a shorter spelling is as likely to be another full form (Russian
августbeside the genitiveавгуста). Every spelling is accepted instead, and the day number and year around the stop are what license keeping it. A month name that merely ends a sentence is untouched —"The event is in May. 12 people attended."still splits.Building the splitter is more expensive than before, so it is compiled once per
Localeand cached rather than rebuilt on every call.Separately,
a\.m\.?/p\.m\.?simplifications are added to the English translation data, ordered before the existinga→1rule. Without them"a.m."was rewritten to"1.m."(withmthen read as the minute abbreviation), and"8:45 p.m."lost its meridiem and parsed twelve hours off as08:45.This also unblocks #1518 (ignore punctuation in language detection). The lookbehind technique is from @AdrianAtZyte's #1485.
Changes since the first review
@AdrianAtZyte — thanks. All three regressions reproduced, and all three are fixed by changing the approach rather than patching around it:
Сейчас август., ukлистопад, plstyczeń). The length heuristic is gone entirely, as described above.we/sat, deso, frmer). Weekday names are no longer part of the rule at all. The cost is"Fri. Jan. 3, 2020", which now splits afterFri.and reports the weekday and the date separately."15. november 2020"and"10. juli 2017"correctly in da and de, through the existing(?<![0-9])lookbehind that is limited to["fi","cs","hu","de","da"]. Only English was broken, and"15. november 2020"is not an English date format. So"Chapter 5. November 2019 was cold."and"Step 2. May the force be with you"behave as on master again, and English gives up the two ticket cases that only ever worked by accident.Each of the nine inputs from the review is now a test — at the splitter level in
tests/test_languages.py, end to end intests/test_search.py.Verification
tests/test_date.pyneeds Unix-onlytime.tzsetand does not collect there.ruff check,ruff format --checkand strictmypyare clean.dateparser_scripts/article_dates.py, 5,000-row sample, 4,851 distinct rows) run on both sides: zero changed rows. Worth being plain about that — dropping the day-number rule also gave up the 63 corpus improvements an earlier version of this PR claimed. What remains does not appear in that corpus at all; its value is the abbreviated-month anda.m./p.m.cases above.tests/test_sentence_split_linear_time.pycovers each shape plus two near-misses.Performance
Sentence splitting in isolation is ~2× slower (5.5 ms vs 2.8 ms on 45 KB); the per-locale build is one-time and cached. End-to-end
search_datesdepends on the text, since keeping a date together leaves fewer fragments to parse — on the text above it comes out faster (267 ms vs 345 ms), so I would not claim a general figure in either direction.