Spaces:
Running
on
Zero
Running
on
Zero
Update scoring_calculation_system.py
Browse files- 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 |
-
|
| 1381 |
-
|
| 1382 |
-
|
| 1383 |
-
|
| 1384 |
-
|
| 1385 |
-
|
| 1386 |
-
|
| 1387 |
-
|
| 1388 |
-
|
| 1389 |
-
'experience'
|
| 1390 |
-
|
| 1391 |
-
|
| 1392 |
-
|
| 1393 |
-
|
| 1394 |
-
|
| 1395 |
-
|
| 1396 |
-
|
| 1397 |
-
|
| 1398 |
-
|
| 1399 |
-
|
| 1400 |
-
|
| 1401 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1402 |
|
| 1403 |
-
|
| 1404 |
-
|
| 1405 |
|
| 1406 |
-
|
| 1407 |
-
|
| 1408 |
|
| 1409 |
-
|
| 1410 |
-
print(f"最終分數: {final}")
|
| 1411 |
-
return round(final, 3)
|
| 1412 |
|
| 1413 |
-
|
| 1414 |
-
|
| 1415 |
-
|
|
|
|
| 1416 |
|
| 1417 |
# 準備返回結果
|
| 1418 |
scores = {k: round(v, 4) for k, v in scores.items()}
|
| 1419 |
-
scores['overall'] =
|
| 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']}
|