Spaces:
Sleeping
Sleeping
Update scoring_calculation_system.py
Browse files- scoring_calculation_system.py +118 -63
scoring_calculation_system.py
CHANGED
@@ -1291,87 +1291,142 @@ def calculate_compatibility_score(breed_info: dict, user_prefs: UserPreferences)
|
|
1291 |
# # print(f"Error in calculate_compatibility_score: {str(e)}")
|
1292 |
# return {k: 0.6 for k in ['space', 'exercise', 'grooming', 'experience', 'health', 'noise', 'overall']}
|
1293 |
|
1294 |
-
|
1295 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1296 |
breed_info['Size'],
|
1297 |
user_prefs.living_space,
|
1298 |
user_prefs.space_for_play,
|
1299 |
breed_info.get('Exercise Needs', 'Moderate')
|
1300 |
-
)
|
1301 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1302 |
breed_info.get('Exercise Needs', 'Moderate'),
|
1303 |
user_prefs.exercise_time
|
1304 |
-
)
|
1305 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1306 |
breed_info.get('Grooming Needs', 'Moderate'),
|
1307 |
user_prefs.grooming_commitment.lower(),
|
1308 |
breed_info['Size']
|
1309 |
-
)
|
1310 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1311 |
breed_info.get('Care Level', 'Moderate'),
|
1312 |
user_prefs.experience_level,
|
1313 |
breed_info.get('Temperament', '')
|
1314 |
-
)
|
1315 |
-
|
1316 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1317 |
}
|
|
|
|
|
|
|
1318 |
|
1319 |
-
#
|
1320 |
-
|
1321 |
-
|
1322 |
-
|
1323 |
-
|
1324 |
-
|
1325 |
-
|
1326 |
-
|
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 |
-
|
1346 |
-
|
1347 |
-
|
1348 |
-
|
1349 |
|
1350 |
-
|
1351 |
-
|
1352 |
-
|
1353 |
-
|
1354 |
-
|
1355 |
-
|
1356 |
-
|
1357 |
-
|
1358 |
-
|
1359 |
-
|
1360 |
-
|
1361 |
-
|
1362 |
-
|
1363 |
-
|
1364 |
-
|
1365 |
-
|
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)
|
1373 |
-
|
|
|
|
|
|
|
1374 |
|
1375 |
except Exception as e:
|
1376 |
-
print(f"
|
|
|
|
|
|
|
|
|
|
|
1377 |
return {k: 0.6 for k in ['space', 'exercise', 'grooming', 'experience', 'health', 'noise', 'overall']}
|
|
|
1291 |
# # print(f"Error in calculate_compatibility_score: {str(e)}")
|
1292 |
# return {k: 0.6 for k in ['space', 'exercise', 'grooming', 'experience', 'health', 'noise', 'overall']}
|
1293 |
|
1294 |
+
#
|
1295 |
+
print("\n=== 開始計算品種相容性分數 ===")
|
1296 |
+
print(f"處理品種: {breed_info.get('Breed', 'Unknown')}")
|
1297 |
+
print(f"品種信息: {breed_info}")
|
1298 |
+
print(f"使用者偏好: {vars(user_prefs)}")
|
1299 |
+
|
1300 |
+
# 1. 計算基礎分數
|
1301 |
+
try:
|
1302 |
+
space_score = calculate_space_score(
|
1303 |
breed_info['Size'],
|
1304 |
user_prefs.living_space,
|
1305 |
user_prefs.space_for_play,
|
1306 |
breed_info.get('Exercise Needs', 'Moderate')
|
1307 |
+
)
|
1308 |
+
print(f"\n空間分數計算結果: {space_score}")
|
1309 |
+
print(f"使用參數 - Size: {breed_info['Size']}, Living Space: {user_prefs.living_space}")
|
1310 |
+
except Exception as e:
|
1311 |
+
print(f"空間分數計算錯誤: {str(e)}")
|
1312 |
+
print(f"錯誤詳情: {traceback.format_exc()}")
|
1313 |
+
raise
|
1314 |
+
|
1315 |
+
try:
|
1316 |
+
exercise_score = calculate_exercise_score(
|
1317 |
breed_info.get('Exercise Needs', 'Moderate'),
|
1318 |
user_prefs.exercise_time
|
1319 |
+
)
|
1320 |
+
print(f"\n運動分數計算結果: {exercise_score}")
|
1321 |
+
print(f"使用參數 - Exercise Needs: {breed_info.get('Exercise Needs', 'Moderate')}, Time: {user_prefs.exercise_time}")
|
1322 |
+
except Exception as e:
|
1323 |
+
print(f"運動分數計算錯誤: {str(e)}")
|
1324 |
+
print(f"錯誤詳情: {traceback.format_exc()}")
|
1325 |
+
raise
|
1326 |
+
|
1327 |
+
try:
|
1328 |
+
grooming_score = calculate_grooming_score(
|
1329 |
breed_info.get('Grooming Needs', 'Moderate'),
|
1330 |
user_prefs.grooming_commitment.lower(),
|
1331 |
breed_info['Size']
|
1332 |
+
)
|
1333 |
+
print(f"\n美容分數計算結果: {grooming_score}")
|
1334 |
+
print(f"使用參數 - Grooming Needs: {breed_info.get('Grooming Needs', 'Moderate')}, Commitment: {user_prefs.grooming_commitment}")
|
1335 |
+
except Exception as e:
|
1336 |
+
print(f"美容分數計算錯誤: {str(e)}")
|
1337 |
+
print(f"錯誤詳情: {traceback.format_exc()}")
|
1338 |
+
raise
|
1339 |
+
|
1340 |
+
try:
|
1341 |
+
experience_score = calculate_experience_score(
|
1342 |
breed_info.get('Care Level', 'Moderate'),
|
1343 |
user_prefs.experience_level,
|
1344 |
breed_info.get('Temperament', '')
|
1345 |
+
)
|
1346 |
+
print(f"\n經驗分數計算結果: {experience_score}")
|
1347 |
+
print(f"使用參數 - Care Level: {breed_info.get('Care Level', 'Moderate')}, Experience: {user_prefs.experience_level}")
|
1348 |
+
except Exception as e:
|
1349 |
+
print(f"經驗分數計算錯誤: {str(e)}")
|
1350 |
+
print(f"錯誤詳情: {traceback.format_exc()}")
|
1351 |
+
raise
|
1352 |
+
|
1353 |
+
try:
|
1354 |
+
health_score = calculate_health_score(breed_info.get('Breed', ''))
|
1355 |
+
print(f"\n健康分數計算結果: {health_score}")
|
1356 |
+
print(f"使用參數 - Breed: {breed_info.get('Breed', '')}")
|
1357 |
+
except Exception as e:
|
1358 |
+
print(f"健康分數計算錯誤: {str(e)}")
|
1359 |
+
print(f"錯誤詳情: {traceback.format_exc()}")
|
1360 |
+
raise
|
1361 |
+
|
1362 |
+
try:
|
1363 |
+
noise_score = calculate_noise_score(breed_info.get('Breed', ''), user_prefs.noise_tolerance)
|
1364 |
+
print(f"\n噪音分數計算結果: {noise_score}")
|
1365 |
+
print(f"使用參數 - Breed: {breed_info.get('Breed', '')}, Noise Tolerance: {user_prefs.noise_tolerance}")
|
1366 |
+
except Exception as e:
|
1367 |
+
print(f"噪音分數計算錯誤: {str(e)}")
|
1368 |
+
print(f"錯誤詳情: {traceback.format_exc()}")
|
1369 |
+
raise
|
1370 |
+
|
1371 |
+
# 整合所有分數
|
1372 |
+
scores = {
|
1373 |
+
'space': space_score,
|
1374 |
+
'exercise': exercise_score,
|
1375 |
+
'grooming': grooming_score,
|
1376 |
+
'experience': experience_score,
|
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:
|
1426 |
+
print(f"\n!!!!! 發生嚴重錯誤 !!!!!")
|
1427 |
+
print(f"錯誤類型: {type(e).__name__}")
|
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']}
|