Spaces:
Running
on
Zero
Running
on
Zero
Update scoring_calculation_system.py
Browse files- scoring_calculation_system.py +62 -37
scoring_calculation_system.py
CHANGED
@@ -1,6 +1,7 @@
|
|
1 |
from dataclasses import dataclass
|
2 |
from breed_health_info import breed_health_info
|
3 |
from breed_noise_info import breed_noise_info
|
|
|
4 |
|
5 |
@dataclass
|
6 |
class UserPreferences:
|
@@ -829,14 +830,6 @@ def calculate_compatibility_score(breed_info: dict, user_prefs: UserPreferences)
|
|
829 |
'noise': calculate_noise_score(breed_info.get('Breed', ''), user_prefs.noise_tolerance)
|
830 |
}
|
831 |
|
832 |
-
# 如果有孩童,計算家庭安全分數
|
833 |
-
if user_prefs.has_children:
|
834 |
-
scores['family_safety'] = calculate_family_safety_score(breed_info, user_prefs.children_age)
|
835 |
-
|
836 |
-
# 計算品種額外加分
|
837 |
-
breed_bonus = calculate_breed_bonus(breed_info, user_prefs)
|
838 |
-
|
839 |
-
# 調整權重配置
|
840 |
weights = {
|
841 |
'space': 0.28,
|
842 |
'exercise': 0.18,
|
@@ -845,43 +838,60 @@ def calculate_compatibility_score(breed_info: dict, user_prefs: UserPreferences)
|
|
845 |
'health': 0.12,
|
846 |
'noise': 0.08
|
847 |
}
|
848 |
-
|
849 |
# 計算基礎加權分數
|
850 |
weighted_score = sum(score * weights[category] for category, score in scores.items())
|
851 |
-
|
852 |
-
#
|
853 |
if user_prefs.has_children:
|
854 |
-
|
855 |
-
|
856 |
-
|
857 |
-
|
858 |
-
|
859 |
-
|
860 |
-
|
861 |
-
|
862 |
-
|
863 |
-
|
864 |
-
|
865 |
-
|
866 |
-
|
867 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
868 |
def amplify_score(score):
|
869 |
-
|
870 |
-
|
|
|
|
|
|
|
|
|
871 |
|
872 |
-
#
|
873 |
-
amplified = pow(adjusted, 2.
|
874 |
|
875 |
-
#
|
876 |
if amplified > 0.85:
|
877 |
amplified = 0.85 + (amplified - 0.85) * 0.6
|
878 |
|
879 |
-
#
|
880 |
-
final_score = max(0.
|
881 |
|
882 |
return round(final_score, 3)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
883 |
|
884 |
-
|
885 |
|
886 |
# except Exception as e:
|
887 |
# print(f"Error details: {str(e)}")
|
@@ -890,7 +900,22 @@ def calculate_compatibility_score(breed_info: dict, user_prefs: UserPreferences)
|
|
890 |
# return {k: 0.5 for k in ['space', 'exercise', 'grooming', 'experience', 'health', 'noise', 'overall']}
|
891 |
|
892 |
except Exception as e:
|
893 |
-
print(f"
|
894 |
-
print(f"
|
895 |
-
print(f"
|
896 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
from dataclasses import dataclass
|
2 |
from breed_health_info import breed_health_info
|
3 |
from breed_noise_info import breed_noise_info
|
4 |
+
import traceback
|
5 |
|
6 |
@dataclass
|
7 |
class UserPreferences:
|
|
|
830 |
'noise': calculate_noise_score(breed_info.get('Breed', ''), user_prefs.noise_tolerance)
|
831 |
}
|
832 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
833 |
weights = {
|
834 |
'space': 0.28,
|
835 |
'exercise': 0.18,
|
|
|
838 |
'health': 0.12,
|
839 |
'noise': 0.08
|
840 |
}
|
841 |
+
|
842 |
# 計算基礎加權分數
|
843 |
weighted_score = sum(score * weights[category] for category, score in scores.items())
|
844 |
+
|
845 |
+
# 如果有孩童,加入家庭安全考量
|
846 |
if user_prefs.has_children:
|
847 |
+
try:
|
848 |
+
family_safety = calculate_family_safety_score(breed_info, user_prefs.children_age)
|
849 |
+
# family_safety 作為調整因子,而不是新的分數項目
|
850 |
+
# 這裡的 0.4 表示 family_safety 最多可以降低 60% 的分數
|
851 |
+
safety_modifier = (family_safety * 0.6) + 0.4
|
852 |
+
weighted_score *= safety_modifier
|
853 |
+
except Exception as e:
|
854 |
+
print(f"Family safety calculation error: {str(e)}")
|
855 |
+
# 發生錯誤時使用較保守的預設值
|
856 |
+
weighted_score *= 0.7
|
857 |
+
|
858 |
+
# 加入品種加分的影響
|
859 |
+
try:
|
860 |
+
breed_bonus = calculate_breed_bonus(breed_info, user_prefs)
|
861 |
+
# breed_bonus 作為加成效果,但限制其影響範圍
|
862 |
+
bonus_modifier = 1 + (breed_bonus * 0.3) # 品種加分最多提升 30%
|
863 |
+
weighted_score *= bonus_modifier
|
864 |
+
except Exception as e:
|
865 |
+
print(f"Breed bonus calculation error: {str(e)}")
|
866 |
+
|
867 |
def amplify_score(score):
|
868 |
+
"""
|
869 |
+
優化後的分數放大函數,確保分數範圍合理且結果一致。
|
870 |
+
主要目的是將分數轉換到更容易理解的範圍,並增加差異性。
|
871 |
+
"""
|
872 |
+
# 基礎調整 - 降低基準點使差異更明顯
|
873 |
+
adjusted = (score - 0.25) * 1.8
|
874 |
|
875 |
+
# 使用較溫和的指數來放大差異,但不會過度誇大
|
876 |
+
amplified = pow(adjusted, 2.2) / 3.5 + score
|
877 |
|
878 |
+
# 處理高分區間,避免分數過度集中
|
879 |
if amplified > 0.85:
|
880 |
amplified = 0.85 + (amplified - 0.85) * 0.6
|
881 |
|
882 |
+
# 確保分數在合理範圍內(0.45-0.95)
|
883 |
+
final_score = max(0.45, min(0.95, amplified))
|
884 |
|
885 |
return round(final_score, 3)
|
886 |
+
|
887 |
+
# 計算最終分數
|
888 |
+
final_score = amplify_score(weighted_score)
|
889 |
+
|
890 |
+
# 準備回傳結果
|
891 |
+
scores = {k: round(v, 4) for k, v in scores.items()}
|
892 |
+
scores['overall'] = round(final_score, 4)
|
893 |
|
894 |
+
return scores
|
895 |
|
896 |
# except Exception as e:
|
897 |
# print(f"Error details: {str(e)}")
|
|
|
900 |
# return {k: 0.5 for k in ['space', 'exercise', 'grooming', 'experience', 'health', 'noise', 'overall']}
|
901 |
|
902 |
except Exception as e:
|
903 |
+
print(f"Critical error in compatibility score calculation:")
|
904 |
+
print(f"Error type: {type(e).__name__}")
|
905 |
+
print(f"Error message: {str(e)}")
|
906 |
+
print(f"Breed info: {breed_info}")
|
907 |
+
print(f"User preferences: {user_prefs.__dict__}")
|
908 |
+
|
909 |
+
# 嘗試返回已計算的分數,若完全失敗則返回預設值
|
910 |
+
try:
|
911 |
+
return scores
|
912 |
+
except:
|
913 |
+
return {
|
914 |
+
'space': 0.7,
|
915 |
+
'exercise': 0.7,
|
916 |
+
'grooming': 0.7,
|
917 |
+
'experience': 0.7,
|
918 |
+
'health': 0.7,
|
919 |
+
'noise': 0.7,
|
920 |
+
'overall': 0.7
|
921 |
+
}
|