DawnC commited on
Commit
56022aa
1 Parent(s): bb2c2e7

Update smart_breed_matcher.py

Browse files
Files changed (1) hide show
  1. smart_breed_matcher.py +55 -6
smart_breed_matcher.py CHANGED
@@ -245,37 +245,86 @@ class SmartBreedMatcher:
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)
 
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 _general_matching(self, description: str, top_n: int = 10) -> List[Dict]:
280
+ """基本的品種匹配邏輯,考慮描述、性格、噪音和健康因素"""
281
  matches = []
282
  for breed in self.dog_data:
283
  breed_name = breed[1]
284
  breed_description = breed[9]
285
  temperament = breed[4]
286
 
287
+ # 計算描述文本和性格的相似度
288
  desc_embedding = self.model.encode(description)
289
  breed_desc_embedding = self.model.encode(breed_description)
290
  breed_temp_embedding = self.model.encode(temperament)
291
 
 
292
  desc_similarity = float(util.pytorch_cos_sim(desc_embedding, breed_desc_embedding))
293
  temp_similarity = float(util.pytorch_cos_sim(desc_embedding, breed_temp_embedding))
294
 
295
+ # 計算噪音相似度和健康分數
296
+ noise_similarity = self._calculate_noise_similarity(breed_name, breed_name)
297
+ health_score = self._calculate_health_score(breed_name)
298
+ health_similarity = 1.0 - abs(health_score - 0.8) # 假設理想健康分數為 0.8
299
+
300
+ # 加權計算分數
301
+ weights = {
302
+ 'description': 0.35,
303
+ 'temperament': 0.25,
304
+ 'noise': 0.2,
305
+ 'health': 0.2
306
+ }
307
+
308
+ # 計算最終分數
309
+ final_score = (
310
+ desc_similarity * weights['description'] +
311
+ temp_similarity * weights['temperament'] +
312
+ noise_similarity * weights['noise'] +
313
+ health_similarity * weights['health']
314
+ )
315
 
316
  matches.append({
317
  'breed': breed_name,
318
  'score': final_score,
319
  'is_preferred': False,
320
  'similarity': final_score,
321
+ 'reason': "Matched based on description, temperament, noise level, and health score"
322
  })
323
 
324
  # 排序並返回前 N 個匹配結果
325
  return sorted(matches, key=lambda x: -x['score'])[:top_n]
326
 
327
+
328
  def match_user_preference(self, description: str, top_n: int = 10) -> List[Dict]:
329
  """根據用戶描述匹配最適合的品種"""
330
  preferred_breed = self._detect_breed_preference(description)