DawnC commited on
Commit
5f3de8c
1 Parent(s): c1ef57f

Update scoring_calculation_system.py

Browse files
Files changed (1) hide show
  1. scoring_calculation_system.py +74 -47
scoring_calculation_system.py CHANGED
@@ -1646,69 +1646,96 @@ def calculate_breed_compatibility_score(scores: dict, user_prefs: UserPreference
1646
  exercise_time = user_prefs.exercise_time
1647
  exercise_type = user_prefs.exercise_type
1648
  temperament = breed_info.get('Temperament', '').lower()
1649
-
1650
- # 定義品種運動特性
1651
- exercise_patterns = {
1652
- 'sprint_type': { # 短跑型,如 Whippet
1653
- 'keywords': ['fast', 'speed', 'agile', 'sprint'],
1654
- 'ideal_time': (30, 90),
1655
- 'ideal_type': 'active_training',
1656
- 'time_penalties': {
1657
- 'over': 0.7, # 運動時間過長的懲罰
1658
- 'under': 0.6 # 運動時間不足的懲罰
 
 
 
 
1659
  }
1660
  },
1661
- 'endurance_type': { # 耐力型,如 Border Collie
1662
- 'keywords': ['herding', 'working', 'energetic', 'tireless'],
1663
- 'ideal_time': (90, 180),
1664
- 'ideal_type': 'moderate_activity',
1665
- 'time_penalties': {
1666
- 'over': 0.9, # 耐力型對超時較寬容
1667
- 'under': 0.5 # 但對運動不足較敏感
 
 
 
1668
  }
1669
  },
1670
- 'moderate_type': { # 一般型
1671
- 'keywords': ['playful', 'active', 'friendly'],
1672
- 'ideal_time': (60, 120),
1673
- 'ideal_type': 'moderate_activity',
1674
- 'time_penalties': {
1675
- 'over': 0.8,
1676
- 'under': 0.7
 
 
 
1677
  }
1678
  }
1679
  }
1680
 
1681
  # 判斷品種的運動類型
1682
- breed_type = 'moderate_type' # 預設值
1683
- for pattern_type, pattern in exercise_patterns.items():
1684
- if any(keyword in temperament for keyword in pattern['keywords']):
1685
- breed_type = pattern_type
1686
- break
 
1687
 
1688
- pattern = exercise_patterns[breed_type]
1689
- min_time, max_time = pattern['ideal_time']
1690
 
1691
  # 計算時間匹配度
1692
- if min_time <= exercise_time <= max_time:
1693
- time_score = 1.0
1694
- elif exercise_time < min_time:
1695
- time_score = pattern['time_penalties']['under']
1696
- else:
1697
- time_score = pattern['time_penalties']['over']
 
 
 
 
 
 
 
 
 
 
1698
 
1699
- # 運動類型匹配度
1700
- type_scores = {
1701
- 'light_walks': 0.7,
1702
- 'moderate_activity': 0.9,
1703
- 'active_training': 1.0
1704
- }
1705
- type_score = type_scores.get(exercise_type, 0.7)
1706
 
1707
  # 特殊情況處理
1708
- if breed_type == 'sprint_type' and exercise_time > 120:
1709
- if exercise_type != 'active_training':
1710
- type_score *= 0.6 # 衝刺型品種不適合長時間中低強度運動
 
 
 
 
 
 
 
 
 
 
1711
 
 
1712
  return (time_score * 0.6) + (type_score * 0.4)
1713
 
1714
  # 第二部分:專業技能需求評估
 
1646
  exercise_time = user_prefs.exercise_time
1647
  exercise_type = user_prefs.exercise_type
1648
  temperament = breed_info.get('Temperament', '').lower()
1649
+ description = breed_info.get('Description', '').lower()
1650
+
1651
+ # 定義更細緻的品種運動特性
1652
+ breed_exercise_patterns = {
1653
+ 'sprint_type': { # Whippet, Saluki
1654
+ 'identifiers': ['fast', 'speed', 'sprint', 'racing', 'coursing'],
1655
+ 'ideal_exercise': {
1656
+ 'active_training': 1.0, # 適合高強度訓練
1657
+ 'moderate_activity': 0.6, # 不太適合持續運動
1658
+ 'light_walks': 0.4 # 不適合純散步
1659
+ },
1660
+ 'time_ranges': {
1661
+ 'ideal': (30, 90), # 理想運動時間
1662
+ 'acceptable': (20, 120) # 可接受範圍
1663
  }
1664
  },
1665
+ 'endurance_type': { # Border Collie, German Shepherd
1666
+ 'identifiers': ['herding', 'working', 'tireless', 'energetic', 'stamina'],
1667
+ 'ideal_exercise': {
1668
+ 'active_training': 0.9, # 適合高強度訓練
1669
+ 'moderate_activity': 1.0, # 最適合持續運動
1670
+ 'light_walks': 0.5 # 不適合純散步
1671
+ },
1672
+ 'time_ranges': {
1673
+ 'ideal': (90, 180), # 理想運動時間
1674
+ 'acceptable': (60, 240) # 可接受範圍
1675
  }
1676
  },
1677
+ 'moderate_type': { # 如 Labrador, Golden Retriever
1678
+ 'identifiers': ['friendly', 'playful', 'adaptable', 'versatile'],
1679
+ 'ideal_exercise': {
1680
+ 'active_training': 0.8,
1681
+ 'moderate_activity': 1.0,
1682
+ 'light_walks': 0.7
1683
+ },
1684
+ 'time_ranges': {
1685
+ 'ideal': (60, 120),
1686
+ 'acceptable': (45, 180)
1687
  }
1688
  }
1689
  }
1690
 
1691
  # 判斷品種的運動類型
1692
+ def determine_breed_type():
1693
+ for breed_type, pattern in breed_exercise_patterns.items():
1694
+ if any(identifier in temperament or identifier in description
1695
+ for identifier in pattern['identifiers']):
1696
+ return breed_type
1697
+ return 'moderate_type' # 預設類型
1698
 
1699
+ breed_type = determine_breed_type()
1700
+ pattern = breed_exercise_patterns[breed_type]
1701
 
1702
  # 計算時間匹配度
1703
+ def calculate_time_match():
1704
+ ideal_min, ideal_max = pattern['time_ranges']['ideal']
1705
+ accept_min, accept_max = pattern['time_ranges']['acceptable']
1706
+
1707
+ if ideal_min <= exercise_time <= ideal_max:
1708
+ return 1.0
1709
+ elif exercise_time < accept_min:
1710
+ return max(0.3, 1 - ((accept_min - exercise_time) / accept_min))
1711
+ elif exercise_time > accept_max:
1712
+ return max(0.3, 1 - ((exercise_time - accept_max) / accept_max))
1713
+ else:
1714
+ # 在可接受範圍內,但不在理想範圍
1715
+ if exercise_time < ideal_min:
1716
+ return 0.7 + (0.3 * (exercise_time - accept_min) / (ideal_min - accept_min))
1717
+ else:
1718
+ return 0.7 + (0.3 * (accept_max - exercise_time) / (accept_max - ideal_max))
1719
 
1720
+ # 計算運動類型匹配度
1721
+ type_score = pattern['ideal_exercise'].get(exercise_type, 0.5)
 
 
 
 
 
1722
 
1723
  # 特殊情況處理
1724
+ def apply_special_adjustments(time_score, type_score):
1725
+ if breed_type == 'sprint_type':
1726
+ if exercise_time > pattern['time_ranges']['acceptable'][1]:
1727
+ return (time_score * 0.5, type_score * 0.6)
1728
+
1729
+ if breed_type == 'endurance_type':
1730
+ if exercise_time < pattern['time_ranges']['ideal'][0]:
1731
+ return (time_score * 0.6, type_score * 0.7)
1732
+
1733
+ return (time_score, type_score)
1734
+
1735
+ time_score = calculate_time_match()
1736
+ time_score, type_score = apply_special_adjustments(time_score, type_score)
1737
 
1738
+ # 最終運動匹配度計算
1739
  return (time_score * 0.6) + (type_score * 0.4)
1740
 
1741
  # 第二部分:專業技能需求評估