Skip to content

Keep full stops that belong to a date inside the date in search_dates - #1577

Merged
alexldide merged 3 commits into
masterfrom
keep-full-stops-inside-dates-in-search
Nov 17, 2026
Merged

alexldide merged 3 commits into
masterfrom
keep-full-stops-inside-dates-in-search

Conversation

@alexldide

@alexldide alexldide commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

search_dates() splits its input into sentences before looking for dates, and Locale._sentence_split treats every full stop as a sentence end. A date containing one is cut in half before any parser sees it, even though dateparser.parse() handles the string fine on its own:

input before after
"Jan. 5, 2020 was a holiday." split at Jan. ("Jan. 5, 2020", 2020-01-05)
"He left on 31 Jul. 1999 and came back." split at Jul. ("on 31 Jul. 1999", 1999-07-31)
"5 déc. 2020" (fr) split at 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_split already 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 Locale and cached rather than rebuilt on every call.

Separately, a\.m\.? / p\.m\.? simplifications are added 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.

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:

  1. Shorter full forms taken for abbreviations (Сейчас август., uk листопад, pl styczeń). The length heuristic is gone entirely, as described above.
  2. Short weekday names that are also ordinary words (en we/sat, de so, fr mer). Weekday names are no longer part of the rule at all. The cost is "Fri. Jan. 3, 2020", which now splits after Fri. and reports the weekday and the date separately.
  3. The day-number rule applying to every language. Dropped. It turned out to be unnecessary: master already parses "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 in tests/test_search.py.

Verification

  • All nine review cases now produce master's output exactly.
  • Full suite passes; the only failures are pre-existing ones local to Windows (3 negative-timestamp, 2 data-integrity encoding/CRLF). tests/test_date.py needs Unix-only time.tzset and does not collect there.
  • ruff check, ruff format --check and strict mypy are clean.
  • Coverage measured against a master worktree with the same command: 311 missed lines on both sides, zero per-file deltas.
  • Real-world corpus (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 and a.m./p.m. cases above.
  • The lookbehinds are variable-length and scan backwards from every full stop, so tests/test_sentence_split_linear_time.py covers 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_dates depends 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.


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.
@alexldide
alexldide force-pushed the keep-full-stops-inside-dates-in-search branch from 28c4acf to 8039232 Compare November 13, 2026 09:32
@codecov

codecov Bot commented Nov 13, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.24%. Comparing base (90544c0) to head (1546dd4).

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@codspeed

codspeed Bot commented Nov 13, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 11 untouched benchmarks


Comparing keep-full-stops-inside-dates-in-search (1546dd4) with master (90544c0)

Open in CodSpeed

@alexldide
alexldide requested review from AdrianAtZyte and serhii73 and removed request for AdrianAtZyte November 13, 2026 11:01

@serhii73 serhii73 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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) returns None, master finds 5 мая 2020 года; the same with uk листопад and pl styczeń.
  • 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 en sat, de so, fr mer).
  • 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), and Step 2. May the force be with you gives '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.

@alexldide

Copy link
Copy Markdown
Contributor Author

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) returns None, master finds 5 мая 2020 года; the same with uk листопад and pl styczeń.
  • 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 en sat, de so, fr mer).
  • 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), and Step 2. May the force be with you gives '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
number or year around the stop is what licenses keeping it), weekday names are
dropped entirely, and so is the day-number rule — master already handles
15. november 2020 in da/de via the existing (?<![0-9]) lookbehind, and it
isn't an English format.

All nine inputs match master exactly, with tests. Two notes: keying on what
follows alone doesn't fix (2), since we. 5 May 2020 is followed by a date;
and with the day-number rule gone the article_dates sweep now shows zero
changed rows vs master, so the 63 improvements I cited earlier went with it.

@alexldide
alexldide requested a review from serhii73 November 13, 2026 12:55

@serhii73 serhii73 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@alexldide
alexldide merged commit 9fbb186 into master Nov 17, 2026
22 checks passed
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.

3 participants