|
from singleton_decorator import singleton
|
|
import re
|
|
from .Cardinal import CardinalVietnamese
|
|
|
|
|
|
@singleton
|
|
class Time:
|
|
def __init__(self):
|
|
super().__init__()
|
|
|
|
self.filter_regex = re.compile(r"[. ]")
|
|
self.time_regex = re.compile(r"^(?P<hour>\d{1,2}) *((?::|.) *(?P<minute>\d{1,2}))? *(?P<suffix>[a-zA-Z\. ]*)$",
|
|
flags=re.I)
|
|
self.full_time_regex = re.compile(
|
|
r"^(?:(?P<hour>\d{1,2}) *:)? *(?P<minute>\d{1,2})(?: *: *(?P<seconds>\d{1,2})(?: *. *(?P<milliseconds>\d{1,3}))?)? *(?P<suffix>[a-zA-Z\. ]*)$",
|
|
flags=re.I)
|
|
self.ampm_time_regex = re.compile(r"^(?P<suffix>[a-zA-Z\. ]*)(?P<hour>\d{1,2})", flags=re.I)
|
|
|
|
self.cardinal = CardinalVietnamese()
|
|
|
|
def convert(self, token: str) -> str:
|
|
token = token.strip()
|
|
result_list = []
|
|
|
|
match = self.time_regex.match(token)
|
|
if match:
|
|
hour, minute, suffix = match.group("hour"), match.group("minute"), match.group("suffix")
|
|
|
|
result_list.append(self.cardinal.convert(hour))
|
|
result_list.append("giờ")
|
|
|
|
if minute and minute != "00":
|
|
result_list.append(self.cardinal.convert(minute))
|
|
result_list.append("phút")
|
|
|
|
if suffix:
|
|
suffix = self.filter_regex.sub("", suffix).lower()
|
|
if suffix == "sa":
|
|
result_list.append("sáng")
|
|
elif suffix == "ch":
|
|
result_list.append("chiều")
|
|
|
|
return " ".join(result_list)
|
|
|
|
match = self.full_time_regex.match(token)
|
|
if match:
|
|
hour, minute, seconds, milliseconds, suffix = match.group("hour"), match.group("minute"), match.group(
|
|
"seconds"), match.group("milliseconds"), match.group("suffix")
|
|
|
|
if hour:
|
|
result_list.append(self.cardinal.convert(hour))
|
|
result_list.append("giờ")
|
|
if minute:
|
|
result_list.append(self.cardinal.convert(minute))
|
|
result_list.append("phút")
|
|
if seconds:
|
|
result_list.append(self.cardinal.convert(seconds))
|
|
result_list.append("giây")
|
|
if milliseconds:
|
|
result_list.append(self.cardinal.convert(milliseconds))
|
|
result_list.append("phần nghìn giây")
|
|
|
|
if suffix:
|
|
suffix = self.filter_regex.sub("", suffix).lower()
|
|
if suffix == "sa":
|
|
result_list.append("sáng")
|
|
elif suffix == "ch":
|
|
result_list.append("chiều")
|
|
|
|
return " ".join(result_list)
|
|
|
|
match = self.ampm_time_regex.match(token)
|
|
if match:
|
|
hour, suffix = match.group("hour"), match.group("suffix")
|
|
|
|
result_list.append(self.cardinal.convert(hour))
|
|
result_list.append("giờ")
|
|
|
|
suffix = self.filter_regex.sub("", suffix).lower()
|
|
if suffix == "sa":
|
|
result_list.append("sáng")
|
|
elif suffix == "ch":
|
|
result_list.append("chiều")
|
|
|
|
return " ".join(result_list)
|
|
|
|
return token
|
|
|
|
|
|
if __name__ == "__main__":
|
|
time = Time()
|
|
|
|
print(time.convert("7:30"))
|
|
print(time.convert("8:00 SA"))
|
|
print(time.convert("3:00 CH"))
|
|
print(time.convert(
|
|
"11:59:59.999"))
|
|
print(time.convert("SA7"))
|
|
print(time.convert("CH5"))
|
|
print(time.convert("9:30:21")) |