Spaces:
Running
on
Zero
Running
on
Zero
Update scoring_calculation_system.py
Browse files- scoring_calculation_system.py +122 -56
scoring_calculation_system.py
CHANGED
|
@@ -1642,101 +1642,167 @@ def calculate_breed_compatibility_score(scores: dict, user_prefs: UserPreference
|
|
| 1642 |
|
| 1643 |
# 第一部分:運動需求評估
|
| 1644 |
def evaluate_exercise_compatibility():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1645 |
exercise_needs = breed_info.get('Exercise Needs', 'MODERATE').upper()
|
| 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': { #
|
| 1654 |
-
'identifiers': ['fast', 'speed', 'sprint', 'racing', 'coursing'],
|
| 1655 |
'ideal_exercise': {
|
| 1656 |
-
'active_training': 1.0,
|
| 1657 |
-
'moderate_activity': 0.
|
| 1658 |
-
'light_walks': 0.
|
| 1659 |
},
|
| 1660 |
'time_ranges': {
|
| 1661 |
-
'ideal': (30,
|
| 1662 |
-
'acceptable': (20,
|
| 1663 |
-
|
|
|
|
|
|
|
| 1664 |
},
|
| 1665 |
-
'endurance_type': { #
|
| 1666 |
-
'identifiers': ['herding', 'working', 'tireless', 'energetic', 'stamina'],
|
| 1667 |
'ideal_exercise': {
|
| 1668 |
-
'active_training': 0.9,
|
| 1669 |
-
'moderate_activity': 1.0,
|
| 1670 |
-
'light_walks': 0.
|
| 1671 |
},
|
| 1672 |
'time_ranges': {
|
| 1673 |
-
'ideal': (90, 180),
|
| 1674 |
-
'acceptable': (60,
|
| 1675 |
-
|
|
|
|
|
|
|
| 1676 |
},
|
| 1677 |
-
'moderate_type': { #
|
| 1678 |
-
'identifiers': ['friendly', 'playful', 'adaptable', 'versatile'],
|
| 1679 |
'ideal_exercise': {
|
| 1680 |
'active_training': 0.8,
|
| 1681 |
'moderate_activity': 1.0,
|
| 1682 |
-
'light_walks': 0.
|
| 1683 |
},
|
| 1684 |
'time_ranges': {
|
| 1685 |
'ideal': (60, 120),
|
| 1686 |
-
'acceptable': (45,
|
| 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 |
-
|
| 1698 |
-
|
| 1699 |
-
|
| 1700 |
-
|
| 1701 |
-
|
| 1702 |
-
|
| 1703 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
|
|
|
| 1711 |
elif exercise_time > accept_max:
|
| 1712 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1713 |
else:
|
| 1714 |
-
# 在可接受範圍內,但不在理想範圍
|
| 1715 |
if exercise_time < ideal_min:
|
| 1716 |
-
|
|
|
|
| 1717 |
else:
|
| 1718 |
-
|
| 1719 |
-
|
| 1720 |
-
|
| 1721 |
-
type_score
|
| 1722 |
-
|
| 1723 |
-
|
| 1724 |
-
|
|
|
|
|
|
|
| 1725 |
if breed_type == 'sprint_type':
|
| 1726 |
-
if exercise_time > pattern['time_ranges']['
|
| 1727 |
-
|
| 1728 |
-
|
| 1729 |
-
|
| 1730 |
-
|
| 1731 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1732 |
|
| 1733 |
-
return
|
| 1734 |
-
|
| 1735 |
-
|
| 1736 |
-
|
| 1737 |
-
|
| 1738 |
-
|
| 1739 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1740 |
|
| 1741 |
# 第二部分:專業技能需求評估
|
| 1742 |
def evaluate_expertise_requirements():
|
|
|
|
| 1642 |
|
| 1643 |
# 第一部分:運動需求評估
|
| 1644 |
def evaluate_exercise_compatibility():
|
| 1645 |
+
"""
|
| 1646 |
+
評估運動需求的匹配度,特別關注:
|
| 1647 |
+
1. 時間與強度的合理搭配
|
| 1648 |
+
2. 不同品種的運動特性
|
| 1649 |
+
3. 運動類型的適配性
|
| 1650 |
+
|
| 1651 |
+
這個函數就像是一個體育教練,需要根據每個"運動員"(狗品種)的特點,
|
| 1652 |
+
為他們制定合適的訓練計劃。
|
| 1653 |
+
"""
|
| 1654 |
exercise_needs = breed_info.get('Exercise Needs', 'MODERATE').upper()
|
| 1655 |
exercise_time = user_prefs.exercise_time
|
| 1656 |
exercise_type = user_prefs.exercise_type
|
| 1657 |
temperament = breed_info.get('Temperament', '').lower()
|
| 1658 |
description = breed_info.get('Description', '').lower()
|
| 1659 |
+
|
| 1660 |
+
# 定義更精確的品種運動特性
|
| 1661 |
breed_exercise_patterns = {
|
| 1662 |
+
'sprint_type': { # 短跑型犬種,如 Whippet, Saluki
|
| 1663 |
+
'identifiers': ['fast', 'speed', 'sprint', 'racing', 'coursing', 'sight hound'],
|
| 1664 |
'ideal_exercise': {
|
| 1665 |
+
'active_training': 1.0, # 完美匹配高強度訓練
|
| 1666 |
+
'moderate_activity': 0.5, # 持續運動不是最佳選擇
|
| 1667 |
+
'light_walks': 0.3 # 輕度運動效果很差
|
| 1668 |
},
|
| 1669 |
'time_ranges': {
|
| 1670 |
+
'ideal': (30, 60), # 最適合的運動時間範圍
|
| 1671 |
+
'acceptable': (20, 90), # 可以接受的時間範圍
|
| 1672 |
+
'penalty_start': 90 # 開始給予懲罰的時間點
|
| 1673 |
+
},
|
| 1674 |
+
'penalty_rate': 0.8 # 超出範圍時的懲罰係數
|
| 1675 |
},
|
| 1676 |
+
'endurance_type': { # 耐力型犬種,如 Border Collie
|
| 1677 |
+
'identifiers': ['herding', 'working', 'tireless', 'energetic', 'stamina', 'athletic'],
|
| 1678 |
'ideal_exercise': {
|
| 1679 |
+
'active_training': 0.9, # 高強度訓練很好
|
| 1680 |
+
'moderate_activity': 1.0, # 持續運動是最佳選擇
|
| 1681 |
+
'light_walks': 0.4 # 輕度運動不足
|
| 1682 |
},
|
| 1683 |
'time_ranges': {
|
| 1684 |
+
'ideal': (90, 180), # 需要較長的運動時間
|
| 1685 |
+
'acceptable': (60, 180),
|
| 1686 |
+
'penalty_start': 60 # 運動時間過短會受罰
|
| 1687 |
+
},
|
| 1688 |
+
'penalty_rate': 0.7
|
| 1689 |
},
|
| 1690 |
+
'moderate_type': { # 一般活動型犬種,如 Labrador
|
| 1691 |
+
'identifiers': ['friendly', 'playful', 'adaptable', 'versatile', 'companion'],
|
| 1692 |
'ideal_exercise': {
|
| 1693 |
'active_training': 0.8,
|
| 1694 |
'moderate_activity': 1.0,
|
| 1695 |
+
'light_walks': 0.6
|
| 1696 |
},
|
| 1697 |
'time_ranges': {
|
| 1698 |
'ideal': (60, 120),
|
| 1699 |
+
'acceptable': (45, 150),
|
| 1700 |
+
'penalty_start': 150
|
| 1701 |
+
},
|
| 1702 |
+
'penalty_rate': 0.6
|
| 1703 |
}
|
| 1704 |
}
|
| 1705 |
+
|
|
|
|
| 1706 |
def determine_breed_type():
|
| 1707 |
+
"""
|
| 1708 |
+
根據品種的描述和性格特徵判斷其運動類型。
|
| 1709 |
+
就像體育教練要��了解運動員的特點才能制定訓練計劃。
|
| 1710 |
+
"""
|
| 1711 |
+
# 優先檢查特殊運動類型的標識符
|
| 1712 |
for breed_type, pattern in breed_exercise_patterns.items():
|
| 1713 |
if any(identifier in temperament or identifier in description
|
| 1714 |
for identifier in pattern['identifiers']):
|
| 1715 |
return breed_type
|
| 1716 |
+
|
| 1717 |
+
# 如果沒有特殊標識,根據運動需求級別判斷
|
| 1718 |
+
if exercise_needs in ['VERY HIGH', 'HIGH']:
|
| 1719 |
+
return 'endurance_type'
|
| 1720 |
+
elif exercise_needs == 'LOW':
|
| 1721 |
+
return 'moderate_type'
|
| 1722 |
+
|
| 1723 |
+
return 'moderate_type'
|
| 1724 |
+
|
| 1725 |
+
def calculate_time_match(pattern):
|
| 1726 |
+
"""
|
| 1727 |
+
計算運動時間的匹配度。
|
| 1728 |
+
這就像在判斷運動時間是否符合訓練計劃。
|
| 1729 |
+
"""
|
| 1730 |
ideal_min, ideal_max = pattern['time_ranges']['ideal']
|
| 1731 |
accept_min, accept_max = pattern['time_ranges']['acceptable']
|
| 1732 |
+
penalty_start = pattern['time_ranges']['penalty_start']
|
| 1733 |
|
| 1734 |
+
# 在理想範圍內
|
| 1735 |
if ideal_min <= exercise_time <= ideal_max:
|
| 1736 |
return 1.0
|
| 1737 |
+
|
| 1738 |
+
# 超出可接受範圍的嚴格懲罰
|
| 1739 |
elif exercise_time < accept_min:
|
| 1740 |
+
deficit = accept_min - exercise_time
|
| 1741 |
+
return max(0.2, 1 - (deficit / accept_min) * 1.2)
|
| 1742 |
elif exercise_time > accept_max:
|
| 1743 |
+
excess = exercise_time - penalty_start
|
| 1744 |
+
penalty = min(0.8, (excess / penalty_start) * pattern['penalty_rate'])
|
| 1745 |
+
return max(0.2, 1 - penalty)
|
| 1746 |
+
|
| 1747 |
+
# 在可接受範圍但不在理想範圍
|
| 1748 |
else:
|
|
|
|
| 1749 |
if exercise_time < ideal_min:
|
| 1750 |
+
progress = (exercise_time - accept_min) / (ideal_min - accept_min)
|
| 1751 |
+
return 0.6 + (0.4 * progress)
|
| 1752 |
else:
|
| 1753 |
+
remaining = (accept_max - exercise_time) / (accept_max - ideal_max)
|
| 1754 |
+
return 0.6 + (0.4 * remaining)
|
| 1755 |
+
|
| 1756 |
+
def apply_special_adjustments(time_score, type_score, breed_type, pattern):
|
| 1757 |
+
"""
|
| 1758 |
+
處理特殊情況,確保運動方式真正符合品種需求。
|
| 1759 |
+
就像確保訓練計劃不會違背運動員的特點。
|
| 1760 |
+
"""
|
| 1761 |
+
# 短跑型品種的特殊處理
|
| 1762 |
if breed_type == 'sprint_type':
|
| 1763 |
+
if exercise_time > pattern['time_ranges']['penalty_start']:
|
| 1764 |
+
# 時間過長的嚴重懲罰
|
| 1765 |
+
time_score *= 0.5
|
| 1766 |
+
# 如果同時運動類型不適合,更嚴重的懲罰
|
| 1767 |
+
if exercise_type != 'active_training':
|
| 1768 |
+
type_score *= 0.4
|
| 1769 |
+
|
| 1770 |
+
# 耐力型品種的特殊處理
|
| 1771 |
+
elif breed_type == 'endurance_type':
|
| 1772 |
+
if exercise_time < pattern['time_ranges']['penalty_start']:
|
| 1773 |
+
time_score *= 0.6
|
| 1774 |
+
# 運動強度不足的懲罰
|
| 1775 |
+
if exercise_type == 'light_walks' and exercise_time > 90:
|
| 1776 |
+
type_score *= 0.5
|
| 1777 |
|
| 1778 |
+
return time_score, type_score
|
| 1779 |
+
|
| 1780 |
+
# 執行評估流程
|
| 1781 |
+
breed_type = determine_breed_type()
|
| 1782 |
+
pattern = breed_exercise_patterns[breed_type]
|
| 1783 |
+
|
| 1784 |
+
# 計算基礎分數
|
| 1785 |
+
time_score = calculate_time_match(pattern)
|
| 1786 |
+
type_score = pattern['ideal_exercise'].get(exercise_type, 0.5)
|
| 1787 |
+
|
| 1788 |
+
# 應用特殊調整
|
| 1789 |
+
time_score, type_score = apply_special_adjustments(time_score, type_score, breed_type, pattern)
|
| 1790 |
+
|
| 1791 |
+
# 根據品種類型決定最終權重
|
| 1792 |
+
if breed_type == 'sprint_type':
|
| 1793 |
+
if exercise_time > pattern['time_ranges']['penalty_start']:
|
| 1794 |
+
# 超時時更重視運動類型的匹配度
|
| 1795 |
+
return (time_score * 0.3) + (type_score * 0.7)
|
| 1796 |
+
else:
|
| 1797 |
+
return (time_score * 0.5) + (type_score * 0.5)
|
| 1798 |
+
elif breed_type == 'endurance_type':
|
| 1799 |
+
if exercise_time < pattern['time_ranges']['penalty_start']:
|
| 1800 |
+
# 時間不足時更重視時間因素
|
| 1801 |
+
return (time_score * 0.7) + (type_score * 0.3)
|
| 1802 |
+
else:
|
| 1803 |
+
return (time_score * 0.6) + (type_score * 0.4)
|
| 1804 |
+
else:
|
| 1805 |
+
return (time_score * 0.5) + (type_score * 0.5)
|
| 1806 |
|
| 1807 |
# 第二部分:專業技能需求評估
|
| 1808 |
def evaluate_expertise_requirements():
|