Repository navigation
Keep full stops after month and weekday names in search_dates - #1485
AdrianAtZyte wants to merge 4 commits into
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #1485 +/- ##
=======================================
Coverage 98.94% 98.95%
=======================================
Files 239 239
Lines 3521 3530 +9
=======================================
+ Hits 3484 3493 +9
Misses 37 37 ☔ 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! This fixes the abbreviations nicely. The problem is that the list also contains the full names, so a sentence that just ends with a weekday or month is no longer split from the next one:
from datetime import datetime
from dateparser.search import search_dates
rb = {"RELATIVE_BASE": datetime(2020, 6, 15, 12)}
search_dates("See you on Monday. 5 people will come.", languages=["en"], settings=rb)
# master: [('on Monday', datetime(2020, 6, 15, 0, 0))]
# PR: [('on Monday. 5', datetime(2020, 5, 15, 0, 0))]
search_dates("The event is in May. 12 people attended.", languages=["en"], settings=rb)
# master: [('in May', datetime(2020, 5, 15, 0, 0))]
# PR: [('in May. 12', datetime(2020, 5, 12, 0, 0))]
search_dates("We met on Friday. Yesterday we left.", languages=["en"], settings=rb)
# master: [..., ('on Friday', datetime(2020, 6, 12, 0, 0)), ('Yesterday', datetime(2020, 6, 14, 12, 0)), ...]
# PR: [..., ('on Friday', datetime(2020, 6, 12, 0, 0))]Restricting it to the abbreviated forms fixed these for me while keeping all your test cases passing: for each month/weekday key, only use the names that are shorter than the longest one (keys with a single name, like "may", are skipped):
names = []
for key in KNOWN_WORD_TOKENS[: KNOWN_WORD_TOKENS.index("december") + 1]:
key_names = self.info.get(key, [])
if len(key_names) > 1:
longest = max(key_names, key=len)
names.extend(n for n in key_names if len(n) < len(longest))Abbreviations that are also ordinary words ("sun", "sat") can still glue sentences ("We sat in the sun. 3 days later we left." now returns nothing), but that is much rarer. Could you add one of the examples above as a test? Also, since _sentence_split runs for every candidate locale during detection, caching the compiled pattern per locale would avoid a slowdown I measured with languages=None (~1.3x).
Resolves #843