diff --git a/app.py b/app.py index 6464079..f1ab5dd 100644 --- a/app.py +++ b/app.py @@ -3124,11 +3124,38 @@ def api_chat(): if score > 0: scored.append((score, a)) scored.sort(key=lambda x: -x[0]) + # Merge exhibitors with similar names (case-insensitive substring match) + merged = {} + merge_order = [] + for score, a in scored: + name_lower = (a['name'] or '').strip().lower() + # Find existing merge target + found = False + for existing_name in merge_order: + if name_lower in existing_name or existing_name in name_lower: + # Merge stands + existing_stands = merged[existing_name]['stand'] + new_stands = (a['stand'] or '').strip() + all_stands = set(s.strip() for s in (existing_stands + '|' + new_stands).split('|') if s.strip()) + merged[existing_name]['stand'] = '|'.join(sorted(all_stands)) + found = True + break + if not found: + merge_order.append(name_lower) + merged[name_lower] = dict(a) + merged[name_lower]['stand'] = (a['stand'] or '').strip() + merged[name_lower]['_score'] = score + + # Replace scored with merged entries + new_scored = [(merged[n]['_score'], merged[n]) for n in merge_order] + new_scored.sort(key=lambda x: -x[0]) # Add top matching exhibitors - for _, a in scored[:20]: - halle_stand = f"{a['halle'] or ''} {a['stand'] or ''}".strip() + for _, a in new_scored[:20]: + stands_raw = (a['stand'] or '').strip() + stands_list = [s.strip() for s in stands_raw.split('|') if s.strip()] + halle_stand = ' | '.join(stands_list) if len(stands_list) > 1 else (stands_list[0] if stands_list else '') land = a['land'] or '' - info = (a['info'] or '').strip()[:150] + info = (a['info'] or '').strip()[:100] web = a['web'] or '' context_parts.append( f"[Aussteller] {a['name']}" @@ -3139,8 +3166,10 @@ def api_chat(): ) # Also include all exhibitors matching the user's search when they ask about halls/stands if any(t in user_message.lower() for t in ['halle', 'stand', 'aussteller', 'verlag', 'verlage', 'publisher', 'spiel']): - for _, a in scored[:40]: - halle_stand = f"{a['halle'] or ''} {a['stand'] or ''}".strip() + for _, a in new_scored[:40]: + stands_raw = (a['stand'] or '').strip() + stands_list = [s.strip() for s in stands_raw.split('|') if s.strip()] + halle_stand = ' | '.join(stands_list) if len(stands_list) > 1 else (stands_list[0] if stands_list else '') land = a['land'] or '' context_parts.append( f"[Aussteller] {a['name']}"