Skip to content

Find a date written next to a relative one without a comma (#695) - #1385

Merged
serhii73 merged 19 commits into
masterfrom
fix/issue-695-search-dates-no-comma
Sep 16, 2026
Merged

serhii73 merged 19 commits into
masterfrom
fix/issue-695-search-dates-no-comma

Conversation

@serhii73

Copy link
Copy Markdown
Collaborator

Fixes #695.

>>> search_dates('Today 13 Feb 2020', languages=['en'], settings={'RELATIVE_BASE': datetime(2020, 2, 13, 20, 7, 6)})
None                     # master
>>> search_dates('Today, 13 Feb 2020', languages=['en'], settings={'RELATIVE_BASE': datetime(2020, 2, 13, 20, 7, 6)})
[('Today', datetime(2020, 2, 13, 20, 7, 6)), ('13 Feb 2020', datetime(2020, 2, 13, 0, 0))]

Why the comma mattered

A chunk that does not parse as one date is split by split_if_not_parsed, which looks for a
separator that the translation and the text it was translated from hold the same number of times.
"today" is translated into "0 day ago", so the translation gains two spaces the text never had:

translated: '0 day ago 13 february 2020'   (5 spaces)
original:   'Today 13 Feb 2020'            (3 spaces)

No splitter lines up, so the chunk is dropped and nothing is returned. The comma form works
because the comma survives the translation one for one.

The fix

Cut the chunk where the relative expression ends instead. split_by_relative_expression looks for
a translated expression (TRANSLATED_RELATIVE_REG, built from freshness_date_parser._UNITS so it
matches the units the translation actually emits and nothing else) and takes the cut only when all
four of these hold:

  1. the expression sits at the start or the end of the chunk β€” anywhere else leaves no boundary;
  2. the rest of the translation keeps one word per remaining original word;
  3. the word at that end really is the one the expression was translated from β€” the locale is asked
    to confirm it by translating that word on its own;
  4. what is left of the chunk is a date.

A chunk that fails any of them is left alone rather than cut at a guess. That is why
'Today 13 Feb 2020 and 14 Mar 2021' still finds nothing: the translation drops the "and", so
(2) fails and there is no honest boundary.

The cut is only looked for when no separator lined up, so a chunk that splits today keeps
splitting exactly the way it does now.

Evidence

24236 passed, 19 skipped, 1 xfailed; ruff and ruff-format clean; every new line and branch is
covered, leaving dateparser/search/search.py at the same 97% and the same four missed statements
as master.

Differential run against master over 17,648 inputs (relative expressions Γ— dates Γ— connectors Γ—
filler words in 24 locales in both orders, multi-word and numeric relative phrases, two relative
tokens in one chunk, multi-language calls, two-sentence chaining, plus every string mined from
tests/):

identical 17,164
gained (master returned None) 351
additive (all of master's tuples kept, more added) 133
changed or lost 0
new exceptions 0

Deliberately not fixed

  • Month and Month Year (e.g. April and May 2019) will return NoneΒ #507 ('April and May 2019') and Dates not getting recognised or wrongly recognised if there are some numbers in the textΒ #787 ("At 30 September 2018 E'000") return None on
    this branch exactly as on master. The mirror bug β€” a skip word translated to nothing β€” needs
    real token alignment plumbed out of Locale.translate_search, which is a much larger change.
  • 'Today 27 Feb 2020 at 5pm' and 'Today 13 Feb 2020 and 14 Mar 2021' stay None: the
    translation drops the "at" / "and", so the chunk cannot be cut honestly.
  • Where a chunk can be cut, the result now matches what search_dates already returns for the
    same text written with a comma β€” including cases where that answer is itself imperfect. Making
    the no-separator form behave like the separator form is the point of the issue.

πŸ€– Generated with Claude Code

serhii73 and others added 19 commits June 22, 2026 11:16
- Add month locative/dative forms (lednu, ΓΊnoru, bΕ™eznu, dubnu, kvΔ›tnu,
  červnu, červenci, srpnu, Ε™Γ­jnu, prosinci) for dates like "v lednu 2023"
- Add July abbreviation "črv"
- Fix "za mΔ›sΓ­c/tΓ½den/rok" not parsing: add accusative simplification
  patterns (previous patterns only matched instrumental -em forms)
- Add tests covering the new expressions

Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
search_dates('Today 13 Feb 2020') found no dates at all, while
'Today, 13 Feb 2020' found both. A chunk that does not parse as one date
is split by looking for a separator that the translation and the text it
was translated from hold the same number of times, and "today" is
translated into "0 day ago": the translation gains two spaces the text
never had, so the whitespace splitter is refused and the whole chunk is
dropped. The comma form works because the comma survives the translation
one for one.

Cut such a chunk where the relative expression ends instead. The cut is
taken only when the expression sits at one end of the chunk, when the
rest of the translation keeps one word per remaining original word, when
the word at that end really is the one the expression was translated
from -- which the locale is asked to confirm by translating it on its
own -- and when what is left of the chunk is a date. A chunk that fails
any of these is left alone rather than cut at a guess, which is why
'Today 13 Feb 2020 and 14 Mar 2021', whose "and" the translation drops,
still finds nothing.

The cut is only looked for when no separator lined up, so a chunk that
splits today keeps splitting exactly the way it does now.

Fixes #695

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@codspeed

codspeed Bot commented Sep 16, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

βœ… 7 untouched benchmarks


Comparing fix/issue-695-search-dates-no-comma (ac0c5ad) with master (a131f78)

Open in CodSpeed

@codecov

codecov Bot commented Sep 16, 2026 •

Copy link
Copy Markdown

Codecov Report

βœ… All modified and coverable lines are covered by tests.
βœ… Project coverage is 97.20%. Comparing base (a131f78) to head (ac0c5ad).

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1385      +/-   ##
==========================================
+ Coverage   97.18%   97.20%   +0.02%     
==========================================
  Files         236      236              
  Lines        3057     3081      +24     
==========================================
+ Hits         2971     2995      +24     
  Misses         86       86              

β˜” 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.

@serhii73
serhii73 merged commit 5e77cac into master Sep 16, 2026
21 checks passed
@serhii73
serhii73 deleted the fix/issue-695-search-dates-no-comma branch September 16, 2026 08:12
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.

Search dates failing to work due to lack of commas?

2 participants