DawnC commited on
Commit
3518387
1 Parent(s): a4a7490

Update scoring_calculation_system.py

Browse files
Files changed (1) hide show
  1. scoring_calculation_system.py +131 -467
scoring_calculation_system.py CHANGED
@@ -206,205 +206,6 @@ def calculate_additional_factors(breed_info: dict, user_prefs: 'UserPreferences'
206
  return factors
207
 
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:
409
  """計算品種與使用者條件的相容性分數的優化版本"""
410
  try:
@@ -493,175 +294,111 @@ def calculate_compatibility_score(breed_info: dict, user_prefs: UserPreferences)
493
  return base_score
494
 
495
 
496
- # def calculate_experience_score(care_level: str, user_experience: str, temperament: str) -> float:
497
- # """
498
- # 計算使用者經驗與品種需求的匹配分數
499
-
500
- # 參數說明:
501
- # care_level: 品種的照顧難度 ("High", "Moderate", "Low")
502
- # user_experience: 使用者經驗等級 ("beginner", "intermediate", "advanced")
503
- # temperament: 品種的性格特徵描述
504
-
505
- # 返回:
506
- # float: 0.2-1.0 之間的匹配分數
507
- # """
508
- # # 基礎分數矩陣 - 更大的分數差異來反映經驗重要性
509
- # base_scores = {
510
- # "High": {
511
- # "beginner": 0.12, # 降低起始分,反映高難度品種對新手的挑戰
512
- # "intermediate": 0.65, # 中級玩家可以應付,但仍有改善空間
513
- # "advanced": 1.0 # 資深者能完全勝任
514
- # },
515
- # "Moderate": {
516
- # "beginner": 0.35, # 適中難度對新手來說仍具挑戰
517
- # "intermediate": 0.82, # 中級玩家有很好的勝任能力
518
- # "advanced": 1.0 # 資深者完全勝任
519
- # },
520
- # "Low": {
521
- # "beginner": 0.72, # 低難度品種適合新手
522
- # "intermediate": 0.92, # 中級玩家幾乎完全勝任
523
- # "advanced": 1.0 # 資深者完全勝任
524
- # }
525
- # }
526
-
527
- # # 取得基礎分數
528
- # score = base_scores.get(care_level, base_scores["Moderate"])[user_experience]
529
-
530
- # # 性格特徵評估 - 根據經驗等級調整權重
531
- # temperament_lower = temperament.lower()
532
- # temperament_adjustments = 0.0
533
-
534
- # if user_experience == "beginner":
535
- # # 新手不適合的特徵 - 更嚴格的懲罰
536
- # difficult_traits = {
537
- # 'stubborn': -0.15, # 加重固執的懲罰
538
- # 'independent': -0.12, # 加重獨立性的懲罰
539
- # 'dominant': -0.12, # 加重支配性的懲罰
540
- # 'strong-willed': -0.10, # 加重強勢的懲罰
541
- # 'protective': -0.08, # 加重保護性的懲罰
542
- # 'aloof': -0.08, # 加重冷漠的懲罰
543
- # 'energetic': -0.06 # 輕微懲罰高能量
544
- # }
545
-
546
- # # 新手友善的特徵 - 提供更多獎勵
547
- # easy_traits = {
548
- # 'gentle': 0.08, # 增加溫和的獎勵
549
- # 'friendly': 0.08, # 增加友善的獎勵
550
- # 'eager to please': 0.08, # 增加順從的獎勵
551
- # 'patient': 0.06, # 獎勵耐心
552
- # 'adaptable': 0.06, # 獎勵適應性
553
- # 'calm': 0.05 # 獎勵冷靜
554
- # }
555
-
556
- # # 計算特徵調整
557
- # for trait, penalty in difficult_traits.items():
558
- # if trait in temperament_lower:
559
- # temperament_adjustments += penalty * 1.2 # 加重新手的懲罰
560
-
561
- # for trait, bonus in easy_traits.items():
562
- # if trait in temperament_lower:
563
- # temperament_adjustments += bonus
564
-
565
- # # 品種特殊調整
566
- # if any(term in temperament_lower for term in ['terrier', 'working', 'guard']):
567
- # temperament_adjustments -= 0.12 # 加重對特定類型品種的懲罰
568
-
569
- # elif user_experience == "intermediate":
570
- # # 中級玩家的調整更加平衡
571
- # moderate_traits = {
572
- # 'intelligent': 0.05, # 獎勵聰明
573
- # 'athletic': 0.04, # 獎勵運動能力
574
- # 'versatile': 0.04, # 獎勵多功能性
575
- # 'stubborn': -0.06, # 輕微懲罰固執
576
- # 'independent': -0.05, # 輕微懲罰獨立性
577
- # 'protective': -0.04 # 輕微懲罰保護性
578
- # }
579
-
580
- # for trait, adjustment in moderate_traits.items():
581
- # if trait in temperament_lower:
582
- # temperament_adjustments += adjustment
583
-
584
- # else: # advanced
585
- # # 資深玩家能夠應對挑戰性特徵
586
- # advanced_traits = {
587
- # 'stubborn': 0.04, # 反轉為優勢
588
- # 'independent': 0.04, # 反轉為優勢
589
- # 'intelligent': 0.05, # 獎勵聰明
590
- # 'protective': 0.04, # 獎勵保護性
591
- # 'strong-willed': 0.03 # 獎勵強勢
592
- # }
593
-
594
- # for trait, bonus in advanced_traits.items():
595
- # if trait in temperament_lower:
596
- # temperament_adjustments += bonus
597
-
598
- # # 確保最終分數在合理範圍內
599
- # final_score = max(0.2, min(1.0, score + temperament_adjustments))
600
- # return final_score
601
-
602
-
603
  def calculate_experience_score(care_level: str, user_experience: str, temperament: str) -> float:
604
- # 首先建立基礎分數矩陣,確保不同難度和經驗等級有明顯差異
 
 
 
 
 
 
 
 
 
 
 
605
  base_scores = {
606
  "High": {
607
- "beginner": 0.2, # 高難度品種對新手極具挑戰
608
- "intermediate": 0.5, # 中級玩家有一定掌握能力
609
- "advanced": 0.7 # 專家也需要謹慎對待
610
  },
611
  "Moderate": {
612
- "beginner": 0.4,
613
- "intermediate": 0.65,
614
- "advanced": 0.8
615
  },
616
  "Low": {
617
- "beginner": 0.6,
618
- "intermediate": 0.75,
619
- "advanced": 0.85
620
  }
621
  }
622
 
623
- # 獲取基礎分數
624
- base_score = base_scores.get(care_level, base_scores["Moderate"])[user_experience]
625
 
626
- # 品種特性評估
627
  temperament_lower = temperament.lower()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
628
 
629
- # 計算品種難度係數
630
- difficulty_traits = {
631
- 'aggressive': 0.3,
632
- 'dominant': 0.25,
633
- 'stubborn': 0.25,
634
- 'independent': 0.2,
635
- 'protective': 0.2,
636
- 'strong-willed': 0.15
637
- }
638
-
639
- difficulty_score = sum(value for trait, value in difficulty_traits.items()
640
- if trait in temperament_lower)
641
-
642
- # 根據經驗等級調整難度的影響
643
- experience_modifiers = {
644
- "beginner": 1.2, # 新手受難度影響最大
645
- "intermediate": 0.8, # 中級玩家受中等影響
646
- "advanced": 0.5 # 專家受較小影響但仍然存在
647
- }
648
-
649
- # 應用經驗調整
650
- difficulty_impact = difficulty_score * experience_modifiers[user_experience]
651
- adjusted_score = base_score * (1 - difficulty_impact)
652
-
653
- # 特殊品種類型的額外調整
654
- breed_type_penalties = {
655
- 'terrier': {'beginner': -0.15, 'intermediate': -0.08, 'advanced': -0.04},
656
- 'working': {'beginner': -0.2, 'intermediate': -0.1, 'advanced': -0.05},
657
- 'guard': {'beginner': -0.25, 'intermediate': -0.12, 'advanced': -0.06}
658
- }
659
-
660
- for breed_type, penalties in breed_type_penalties.items():
661
- if breed_type in temperament_lower:
662
- adjusted_score += penalties[user_experience]
663
-
664
- return max(0.2, min(0.95, adjusted_score))
665
 
666
 
667
  def calculate_health_score(breed_name: str) -> float:
@@ -781,75 +518,6 @@ def calculate_compatibility_score(breed_info: dict, user_prefs: UserPreferences)
781
 
782
  return max(0.2, min(1.0, final_score))
783
 
784
- # # 計算所有基礎分數
785
- # scores = {
786
- # 'space': calculate_space_score(
787
- # breed_info['Size'],
788
- # user_prefs.living_space,
789
- # user_prefs.space_for_play,
790
- # breed_info.get('Exercise Needs', 'Moderate')
791
- # ),
792
- # 'exercise': calculate_exercise_score(
793
- # breed_info.get('Exercise Needs', 'Moderate'),
794
- # user_prefs.exercise_time
795
- # ),
796
- # 'grooming': calculate_grooming_score(
797
- # breed_info.get('Grooming Needs', 'Moderate'),
798
- # user_prefs.grooming_commitment.lower(),
799
- # breed_info['Size']
800
- # ),
801
- # 'experience': calculate_experience_score(
802
- # breed_info.get('Care Level', 'Moderate'),
803
- # user_prefs.experience_level,
804
- # breed_info.get('Temperament', '')
805
- # ),
806
- # 'health': calculate_health_score(breed_info.get('Breed', '')),
807
- # 'noise': calculate_noise_score(breed_info.get('Breed', ''), user_prefs.noise_tolerance)
808
- # }
809
-
810
-
811
- # # 優化權重配置
812
- # weights = {
813
- # 'space': 0.28,
814
- # 'exercise': 0.18,
815
- # 'grooming': 0.12,
816
- # 'experience': 0.22,
817
- # 'health': 0.12,
818
- # 'noise': 0.08
819
- # }
820
-
821
- # # 計算加權總分
822
- # weighted_score = sum(score * weights[category] for category, score in scores.items())
823
-
824
- # def amplify_score(score):
825
- # """
826
- # 優化分數放大函數,確保分數範圍合理且結果一致
827
- # """
828
- # # 基礎調整
829
- # adjusted = (score - 0.35) * 1.8
830
-
831
- # # 使用 3.2 次方使曲線更平滑
832
- # amplified = pow(adjusted, 3.2) / 5.8 + score
833
-
834
- # # 特別處理高分區間,確保不超過95%
835
- # if amplified > 0.90:
836
- # # 壓縮高分區間,確保最高到95%
837
- # amplified = 0.90 + (amplified - 0.90) * 0.5
838
-
839
- # # 確保最終分數在合理範圍內(0.55-0.95)
840
- # final_score = max(0.55, min(0.95, amplified))
841
-
842
- # # 四捨五入到小數點後第三位
843
- # return round(final_score, 3)
844
-
845
- # final_score = amplify_score(weighted_score)
846
-
847
- # # 四捨五入所有分數
848
- # scores = {k: round(v, 4) for k, v in scores.items()}
849
- # scores['overall'] = round(final_score, 4)
850
-
851
- # return scores
852
-
853
  # 計算所有基礎分數
854
  scores = {
855
  'space': calculate_space_score(
@@ -875,56 +543,52 @@ def calculate_compatibility_score(breed_info: dict, user_prefs: UserPreferences)
875
  'health': calculate_health_score(breed_info.get('Breed', '')),
876
  'noise': calculate_noise_score(breed_info.get('Breed', ''), user_prefs.noise_tolerance)
877
  }
878
-
879
- # 2. 計算品種加分
880
- if user_prefs.has_children:
881
- scores['family_safety'] = calculate_family_safety_score(breed_info, user_prefs.children_age)
882
- weights = {
883
- 'space': 0.22,
884
- 'exercise': 0.15,
885
- 'grooming': 0.10,
886
- 'experience': 0.20,
887
- 'health': 0.10,
888
- 'noise': 0.08,
889
- 'family_safety': 0.15
890
- }
891
- else:
892
- weights = {
893
- 'space': 0.28,
894
- 'exercise': 0.18,
895
- 'grooming': 0.12,
896
- 'experience': 0.22,
897
- 'health': 0.12,
898
- 'noise': 0.08
899
- }
900
 
901
- # 3. 計算加權分數
902
- weighted_score = sum(score * weights[category] for category, score in scores.items())
903
 
904
- # 4. 加入品種加分影響(限制在合理範圍內)
905
- breed_bonus = calculate_breed_bonus(breed_info, user_prefs)
906
- final_score = weighted_score * (1 + breed_bonus * 0.2)
 
 
 
 
 
 
907
 
908
- # 5. 最終分數調整
909
- def amplify_score(score):
910
- adjusted = (score - 0.3) * 1.6
911
- amplified = pow(adjusted, 2.5) / 4.0 + score
912
- return max(0.45, min(0.95, amplified))
913
 
914
- final_score = amplify_score(final_score)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
915
 
916
- # 6. 整理回傳結果
 
 
917
  scores = {k: round(v, 4) for k, v in scores.items()}
918
  scores['overall'] = round(final_score, 4)
919
-
920
- return scores
921
 
922
- # except Exception as e:
923
- # print(f"Error details: {str(e)}")
924
- # print(f"breed_info: {breed_info}")
925
- # # print(f"Error in calculate_compatibility_score: {str(e)}")
926
- # return {k: 0.5 for k in ['space', 'exercise', 'grooming', 'experience', 'health', 'noise', 'overall']}
927
 
928
  except Exception as e:
929
- print(f"Error in calculate_compatibility_score: {str(e)}")
930
- return {k: 0.7 for k in ['space', 'exercise', 'grooming', 'experience', 'health', 'noise', 'overall']}
 
 
 
206
  return factors
207
 
208
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
209
  def calculate_compatibility_score(breed_info: dict, user_prefs: UserPreferences) -> dict:
210
  """計算品種與使用者條件的相容性分數的優化版本"""
211
  try:
 
294
  return base_score
295
 
296
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
297
  def calculate_experience_score(care_level: str, user_experience: str, temperament: str) -> float:
298
+ """
299
+ 計算使用者經驗與品種需求的匹配分數
300
+
301
+ 參數說明:
302
+ care_level: 品種的照顧難度 ("High", "Moderate", "Low")
303
+ user_experience: 使用者經驗等級 ("beginner", "intermediate", "advanced")
304
+ temperament: 品種的性格特徵描述
305
+
306
+ 返回:
307
+ float: 0.2-1.0 之間的匹配分數
308
+ """
309
+ # 基礎分數矩陣 - 更大的分數差異來反映經驗重要性
310
  base_scores = {
311
  "High": {
312
+ "beginner": 0.12, # 降低起始分,反映高難度品種對新手的挑戰
313
+ "intermediate": 0.65, # 中級玩家可以應付,但仍有改善空間
314
+ "advanced": 1.0 # 資深者能完全勝任
315
  },
316
  "Moderate": {
317
+ "beginner": 0.35, # 適中難度對新手來說仍具挑戰
318
+ "intermediate": 0.82, # 中級玩家有很好的勝任能力
319
+ "advanced": 1.0 # 資深者完全勝任
320
  },
321
  "Low": {
322
+ "beginner": 0.72, # 低難度品種適合新手
323
+ "intermediate": 0.92, # 中級玩家幾乎完全勝���
324
+ "advanced": 1.0 # 資深者完全勝任
325
  }
326
  }
327
 
328
+ # 取得基礎分數
329
+ score = base_scores.get(care_level, base_scores["Moderate"])[user_experience]
330
 
331
+ # 性格特徵評估 - 根據經驗等級調整權重
332
  temperament_lower = temperament.lower()
333
+ temperament_adjustments = 0.0
334
+
335
+ if user_experience == "beginner":
336
+ # 新手不適合的特徵 - 更嚴格的懲罰
337
+ difficult_traits = {
338
+ 'stubborn': -0.15, # 加重固執的懲罰
339
+ 'independent': -0.12, # 加重獨立性的懲罰
340
+ 'dominant': -0.12, # 加重支配性的懲罰
341
+ 'strong-willed': -0.10, # 加重強勢的懲罰
342
+ 'protective': -0.08, # 加重保護性的懲罰
343
+ 'aloof': -0.08, # 加重冷漠的懲罰
344
+ 'energetic': -0.06 # 輕微懲罰高能量
345
+ }
346
+
347
+ # 新手友善的特徵 - 提供更多獎勵
348
+ easy_traits = {
349
+ 'gentle': 0.08, # 增加溫和的獎勵
350
+ 'friendly': 0.08, # 增加友善的獎勵
351
+ 'eager to please': 0.08, # 增加順從的獎勵
352
+ 'patient': 0.06, # 獎勵耐心
353
+ 'adaptable': 0.06, # 獎勵適應性
354
+ 'calm': 0.05 # 獎勵冷靜
355
+ }
356
+
357
+ # 計算特徵調整
358
+ for trait, penalty in difficult_traits.items():
359
+ if trait in temperament_lower:
360
+ temperament_adjustments += penalty * 1.2 # 加重新手的懲罰
361
+
362
+ for trait, bonus in easy_traits.items():
363
+ if trait in temperament_lower:
364
+ temperament_adjustments += bonus
365
+
366
+ # 品種特殊調整
367
+ if any(term in temperament_lower for term in ['terrier', 'working', 'guard']):
368
+ temperament_adjustments -= 0.12 # 加重對特定類型品種的懲罰
369
+
370
+ elif user_experience == "intermediate":
371
+ # 中級玩家的調整更加平衡
372
+ moderate_traits = {
373
+ 'intelligent': 0.05, # 獎勵聰明
374
+ 'athletic': 0.04, # 獎勵運動能力
375
+ 'versatile': 0.04, # 獎勵多功能性
376
+ 'stubborn': -0.06, # 輕微懲罰固執
377
+ 'independent': -0.05, # 輕微懲罰獨立性
378
+ 'protective': -0.04 # 輕微懲罰保護性
379
+ }
380
+
381
+ for trait, adjustment in moderate_traits.items():
382
+ if trait in temperament_lower:
383
+ temperament_adjustments += adjustment
384
+
385
+ else: # advanced
386
+ # 資深玩家能夠應對挑戰性特徵
387
+ advanced_traits = {
388
+ 'stubborn': 0.04, # 反轉為優勢
389
+ 'independent': 0.04, # 反轉為優勢
390
+ 'intelligent': 0.05, # 獎勵聰明
391
+ 'protective': 0.04, # 獎勵保護性
392
+ 'strong-willed': 0.03 # 獎勵強勢
393
+ }
394
+
395
+ for trait, bonus in advanced_traits.items():
396
+ if trait in temperament_lower:
397
+ temperament_adjustments += bonus
398
 
399
+ # 確保最終分數在合理範圍內
400
+ final_score = max(0.2, min(1.0, score + temperament_adjustments))
401
+ return final_score
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
402
 
403
 
404
  def calculate_health_score(breed_name: str) -> float:
 
518
 
519
  return max(0.2, min(1.0, final_score))
520
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
521
  # 計算所有基礎分數
522
  scores = {
523
  'space': calculate_space_score(
 
543
  'health': calculate_health_score(breed_info.get('Breed', '')),
544
  'noise': calculate_noise_score(breed_info.get('Breed', ''), user_prefs.noise_tolerance)
545
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
546
 
 
 
547
 
548
+ # 優化權重配置
549
+ weights = {
550
+ 'space': 0.28,
551
+ 'exercise': 0.18,
552
+ 'grooming': 0.12,
553
+ 'experience': 0.22,
554
+ 'health': 0.12,
555
+ 'noise': 0.08
556
+ }
557
 
558
+ # 計算加權總分
559
+ weighted_score = sum(score * weights[category] for category, score in scores.items())
 
 
 
560
 
561
+ def amplify_score(score):
562
+ """
563
+ 優化分數放大函數,確保分數範圍合理且結果一致
564
+ """
565
+ # 基礎調整
566
+ adjusted = (score - 0.35) * 1.8
567
+
568
+ # 使用 3.2 次方使曲線更平滑
569
+ amplified = pow(adjusted, 3.2) / 5.8 + score
570
+
571
+ # 特別處理高分區間,確保不超過95%
572
+ if amplified > 0.90:
573
+ # 壓縮高分區間,確保最高到95%
574
+ amplified = 0.90 + (amplified - 0.90) * 0.5
575
+
576
+ # 確保最終分數在合理範圍內(0.55-0.95)
577
+ final_score = max(0.55, min(0.95, amplified))
578
+
579
+ # 四捨五入到小數點後第三位
580
+ return round(final_score, 3)
581
 
582
+ final_score = amplify_score(weighted_score)
583
+
584
+ # 四捨五入所有分數
585
  scores = {k: round(v, 4) for k, v in scores.items()}
586
  scores['overall'] = round(final_score, 4)
 
 
587
 
588
+ return scores
 
 
 
 
589
 
590
  except Exception as e:
591
+ print(f"Error details: {str(e)}")
592
+ print(f"breed_info: {breed_info}")
593
+ # print(f"Error in calculate_compatibility_score: {str(e)}")
594
+ return {k: 0.6 for k in ['space', 'exercise', 'grooming', 'experience', 'health', 'noise', 'overall']}