File size: 1,071 Bytes
72e96d1
570f7bd
72e96d1
 
 
570f7bd
c1bc4eb
570f7bd
72e96d1
 
 
 
 
570f7bd
72e96d1
 
 
 
 
 
 
 
 
 
 
570f7bd
c1bc4eb
72e96d1
570f7bd
72e96d1
 
 
 
 
 
 
570f7bd
72e96d1
 
 
 
570f7bd
c1bc4eb
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
import os
import re
import logging

log = logging.getLogger(__name__)


class AmbiguityDetector:
    """Improved AmbiSQL-style ambiguity detection.

    - Skips detection entirely in DEV_MODE.
    - Ignores qualified references like 'artist.name'.
    """

    AMBIGUOUS_TERMS = [
        "recent",
        "top",
        "name",
        "rank",
        "latest",
        "id",
        "title",
        "date",
        "type",
    ]

    def detect(self, query: str, schema_preview: str) -> list[str]:
        # Normalize query
        q_lower = query.lower()

        # Skip ambiguity checks entirely in dev mode
        if os.getenv("DEV_MODE") == "1":
            log.warning("Skipping ambiguity detection (DEV_MODE=1).")
            return []

        hits = []
        for term in self.AMBIGUOUS_TERMS:
            # Match only standalone words, not qualified like 'artist.name'
            pattern = rf"(?<!\.)\b{term}\b"
            if re.search(pattern, q_lower):
                hits.append(f"The term '{term}' is ambiguous in this query.")

        return hits