Spaces:
Running
Running
Update smart_breed_matcher.py
Browse files- smart_breed_matcher.py +0 -39
smart_breed_matcher.py
CHANGED
@@ -72,45 +72,6 @@ class SmartBreedMatcher:
|
|
72 |
|
73 |
return sorted(similarities, key=lambda x: x[1], reverse=True)[:top_n]
|
74 |
|
75 |
-
def _calculate_breed_similarity(self, breed1_features: Dict, breed2_features: Dict) -> float:
|
76 |
-
"""計算兩個品種之間的相似度,包含健康因素"""
|
77 |
-
# 計算描述文本的相似度
|
78 |
-
desc1_embedding = self.model.encode(breed1_features['description'])
|
79 |
-
desc2_embedding = self.model.encode(breed2_features['description'])
|
80 |
-
description_similarity = float(util.pytorch_cos_sim(desc1_embedding, desc2_embedding))
|
81 |
-
|
82 |
-
# 基本特徵相似度
|
83 |
-
size_similarity = 1.0 if breed1_features['size'] == breed2_features['size'] else 0.5
|
84 |
-
exercise_similarity = 1.0 if breed1_features['exercise'] == breed2_features['exercise'] else 0.5
|
85 |
-
|
86 |
-
# 性格相似度
|
87 |
-
temp1_embedding = self.model.encode(breed1_features['temperament'])
|
88 |
-
temp2_embedding = self.model.encode(breed2_features['temperament'])
|
89 |
-
temperament_similarity = float(util.pytorch_cos_sim(temp1_embedding, temp2_embedding))
|
90 |
-
|
91 |
-
# 健康分數相似度
|
92 |
-
health_score1 = self._calculate_health_score(breed1_features['breed_name'])
|
93 |
-
health_score2 = self._calculate_health_score(breed2_features['breed_name'])
|
94 |
-
health_similarity = 1.0 - abs(health_score1 - health_score2)
|
95 |
-
|
96 |
-
# 加權計算
|
97 |
-
weights = {
|
98 |
-
'description': 0.3,
|
99 |
-
'temperament': 0.25,
|
100 |
-
'exercise': 0.15,
|
101 |
-
'size': 0.1,
|
102 |
-
'health': 0.2
|
103 |
-
}
|
104 |
-
|
105 |
-
final_similarity = (
|
106 |
-
description_similarity * weights['description'] +
|
107 |
-
temperament_similarity * weights['temperament'] +
|
108 |
-
exercise_similarity * weights['exercise'] +
|
109 |
-
size_similarity * weights['size'] +
|
110 |
-
health_similarity * weights['health']
|
111 |
-
)
|
112 |
-
|
113 |
-
return final_similarity
|
114 |
|
115 |
def _calculate_breed_similarity(self, breed1_features: Dict, breed2_features: Dict) -> float:
|
116 |
"""計算兩個品種之間的相似度,包含健康和噪音因素"""
|
|
|
72 |
|
73 |
return sorted(similarities, key=lambda x: x[1], reverse=True)[:top_n]
|
74 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
75 |
|
76 |
def _calculate_breed_similarity(self, breed1_features: Dict, breed2_features: Dict) -> float:
|
77 |
"""計算兩個品種之間的相似度,包含健康和噪音因素"""
|