DawnC commited on
Commit
9950a7e
1 Parent(s): 0e8e44e

Update scoring_calculation_system.py

Browse files
Files changed (1) hide show
  1. scoring_calculation_system.py +69 -36
scoring_calculation_system.py CHANGED
@@ -1377,49 +1377,83 @@ def calculate_compatibility_score(breed_info: dict, user_prefs: UserPreferences)
1377
  'health': health_score,
1378
  'noise': noise_score
1379
  }
1380
- print("\n=== 所有基礎分數 ===")
1381
- for category, score in scores.items():
1382
- print(f"{category}: {score}")
1383
-
1384
- # 計算加權分數
1385
- weights = {
1386
- 'space': 0.28,
1387
- 'exercise': 0.18,
1388
- 'grooming': 0.12,
1389
- 'experience': 0.22,
1390
- 'health': 0.12,
1391
- 'noise': 0.08
1392
- }
1393
-
1394
- weighted_score = sum(score * weights[category] for category, score in scores.items())
1395
- print(f"\n加權前總分: {weighted_score}")
1396
-
1397
- # 分數放大
1398
- def amplify_score(score):
1399
- print(f"\n開始分數放大,原始分數: {score}")
1400
- adjusted = (score - 0.35) * 1.8
1401
- print(f"調整後分數 (adjusted): {adjusted}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1402
 
1403
- amplified = pow(adjusted, 3.2) / 5.8 + score
1404
- print(f"放大後分數 (amplified): {amplified}")
1405
 
1406
- if amplified > 0.90:
1407
- amplified = 0.90 + (amplified - 0.90) * 0.5
1408
 
1409
- final = max(0.55, min(0.95, amplified))
1410
- print(f"最終分數: {final}")
1411
- return round(final, 3)
1412
 
1413
- final_score = amplify_score(weighted_score)
1414
- print(f"\n=== 最終計算結果 ===")
1415
- print(f"最終分數: {final_score}")
 
1416
 
1417
  # 準備返回結果
1418
  scores = {k: round(v, 4) for k, v in scores.items()}
1419
- scores['overall'] = round(final_score, 4)
1420
 
1421
- print("\n=== 返回結果 ===")
1422
- print(scores)
1423
  return scores
1424
 
1425
  except Exception as e:
@@ -1428,5 +1462,4 @@ def calculate_compatibility_score(breed_info: dict, user_prefs: UserPreferences)
1428
  print(f"錯誤訊息: {str(e)}")
1429
  print(f"完整錯誤追蹤:")
1430
  print(traceback.format_exc())
1431
- print("\n返回默認值...")
1432
  return {k: 0.6 for k in ['space', 'exercise', 'grooming', 'experience', 'health', 'noise', 'overall']}
 
1377
  'health': health_score,
1378
  'noise': noise_score
1379
  }
1380
+
1381
+ # 首先處理極端情況
1382
+ def check_critical_issues(scores: dict, breed_info: dict) -> float:
1383
+ """
1384
+ 檢查關鍵問題並計算懲罰係數
1385
+ """
1386
+ penalty = 1.0
1387
+
1388
+ # 檢查經驗分數 - 如果太低表示品種太難駕馭
1389
+ if scores['experience'] < 0.3:
1390
+ penalty *= 0.8
1391
+
1392
+ # 檢查空間分數 - 特別是對公寓的情況
1393
+ if user_prefs.living_space == 'apartment' and scores['space'] < 0.4:
1394
+ penalty *= 0.85
1395
+
1396
+ # 檢查健康分數 - 健康問題是重要考量
1397
+ if scores['health'] < 0.4:
1398
+ penalty *= 0.9
1399
+
1400
+ return penalty
1401
+
1402
+ # 計算權重和加權分數
1403
+ def calculate_weighted_score(scores: dict) -> float:
1404
+ """
1405
+ 使用動態權重計算加權分數
1406
+ """
1407
+ base_weights = {
1408
+ 'space': 0.28,
1409
+ 'exercise': 0.18,
1410
+ 'grooming': 0.12,
1411
+ 'experience': 0.22,
1412
+ 'health': 0.12,
1413
+ 'noise': 0.08
1414
+ }
1415
+
1416
+ # 根據居住環境調整權重
1417
+ if user_prefs.living_space == 'apartment':
1418
+ base_weights['space'] *= 1.2
1419
+ base_weights['noise'] *= 1.2
1420
+
1421
+ # 根據經驗等級調整權重
1422
+ if user_prefs.experience_level == 'beginner':
1423
+ base_weights['experience'] *= 1.3
1424
+
1425
+ # 重新正規化權重
1426
+ total_weight = sum(base_weights.values())
1427
+ weights = {k: v/total_weight for k, v in base_weights.items()}
1428
+
1429
+ # 計算加權分數
1430
+ return sum(score * weights[category] for category, score in scores.items())
1431
+
1432
+ # 計算最終分數
1433
+ def calculate_final_score(base_score: float, penalty: float) -> float:
1434
+ """
1435
+ 計算並調整最終分數,確保合理的分數分布
1436
+ """
1437
+ # 應用懲罰係數
1438
+ adjusted_score = base_score * penalty
1439
 
1440
+ # 將分數映射到期望的範圍(0.55-0.95)
1441
+ mapped_score = 0.55 + (adjusted_score * 0.4)
1442
 
1443
+ # 確保分數在合理範圍內
1444
+ final = max(0.55, min(0.95, mapped_score))
1445
 
1446
+ return round(final, 4)
 
 
1447
 
1448
+ # 執行計算流程
1449
+ penalty = check_critical_issues(scores, breed_info)
1450
+ weighted_score = calculate_weighted_score(scores)
1451
+ final_score = calculate_final_score(weighted_score, penalty)
1452
 
1453
  # 準備返回結果
1454
  scores = {k: round(v, 4) for k, v in scores.items()}
1455
+ scores['overall'] = final_score
1456
 
 
 
1457
  return scores
1458
 
1459
  except Exception as e:
 
1462
  print(f"錯誤訊息: {str(e)}")
1463
  print(f"完整錯誤追蹤:")
1464
  print(traceback.format_exc())
 
1465
  return {k: 0.6 for k in ['space', 'exercise', 'grooming', 'experience', 'health', 'noise', 'overall']}