File size: 8,422 Bytes
e1c010e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
"""
Object Mapper — Synonym normalization for bridging YOLO labels
and natural-language user commands.

YOLO may detect "cup" while the user says "mug". This module provides
a mapping layer and fuzzy-match helper to reconcile the two vocabularies.
"""

import logging
from difflib import SequenceMatcher

logger = logging.getLogger(__name__)

# ---------------------------------------------------------------------------
# Synonym Map: user-language → canonical YOLO label
# ---------------------------------------------------------------------------
SYNONYM_MAP: dict[str, str] = {
    # ---------------------------------------------------------------
    # COCO has 80 classes. This map bridges common user vocabulary
    # to the actual YOLO/COCO label strings.
    # ---------------------------------------------------------------

    # person
    "human": "person",
    "worker": "person",
    "operator": "person",
    "man": "person",
    "woman": "person",
    "people": "person",
    "employee": "person",
    "technician": "person",

    # bicycle
    "bike": "bicycle",
    "cycle": "bicycle",

    # car
    "automobile": "car",
    "vehicle": "car",
    "sedan": "car",

    # motorcycle
    "motorbike": "motorcycle",
    "scooter": "motorcycle",

    # truck
    "van": "truck",
    "lorry": "truck",
    "pickup": "truck",

    # boat
    "ship": "boat",
    "vessel": "boat",
    "canoe": "boat",

    # traffic light
    "signal": "traffic light",
    "stoplight": "traffic light",

    # backpack
    "bag": "backpack",
    "rucksack": "backpack",
    "knapsack": "backpack",

    # umbrella
    "parasol": "umbrella",

    # handbag
    "purse": "handbag",
    "clutch": "handbag",

    # suitcase (toolboxes, cases look like suitcases to YOLO)
    "briefcase": "suitcase",
    "toolbox": "suitcase",
    "case": "suitcase",
    "luggage": "suitcase",
    "tool box": "suitcase",

    # bottle
    "water bottle": "bottle",
    "flask": "bottle",
    "jug": "bottle",
    "canteen": "bottle",
    "container": "bottle",

    # wine glass
    "goblet": "wine glass",
    "chalice": "wine glass",
    "glass": "wine glass",

    # cup
    "mug": "cup",
    "teacup": "cup",
    "coffee cup": "cup",
    "tumbler": "cup",

    # fork
    "prong": "fork",

    # knife (wrenches, tools often detected as knife-like)
    "blade": "knife",
    "wrench": "knife",
    "spanner": "knife",
    "screwdriver": "knife",
    "tool": "knife",

    # spoon
    "ladle": "spoon",
    "scoop": "spoon",

    # bowl
    "tray": "bowl",
    "dish": "bowl",
    "plate": "bowl",
    "bin": "bowl",
    "basket": "bowl",

    # banana / apple / orange (fruits)
    "fruit": "apple",

    # sandwich
    "sub": "sandwich",
    "burger": "sandwich",
    "wrap": "sandwich",

    # pizza
    "pie": "pizza",

    # cake
    "pastry": "cake",
    "dessert": "cake",

    # chair
    "seat": "chair",
    "stool": "chair",
    "office chair": "chair",

    # couch
    "sofa": "couch",
    "loveseat": "couch",
    "settee": "couch",

    # bed
    "mattress": "bed",
    "cot": "bed",

    # dining table
    "desk": "dining table",
    "table": "dining table",
    "workbench": "dining table",
    "bench": "dining table",
    "counter": "dining table",

    # tv
    "monitor": "tv",
    "screen": "tv",
    "display": "tv",
    "television": "tv",

    # laptop
    "notebook": "laptop",
    "computer": "laptop",
    "macbook": "laptop",

    # mouse
    "trackpad": "mouse",

    # remote
    "controller": "remote",
    "remote control": "remote",

    # keyboard
    "keypad": "keyboard",

    # cell phone
    "mobile": "cell phone",
    "phone": "cell phone",
    "smartphone": "cell phone",
    "iphone": "cell phone",
    "handset": "cell phone",
    "scanner": "cell phone",

    # microwave
    "microwave oven": "microwave",

    # oven
    "stove": "oven",
    "furnace": "oven",

    # refrigerator
    "fridge": "refrigerator",
    "cooler": "refrigerator",
    "freezer": "refrigerator",

    # book
    "notebook paper": "book",
    "manual": "book",
    "textbook": "book",
    "binder": "book",

    # clock
    "watch": "clock",
    "timer": "clock",

    # vase
    "pot": "vase",
    "planter": "vase",
    "jar": "vase",

    # scissors
    "cutter": "scissors",
    "shears": "scissors",
    "clipper": "scissors",
    "pliers": "scissors",
    "wire cutter": "scissors",

    # teddy bear
    "stuffed animal": "teddy bear",
    "plush": "teddy bear",
    "toy": "teddy bear",

    # potted plant
    "plant": "potted plant",
    "houseplant": "potted plant",
    "flower": "potted plant",

    # -----------------------------------------------------------
    # Domain-specific aliases for industrial / demo contexts
    # -----------------------------------------------------------
    "damaged part": "knife",
    "defective part": "knife",
    "component": "knife",
    "metal part": "knife",
    "part": "knife",
    "piece": "knife",

    "red bin": "bowl",
    "blue bin": "bowl",
    "sorting bin": "bowl",
    "reject bin": "bowl",
    "accept bin": "bowl",

    "circuit board": "keyboard",
    "pcb": "keyboard",
    "board": "keyboard",

    "hard hat": "sports ball",
    "helmet": "sports ball",
    "safety hat": "sports ball",

    "gloves": "backpack",
    "safety gloves": "backpack",

    "pallet": "suitcase",
    "crate": "suitcase",
    "carton": "suitcase",
    "box": "suitcase",

    "hand truck": "bicycle",
    "dolly": "bicycle",
    "cart": "bicycle",

    "forklift": "truck",

    "clipboard": "book",
    "blueprint": "book",

    "stethoscope": "tie",
    "syringe": "toothbrush",
    "safety goggles": "wine glass",
    "goggles": "wine glass",

    "oil can": "bottle",
    "spray can": "bottle",
    "battery": "suitcase",
    "car battery": "suitcase",

    "multimeter": "cell phone",
    "meter": "cell phone",
    "soldering iron": "knife",
    "magnifying lamp": "clock",
}



def normalize_label(label: str) -> str:
    """
    Normalize a label using the synonym map.

    Args:
        label: Raw label string (from user command or YOLO).

    Returns:
        The canonical label if a synonym exists, otherwise the
        original label lowercased and stripped.
    """
    cleaned = label.strip().lower()
    return SYNONYM_MAP.get(cleaned, cleaned)


def _similarity(a: str, b: str) -> float:
    """Return a 0-1 similarity score between two strings."""
    return SequenceMatcher(None, a.lower(), b.lower()).ratio()


def find_best_match(
    query: str,
    detected_labels: list[str],
    threshold: float = 0.4,
) -> str | None:
    """
    Find the best matching detected label for a user query term.

    Strategy:
        1. Exact match after normalization.
        2. Substring containment check.
        3. Fuzzy similarity above *threshold*.

    Args:
        query: The term from the user command (e.g. "mug").
        detected_labels: Labels returned by the vision detector.
        threshold: Minimum similarity score for fuzzy matching.

    Returns:
        The best matching label, or ``None`` if no match is found.
    """
    normalized_query = normalize_label(query)
    normalized_detected = {normalize_label(lbl): lbl for lbl in detected_labels}

    # 1. Exact match on normalized labels
    if normalized_query in normalized_detected:
        logger.info(
            "Exact match: '%s' → '%s'",
            query,
            normalized_detected[normalized_query],
        )
        return normalized_detected[normalized_query]

    # 2. Substring containment
    for norm_lbl, orig_lbl in normalized_detected.items():
        if normalized_query in norm_lbl or norm_lbl in normalized_query:
            logger.info(
                "Substring match: '%s' → '%s'", query, orig_lbl
            )
            return orig_lbl

    # 3. Fuzzy matching
    best_score = 0.0
    best_label: str | None = None
    for norm_lbl, orig_lbl in normalized_detected.items():
        score = _similarity(normalized_query, norm_lbl)
        if score > best_score:
            best_score = score
            best_label = orig_lbl

    if best_label and best_score >= threshold:
        logger.info(
            "Fuzzy match: '%s' → '%s' (score=%.2f)",
            query,
            best_label,
            best_score,
        )
        return best_label

    logger.warning("No match found for query '%s'", query)
    return None