Spaces:
Running
on
Zero
Running
on
Zero
Update scoring_calculation_system.py
Browse files- scoring_calculation_system.py +173 -74
scoring_calculation_system.py
CHANGED
@@ -208,102 +208,201 @@ def calculate_additional_factors(breed_info: dict, user_prefs: 'UserPreferences'
|
|
208 |
|
209 |
@staticmethod
|
210 |
def calculate_family_safety_score(breed_info: dict, children_age: str) -> float:
|
211 |
-
"""
|
212 |
-
計算品種與家庭/兒童的安全相容性分數,作為calculate_compatibility_score的一部分
|
213 |
-
|
214 |
-
參數:
|
215 |
-
breed_info (dict): 品種資訊
|
216 |
-
children_age (str): 兒童年齡組別 ('toddler', 'school_age', 'teenager')
|
217 |
-
|
218 |
-
返回:
|
219 |
-
float: 0.2-0.95之間的安全分數
|
220 |
-
"""
|
221 |
temperament = breed_info.get('Temperament', '').lower()
|
222 |
size = breed_info.get('Size', 'Medium')
|
223 |
|
224 |
-
#
|
225 |
base_safety_scores = {
|
226 |
-
|
227 |
-
|
228 |
-
|
229 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
230 |
}
|
231 |
-
safety_score = base_safety_scores.get(size, 0.60)
|
232 |
|
233 |
-
#
|
234 |
-
|
|
|
|
|
|
|
235 |
'toddler': {
|
236 |
-
'
|
237 |
-
'
|
238 |
-
|
239 |
-
|
240 |
-
|
241 |
-
|
242 |
-
}
|
243 |
},
|
244 |
'school_age': {
|
245 |
-
'
|
246 |
-
'
|
247 |
-
|
248 |
-
|
249 |
-
|
250 |
-
|
251 |
-
}
|
252 |
},
|
253 |
'teenager': {
|
254 |
-
'
|
255 |
-
'
|
256 |
-
|
257 |
-
|
258 |
-
|
259 |
-
|
260 |
-
}
|
261 |
}
|
262 |
}
|
263 |
|
264 |
-
#
|
265 |
-
|
266 |
-
|
267 |
-
|
268 |
-
|
269 |
-
|
270 |
-
|
271 |
-
'
|
272 |
-
|
273 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
274 |
}
|
275 |
-
|
276 |
-
# 特殊風險評估加重
|
277 |
-
if 'history of' in breed_info.get('Description', '').lower():
|
278 |
-
safety_score -= 0.25 # 從 -0.15 加重到 -0.25
|
279 |
-
if 'requires experienced' in breed_info.get('Description', '').lower():
|
280 |
-
safety_score -= 0.20 # 從 -0.10 加重到 -0.20
|
281 |
|
282 |
-
#
|
283 |
-
for trait, bonus in
|
284 |
if trait in temperament:
|
285 |
-
safety_score += bonus
|
286 |
-
|
287 |
-
for trait, penalty in dangerous_traits.items():
|
288 |
-
if trait in temperament:
|
289 |
-
# 對幼童加重懲罰
|
290 |
-
if children_age == 'toddler':
|
291 |
-
safety_score += penalty * 1.3
|
292 |
-
# 對青少年略微減輕懲罰
|
293 |
-
elif children_age == 'teenager':
|
294 |
-
safety_score += penalty * 0.8
|
295 |
-
else:
|
296 |
-
safety_score += penalty
|
297 |
|
298 |
-
#
|
299 |
description = breed_info.get('Description', '').lower()
|
300 |
if 'history of' in description:
|
301 |
-
safety_score -= 0.
|
302 |
if 'requires experienced' in description:
|
303 |
-
safety_score -= 0.
|
304 |
-
|
305 |
-
#
|
306 |
return max(0.2, min(0.95, safety_score))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
307 |
|
308 |
|
309 |
def calculate_compatibility_score(breed_info: dict, user_prefs: UserPreferences) -> dict:
|
|
|
208 |
|
209 |
@staticmethod
|
210 |
def calculate_family_safety_score(breed_info: dict, children_age: str) -> float:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
211 |
temperament = breed_info.get('Temperament', '').lower()
|
212 |
size = breed_info.get('Size', 'Medium')
|
213 |
|
214 |
+
# 基礎安全分數必須根據孩童年齡有所不同
|
215 |
base_safety_scores = {
|
216 |
+
'toddler': {
|
217 |
+
"Small": 0.85, # 幼童與小型犬相對安全
|
218 |
+
"Medium": 0.60, # 中型犬需要更多注意
|
219 |
+
"Large": 0.40, # 大型犬風險較高
|
220 |
+
"Giant": 0.30 # 巨型犬風險最高
|
221 |
+
},
|
222 |
+
'school_age': {
|
223 |
+
"Small": 0.90, # 學齡兒童與小型犬很合適
|
224 |
+
"Medium": 0.75, # 中型犬可以接受
|
225 |
+
"Large": 0.55, # 大型犬需要注意
|
226 |
+
"Giant": 0.45 # 巨型犬仍需謹慎
|
227 |
+
},
|
228 |
+
'teenager': {
|
229 |
+
"Small": 0.95, # 青少年幾乎能應付所有小型犬
|
230 |
+
"Medium": 0.85, # 中型犬很合適
|
231 |
+
"Large": 0.70, # 大型犬可以考慮
|
232 |
+
"Giant": 0.60 # 巨型犬仍需小心
|
233 |
+
}
|
234 |
}
|
|
|
235 |
|
236 |
+
# 根據孩童年齡選擇對應的基礎分數
|
237 |
+
safety_score = base_safety_scores[children_age][size]
|
238 |
+
|
239 |
+
# 年齡特定的危險特徵評估
|
240 |
+
age_specific_dangerous_traits = {
|
241 |
'toddler': {
|
242 |
+
'aggressive': -0.40, # 幼童最危險
|
243 |
+
'territorial': -0.35,
|
244 |
+
'protective': -0.30,
|
245 |
+
'nervous': -0.30,
|
246 |
+
'dominant': -0.25,
|
247 |
+
'energetic': -0.20 # 過度活潑對幼童也是風險
|
|
|
248 |
},
|
249 |
'school_age': {
|
250 |
+
'aggressive': -0.30,
|
251 |
+
'territorial': -0.25,
|
252 |
+
'protective': -0.20,
|
253 |
+
'nervous': -0.20,
|
254 |
+
'dominant': -0.15,
|
255 |
+
'energetic': -0.10
|
|
|
256 |
},
|
257 |
'teenager': {
|
258 |
+
'aggressive': -0.20,
|
259 |
+
'territorial': -0.15,
|
260 |
+
'protective': -0.10,
|
261 |
+
'nervous': -0.15,
|
262 |
+
'dominant': -0.10,
|
263 |
+
'energetic': -0.05
|
|
|
264 |
}
|
265 |
}
|
266 |
|
267 |
+
# 套用年齡特定的特徵評估
|
268 |
+
for trait, penalty in age_specific_dangerous_traits[children_age].items():
|
269 |
+
if trait in temperament:
|
270 |
+
safety_score += penalty
|
271 |
+
|
272 |
+
# 正面特徵評估(根據年齡調整獎勵程度)
|
273 |
+
positive_traits_by_age = {
|
274 |
+
'toddler': {
|
275 |
+
'gentle': 0.15,
|
276 |
+
'patient': 0.15,
|
277 |
+
'calm': 0.12,
|
278 |
+
'tolerant': 0.12
|
279 |
+
},
|
280 |
+
'school_age': {
|
281 |
+
'gentle': 0.12,
|
282 |
+
'patient': 0.12,
|
283 |
+
'playful': 0.10,
|
284 |
+
'friendly': 0.10
|
285 |
+
},
|
286 |
+
'teenager': {
|
287 |
+
'friendly': 0.10,
|
288 |
+
'playful': 0.10,
|
289 |
+
'adaptable': 0.08,
|
290 |
+
'trainable': 0.08
|
291 |
+
}
|
292 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
293 |
|
294 |
+
# 套用正面特徵評估
|
295 |
+
for trait, bonus in positive_traits_by_age[children_age].items():
|
296 |
if trait in temperament:
|
297 |
+
safety_score += bonus
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
298 |
|
299 |
+
# 特殊風險評估(對所有年齡都很重要)
|
300 |
description = breed_info.get('Description', '').lower()
|
301 |
if 'history of' in description:
|
302 |
+
safety_score -= 0.25
|
303 |
if 'requires experienced' in description:
|
304 |
+
safety_score -= 0.15
|
305 |
+
|
306 |
+
# 確保分數在合理範圍內
|
307 |
return max(0.2, min(0.95, safety_score))
|
308 |
+
|
309 |
+
# def calculate_family_safety_score(breed_info: dict, children_age: str) -> float:
|
310 |
+
# """
|
311 |
+
# 計算品種與家庭/兒童的安全相容性分數,作為calculate_compatibility_score的一部分
|
312 |
+
|
313 |
+
# 參數:
|
314 |
+
# breed_info (dict): 品種資訊
|
315 |
+
# children_age (str): 兒童年齡組別 ('toddler', 'school_age', 'teenager')
|
316 |
+
|
317 |
+
# 返回:
|
318 |
+
# float: 0.2-0.95之間的安全分數
|
319 |
+
# """
|
320 |
+
# temperament = breed_info.get('Temperament', '').lower()
|
321 |
+
# size = breed_info.get('Size', 'Medium')
|
322 |
+
|
323 |
+
# # 基礎安全分數(根據體型)
|
324 |
+
# base_safety_scores = {
|
325 |
+
# "Small": 0.80, # 從 0.85 降至 0.80
|
326 |
+
# "Medium": 0.65, # 從 0.75 降至 0.65
|
327 |
+
# "Large": 0.50, # 從 0.65 降至 0.50
|
328 |
+
# "Giant": 0.40 # 從 0.55 降至 0.40
|
329 |
+
# }
|
330 |
+
# safety_score = base_safety_scores.get(size, 0.60)
|
331 |
+
|
332 |
+
# # 加強年齡相關的調整力度
|
333 |
+
# age_factors = {
|
334 |
+
# 'toddler': {
|
335 |
+
# 'base_modifier': -0.25, # 從 -0.15 降至 -0.25
|
336 |
+
# 'size_penalty': {
|
337 |
+
# "Small": -0.10, # 從 -0.05 降至 -0.10
|
338 |
+
# "Medium": -0.20, # 從 -0.10 降至 -0.20
|
339 |
+
# "Large": -0.30, # 從 -0.20 降至 -0.30
|
340 |
+
# "Giant": -0.35 # 從 -0.25 降至 -0.35
|
341 |
+
# }
|
342 |
+
# },
|
343 |
+
# 'school_age': {
|
344 |
+
# 'base_modifier': -0.15, # 從 -0.08 降至 -0.15
|
345 |
+
# 'size_penalty': {
|
346 |
+
# "Small": -0.05,
|
347 |
+
# "Medium": -0.10,
|
348 |
+
# "Large": -0.20,
|
349 |
+
# "Giant": -0.25
|
350 |
+
# }
|
351 |
+
# },
|
352 |
+
# 'teenager': {
|
353 |
+
# 'base_modifier': -0.08, # 從 -0.05 降至 -0.08
|
354 |
+
# 'size_penalty': {
|
355 |
+
# "Small": -0.02,
|
356 |
+
# "Medium": -0.05,
|
357 |
+
# "Large": -0.10,
|
358 |
+
# "Giant": -0.15
|
359 |
+
# }
|
360 |
+
# }
|
361 |
+
# }
|
362 |
+
|
363 |
+
# # 加強對危險特徵的評估
|
364 |
+
# dangerous_traits = {
|
365 |
+
# 'aggressive': -0.35, # 從 -0.25 加重到 -0.35
|
366 |
+
# 'territorial': -0.30, # 從 -0.20 加重到 -0.30
|
367 |
+
# 'protective': -0.25, # 從 -0.15 加重到 -0.25
|
368 |
+
# 'nervous': -0.25, # 從 -0.15 加重到 -0.25
|
369 |
+
# 'dominant': -0.20, # 從 -0.15 加重到 -0.20
|
370 |
+
# 'strong-willed': -0.18, # 從 -0.12 加重到 -0.18
|
371 |
+
# 'independent': -0.15, # 從 -0.10 加重到 -0.15
|
372 |
+
# 'energetic': -0.12 # 從 -0.08 加重到 -0.12
|
373 |
+
# }
|
374 |
+
|
375 |
+
# # 特殊風險評估加重
|
376 |
+
# if 'history of' in breed_info.get('Description', '').lower():
|
377 |
+
# safety_score -= 0.25 # 從 -0.15 加重到 -0.25
|
378 |
+
# if 'requires experienced' in breed_info.get('Description', '').lower():
|
379 |
+
# safety_score -= 0.20 # 從 -0.10 加重到 -0.20
|
380 |
+
|
381 |
+
# # 計算特徵分數
|
382 |
+
# for trait, bonus in positive_traits.items():
|
383 |
+
# if trait in temperament:
|
384 |
+
# safety_score += bonus * 0.8 # 降低正面特徵的影響力
|
385 |
+
|
386 |
+
# for trait, penalty in dangerous_traits.items():
|
387 |
+
# if trait in temperament:
|
388 |
+
# # 對幼童加重懲罰
|
389 |
+
# if children_age == 'toddler':
|
390 |
+
# safety_score += penalty * 1.3
|
391 |
+
# # 對青少年略微減輕懲罰
|
392 |
+
# elif children_age == 'teenager':
|
393 |
+
# safety_score += penalty * 0.8
|
394 |
+
# else:
|
395 |
+
# safety_score += penalty
|
396 |
+
|
397 |
+
# # 特殊風險評估
|
398 |
+
# description = breed_info.get('Description', '').lower()
|
399 |
+
# if 'history of' in description:
|
400 |
+
# safety_score -= 0.15
|
401 |
+
# if 'requires experienced' in description:
|
402 |
+
# safety_score -= 0.10
|
403 |
+
|
404 |
+
# # 將分數限制在合理範圍內
|
405 |
+
# return max(0.2, min(0.95, safety_score))
|
406 |
|
407 |
|
408 |
def calculate_compatibility_score(breed_info: dict, user_prefs: UserPreferences) -> dict:
|