Spaces:
Running
on
Zero
Running
on
Zero
Update breed_recommendation.py
Browse files- breed_recommendation.py +41 -0
breed_recommendation.py
CHANGED
@@ -9,6 +9,47 @@ from scoring_calculation_system import UserPreferences, calculate_compatibility_
|
|
9 |
from recommendation_html_format import format_recommendation_html, get_breed_recommendations
|
10 |
from search_history import create_history_tab, create_history_component
|
11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
def create_recommendation_tab(UserPreferences, get_breed_recommendations, format_recommendation_html, history_component):
|
13 |
|
14 |
with gr.TabItem("Breed Recommendation"):
|
|
|
9 |
from recommendation_html_format import format_recommendation_html, get_breed_recommendations
|
10 |
from search_history import create_history_tab, create_history_component
|
11 |
|
12 |
+
|
13 |
+
def filter_breed_matches(user_prefs: UserPreferences, top_n: int = 10) -> List[Dict]:
|
14 |
+
"""
|
15 |
+
根據使用者偏好篩選並推薦狗狗品種。
|
16 |
+
|
17 |
+
Parameters:
|
18 |
+
user_prefs: 使用者偏好設定
|
19 |
+
top_n: 要返回的推薦數量
|
20 |
+
|
21 |
+
Returns:
|
22 |
+
List[Dict]: 排序後的推薦品種列表
|
23 |
+
"""
|
24 |
+
all_breeds = []
|
25 |
+
for breed_info in breed_database:
|
26 |
+
score = calculate_compatibility_score(breed_info, user_prefs)
|
27 |
+
if score is not None: # 只添加未被過濾的品種
|
28 |
+
all_breeds.append({
|
29 |
+
'breed': breed_info['Breed'],
|
30 |
+
'final_score': score['overall'],
|
31 |
+
'base_score': score.get('base_score', 0),
|
32 |
+
'bonus_score': score.get('bonus_score', 0),
|
33 |
+
'size': breed_info['Size'],
|
34 |
+
'scores': score
|
35 |
+
})
|
36 |
+
|
37 |
+
# 根據體型偏好過濾
|
38 |
+
if user_prefs.size_preference != "no_preference":
|
39 |
+
filtered_breeds = [b for b in all_breeds if b['size'].lower() == user_prefs.size_preference.lower()]
|
40 |
+
# 如果符合體型的品種太少,調整返回數量
|
41 |
+
if len(filtered_breeds) < 5: # 設定最少要有5種品種
|
42 |
+
top_n = len(filtered_breeds)
|
43 |
+
else:
|
44 |
+
filtered_breeds = all_breeds
|
45 |
+
|
46 |
+
# 為每個品種添加排名
|
47 |
+
sorted_breeds = sorted(filtered_breeds, key=lambda x: x['final_score'], reverse=True)
|
48 |
+
for i, breed in enumerate(sorted_breeds, 1):
|
49 |
+
breed['rank'] = i
|
50 |
+
|
51 |
+
return sorted_breeds[:top_n]
|
52 |
+
|
53 |
def create_recommendation_tab(UserPreferences, get_breed_recommendations, format_recommendation_html, history_component):
|
54 |
|
55 |
with gr.TabItem("Breed Recommendation"):
|