Skip to content

Commit 0cd3857

Browse files
committed
[dateparser] Fix parsing very short weekday names
- improve the tokenization to handle suffixes like "th", "st", "nd", "rd" attached to numbers - remove remove_multiple_occurrences this method - fix tests to use the current year instead of 2025 - #1170
1 parent 1865917 commit 0cd3857

3 files changed

Lines changed: 15 additions & 17 deletions

File tree

dateparser/languages/dictionary.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,14 +201,25 @@ def _split_by_known_words(self, string: str, keep_formatting: bool):
201201
curr_split = (
202202
[known] if self._should_capture(known, keep_formatting) else []
203203
)
204+
204205
if unparsed and self._should_capture(unparsed, keep_formatting):
205206
curr_split = (
206207
self._split_by_numerals(unparsed, keep_formatting) + curr_split
207208
)
209+
208210
if unknown:
209211
string = unknown if string != unknown else ""
210212

211-
splitted.extend(curr_split)
213+
for token in curr_split:
214+
if (
215+
splitted
216+
and splitted[-1].isdigit()
217+
and token in {"st", "nd", "rd", "th"}
218+
):
219+
continue
220+
221+
splitted.append(token)
222+
212223
return splitted
213224

214225
def _split_by_numerals(self, string, keep_formatting):

dateparser/languages/locale.py

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -122,18 +122,6 @@ def weekdays(self):
122122
]
123123
return weekdays
124124

125-
def remove_multiple_occurrences(self, date_str_tokens: list):
126-
# first occurrence of day of the week will be considered
127-
# followings occurrence(s) will be skipped and removed from the token list.
128-
weekdays_counter = 0
129-
for i, token in enumerate(date_str_tokens):
130-
if token in self.weekdays:
131-
weekdays_counter += 1
132-
133-
if weekdays_counter > 1:
134-
date_str_tokens.pop(i)
135-
continue
136-
137125
def translate(self, date_string, keep_formatting=False, settings=None):
138126
"""
139127
Translate the date string to its English equivalent.
@@ -169,8 +157,6 @@ def translate(self, date_string, keep_formatting=False, settings=None):
169157
date_string_tokens[i] = dictionary[word] or fallback
170158
if "in" in date_string_tokens:
171159
date_string_tokens = self._clear_future_words(date_string_tokens)
172-
173-
self.remove_multiple_occurrences(date_string_tokens)
174160
return self._join(
175161
list(filter(bool, date_string_tokens)),
176162
separator="" if keep_formatting else " ",

tests/test_search.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1098,9 +1098,10 @@ def test_search_dates_with_prepositions(self):
10981098
add_detected_language=True,
10991099
languages=["ru"],
11001100
)
1101+
current_year = datetime.datetime.now().year
11011102
expected = [
1102-
("12 января", datetime.datetime(2025, 1, 12, 0, 0), "ru"),
1103-
("30 апреля", datetime.datetime(2025, 4, 30, 0, 0), "ru"),
1103+
("12 января", datetime.datetime(current_year, 1, 12, 0, 0), "ru"),
1104+
("30 апреля", datetime.datetime(current_year, 4, 30, 0, 0), "ru"),
11041105
]
11051106
assert result == expected
11061107

0 commit comments

Comments
 (0)