Spaces:
Running
on
Zero
Running
on
Zero
Update scoring_calculation_system.py
Browse files- scoring_calculation_system.py +30 -59
scoring_calculation_system.py
CHANGED
@@ -929,61 +929,49 @@ def calculate_compatibility_score(breed_info: dict, user_prefs: UserPreferences)
|
|
929 |
'noise': calculate_noise_score(breed_info.get('Breed', ''), user_prefs.noise_tolerance)
|
930 |
}
|
931 |
|
932 |
-
|
933 |
-
|
934 |
-
|
935 |
-
'
|
936 |
-
|
937 |
-
|
938 |
-
|
939 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
940 |
|
941 |
-
#
|
942 |
weighted_score = sum(score * weights[category] for category, score in scores.items())
|
943 |
|
944 |
-
# 如果有孩童,加入家庭安全考量
|
945 |
-
if user_prefs.has_children:
|
946 |
-
try:
|
947 |
-
family_safety = calculate_family_safety_score(breed_info, user_prefs.children_age)
|
948 |
-
# family_safety 作為調整因子,而不是新的分數項目
|
949 |
-
# 這裡的 0.4 表示 family_safety 最多可以降低 60% 的分數
|
950 |
-
safety_modifier = (family_safety * 0.6) + 0.4
|
951 |
-
weighted_score *= safety_modifier
|
952 |
-
except Exception as e:
|
953 |
-
print(f"Family safety calculation error: {str(e)}")
|
954 |
-
# 發生錯誤時使用較保守的預設值
|
955 |
-
weighted_score *= 0.7
|
956 |
-
|
957 |
# 加入品種加分的影響
|
958 |
-
|
959 |
-
|
960 |
-
# breed_bonus 作為加成效果,但限制其影響範圍
|
961 |
-
bonus_modifier = 1 + (breed_bonus * 0.3) # 品種加分最多提升 30%
|
962 |
-
weighted_score *= bonus_modifier
|
963 |
-
except Exception as e:
|
964 |
-
print(f"Breed bonus calculation error: {str(e)}")
|
965 |
|
|
|
966 |
def amplify_score(score):
|
967 |
-
"""
|
968 |
-
優化後的分數放大函數,確保分數範圍合理且結果一致。
|
969 |
-
主要目的是將分數轉換到更容易理解的範圍,並增加差異性。
|
970 |
-
"""
|
971 |
-
# 基礎調整 - 降低基準點使差異更明顯
|
972 |
adjusted = (score - 0.25) * 1.8
|
973 |
-
|
974 |
-
# 使用較溫和的指數來放大差異,但不會過度誇大
|
975 |
amplified = pow(adjusted, 2.2) / 3.5 + score
|
976 |
|
977 |
-
# 處理高分區間,避免分數過度集中
|
978 |
if amplified > 0.85:
|
979 |
amplified = 0.85 + (amplified - 0.85) * 0.6
|
980 |
|
981 |
-
# 確保分數在合理範圍內(0.45-0.95)
|
982 |
final_score = max(0.45, min(0.95, amplified))
|
983 |
-
|
984 |
return round(final_score, 3)
|
985 |
|
986 |
-
# 計算最終分數
|
987 |
final_score = amplify_score(weighted_score)
|
988 |
|
989 |
# 準備回傳結果
|
@@ -999,22 +987,5 @@ def calculate_compatibility_score(breed_info: dict, user_prefs: UserPreferences)
|
|
999 |
# return {k: 0.5 for k in ['space', 'exercise', 'grooming', 'experience', 'health', 'noise', 'overall']}
|
1000 |
|
1001 |
except Exception as e:
|
1002 |
-
print(f"
|
1003 |
-
|
1004 |
-
print(f"Error message: {str(e)}")
|
1005 |
-
print(f"Breed info: {breed_info}")
|
1006 |
-
print(f"User preferences: {user_prefs.__dict__}")
|
1007 |
-
|
1008 |
-
# 嘗試返回已計算的分數,若完全失敗則返回預設值
|
1009 |
-
try:
|
1010 |
-
return scores
|
1011 |
-
except:
|
1012 |
-
return {
|
1013 |
-
'space': 0.7,
|
1014 |
-
'exercise': 0.7,
|
1015 |
-
'grooming': 0.7,
|
1016 |
-
'experience': 0.7,
|
1017 |
-
'health': 0.7,
|
1018 |
-
'noise': 0.7,
|
1019 |
-
'overall': 0.7
|
1020 |
-
}
|
|
|
929 |
'noise': calculate_noise_score(breed_info.get('Breed', ''), user_prefs.noise_tolerance)
|
930 |
}
|
931 |
|
932 |
+
# 當有孩童時,將 family_safety 加入為一個普通分數項目
|
933 |
+
if user_prefs.has_children:
|
934 |
+
family_safety = calculate_family_safety_score(breed_info, user_prefs.children_age)
|
935 |
+
scores['family_safety'] = family_safety
|
936 |
+
# 調整權重以包含家庭安全分數
|
937 |
+
weights = {
|
938 |
+
'space': 0.22, # 降低其他權重以容納family_safety
|
939 |
+
'exercise': 0.15,
|
940 |
+
'grooming': 0.10,
|
941 |
+
'experience': 0.18,
|
942 |
+
'health': 0.10,
|
943 |
+
'noise': 0.05,
|
944 |
+
'family_safety': 0.20 # 給予適當的權重
|
945 |
+
}
|
946 |
+
else:
|
947 |
+
# 原本的權重
|
948 |
+
weights = {
|
949 |
+
'space': 0.28,
|
950 |
+
'exercise': 0.18,
|
951 |
+
'grooming': 0.12,
|
952 |
+
'experience': 0.22,
|
953 |
+
'health': 0.12,
|
954 |
+
'noise': 0.08
|
955 |
+
}
|
956 |
|
957 |
+
# 計算加權分數
|
958 |
weighted_score = sum(score * weights[category] for category, score in scores.items())
|
959 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
960 |
# 加入品種加分的影響
|
961 |
+
breed_bonus = calculate_breed_bonus(breed_info, user_prefs)
|
962 |
+
weighted_score *= (1 + breed_bonus * 0.2) # 品種加分的影響限制在 20%
|
|
|
|
|
|
|
|
|
|
|
963 |
|
964 |
+
# 保持原有的 amplify_score 函數
|
965 |
def amplify_score(score):
|
|
|
|
|
|
|
|
|
|
|
966 |
adjusted = (score - 0.25) * 1.8
|
|
|
|
|
967 |
amplified = pow(adjusted, 2.2) / 3.5 + score
|
968 |
|
|
|
969 |
if amplified > 0.85:
|
970 |
amplified = 0.85 + (amplified - 0.85) * 0.6
|
971 |
|
|
|
972 |
final_score = max(0.45, min(0.95, amplified))
|
|
|
973 |
return round(final_score, 3)
|
974 |
|
|
|
975 |
final_score = amplify_score(weighted_score)
|
976 |
|
977 |
# 準備回傳結果
|
|
|
987 |
# return {k: 0.5 for k in ['space', 'exercise', 'grooming', 'experience', 'health', 'noise', 'overall']}
|
988 |
|
989 |
except Exception as e:
|
990 |
+
print(f"Error in calculate_compatibility_score: {str(e)}")
|
991 |
+
return {k: 0.7 for k in ['space', 'exercise', 'grooming', 'experience', 'health', 'noise', 'overall']}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|