Spaces:
Running
Running
Update scoring_calculation_system.py
Browse files- scoring_calculation_system.py +58 -48
scoring_calculation_system.py
CHANGED
@@ -23,6 +23,9 @@ class UserPreferences:
|
|
23 |
climate: str # "cold", "moderate", "hot"
|
24 |
health_sensitivity: str = "medium"
|
25 |
barking_acceptance: str = None
|
|
|
|
|
|
|
26 |
|
27 |
def __post_init__(self):
|
28 |
"""在初始化後運行,用於設置派生值"""
|
@@ -426,19 +429,19 @@ def calculate_compatibility_score(breed_info: dict, user_prefs: UserPreferences)
|
|
426 |
# 重新設計基礎分數矩陣,降低普遍分數以增加區別度
|
427 |
base_scores = {
|
428 |
"Small": {
|
429 |
-
"apartment": 0.
|
430 |
-
"house_small": 0.
|
431 |
-
"house_large": 0.
|
432 |
},
|
433 |
"Medium": {
|
434 |
-
"apartment": 0.
|
435 |
-
"house_small": 0.
|
436 |
-
"house_large": 0.
|
437 |
},
|
438 |
"Large": {
|
439 |
-
"apartment": 0.
|
440 |
-
"house_small": 0.
|
441 |
-
"house_large": 0.
|
442 |
},
|
443 |
"Giant": {
|
444 |
"apartment": 0.10, # 更嚴格的限制
|
@@ -572,7 +575,7 @@ def calculate_compatibility_score(breed_info: dict, user_prefs: UserPreferences)
|
|
572 |
if exercise_time >= breed_level['ideal']:
|
573 |
if exercise_time > breed_level['max']:
|
574 |
# 運動時間過長,適度降分
|
575 |
-
time_score = 0.15 - (0.
|
576 |
else:
|
577 |
time_score = 0.15
|
578 |
elif exercise_time >= breed_level['min']:
|
@@ -582,7 +585,7 @@ def calculate_compatibility_score(breed_info: dict, user_prefs: UserPreferences)
|
|
582 |
else:
|
583 |
# 運動時間不足,根據差距程度扣分
|
584 |
time_ratio = max(0, exercise_time / breed_level['min'])
|
585 |
-
time_score = -0.
|
586 |
|
587 |
# 運動類型匹配度評估
|
588 |
type_score = 0.0
|
@@ -737,19 +740,19 @@ def calculate_compatibility_score(breed_info: dict, user_prefs: UserPreferences)
|
|
737 |
# 基礎分數矩陣 - 大幅擴大不同經驗等級的分數差異
|
738 |
base_scores = {
|
739 |
"High": {
|
740 |
-
"beginner": 0.
|
741 |
-
"intermediate": 0.
|
742 |
"advanced": 1.0 # 資深者能完全勝任
|
743 |
},
|
744 |
"Moderate": {
|
745 |
-
"beginner": 0.
|
746 |
-
"intermediate": 0.
|
747 |
-
"advanced":
|
748 |
},
|
749 |
"Low": {
|
750 |
-
"beginner": 0.
|
751 |
-
"intermediate": 0.
|
752 |
-
"advanced":
|
753 |
}
|
754 |
}
|
755 |
|
@@ -1229,12 +1232,20 @@ def calculate_breed_compatibility_score(scores: dict, user_prefs: UserPreference
|
|
1229 |
}
|
1230 |
|
1231 |
# 動態調整權重
|
|
|
|
|
|
|
|
|
|
|
1232 |
if user_prefs.living_space == 'apartment':
|
1233 |
-
weights['space'] *= 1.
|
1234 |
-
weights['noise'] *= 1.3
|
1235 |
-
|
1236 |
-
|
1237 |
-
|
|
|
|
|
|
|
1238 |
|
1239 |
# 正規化權重
|
1240 |
total_weight = sum(weights.values())
|
@@ -1252,32 +1263,31 @@ def calculate_breed_compatibility_score(scores: dict, user_prefs: UserPreference
|
|
1252 |
|
1253 |
def amplify_score_extreme(score: float) -> float:
|
1254 |
"""
|
1255 |
-
|
1256 |
-
提供更大的分數範圍和更明顯的差異
|
1257 |
|
1258 |
轉換邏輯:
|
1259 |
-
- 極差匹配 (0.0-0.
|
1260 |
-
- 較差匹配 (0.
|
1261 |
-
- 中等匹配 (0.
|
1262 |
-
- 良好匹配 (0.
|
1263 |
-
- 優秀匹配 (0.
|
1264 |
"""
|
1265 |
-
if score < 0.
|
1266 |
-
#
|
1267 |
-
return 0.
|
1268 |
-
elif score < 0.
|
1269 |
# 較差匹配:緩慢增長
|
1270 |
-
position = (score - 0.
|
1271 |
-
return 0.
|
1272 |
-
elif score < 0.
|
1273 |
-
#
|
1274 |
-
position = (score - 0.
|
1275 |
-
return 0.
|
1276 |
-
elif score < 0.
|
1277 |
-
#
|
1278 |
-
position = (score - 0.
|
1279 |
-
return 0.
|
1280 |
else:
|
1281 |
-
#
|
1282 |
-
position = (score - 0.
|
1283 |
-
return 0.
|
|
|
23 |
climate: str # "cold", "moderate", "hot"
|
24 |
health_sensitivity: str = "medium"
|
25 |
barking_acceptance: str = None
|
26 |
+
|
27 |
+
training_commitment: str = "medium" # "low", "medium", "high" - 訓練投入程度
|
28 |
+
living_environment: str = "ground_floor" # "ground_floor", "with_elevator", "walk_up" - 居住環境細節
|
29 |
|
30 |
def __post_init__(self):
|
31 |
"""在初始化後運行,用於設置派生值"""
|
|
|
429 |
# 重新設計基礎分數矩陣,降低普遍分數以增加區別度
|
430 |
base_scores = {
|
431 |
"Small": {
|
432 |
+
"apartment": 0.90, # 降低滿分機會
|
433 |
+
"house_small": 0.85, # 小型犬不應在大空間得到太高分數
|
434 |
+
"house_large": 0.80 # 避免小型犬總是得到最高分
|
435 |
},
|
436 |
"Medium": {
|
437 |
+
"apartment": 0.40, # 維持對公寓環境的限制
|
438 |
+
"house_small": 0.80, # 適中的分數
|
439 |
+
"house_large": 0.90 # 給予合理的獎勵
|
440 |
},
|
441 |
"Large": {
|
442 |
+
"apartment": 0.10, # 加重對大型犬在公寓的限制
|
443 |
+
"house_small": 0.60, # 中等適合度
|
444 |
+
"house_large": 0.95 # 最適合的環境
|
445 |
},
|
446 |
"Giant": {
|
447 |
"apartment": 0.10, # 更嚴格的限制
|
|
|
575 |
if exercise_time >= breed_level['ideal']:
|
576 |
if exercise_time > breed_level['max']:
|
577 |
# 運動時間過長,適度降分
|
578 |
+
time_score = 0.15 - (0.08 * (exercise_time - breed_level['max']) / 30)
|
579 |
else:
|
580 |
time_score = 0.15
|
581 |
elif exercise_time >= breed_level['min']:
|
|
|
585 |
else:
|
586 |
# 運動時間不足,根據差距程度扣分
|
587 |
time_ratio = max(0, exercise_time / breed_level['min'])
|
588 |
+
time_score = -0.20 * (1 - time_ratio)
|
589 |
|
590 |
# 運動類型匹配度評估
|
591 |
type_score = 0.0
|
|
|
740 |
# 基礎分數矩陣 - 大幅擴大不同經驗等級的分數差異
|
741 |
base_scores = {
|
742 |
"High": {
|
743 |
+
"beginner": 0.15, # 降低起始分,高難度品種對新手幾乎不推薦
|
744 |
+
"intermediate": 0.65, # 中級玩家仍需謹慎
|
745 |
"advanced": 1.0 # 資深者能完全勝任
|
746 |
},
|
747 |
"Moderate": {
|
748 |
+
"beginner": 0.40, # 適中難度對新手仍具挑戰
|
749 |
+
"intermediate": 0.85, # 中級玩家較適合
|
750 |
+
"advanced": 0.95 # 資深者完全勝任
|
751 |
},
|
752 |
"Low": {
|
753 |
+
"beginner": 0.85, # 新手友善品種
|
754 |
+
"intermediate": 0.90, # 中級玩家幾乎完全勝任
|
755 |
+
"advanced": 0.85 # 資深者完全勝任
|
756 |
}
|
757 |
}
|
758 |
|
|
|
1232 |
}
|
1233 |
|
1234 |
# 動態調整權重
|
1235 |
+
if user_prefs.has_children:
|
1236 |
+
if user_prefs.children_age == 'toddler':
|
1237 |
+
weights['noise'] *= 1.5 # 幼童對噪音更敏感
|
1238 |
+
weights['experience'] *= 1.3 # 需要更有經驗的飼主
|
1239 |
+
|
1240 |
if user_prefs.living_space == 'apartment':
|
1241 |
+
weights['space'] *= 1.4 # 公寓空間限制更重要
|
1242 |
+
weights['noise'] *= 1.3 # 噪音問題更重要
|
1243 |
+
|
1244 |
+
# 運動時間極端情況
|
1245 |
+
if user_prefs.exercise_time < 30:
|
1246 |
+
weights['exercise'] *= 1.5 # 運動時間極少時加重權重
|
1247 |
+
elif user_prefs.exercise_time > 150:
|
1248 |
+
weights['exercise'] *= 1.3 # 運動時間充足時略微加重
|
1249 |
|
1250 |
# 正規化權重
|
1251 |
total_weight = sum(weights.values())
|
|
|
1263 |
|
1264 |
def amplify_score_extreme(score: float) -> float:
|
1265 |
"""
|
1266 |
+
改進的分數轉換函數,提供更大的分數區間和更明顯的差異
|
|
|
1267 |
|
1268 |
轉換邏輯:
|
1269 |
+
- 極差匹配 (0.0-0.2) -> 50-60%
|
1270 |
+
- 較差匹配 (0.2-0.4) -> 60-70%
|
1271 |
+
- 中等匹配 (0.4-0.6) -> 70-82%
|
1272 |
+
- 良好匹配 (0.6-0.8) -> 82-90%
|
1273 |
+
- 優秀匹配 (0.8-1.0) -> 90-98%
|
1274 |
"""
|
1275 |
+
if score < 0.2:
|
1276 |
+
# 極差匹配:更低的起始分數
|
1277 |
+
return 0.50 + (score / 0.2) * 0.10
|
1278 |
+
elif score < 0.4:
|
1279 |
# 較差匹配:緩慢增長
|
1280 |
+
position = (score - 0.2) / 0.2
|
1281 |
+
return 0.60 + position * 0.10
|
1282 |
+
elif score < 0.6:
|
1283 |
+
# 中等匹配:較大的分數增長
|
1284 |
+
position = (score - 0.4) / 0.2
|
1285 |
+
return 0.70 + position * 0.12
|
1286 |
+
elif score < 0.8:
|
1287 |
+
# 良好匹配:快速增長
|
1288 |
+
position = (score - 0.6) / 0.2
|
1289 |
+
return 0.82 + position * 0.08
|
1290 |
else:
|
1291 |
+
# 優秀匹配:達到更高分數
|
1292 |
+
position = (score - 0.8) / 0.2
|
1293 |
+
return 0.90 + position * 0.08
|