File size: 1,969 Bytes
b24aee4
df37f6e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b24aee4
60ad1c1
b24aee4
 
df37f6e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from loguru import logger
from app.dto.response import DialogServiceResult
from app.services.helper import extra_time_dialogflow


class TimeService:

    @staticmethod
    def check_time_ambiguity(time: str | dict) -> bool:
        """
        Kiểm tra xem thời gian có phải là dạng mơ hồ (ambiguous) hay không.

        Args:
            time (str | list): Thời gian có thể là chuỗi hoặc danh sách các thời gian.

        Returns:
            bool: True nếu thời gian là mơ hồ, False nếu rõ ràng.
        """
        if not time:
            logger.error("[TimeService][check_time_ambiguity] Time is empty")
            raise ValueError("Time is required")

        time = extra_time_dialogflow(time)
        return isinstance(time, list)

    @staticmethod
    def get_time_ambiguous_selection(time: str | dict) -> DialogServiceResult:
        """
        Trả về danh sách các lựa chọn thời gian nếu thời gian là mơ hồ.

        Args:
            time (str | list): Thời gian có thể là chuỗi hoặc danh sách các thời gian.

        Returns:
            list[str]: Danh sách các lựa chọn thời gian nếu mơ hồ, rỗng nếu không.
        """
        time = extra_time_dialogflow(time)

        if isinstance(time, str):
            # Nếu thời gian là chuỗi rõ ràng, trả về False và chuỗi đó
            parameters = {"is_time_ambiguous": False}
            return DialogServiceResult(parameters=parameters)

        if isinstance(time, list):

            text = "Vui lòng chọn thời gian cụ thể:\n"
            text += "\n".join(f"{i+1}. {t}" for i, t in enumerate(time))

            return DialogServiceResult(
                text=[text],
                payload=time,
                parameters={"is_time_ambiguous": True},
            )
        text = "Vui lòng chỉ định thời gian cụ thể"
        return DialogServiceResult(text=[text])