Spaces:
Running
Running
Update scoring_calculation_system.py
Browse files- scoring_calculation_system.py +71 -86
scoring_calculation_system.py
CHANGED
@@ -601,58 +601,67 @@ def calculate_compatibility_score(breed_info: dict, user_prefs: UserPreferences)
|
|
601 |
|
602 |
|
603 |
def calculate_experience_score(care_level: str, user_experience: str, temperament: str) -> float:
|
604 |
-
#
|
605 |
base_scores = {
|
606 |
"High": {
|
607 |
-
"beginner": 0.
|
608 |
-
"intermediate": 0.
|
609 |
-
"advanced": 0.
|
610 |
},
|
611 |
"Moderate": {
|
612 |
-
"beginner": 0.
|
613 |
-
"intermediate": 0.
|
614 |
-
"advanced": 0.
|
615 |
},
|
616 |
"Low": {
|
617 |
-
"beginner": 0.
|
618 |
-
"intermediate": 0.
|
619 |
-
"advanced": 0.
|
620 |
}
|
621 |
}
|
622 |
|
|
|
623 |
base_score = base_scores.get(care_level, base_scores["Moderate"])[user_experience]
|
624 |
|
625 |
-
#
|
626 |
-
|
627 |
-
|
628 |
-
|
629 |
-
|
630 |
-
|
631 |
-
|
632 |
-
|
633 |
-
|
634 |
-
|
635 |
-
|
636 |
-
'positive': {'intelligent': 0.06, 'independent': 0.06, 'protective': 0.05},
|
637 |
-
'negative': {'aggressive': -0.1, 'nervous': -0.08, 'unpredictable': -0.08}
|
638 |
-
}
|
639 |
}
|
640 |
|
641 |
-
|
642 |
-
|
643 |
|
644 |
-
#
|
645 |
-
|
646 |
-
|
647 |
-
|
648 |
-
|
649 |
-
|
650 |
-
|
651 |
-
|
652 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
653 |
|
654 |
-
|
655 |
-
|
|
|
|
|
|
|
656 |
|
657 |
|
658 |
def calculate_health_score(breed_name: str) -> float:
|
@@ -867,68 +876,44 @@ def calculate_compatibility_score(breed_info: dict, user_prefs: UserPreferences)
|
|
867 |
'noise': calculate_noise_score(breed_info.get('Breed', ''), user_prefs.noise_tolerance)
|
868 |
}
|
869 |
|
870 |
-
|
871 |
-
breed_bonus = calculate_breed_bonus(breed_info, user_prefs)
|
872 |
-
|
873 |
-
# 3. 如果有孩童,計算安全分數,但不直接修改基礎分數
|
874 |
if user_prefs.has_children:
|
875 |
-
family_safety = calculate_family_safety_score(breed_info, user_prefs.children_age)
|
876 |
-
|
877 |
-
# 創建安全性調整係數
|
878 |
-
safety_adjustments = {
|
879 |
-
'toddler': 0.6, # 對幼童最嚴格
|
880 |
-
'school_age': 0.75, # 學齡兒童較寬鬆
|
881 |
-
'teenager': 0.85 # 青少年最寬鬆
|
882 |
-
}
|
883 |
-
|
884 |
-
# 調整權重而不是直接修改分數
|
885 |
-
safety_weight = safety_adjustments[user_prefs.children_age]
|
886 |
-
|
887 |
-
# 修改最終加權計算方式
|
888 |
weights = {
|
889 |
-
'space': 0.22,
|
890 |
-
'exercise': 0.15,
|
891 |
-
'grooming': 0.10,
|
892 |
-
'experience': 0.
|
893 |
-
'health': 0.10,
|
894 |
-
'noise': 0.
|
|
|
895 |
}
|
896 |
-
|
897 |
-
# 加入安全性權重
|
898 |
-
final_score = (
|
899 |
-
sum(score * weights[category] for category, score in scores.items()) * safety_weight +
|
900 |
-
family_safety * (1 - safety_weight)
|
901 |
-
)
|
902 |
-
|
903 |
-
# 品種加分也要考慮安全性
|
904 |
-
breed_bonus *= family_safety
|
905 |
-
|
906 |
else:
|
907 |
-
# 原有的權重
|
908 |
weights = {
|
909 |
-
'space': 0.28,
|
910 |
-
'exercise': 0.18,
|
911 |
-
'grooming': 0.12,
|
912 |
-
'experience': 0.22,
|
913 |
-
'health': 0.12,
|
914 |
-
'noise': 0.08
|
915 |
}
|
916 |
-
final_score = sum(score * weights[category] for category, score in scores.items())
|
917 |
|
918 |
-
#
|
919 |
-
|
|
|
|
|
|
|
|
|
920 |
|
921 |
# 5. 最終分數調整
|
922 |
def amplify_score(score):
|
923 |
-
adjusted = (score - 0.
|
924 |
-
amplified = pow(adjusted, 2.
|
925 |
-
if amplified > 0.85:
|
926 |
-
amplified = 0.85 + (amplified - 0.85) * 0.6
|
927 |
return max(0.45, min(0.95, amplified))
|
928 |
|
929 |
final_score = amplify_score(final_score)
|
930 |
-
|
931 |
-
# 6.
|
932 |
scores = {k: round(v, 4) for k, v in scores.items()}
|
933 |
scores['overall'] = round(final_score, 4)
|
934 |
|
|
|
601 |
|
602 |
|
603 |
def calculate_experience_score(care_level: str, user_experience: str, temperament: str) -> float:
|
604 |
+
# 首先建立基礎分數矩陣,確保不同難度和經驗等級有明顯差異
|
605 |
base_scores = {
|
606 |
"High": {
|
607 |
+
"beginner": 0.2, # 高難度品種對新手極具挑戰
|
608 |
+
"intermediate": 0.5, # 中級玩家有一定掌握能力
|
609 |
+
"advanced": 0.7 # 專家也需要謹慎對待
|
610 |
},
|
611 |
"Moderate": {
|
612 |
+
"beginner": 0.4,
|
613 |
+
"intermediate": 0.65,
|
614 |
+
"advanced": 0.8
|
615 |
},
|
616 |
"Low": {
|
617 |
+
"beginner": 0.6,
|
618 |
+
"intermediate": 0.75,
|
619 |
+
"advanced": 0.85
|
620 |
}
|
621 |
}
|
622 |
|
623 |
+
# 獲取基礎分數
|
624 |
base_score = base_scores.get(care_level, base_scores["Moderate"])[user_experience]
|
625 |
|
626 |
+
# 品種特性評估
|
627 |
+
temperament_lower = temperament.lower()
|
628 |
+
|
629 |
+
# 計算品種難度係數
|
630 |
+
difficulty_traits = {
|
631 |
+
'aggressive': 0.3,
|
632 |
+
'dominant': 0.25,
|
633 |
+
'stubborn': 0.25,
|
634 |
+
'independent': 0.2,
|
635 |
+
'protective': 0.2,
|
636 |
+
'strong-willed': 0.15
|
|
|
|
|
|
|
637 |
}
|
638 |
|
639 |
+
difficulty_score = sum(value for trait, value in difficulty_traits.items()
|
640 |
+
if trait in temperament_lower)
|
641 |
|
642 |
+
# 根據經驗等級調整難度的影響
|
643 |
+
experience_modifiers = {
|
644 |
+
"beginner": 1.2, # 新手受難度影響最大
|
645 |
+
"intermediate": 0.8, # 中級玩家受中等影響
|
646 |
+
"advanced": 0.5 # 專家受較小影響但仍然存在
|
647 |
+
}
|
648 |
+
|
649 |
+
# 應用經驗調整
|
650 |
+
difficulty_impact = difficulty_score * experience_modifiers[user_experience]
|
651 |
+
adjusted_score = base_score * (1 - difficulty_impact)
|
652 |
+
|
653 |
+
# 特殊品種類型的額外調整
|
654 |
+
breed_type_penalties = {
|
655 |
+
'terrier': {'beginner': -0.15, 'intermediate': -0.08, 'advanced': -0.04},
|
656 |
+
'working': {'beginner': -0.2, 'intermediate': -0.1, 'advanced': -0.05},
|
657 |
+
'guard': {'beginner': -0.25, 'intermediate': -0.12, 'advanced': -0.06}
|
658 |
+
}
|
659 |
|
660 |
+
for breed_type, penalties in breed_type_penalties.items():
|
661 |
+
if breed_type in temperament_lower:
|
662 |
+
adjusted_score += penalties[user_experience]
|
663 |
+
|
664 |
+
return max(0.2, min(0.95, adjusted_score))
|
665 |
|
666 |
|
667 |
def calculate_health_score(breed_name: str) -> float:
|
|
|
876 |
'noise': calculate_noise_score(breed_info.get('Breed', ''), user_prefs.noise_tolerance)
|
877 |
}
|
878 |
|
879 |
+
# 2. 計算品種加分
|
|
|
|
|
|
|
880 |
if user_prefs.has_children:
|
881 |
+
scores['family_safety'] = calculate_family_safety_score(breed_info, user_prefs.children_age)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
882 |
weights = {
|
883 |
+
'space': 0.22,
|
884 |
+
'exercise': 0.15,
|
885 |
+
'grooming': 0.10,
|
886 |
+
'experience': 0.20,
|
887 |
+
'health': 0.10,
|
888 |
+
'noise': 0.08,
|
889 |
+
'family_safety': 0.15
|
890 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
891 |
else:
|
|
|
892 |
weights = {
|
893 |
+
'space': 0.28,
|
894 |
+
'exercise': 0.18,
|
895 |
+
'grooming': 0.12,
|
896 |
+
'experience': 0.22,
|
897 |
+
'health': 0.12,
|
898 |
+
'noise': 0.08
|
899 |
}
|
|
|
900 |
|
901 |
+
# 3. 計算加權分數
|
902 |
+
weighted_score = sum(score * weights[category] for category, score in scores.items())
|
903 |
+
|
904 |
+
# 4. 加入品種加分影響(限制在合理範圍內)
|
905 |
+
breed_bonus = calculate_breed_bonus(breed_info, user_prefs)
|
906 |
+
final_score = weighted_score * (1 + breed_bonus * 0.2)
|
907 |
|
908 |
# 5. 最終分數調整
|
909 |
def amplify_score(score):
|
910 |
+
adjusted = (score - 0.3) * 1.6
|
911 |
+
amplified = pow(adjusted, 2.5) / 4.0 + score
|
|
|
|
|
912 |
return max(0.45, min(0.95, amplified))
|
913 |
|
914 |
final_score = amplify_score(final_score)
|
915 |
+
|
916 |
+
# 6. 整理回傳結果
|
917 |
scores = {k: round(v, 4) for k, v in scores.items()}
|
918 |
scores['overall'] = round(final_score, 4)
|
919 |
|