File size: 11,471 Bytes
cba1186
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Professional Product Search Engine for Trek Chatbot
Implements intelligent product matching with fuzzy search and NLP techniques
"""

import re
from difflib import SequenceMatcher
from typing import List, Tuple, Dict, Optional
import unicodedata

class ProductSearchEngine:
    """Advanced product search with intelligent matching"""
    
    def __init__(self, products: List[Tuple]):
        """
        Initialize with products list
        products: List of tuples (short_name, product_info, full_name)
        """
        self.products = products
        self.product_index = self._build_index()
        
    def _build_index(self) -> Dict:
        """Build search index for faster lookups"""
        index = {
            'by_name': {},
            'by_words': {},
            'by_category': {},
            'by_model': {},
            'normalized': {}
        }
        
        for product in self.products:
            short_name = product[0]
            full_name = product[2]
            
            # Normalize and store
            normalized_full = self._normalize_text(full_name)
            normalized_short = self._normalize_text(short_name)
            
            # Store by full name
            index['by_name'][normalized_full] = product
            index['normalized'][normalized_full] = full_name
            
            # Extract and index words
            words = normalized_full.split()
            for word in words:
                if len(word) > 2:  # Skip very short words
                    if word not in index['by_words']:
                        index['by_words'][word] = []
                    index['by_words'][word].append(product)
            
            # Extract model numbers and categories
            model_match = re.search(r'\b(\d+\.?\d*)\b', full_name)
            if model_match:
                model_num = model_match.group(1)
                if model_num not in index['by_model']:
                    index['by_model'][model_num] = []
                index['by_model'][model_num].append(product)
            
            # Category extraction (first word often represents category)
            if words:
                category = words[0]
                if category not in index['by_category']:
                    index['by_category'][category] = []
                index['by_category'][category].append(product)
                
        return index
    
    def _normalize_text(self, text: str) -> str:
        """Normalize text for better matching"""
        if not text:
            return ""
        
        # Convert to lowercase
        text = text.lower()
        
        # Remove Turkish characters
        replacements = {
            'ı': 'i', 'İ': 'i', 'ş': 's', 'Ş': 's',
            'ğ': 'g', 'Ğ': 'g', 'ü': 'u', 'Ü': 'u',
            'ö': 'o', 'Ö': 'o', 'ç': 'c', 'Ç': 'c'
        }
        for tr_char, eng_char in replacements.items():
            text = text.replace(tr_char, eng_char)
        
        # Remove special characters but keep spaces and numbers
        text = re.sub(r'[^\w\s\d\.]', ' ', text)
        
        # Normalize whitespace
        text = ' '.join(text.split())
        
        return text
    
    def _calculate_similarity(self, str1: str, str2: str) -> float:
        """Calculate similarity between two strings"""
        return SequenceMatcher(None, str1, str2).ratio()
    
    def search(self, query: str, threshold: float = 0.6) -> List[Tuple[float, Tuple]]:
        """
        Search for products matching the query
        Returns list of (score, product) tuples sorted by relevance
        """
        query_normalized = self._normalize_text(query)
        query_words = query_normalized.split()
        
        results = {}
        
        # 1. Exact match
        if query_normalized in self.product_index['by_name']:
            product = self.product_index['by_name'][query_normalized]
            results[id(product)] = (1.0, product)
        
        # 2. Model number search
        model_match = re.search(r'\b(\d+\.?\d*)\b', query)
        if model_match:
            model_num = model_match.group(1)
            if model_num in self.product_index['by_model']:
                for product in self.product_index['by_model'][model_num]:
                    if id(product) not in results:
                        # Check if model number is in correct context
                        score = 0.9 if model_num in product[2].lower() else 0.7
                        results[id(product)] = (score, product)
        
        # 3. Word-based search with scoring
        word_matches = {}
        for word in query_words:
            if len(word) > 2 and word in self.product_index['by_words']:
                for product in self.product_index['by_words'][word]:
                    if id(product) not in word_matches:
                        word_matches[id(product)] = {'count': 0, 'product': product}
                    word_matches[id(product)]['count'] += 1
        
        # Calculate word match scores
        for product_id, match_info in word_matches.items():
            product = match_info['product']
            matched_count = match_info['count']
            total_query_words = len([w for w in query_words if len(w) > 2])
            
            if total_query_words > 0:
                word_score = matched_count / total_query_words
                
                # Boost score if all important words match
                if matched_count == total_query_words:
                    word_score = min(word_score * 1.2, 0.95)
                
                # Check word order for better scoring
                product_text = self._normalize_text(product[2])
                if query_normalized in product_text:
                    word_score = min(word_score * 1.3, 0.98)
                
                if id(product) not in results or results[id(product)][0] < word_score:
                    results[id(product)] = (word_score, product)
        
        # 4. Fuzzy matching for all products
        for product in self.products:
            product_normalized = self._normalize_text(product[2])
            similarity = self._calculate_similarity(query_normalized, product_normalized)
            
            # Substring matching
            if query_normalized in product_normalized:
                similarity = max(similarity, 0.8)
            
            # Check if product contains all query words (in any order)
            if all(word in product_normalized for word in query_words if len(word) > 2):
                similarity = max(similarity, 0.75)
            
            if similarity >= threshold:
                if id(product) not in results or results[id(product)][0] < similarity:
                    results[id(product)] = (similarity, product)
        
        # 5. Category-based fallback
        if not results and query_words:
            category = query_words[0]
            if category in self.product_index['by_category']:
                for product in self.product_index['by_category'][category]:
                    results[id(product)] = (0.5, product)
        
        # Convert to list and sort by score
        result_list = list(results.values())
        result_list.sort(key=lambda x: x[0], reverse=True)
        
        return result_list
    
    def find_best_match(self, query: str) -> Optional[Tuple]:
        """Find the single best matching product"""
        results = self.search(query)
        if results and results[0][0] >= 0.6:
            return results[0][1]
        return None
    
    def find_similar_products(self, product_name: str, limit: int = 5) -> List[Tuple]:
        """Find products similar to the given product name"""
        results = self.search(product_name)
        similar = []
        
        # Skip the first result if it's an exact match
        start_idx = 1 if results and results[0][0] > 0.95 else 0
        
        for score, product in results[start_idx:start_idx + limit]:
            if score >= 0.5:
                similar.append(product)
        
        return similar
    
    def extract_product_context(self, query: str) -> Dict:
        """Extract context from query (size, color, type, etc.)"""
        context = {
            'sizes': [],
            'colors': [],
            'types': [],
            'features': [],
            'price_range': None
        }
        
        # Size detection
        size_patterns = [
            r'\b(xs|s|m|l|xl|xxl|2xl|3xl)\b',
            r'\b(\d{2})\b(?=\s*beden|\s*numara|$)',  # 44, 46, etc.
            r'\b(small|medium|large)\b'
        ]
        for pattern in size_patterns:
            matches = re.findall(pattern, query.lower())
            context['sizes'].extend(matches)
        
        # Color detection
        colors = ['siyah', 'beyaz', 'mavi', 'kirmizi', 'yesil', 'gri', 'turuncu', 
                 'black', 'white', 'blue', 'red', 'green', 'grey', 'gray', 'orange']
        for color in colors:
            if color in query.lower():
                context['colors'].append(color)
        
        # Type detection
        types = ['erkek', 'kadin', 'cocuk', 'yol', 'dag', 'sehir', 'elektrikli', 
                'karbon', 'aluminyum', 'gravel', 'hybrid']
        for type_word in types:
            if type_word in query.lower():
                context['types'].append(type_word)
        
        # Feature detection
        features = ['disk fren', 'shimano', 'sram', 'karbon', 'aluminyum', 
                   'hidrolik', 'mekanik', '29 jant', '27.5 jant']
        for feature in features:
            if feature in query.lower():
                context['features'].append(feature)
        
        # Price range detection
        price_match = re.search(r'(\d+)\.?(\d*)\s*(bin|tl)', query.lower())
        if price_match:
            price = float(price_match.group(1) + ('.' + price_match.group(2) if price_match.group(2) else ''))
            if 'bin' in price_match.group(3):
                price *= 1000
            context['price_range'] = price
        
        return context
    
    def generate_suggestions(self, failed_query: str) -> List[str]:
        """Generate suggestions for failed searches"""
        suggestions = []
        query_normalized = self._normalize_text(failed_query)
        query_words = query_normalized.split()
        
        # Find products with partial matches
        partial_matches = set()
        for word in query_words:
            if len(word) > 3:
                for product_word in self.product_index['by_words']:
                    if word in product_word or product_word in word:
                        partial_matches.add(product_word)
        
        # Generate suggestions from partial matches
        for match in list(partial_matches)[:5]:
            if match in self.product_index['by_words']:
                products = self.product_index['by_words'][match]
                if products:
                    suggestions.append(products[0][2])
        
        # Add category suggestions
        for category in list(self.product_index['by_category'].keys())[:3]:
            if any(word in category for word in query_words):
                category_products = self.product_index['by_category'][category]
                if category_products:
                    suggestions.append(category_products[0][2])
        
        return list(set(suggestions))[:5]  # Return unique suggestions