Spaces:
Running
on
Zero
Running
on
Zero
Update search_history.py
Browse files- search_history.py +38 -7
search_history.py
CHANGED
|
@@ -26,7 +26,7 @@ class SearchHistoryComponent:
|
|
| 26 |
for entry in reversed(history_data):
|
| 27 |
timestamp = entry.get('timestamp', 'Unknown time')
|
| 28 |
search_type = entry.get('search_type', 'criteria')
|
| 29 |
-
results = entry.get('results', [])
|
| 30 |
|
| 31 |
# 顯示時間戳記和搜尋類型
|
| 32 |
html += f"""
|
|
@@ -122,20 +122,51 @@ class SearchHistoryComponent:
|
|
| 122 |
return "Error refreshing history"
|
| 123 |
|
| 124 |
def save_search(self, user_preferences: Optional[dict] = None,
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
"""
|
|
|
|
|
|
|
|
|
|
| 129 |
Args:
|
| 130 |
user_preferences: 使用者偏好設定 (僅用於criteria搜尋)
|
|
|
|
| 131 |
results: 推薦結果列表
|
|
|
|
| 132 |
search_type: 搜尋類型 ("criteria" 或 "description")
|
|
|
|
| 133 |
description: 使用者輸入的描述 (僅用於description搜尋)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 135 |
return self.history_manager.save_history(
|
| 136 |
user_preferences=user_preferences,
|
| 137 |
-
results=
|
| 138 |
-
search_type='criteria'
|
| 139 |
)
|
| 140 |
|
| 141 |
def create_history_component():
|
|
|
|
| 26 |
for entry in reversed(history_data):
|
| 27 |
timestamp = entry.get('timestamp', 'Unknown time')
|
| 28 |
search_type = entry.get('search_type', 'criteria')
|
| 29 |
+
results = entry.get('results', [])
|
| 30 |
|
| 31 |
# 顯示時間戳記和搜尋類型
|
| 32 |
html += f"""
|
|
|
|
| 122 |
return "Error refreshing history"
|
| 123 |
|
| 124 |
def save_search(self, user_preferences: Optional[dict] = None,
|
| 125 |
+
results: list = None,
|
| 126 |
+
search_type: str = "criteria",
|
| 127 |
+
description: str = None) -> bool:
|
| 128 |
+
"""
|
| 129 |
+
保存搜索結果到歷史記錄
|
| 130 |
+
這個方法負責處理搜尋結果的保存,並確保只保存前15個最相關的推薦結果。
|
| 131 |
+
在保存之前,會處理結果數據確保格式正確且包含所需的所有資訊。
|
| 132 |
Args:
|
| 133 |
user_preferences: 使用者偏好設定 (僅用於criteria搜尋)
|
| 134 |
+
包含所有搜尋條件如居住空間、運動時間等
|
| 135 |
results: 推薦結果列表
|
| 136 |
+
包含所有推薦的狗品種及其評分
|
| 137 |
search_type: 搜尋類型 ("criteria" 或 "description")
|
| 138 |
+
用於標識搜尋方式
|
| 139 |
description: 使用者輸入的描述 (僅用於description搜尋)
|
| 140 |
+
用於自然語言搜尋時的描述文本
|
| 141 |
+
|
| 142 |
+
Returns:
|
| 143 |
+
bool: 表示保存是否成功
|
| 144 |
"""
|
| 145 |
+
# 首先確保結果不為空且為列表
|
| 146 |
+
if results and isinstance(results, list):
|
| 147 |
+
# 只取前15個結果
|
| 148 |
+
processed_results = []
|
| 149 |
+
for result in results[:15]: # 限制為前15個結果
|
| 150 |
+
# 確保每個結果都包含必要的資訊
|
| 151 |
+
if isinstance(result, dict):
|
| 152 |
+
processed_result = {
|
| 153 |
+
'breed': result.get('breed', 'Unknown'),
|
| 154 |
+
'overall_score': result.get('final_score', 0), # 使用 final_score 作為 overall_score
|
| 155 |
+
'rank': result.get('rank', 0),
|
| 156 |
+
'base_score': result.get('base_score', 0),
|
| 157 |
+
'bonus_score': result.get('bonus_score', 0),
|
| 158 |
+
'scores': result.get('scores', {})
|
| 159 |
+
}
|
| 160 |
+
processed_results.append(processed_result)
|
| 161 |
+
else:
|
| 162 |
+
# 如果沒有結果,創建空列表
|
| 163 |
+
processed_results = []
|
| 164 |
+
|
| 165 |
+
# 調用 history_manager 的 save_history 方法保存處理過的結果
|
| 166 |
return self.history_manager.save_history(
|
| 167 |
user_preferences=user_preferences,
|
| 168 |
+
results=processed_results, # 使用處理過的結果
|
| 169 |
+
search_type='criteria'
|
| 170 |
)
|
| 171 |
|
| 172 |
def create_history_component():
|