/
time_parser_config.py
101 lines (79 loc) · 3.41 KB
/
time_parser_config.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
from typing import List, Pattern, Dict
import regex
from recognizers_text.utilities import RegExpUtility
from ...resources.french_date_time import FrenchDateTime
from ..base_time import TimeParserConfiguration, AdjustParams
from ..base_configs import BaseDateParserConfiguration, DateTimeUtilityConfiguration
from .time_extractor_config import FrenchTimeExtractorConfiguration
class FrenchTimeParserConfiguration(TimeParserConfiguration):
@property
def time_token_prefix(self) -> str:
return self._time_token_prefix
@property
def at_regex(self) -> Pattern:
return self._at_regex
@property
def time_regexes(self) -> List[Pattern]:
return self._time_regexes
@property
def numbers(self) -> Dict[str, int]:
return self._numbers
@property
def utility_configuration(self) -> DateTimeUtilityConfiguration:
return self._utility_configuration
def __init__(self, config: BaseDateParserConfiguration):
self._time_token_prefix: str = FrenchDateTime.TimeTokenPrefix
self._at_regex: Pattern = RegExpUtility.get_safe_reg_exp(
FrenchDateTime.AtRegex)
self._time_regexes: List[Pattern] = FrenchTimeExtractorConfiguration.get_time_regex_list(
)
self.less_than_one_hour = RegExpUtility.get_safe_reg_exp(
FrenchDateTime.LessThanOneHour)
self.time_suffix = RegExpUtility.get_safe_reg_exp(
FrenchDateTime.TimeSuffix)
self._utility_configuration = config.utility_configuration
self._numbers: Dict[str, int] = config.numbers
def adjust_by_prefix(self, prefix: str, adjust: AdjustParams):
delta_min = 0
trimmed_prefix = prefix.strip().lower()
if trimmed_prefix.endswith('demie'):
delta_min = 30
elif trimmed_prefix.endswith('un quart') or trimmed_prefix.endswith('quart'):
delta_min = 15
elif trimmed_prefix.endswith('trois quarts'):
delta_min = 45
else:
match = regex.search(self.less_than_one_hour, trimmed_prefix)
if match:
min_str = RegExpUtility.get_group(match, 'deltamin')
if min_str:
delta_min = int(min_str)
else:
min_str = RegExpUtility.get_group(
match, 'deltaminnum').lower()
delta_min = self.numbers.get(min_str)
if trimmed_prefix.endswith('à'):
delta_min = delta_min * -1
adjust.minute += delta_min
if adjust.minute < 0:
adjust.minute += 60
adjust.hour -= 1
adjust.has_minute = True
def adjust_by_suffix(self, suffix: str, adjust: AdjustParams):
suffix = suffix.strip().lower()
delta_hour = 0
match = regex.match(self.time_suffix, suffix)
if match and match.group() == suffix:
oclock_str = RegExpUtility.get_group(match, 'heures')
if not oclock_str:
am_str = RegExpUtility.get_group(match, 'am')
if am_str:
if adjust.hour >= 12:
delta_hour -= 12
adjust.has_am = True
pm_str = RegExpUtility.get_group(match, 'pm')
if pm_str:
if adjust.hour < 12:
delta_hour = 12
adjust.has_pm = True
adjust.hour = (adjust.hour + delta_hour) % 24