DawnC commited on
Commit
b0d91ac
1 Parent(s): 8605cc7

Update smart_breed_matcher.py

Browse files
Files changed (1) hide show
  1. smart_breed_matcher.py +31 -0
smart_breed_matcher.py CHANGED
@@ -245,6 +245,37 @@ class SmartBreedMatcher:
245
 
246
  return similarity
247
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
248
  def match_user_preference(self, description: str, top_n: int = 10) -> List[Dict]:
249
  """根據用戶描述匹配最適合的品種"""
250
  preferred_breed = self._detect_breed_preference(description)
 
245
 
246
  return similarity
247
 
248
+ def _general_matching(self, description: str, top_n: int = 10) -> List[Dict]:
249
+ """基本的品種匹配邏輯"""
250
+ matches = []
251
+ for breed in self.dog_data:
252
+ breed_name = breed[1]
253
+ breed_description = breed[9]
254
+ temperament = breed[4]
255
+
256
+ # 計算相似度
257
+ desc_embedding = self.model.encode(description)
258
+ breed_desc_embedding = self.model.encode(breed_description)
259
+ breed_temp_embedding = self.model.encode(temperament)
260
+
261
+ # 計算描述和性格的相似度
262
+ desc_similarity = float(util.pytorch_cos_sim(desc_embedding, breed_desc_embedding))
263
+ temp_similarity = float(util.pytorch_cos_sim(desc_embedding, breed_temp_embedding))
264
+
265
+ # 結合分數
266
+ final_score = (desc_similarity * 0.6 + temp_similarity * 0.4)
267
+
268
+ matches.append({
269
+ 'breed': breed_name,
270
+ 'score': final_score,
271
+ 'is_preferred': False,
272
+ 'similarity': final_score,
273
+ 'reason': "Matched based on general description and temperament"
274
+ })
275
+
276
+ # 排序並返回前 N 個匹配結果
277
+ return sorted(matches, key=lambda x: -x['score'])[:top_n]
278
+
279
  def match_user_preference(self, description: str, top_n: int = 10) -> List[Dict]:
280
  """根據用戶描述匹配最適合的品種"""
281
  preferred_breed = self._detect_breed_preference(description)