DawnC commited on
Commit
7a93615
1 Parent(s): 5720b16

Update scoring_calculation_system.py

Browse files
Files changed (1) hide show
  1. scoring_calculation_system.py +46 -47
scoring_calculation_system.py CHANGED
@@ -929,69 +929,68 @@ 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
- weights = {
934
- 'space': 0.28,
935
- 'exercise': 0.18,
936
- 'grooming': 0.12,
937
- 'experience': 0.22,
938
- 'health': 0.12,
939
- 'noise': 0.08
940
- }
941
 
942
- # 如果有孩童,我們需要調整各個分數,而不是增加新的分數項目
943
  if user_prefs.has_children:
944
  family_safety = calculate_family_safety_score(breed_info, user_prefs.children_age)
945
 
946
- # 根據孩童年齡設定不同的調整強度
947
- age_adjustments = {
948
- 'toddler': {
949
- 'space': (0.5, 0.5), # (基礎係數, 安全係數權重)
950
- 'experience': (0.4, 0.6), # 幼童需要更嚴格的經驗要求
951
- 'exercise': (0.6, 0.4),
952
- 'noise': (0.5, 0.5)
953
- },
954
- 'school_age': {
955
- 'space': (0.6, 0.4),
956
- 'experience': (0.5, 0.5),
957
- 'exercise': (0.7, 0.3),
958
- 'noise': (0.6, 0.4)
959
- },
960
- 'teenager': {
961
- 'space': (0.7, 0.3),
962
- 'experience': (0.6, 0.4),
963
- 'exercise': (0.8, 0.2),
964
- 'noise': (0.7, 0.3)
965
- }
966
  }
967
 
968
- adjustments = age_adjustments[user_prefs.children_age]
969
-
970
- # 應用更嚴格的調整
971
- for category, (base_coef, safety_weight) in adjustments.items():
972
- scores[category] *= (base_coef + (family_safety * safety_weight))
 
 
 
 
 
 
 
973
 
974
- # 品種加分也需要根據年齡組進行調整
975
- breed_bonus_adjustments = {
976
- 'toddler': 0.4, # 幼童情況下品種加分影響最小
977
- 'school_age': 0.6,
978
- 'teenager': 0.8
 
 
 
 
 
 
 
 
 
 
 
 
 
979
  }
980
- breed_bonus *= (breed_bonus_adjustments[user_prefs.children_age] * family_safety)
981
-
982
- weighted_score = sum(score * weights[category] for category, score in scores.items())
983
- weighted_score *= (1 + breed_bonus * 0.2)
984
 
 
985
  def amplify_score(score):
986
  adjusted = (score - 0.25) * 1.8
987
  amplified = pow(adjusted, 2.2) / 3.5 + score
988
  if amplified > 0.85:
989
  amplified = 0.85 + (amplified - 0.85) * 0.6
990
- final_score = max(0.45, min(0.95, amplified))
991
- return round(final_score, 3)
992
 
993
- final_score = amplify_score(weighted_score)
994
 
 
995
  scores = {k: round(v, 4) for k, v in scores.items()}
996
  scores['overall'] = round(final_score, 4)
997
 
 
929
  'noise': calculate_noise_score(breed_info.get('Breed', ''), user_prefs.noise_tolerance)
930
  }
931
 
932
+ # 2. 計算品種加分
933
+ breed_bonus = calculate_breed_bonus(breed_info, user_prefs)
 
 
 
 
 
 
 
934
 
935
+ # 3. 如果有孩童,計算安全分數,但不直接修改基礎分數
936
  if user_prefs.has_children:
937
  family_safety = calculate_family_safety_score(breed_info, user_prefs.children_age)
938
 
939
+ # 創建安全性調整係數
940
+ safety_adjustments = {
941
+ 'toddler': 0.6, # 對幼童最嚴格
942
+ 'school_age': 0.75, # 學齡兒童較寬鬆
943
+ 'teenager': 0.85 # 青少年最寬鬆
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
944
  }
945
 
946
+ # 調整權重而不是直接修改分數
947
+ safety_weight = safety_adjustments[user_prefs.children_age]
948
+
949
+ # 修改最終加權計算方式
950
+ weights = {
951
+ 'space': 0.22,
952
+ 'exercise': 0.15,
953
+ 'grooming': 0.10,
954
+ 'experience': 0.18,
955
+ 'health': 0.10,
956
+ 'noise': 0.05
957
+ }
958
 
959
+ # 加入安全性權重
960
+ final_score = (
961
+ sum(score * weights[category] for category, score in scores.items()) * safety_weight +
962
+ family_safety * (1 - safety_weight)
963
+ )
964
+
965
+ # 品種加分也要考慮安全性
966
+ breed_bonus *= family_safety
967
+
968
+ else:
969
+ # 原有的權重
970
+ weights = {
971
+ 'space': 0.28,
972
+ 'exercise': 0.18,
973
+ 'grooming': 0.12,
974
+ 'experience': 0.22,
975
+ 'health': 0.12,
976
+ 'noise': 0.08
977
  }
978
+ final_score = sum(score * weights[category] for category, score in scores.items())
979
+
980
+ # 4. 應用品種加分
981
+ final_score *= (1 + breed_bonus * 0.2)
982
 
983
+ # 5. 最終分數調整
984
  def amplify_score(score):
985
  adjusted = (score - 0.25) * 1.8
986
  amplified = pow(adjusted, 2.2) / 3.5 + score
987
  if amplified > 0.85:
988
  amplified = 0.85 + (amplified - 0.85) * 0.6
989
+ return max(0.45, min(0.95, amplified))
 
990
 
991
+ final_score = amplify_score(final_score)
992
 
993
+ # 6. 回傳結果
994
  scores = {k: round(v, 4) for k, v in scores.items()}
995
  scores['overall'] = round(final_score, 4)
996