Spaces:
Running
on
Zero
Running
on
Zero
Update scoring_calculation_system.py
Browse files- scoring_calculation_system.py +38 -53
scoring_calculation_system.py
CHANGED
@@ -1326,66 +1326,51 @@ def calculate_compatibility_score(breed_info: dict, user_prefs: UserPreferences)
|
|
1326 |
'noise': 0.08
|
1327 |
}
|
1328 |
|
1329 |
-
# 3.
|
1330 |
-
|
1331 |
-
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1356 |
|
1357 |
-
#
|
1358 |
-
|
1359 |
-
score *= 0.85 # 有極低分項目時降低整體分數
|
1360 |
|
1361 |
-
#
|
1362 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1363 |
|
1364 |
-
#
|
1365 |
-
final_score =
|
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)}")
|
1390 |
-
print(f"breed_info: {breed_info}")
|
1391 |
return {k: 0.6 for k in ['space', 'exercise', 'grooming', 'experience', 'health', 'noise', 'overall']}
|
|
|
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)
|
1372 |
+
return {k: round(v, 4) for k, v in scores.items()}
|
|
|
1373 |
|
1374 |
except Exception as e:
|
1375 |
print(f"Error details: {str(e)}")
|
|
|
1376 |
return {k: 0.6 for k in ['space', 'exercise', 'grooming', 'experience', 'health', 'noise', 'overall']}
|