File size: 4,049 Bytes
ddd337d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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"))  # Kết quả mong đợi: bảy giờ ba mươi phút
    print(time.convert("8:00 SA"))  # Kết quả mong đợi: tám giờ sáng
    print(time.convert("3:00 CH"))  # Kết quả mong đợi: ba giờ chiều
    print(time.convert(
        "11:59:59.999"))  # Kết quả mong đợi: mười một giờ năm mươi chín phút năm mươi chín giây chín trăm chín mươi chín phần nghìn giây
    print(time.convert("SA7"))  # Kết quả mong đợi: bảy giờ sáng
    print(time.convert("CH5"))  # Kết quả mong đợi: năm giờ chiều
    print(time.convert("9:30:21"))  # Kết quả mong đợi: năm giờ chiều