File size: 1,665 Bytes
732e77c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import re
from typing import Optional


# Simple, deterministic rules:
def _intent_greeting(text: str) -> Optional[str]:
    if re.search(r'\b(hi|hello|hey)\b', text, re.IGNORECASE):
        return 'Hi there! I am an anonymous, rule-based helper. Ask me about hours, contact, or help.'
    return None


def _intent_help(text: str) -> Optional[str]:
    if re.search(r'\b(help|what can you do|commands)\b', text, re.IGNORECASE):
        return ("I’m a simple rule-based bot. Try:\n"
                "- 'hours' to see hours\n"
                "- 'contact' to get contact info\n"
                "- 'reverse <text>' to reverse text\n")
    return None


def _intent_hours(text: str) -> Optional[str]:
    if re.search(r'\bhours?\b', text, re.IGNORECASE):
        return 'We are open Mon-Fri, 9am-5am (local time).'
    return None


def _intent_contact(text: str) -> Optional[str]:
    if re.search(r'\b(contact|support|reach)\b', text, re.IGNORECASE):
        return 'You can reach support at our website contact form.'
    return None


def _intent_reverse(text: str) -> Optional[str]:
    s = text.strip()
    if s.lower().startswith('reverse'):
        payload = s[7:].strip()
        return payload[::-1] if payload else "There's nothing to reverse."
    return None


def _fallback(_text: str) -> str:
    return "I am not sure how to help with that. Type 'help' to see what I can do."


RULES = [
    _intent_reverse,
    _intent_greeting,
    _intent_help,
    _intent_hours,
    _intent_contact
]


def route(text: str) -> str:
    for rule in RULES:
        resp = rule(text)
        if resp is not None:
            return resp
    return _fallback(text)