Skip to content

Commit

Permalink
United exception handling instead of individual
Browse files Browse the repository at this point in the history
  • Loading branch information
nsomabalint committed May 21, 2024
1 parent 3d44942 commit 9571752
Showing 1 changed file with 22 additions and 22 deletions.
44 changes: 22 additions & 22 deletions hun_date_parser/date_parser/datetime_extractor.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,11 @@ def text2datetime(input_sentence: str, now: datetime = datetime.now(),
:param realistic_year_required: Defines whether to restrict year candidates to be between 1900 and 2100.
:return: list of datetime interval dictionaries
"""
try:
datetime_extractor = DatetimeExtractor(now=now,
output_container='datetime',
search_scope=search_scope,
realistic_year_required=realistic_year_required)
return datetime_extractor.parse_datetime(sentence=input_sentence)
except:
return []
datetime_extractor = DatetimeExtractor(now=now,
output_container='datetime',
search_scope=search_scope,
realistic_year_required=realistic_year_required)
return datetime_extractor.parse_datetime(sentence=input_sentence)


def text2date(input_sentence: str, now: datetime = datetime.now(),
Expand All @@ -63,13 +60,10 @@ def text2date(input_sentence: str, now: datetime = datetime.now(),
:param realistic_year_required: Defines whether to restrict year candidates to be between 1900 and 2100.
:return: list of date interval dictionaries
"""
try:
datetime_extractor = DatetimeExtractor(now=now, output_container='date',
search_scope=search_scope,
realistic_year_required=realistic_year_required)
return datetime_extractor.parse_datetime(sentence=input_sentence)
except:
return []
datetime_extractor = DatetimeExtractor(now=now, output_container='date',
search_scope=search_scope,
realistic_year_required=realistic_year_required)
return datetime_extractor.parse_datetime(sentence=input_sentence)


def text2time(input_sentence: str, now: datetime = datetime.now(),
Expand All @@ -83,13 +77,10 @@ def text2time(input_sentence: str, now: datetime = datetime.now(),
:param realistic_year_required: Defines whether to restrict year candidates to be between 1900 and 2100.
:return: list of time interval dictionaries
"""
try:
datetime_extractor = DatetimeExtractor(now=now, output_container='time',
search_scope=search_scope,
realistic_year_required=realistic_year_required)
return datetime_extractor.parse_datetime(sentence=input_sentence)
except:
return []
datetime_extractor = DatetimeExtractor(now=now, output_container='time',
search_scope=search_scope,
realistic_year_required=realistic_year_required)
return datetime_extractor.parse_datetime(sentence=input_sentence)


def match_rules(now: datetime, sentence: str,
Expand Down Expand Up @@ -356,6 +347,15 @@ def assemble_datetime(self, now: datetime,
return None

def parse_datetime(self, sentence: str) -> List[Dict[str, datelike]]:
"""
Fail-safe wrapper around _parse_datetime. All possible exceptions will be caught and an empty list is returned.
"""
try:
return self._parse_datetime(sentence)
except:
return []

def _parse_datetime(self, sentence: str) -> List[Dict[str, datelike]]:
"""
Extracts list of datetime intervals from input sentence.
:param sentence: Input sentence string.
Expand Down

0 comments on commit 9571752

Please sign in to comment.