DawnC commited on
Commit
f604683
1 Parent(s): 78534c5

Update scoring_calculation_system.py

Browse files
Files changed (1) hide show
  1. scoring_calculation_system.py +46 -45
scoring_calculation_system.py CHANGED
@@ -1316,56 +1316,57 @@ def calculate_compatibility_score(breed_info: dict, user_prefs: UserPreferences)
1316
  'noise': calculate_noise_score(breed_info.get('Breed', ''), user_prefs.noise_tolerance)
1317
  }
1318
 
1319
- # 2. 設定基礎權重
1320
- weights = {
1321
- 'space': 0.28,
1322
- 'exercise': 0.18,
1323
- 'grooming': 0.12,
1324
- 'experience': 0.22,
1325
- 'health': 0.12,
1326
- 'noise': 0.08
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1327
  }
1328
 
1329
- # 3. 改進的分數整合方法
1330
- def calculate_final_score(scores: dict, weights: dict, breed_info: dict) -> float:
1331
- weighted_components = []
 
 
1332
 
1333
- for category, score in scores.items():
1334
- # 先調整個別分數,保持差異性
1335
- if score < 0.4:
1336
- adjusted = score * 0.7 # 低分更低
1337
- elif score > 0.8:
1338
- adjusted = score * 1.1 # 高分更高
1339
- else:
1340
- adjusted = score # 中間分數保持不變
1341
 
1342
- # 應用權重
1343
- weighted_components.append(adjusted * weights[category])
1344
-
1345
- # 計算基礎加權分數
1346
- base_score = sum(weighted_components)
1347
-
1348
- # 關鍵條件檢查和調整
1349
- size = breed_info['Size']
1350
- temperament = breed_info.get('Temperament', '').lower()
1351
-
1352
- # 應用條件調整
1353
- if user_prefs.living_space == 'apartment':
1354
- if size in ['Large', 'Giant']:
1355
- base_score *= 0.8
1356
- elif 'high energy' in temperament:
1357
- base_score *= 0.85
1358
-
1359
- if user_prefs.experience_level == 'beginner':
1360
- if any(trait in temperament for trait in ['stubborn', 'dominant']):
1361
- base_score *= 0.85
1362
-
1363
- # 最終分數映射到合理範圍
1364
- final_score = 0.6 + (base_score - 0.5) * 1.5
1365
- return max(0.55, min(0.95, final_score))
1366
-
1367
  # 4. 計算最終分數
1368
- final_score = calculate_final_score(scores, weights, breed_info)
 
 
 
 
 
 
 
 
1369
 
1370
  # 5. 準備返回結果
1371
  scores['overall'] = round(final_score, 4)
 
1316
  'noise': calculate_noise_score(breed_info.get('Breed', ''), user_prefs.noise_tolerance)
1317
  }
1318
 
1319
+ # 2. 直接識別關鍵問題
1320
+ penalties = []
1321
+
1322
+ # 檢查重要的不適配情況
1323
+ if user_prefs.living_space == 'apartment':
1324
+ if breed_info['Size'] == 'Large':
1325
+ penalties.append(0.75) # 大型犬在公寓
1326
+ elif breed_info['Size'] == 'Giant':
1327
+ penalties.append(0.65) # 超大型犬在公寓
1328
+
1329
+ if user_prefs.experience_level == 'beginner':
1330
+ temperament = breed_info.get('Temperament', '').lower()
1331
+ if 'stubborn' in temperament or 'dominant' in temperament:
1332
+ penalties.append(0.80) # 新手配到難訓練的犬種
1333
+
1334
+ # 3. 計算基礎分數
1335
+ importance_factors = {
1336
+ 'space': 3.0, # 空間評分的重要性提高
1337
+ 'exercise': 2.0, # 運動需求次之
1338
+ 'experience': 2.5, # 經驗要求很重要
1339
+ 'health': 1.5, # 健康因素適中
1340
+ 'grooming': 1.2, # 美容需求較次要
1341
+ 'noise': 1.8 # 噪音也要考慮
1342
  }
1343
 
1344
+ # 調整後的分數計算
1345
+ adjusted_scores = []
1346
+ for category, score in scores.items():
1347
+ # 根據重要性調整分數
1348
+ factor = importance_factors[category]
1349
 
1350
+ # 分數調整:保持差異性
1351
+ if score < 0.4:
1352
+ adjusted = score * 0.7 # 低分更低
1353
+ elif score > 0.8:
1354
+ adjusted = score * 1.1 # 高分更高
1355
+ else:
1356
+ adjusted = score
 
1357
 
1358
+ adjusted_scores.append(adjusted * factor)
1359
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1360
  # 4. 計算最終分數
1361
+ base_score = sum(adjusted_scores) / sum(importance_factors.values())
1362
+
1363
+ # 應用懲罰(如果有)
1364
+ final_score = base_score
1365
+ if penalties:
1366
+ final_score *= min(penalties) # 使用最嚴重的懲罰
1367
+
1368
+ # 確保分數在合理範圍內
1369
+ final_score = max(0.55, min(0.95, final_score))
1370
 
1371
  # 5. 準備返回結果
1372
  scores['overall'] = round(final_score, 4)