Spaces:
Sleeping
Sleeping
Update scoring_calculation_system.py
Browse files- scoring_calculation_system.py +54 -88
scoring_calculation_system.py
CHANGED
@@ -1316,108 +1316,74 @@ 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 |
-
|
1321 |
-
|
1322 |
-
|
1323 |
-
|
1324 |
-
|
1325 |
-
|
1326 |
-
|
1327 |
-
|
1328 |
-
'grooming': 0.12,
|
1329 |
-
'experience': 0.22,
|
1330 |
-
'health': 0.12,
|
1331 |
-
'noise': 0.08
|
1332 |
-
}
|
1333 |
-
|
1334 |
-
# 公寓住戶需要更注重空間和噪音
|
1335 |
-
if user_prefs.living_space == 'apartment':
|
1336 |
-
if scores['space'] < 0.6: # 空間評分不理想時更重視
|
1337 |
-
weights['space'] *= 1.25
|
1338 |
-
weights['noise'] *= 1.15
|
1339 |
-
|
1340 |
-
# 新手飼主需要更注重經驗要求
|
1341 |
-
if user_prefs.experience_level == 'beginner':
|
1342 |
-
if scores['experience'] < 0.5: # 經驗需求較高時更重視
|
1343 |
-
weights['experience'] *= 1.3
|
1344 |
-
|
1345 |
-
# 有孩童時的特殊考量
|
1346 |
-
if user_prefs.has_children:
|
1347 |
-
child_age_weights = {
|
1348 |
-
'toddler': {'experience': 1.3, 'health': 1.2, 'noise': 1.2},
|
1349 |
-
'school_age': {'experience': 1.2, 'health': 1.1, 'noise': 1.1},
|
1350 |
-
'teenager': {'experience': 1.1, 'health': 1.05, 'noise': 1.05}
|
1351 |
-
}
|
1352 |
-
|
1353 |
-
age_adjustments = child_age_weights.get(user_prefs.children_age,
|
1354 |
-
child_age_weights['school_age'])
|
1355 |
-
for key, mult in age_adjustments.items():
|
1356 |
-
weights[key] *= mult
|
1357 |
-
|
1358 |
-
# 重新正規化權重總和為1
|
1359 |
-
total = sum(weights.values())
|
1360 |
-
return {k: v/total for k, v in weights.items()}
|
1361 |
|
1362 |
-
# 3.
|
1363 |
-
|
|
|
|
|
|
|
1364 |
|
1365 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1366 |
weighted_score = sum(score * weights[category] for category, score in scores.items())
|
1367 |
|
1368 |
-
# 5.
|
1369 |
-
def amplify_score(raw_score, scores
|
1370 |
"""
|
1371 |
-
|
1372 |
-
考慮原始分數的分布和關鍵條件的影響。
|
1373 |
"""
|
1374 |
-
#
|
1375 |
-
|
1376 |
-
|
1377 |
-
# 使用S型曲線轉換,使中間範圍的差異更明顯
|
1378 |
-
transformed = 1 / (1 + math.exp(-6 * (normalized - 0.5)))
|
1379 |
|
1380 |
-
#
|
1381 |
-
score = 0.
|
1382 |
|
1383 |
-
#
|
1384 |
-
|
1385 |
-
|
1386 |
-
(scores['noise'] < 0.3 and user_prefs.living_space == 'apartment', 0.85),
|
1387 |
-
(scores['health'] < 0.3, 0.9),
|
1388 |
-
(scores['space'] < 0.3 and user_prefs.living_space == 'apartment', 0.85)
|
1389 |
-
]
|
1390 |
|
1391 |
-
#
|
1392 |
-
|
1393 |
-
if condition:
|
1394 |
-
score *= factor
|
1395 |
-
|
1396 |
-
return round(max(0.6, min(0.95, score)), 4)
|
1397 |
|
1398 |
# 6. 計算最終分數
|
1399 |
-
final_score = amplify_score(weighted_score, scores
|
1400 |
|
1401 |
-
# 7.
|
1402 |
-
|
1403 |
-
|
1404 |
-
|
1405 |
-
|
1406 |
-
|
1407 |
-
|
1408 |
-
|
1409 |
-
|
1410 |
-
|
1411 |
-
|
1412 |
-
|
1413 |
-
|
1414 |
-
|
1415 |
|
1416 |
-
# 8.
|
1417 |
-
|
|
|
1418 |
|
1419 |
-
|
1420 |
-
return {k: round(v, 4) for k, v in final_scores.items()}
|
1421 |
|
1422 |
except Exception as e:
|
1423 |
print(f"Error details: {str(e)}")
|
|
|
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 |
+
if user_prefs.living_space == 'apartment':
|
1331 |
+
if scores['space'] < 0.7: # 空間不足時加重權重
|
1332 |
+
weights['space'] *= 1.3
|
1333 |
+
weights['noise'] *= 1.2
|
1334 |
|
1335 |
+
if user_prefs.experience_level == 'beginner':
|
1336 |
+
if scores['experience'] < 0.6: # 經驗要求高時加重權重
|
1337 |
+
weights['experience'] *= 1.4
|
1338 |
+
|
1339 |
+
# 重新正規化權重
|
1340 |
+
total = sum(weights.values())
|
1341 |
+
weights = {k: v/total for k, v in weights.items()}
|
1342 |
+
|
1343 |
+
# 4. 計算加權分數
|
1344 |
weighted_score = sum(score * weights[category] for category, score in scores.items())
|
1345 |
|
1346 |
+
# 5. 新的分數放大函數
|
1347 |
+
def amplify_score(raw_score, scores):
|
1348 |
"""
|
1349 |
+
直接的分數轉換,保持分數差異性
|
|
|
1350 |
"""
|
1351 |
+
# 基礎分數調整:擴大差異
|
1352 |
+
adjusted = (raw_score - 0.5) * 1.8
|
|
|
|
|
|
|
1353 |
|
1354 |
+
# 線性轉換到目標範圍
|
1355 |
+
score = 0.7 + adjusted * 0.5
|
1356 |
|
1357 |
+
# 處理極端情況
|
1358 |
+
if any(v < 0.4 for v in scores.values()):
|
1359 |
+
score *= 0.85 # 有極低分項目時降低整體分數
|
|
|
|
|
|
|
|
|
1360 |
|
1361 |
+
# 確保分數在合理範圍內
|
1362 |
+
return max(0.55, min(0.95, score))
|
|
|
|
|
|
|
|
|
1363 |
|
1364 |
# 6. 計算最終分數
|
1365 |
+
final_score = amplify_score(weighted_score, scores)
|
1366 |
|
1367 |
+
# 7. 品種特定調整
|
1368 |
+
breed_name = breed_info.get('Breed', '')
|
1369 |
+
temperament = breed_info.get('Temperament', '').lower()
|
1370 |
+
|
1371 |
+
# 根據具體條件進行品種特定的調整
|
1372 |
+
if user_prefs.living_space == 'apartment':
|
1373 |
+
if breed_info['Size'] in ['Large', 'Giant']:
|
1374 |
+
final_score *= 0.85
|
1375 |
+
elif 'high energy' in temperament or 'very active' in temperament:
|
1376 |
+
final_score *= 0.9
|
1377 |
+
|
1378 |
+
if user_prefs.experience_level == 'beginner':
|
1379 |
+
if any(trait in temperament for trait in ['dominant', 'stubborn', 'independent']):
|
1380 |
+
final_score *= 0.88
|
1381 |
|
1382 |
+
# 8. 整理並返回結果
|
1383 |
+
scores = {k: round(v, 4) for k, v in scores.items()}
|
1384 |
+
scores['overall'] = round(final_score, 4)
|
1385 |
|
1386 |
+
return scores
|
|
|
1387 |
|
1388 |
except Exception as e:
|
1389 |
print(f"Error details: {str(e)}")
|