DawnC commited on
Commit
e0dbe8c
1 Parent(s): ec8f3a1

Delete smart_breed_matcher.py

Browse files
Files changed (1) hide show
  1. smart_breed_matcher.py +0 -382
smart_breed_matcher.py DELETED
@@ -1,382 +0,0 @@
1
-
2
- import torch
3
- import numpy as np
4
- from typing import List, Dict, Tuple, Optional
5
- from dataclasses import dataclass
6
- from breed_health_info import breed_health_info
7
- from breed_noise_info import breed_noise_info
8
- from dog_database import dog_data
9
- from scoring_calculation_system import UserPreferences
10
- from sentence_transformers import SentenceTransformer, util
11
-
12
- class SmartBreedMatcher:
13
- def __init__(self, dog_data: List[Tuple]):
14
- self.dog_data = dog_data
15
- self.model = SentenceTransformer('all-mpnet-base-v2')
16
- self._embedding_cache = {}
17
-
18
- def _get_cached_embedding(self, text: str) -> torch.Tensor:
19
- if text not in self._embedding_cache:
20
- self._embedding_cache[text] = self.model.encode(text)
21
- return self._embedding_cache[text]
22
-
23
- def _categorize_breeds(self) -> Dict:
24
- """自動將狗品種分類"""
25
- categories = {
26
- 'working_dogs': [],
27
- 'herding_dogs': [],
28
- 'hunting_dogs': [],
29
- 'companion_dogs': [],
30
- 'guard_dogs': []
31
- }
32
-
33
- for breed_info in self.dog_data:
34
- description = breed_info[9].lower()
35
- temperament = breed_info[4].lower()
36
-
37
- # 根據描述和性格特徵自動分類
38
- if any(word in description for word in ['herding', 'shepherd', 'cattle', 'flock']):
39
- categories['herding_dogs'].append(breed_info[1])
40
- elif any(word in description for word in ['hunting', 'hunt', 'retriever', 'pointer']):
41
- categories['hunting_dogs'].append(breed_info[1])
42
- elif any(word in description for word in ['companion', 'toy', 'family', 'lap']):
43
- categories['companion_dogs'].append(breed_info[1])
44
- elif any(word in description for word in ['guard', 'protection', 'watchdog']):
45
- categories['guard_dogs'].append(breed_info[1])
46
- elif any(word in description for word in ['working', 'draft', 'cart']):
47
- categories['working_dogs'].append(breed_info[1])
48
-
49
- return categories
50
-
51
- def find_similar_breeds(self, breed_name: str, top_n: int = 5) -> List[Tuple[str, float]]:
52
- """找出與指定品種最相似的其他品種"""
53
- target_breed = next((breed for breed in self.dog_data if breed[1] == breed_name), None)
54
- if not target_breed:
55
- return []
56
-
57
- # 獲取目標品種的特徵
58
- target_features = {
59
- 'breed_name': target_breed[1], # 添加品種名稱
60
- 'size': target_breed[2],
61
- 'temperament': target_breed[4],
62
- 'exercise': target_breed[7],
63
- 'description': target_breed[9]
64
- }
65
-
66
- similarities = []
67
- for breed in self.dog_data:
68
- if breed[1] != breed_name:
69
- breed_features = {
70
- 'breed_name': breed[1], # 添加品種名稱
71
- 'size': breed[2],
72
- 'temperament': breed[4],
73
- 'exercise': breed[7],
74
- 'description': breed[9]
75
- }
76
-
77
- similarity_score = self._calculate_breed_similarity(target_features, breed_features)
78
- similarities.append((breed[1], similarity_score))
79
-
80
- return sorted(similarities, key=lambda x: x[1], reverse=True)[:top_n]
81
-
82
-
83
- def _calculate_breed_similarity(self, breed1_features: Dict, breed2_features: Dict) -> float:
84
- """計算兩個品種之間的相似度,包含健康和噪音因素"""
85
- # 計算描述文本的相似度
86
- desc1_embedding = self._get_cached_embedding(breed1_features['description'])
87
- desc2_embedding = self._get_cached_embedding(breed2_features['description'])
88
- description_similarity = float(util.pytorch_cos_sim(desc1_embedding, desc2_embedding))
89
-
90
- # 基本特徵相似度
91
- size_similarity = 1.0 if breed1_features['size'] == breed2_features['size'] else 0.5
92
- exercise_similarity = 1.0 if breed1_features['exercise'] == breed2_features['exercise'] else 0.5
93
-
94
- # 性格相似度
95
- temp1_embedding = self._get_cached_embedding(breed1_features['temperament'])
96
- temp2_embedding = self._get_cached_embedding(breed2_features['temperament'])
97
- temperament_similarity = float(util.pytorch_cos_sim(temp1_embedding, temp2_embedding))
98
-
99
- # 健康分數相似度
100
- health_score1 = self._calculate_health_score(breed1_features['breed_name'])
101
- health_score2 = self._calculate_health_score(breed2_features['breed_name'])
102
- health_similarity = 1.0 - abs(health_score1 - health_score2)
103
-
104
- # 噪音水平相似度
105
- noise_similarity = self._calculate_noise_similarity(
106
- breed1_features['breed_name'],
107
- breed2_features['breed_name']
108
- )
109
-
110
- # 加權計算
111
- weights = {
112
- 'description': 0.25,
113
- 'temperament': 0.20,
114
- 'exercise': 0.2,
115
- 'size': 0.05,
116
- 'health': 0.15,
117
- 'noise': 0.15
118
- }
119
-
120
- final_similarity = (
121
- description_similarity * weights['description'] +
122
- temperament_similarity * weights['temperament'] +
123
- exercise_similarity * weights['exercise'] +
124
- size_similarity * weights['size'] +
125
- health_similarity * weights['health'] +
126
- noise_similarity * weights['noise']
127
- )
128
-
129
- return final_similarity
130
-
131
-
132
- def _calculate_final_scores(self, breed_name: str, base_scores: Dict,
133
- smart_score: float, is_preferred: bool,
134
- similarity_score: float = 0.0) -> Dict:
135
- """
136
- 計算最終分數,包含基礎分數和獎勵分數
137
-
138
- Args:
139
- breed_name: 品種名稱
140
- base_scores: 基礎評分 (空間、運動等)
141
- smart_score: 智能匹配分數
142
- is_preferred: 是否為用戶指定品種
143
- similarity_score: 與指定品種的相似度 (0-1)
144
- """
145
- # 基礎權重
146
- weights = {
147
- 'base': 0.6, # 基礎分數權重
148
- 'smart': 0.25, # 智能匹配權重
149
- 'bonus': 0.15 # 獎勵分數權重
150
- }
151
-
152
- # 計算基礎分數
153
- base_score = base_scores.get('overall', 0.7)
154
-
155
- # 計算獎勵分數
156
- bonus_score = 0.0
157
- if is_preferred:
158
- # 用戶指定品種獲得最高獎勵
159
- bonus_score = 0.95
160
- elif similarity_score > 0:
161
- # 相似品種獲得部分獎勵,但不超過80%的最高獎勵
162
- bonus_score = min(0.8, similarity_score) * 0.95
163
-
164
- # 計算最終分數
165
- final_score = (
166
- base_score * weights['base'] +
167
- smart_score * weights['smart'] +
168
- bonus_score * weights['bonus']
169
- )
170
-
171
- # 更新各項分數
172
- scores = base_scores.copy()
173
-
174
- # 如果是用戶指定品種,稍微提升各項基礎分數,但保持合理範圍
175
- if is_preferred:
176
- for key in scores:
177
- if key != 'overall':
178
- scores[key] = min(1.0, scores[key] * 1.1) # 最多提升10%
179
-
180
- # 為相似品種調整分數
181
- elif similarity_score > 0:
182
- boost_factor = 1.0 + (similarity_score * 0.05) # 最多提升5%
183
- for key in scores:
184
- if key != 'overall':
185
- scores[key] = min(0.95, scores[key] * boost_factor) # 確保不超過95%
186
-
187
- return {
188
- 'final_score': round(final_score, 4),
189
- 'base_score': round(base_score, 4),
190
- 'bonus_score': round(bonus_score, 4),
191
- 'scores': {k: round(v, 4) for k, v in scores.items()}
192
- }
193
-
194
- def _calculate_health_score(self, breed_name: str) -> float:
195
- """計算品種的健康分數"""
196
- if breed_name not in breed_health_info:
197
- return 0.5
198
-
199
- health_notes = breed_health_info[breed_name]['health_notes'].lower()
200
-
201
- # 嚴重健康問題
202
- severe_conditions = [
203
- 'cancer', 'cardiomyopathy', 'epilepsy', 'dysplasia',
204
- 'bloat', 'progressive', 'syndrome'
205
- ]
206
-
207
- # 中等健康問題
208
- moderate_conditions = [
209
- 'allergies', 'infections', 'thyroid', 'luxation',
210
- 'skin problems', 'ear'
211
- ]
212
-
213
- severe_count = sum(1 for condition in severe_conditions if condition in health_notes)
214
- moderate_count = sum(1 for condition in moderate_conditions if condition in health_notes)
215
-
216
- health_score = 1.0
217
- health_score -= (severe_count * 0.1)
218
- health_score -= (moderate_count * 0.05)
219
-
220
- # 特殊條件調整(根據用戶偏好)
221
- if hasattr(self, 'user_preferences'):
222
- if self.user_preferences.has_children:
223
- if 'requires frequent' in health_notes or 'regular monitoring' in health_notes:
224
- health_score *= 0.9
225
-
226
- if self.user_preferences.health_sensitivity == 'high':
227
- health_score *= 0.9
228
-
229
- return max(0.3, min(1.0, health_score))
230
-
231
-
232
-
233
- def _calculate_noise_similarity(self, breed1: str, breed2: str) -> float:
234
- """計算兩個品種的噪音相似度"""
235
- noise_levels = {
236
- 'Low': 1,
237
- 'Moderate': 2,
238
- 'High': 3,
239
- 'Unknown': 2 # 默認為中等
240
- }
241
-
242
- noise1 = breed_noise_info.get(breed1, {}).get('noise_level', 'Unknown')
243
- noise2 = breed_noise_info.get(breed2, {}).get('noise_level', 'Unknown')
244
-
245
- # 獲取數值級別
246
- level1 = noise_levels.get(noise1, 2)
247
- level2 = noise_levels.get(noise2, 2)
248
-
249
- # 計算差異並歸一化
250
- difference = abs(level1 - level2)
251
- similarity = 1.0 - (difference / 2) # 最大差異是2,所以除以2來歸一化
252
-
253
- return similarity
254
-
255
- def _general_matching(self, description: str, top_n: int = 10) -> List[Dict]:
256
- """基本的品種匹配邏輯,考慮描述、性格、噪音和健康因素"""
257
- matches = []
258
- # 預先計算描述的 embedding 並快取
259
- desc_embedding = self._get_cached_embedding(description)
260
-
261
- for breed in self.dog_data:
262
- breed_name = breed[1]
263
- breed_description = breed[9]
264
- temperament = breed[4]
265
-
266
- # 使用快取計算相似度
267
- breed_desc_embedding = self._get_cached_embedding(breed_description)
268
- breed_temp_embedding = self._get_cached_embedding(temperament)
269
-
270
- desc_similarity = float(util.pytorch_cos_sim(desc_embedding, breed_desc_embedding))
271
- temp_similarity = float(util.pytorch_cos_sim(desc_embedding, breed_temp_embedding))
272
-
273
- # 其餘計算保持不變
274
- noise_similarity = self._calculate_noise_similarity(breed_name, breed_name)
275
- health_score = self._calculate_health_score(breed_name)
276
- health_similarity = 1.0 - abs(health_score - 0.8)
277
-
278
- weights = {
279
- 'description': 0.35,
280
- 'temperament': 0.25,
281
- 'noise': 0.2,
282
- 'health': 0.2
283
- }
284
-
285
- final_score = (
286
- desc_similarity * weights['description'] +
287
- temp_similarity * weights['temperament'] +
288
- noise_similarity * weights['noise'] +
289
- health_similarity * weights['health']
290
- )
291
-
292
- matches.append({
293
- 'breed': breed_name,
294
- 'score': final_score,
295
- 'is_preferred': False,
296
- 'similarity': final_score,
297
- 'reason': "Matched based on description, temperament, noise level, and health score"
298
- })
299
-
300
- return sorted(matches, key=lambda x: -x['score'])[:top_n]
301
-
302
-
303
- def match_user_preference(self, description: str, top_n: int = 10) -> List[Dict]:
304
- """根據用戶描述匹配最適合的品種"""
305
- preferred_breed = self._detect_breed_preference(description)
306
-
307
- matches = []
308
- if preferred_breed:
309
- similar_breeds = self.find_similar_breeds(preferred_breed, top_n=top_n)
310
-
311
- # 首先添加偏好品種
312
- breed_info = next((breed for breed in self.dog_data if breed[1] == preferred_breed), None)
313
- if breed_info:
314
- health_score = self._calculate_health_score(preferred_breed)
315
- noise_info = breed_noise_info.get(preferred_breed, {
316
- "noise_level": "Unknown",
317
- "noise_notes": "No noise information available"
318
- })
319
-
320
- # 偏好品種必定是最高分
321
- matches.append({
322
- 'breed': preferred_breed,
323
- 'score': 1.0,
324
- 'is_preferred': True,
325
- 'similarity': 1.0,
326
- 'health_score': health_score,
327
- 'noise_level': noise_info['noise_level'],
328
- 'reason': "Directly matched your preferred breed"
329
- })
330
-
331
- # 添加相似品種
332
- for breed_name, similarity in similar_breeds:
333
- if breed_name != preferred_breed:
334
- health_score = self._calculate_health_score(breed_name)
335
- noise_info = breed_noise_info.get(breed_name, {
336
- "noise_level": "Unknown",
337
- "noise_notes": "No noise information available"
338
- })
339
-
340
- # 調整相似品種分數計算
341
- base_similarity = similarity * 0.6
342
- health_factor = health_score * 0.2
343
- noise_factor = self._calculate_noise_similarity(preferred_breed, breed_name) * 0.2
344
-
345
- # 確保相似品種分數不會超過偏好品種
346
- final_score = min(0.95, base_similarity + health_factor + noise_factor)
347
-
348
- matches.append({
349
- 'breed': breed_name,
350
- 'score': final_score,
351
- 'is_preferred': False,
352
- 'similarity': similarity,
353
- 'health_score': health_score,
354
- 'noise_level': noise_info['noise_level'],
355
- 'reason': f"Similar to {preferred_breed} in characteristics, health profile, and noise level"
356
- })
357
- else:
358
- matches = self._general_matching(description, top_n)
359
-
360
- return sorted(matches,
361
- key=lambda x: (-int(x.get('is_preferred', False)),
362
- -x['score'], # 降序排列
363
- x['breed']))[:top_n]
364
-
365
- def _detect_breed_preference(self, description: str) -> Optional[str]:
366
- """檢測用戶是否提到特定品種"""
367
- description_lower = description.lower()
368
-
369
- for breed_info in self.dog_data:
370
- breed_name = breed_info[1]
371
- normalized_breed = breed_name.lower().replace('_', ' ')
372
-
373
- if any(phrase in description_lower for phrase in [
374
- f"love {normalized_breed}",
375
- f"like {normalized_breed}",
376
- f"prefer {normalized_breed}",
377
- f"want {normalized_breed}",
378
- normalized_breed
379
- ]):
380
- return breed_name
381
-
382
- return None