Spaces:
Running
on
Zero
Running
on
Zero
Update scoring_calculation_system.py
Browse files- scoring_calculation_system.py +311 -171
scoring_calculation_system.py
CHANGED
@@ -1294,44 +1294,101 @@ def calculate_environmental_fit(breed_info: dict, user_prefs: UserPreferences) -
|
|
1294 |
|
1295 |
return min(0.2, adaptability_score)
|
1296 |
|
1297 |
-
|
1298 |
# def calculate_breed_compatibility_score(scores: dict, user_prefs: UserPreferences, breed_info: dict) -> float:
|
1299 |
# """
|
1300 |
# 重構的品種相容性評分系統
|
1301 |
-
#
|
1302 |
# """
|
1303 |
# def evaluate_perfect_conditions():
|
1304 |
-
# """
|
1305 |
# perfect_matches = {
|
1306 |
-
# 'size_match':
|
1307 |
-
# 'exercise_match':
|
1308 |
-
# 'experience_match':
|
1309 |
# 'general_match': False
|
1310 |
# }
|
1311 |
|
1312 |
-
# #
|
1313 |
# if user_prefs.living_space == 'apartment':
|
1314 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1315 |
# elif user_prefs.living_space == 'house_large':
|
1316 |
-
#
|
|
|
|
|
|
|
|
|
|
|
1317 |
|
1318 |
-
# #
|
1319 |
# exercise_needs = breed_info.get('Exercise Needs', 'MODERATE').upper()
|
1320 |
-
#
|
1321 |
-
|
1322 |
-
#
|
1323 |
-
#
|
1324 |
-
#
|
1325 |
-
#
|
1326 |
-
|
1327 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1328 |
# care_level = breed_info.get('Care Level', 'MODERATE')
|
1329 |
-
# if care_level == 'High'
|
1330 |
-
#
|
1331 |
-
#
|
1332 |
-
#
|
1333 |
-
#
|
1334 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1335 |
|
1336 |
# # 一般條件匹配
|
1337 |
# if all(score >= 0.85 for score in scores.values()):
|
@@ -1340,11 +1397,11 @@ def calculate_environmental_fit(breed_info: dict, user_prefs: UserPreferences) -
|
|
1340 |
# return perfect_matches
|
1341 |
|
1342 |
# def calculate_weights():
|
1343 |
-
# """
|
1344 |
# base_weights = {
|
1345 |
# 'space': 0.20,
|
1346 |
# 'exercise': 0.20,
|
1347 |
-
# 'experience': 0.20,
|
1348 |
# 'grooming': 0.15,
|
1349 |
# 'health': 0.15,
|
1350 |
# 'noise': 0.10
|
@@ -1353,15 +1410,25 @@ def calculate_environmental_fit(breed_info: dict, user_prefs: UserPreferences) -
|
|
1353 |
# # 極端條件權重調整
|
1354 |
# multipliers = {}
|
1355 |
|
1356 |
-
# #
|
1357 |
# if user_prefs.experience_level == 'beginner':
|
1358 |
-
#
|
|
|
|
|
|
|
1359 |
# elif user_prefs.experience_level == 'advanced':
|
1360 |
-
#
|
1361 |
-
|
1362 |
-
#
|
1363 |
-
#
|
1364 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1365 |
# elif user_prefs.exercise_time < 30:
|
1366 |
# multipliers['exercise'] = 3.5
|
1367 |
|
@@ -1370,12 +1437,37 @@ def calculate_environmental_fit(breed_info: dict, user_prefs: UserPreferences) -
|
|
1370 |
# multipliers['space'] = 2.5
|
1371 |
# multipliers['noise'] = 2.0
|
1372 |
|
|
|
|
|
|
|
|
|
1373 |
# # 應用乘數
|
1374 |
# for key, multiplier in multipliers.items():
|
1375 |
# base_weights[key] *= multiplier
|
1376 |
|
1377 |
# return base_weights
|
1378 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1379 |
# # 評估完美匹配條件
|
1380 |
# perfect_conditions = evaluate_perfect_conditions()
|
1381 |
|
@@ -1389,129 +1481,162 @@ def calculate_environmental_fit(breed_info: dict, user_prefs: UserPreferences) -
|
|
1389 |
# # 計算基礎分數
|
1390 |
# base_score = sum(scores[k] * normalized_weights[k] for k in scores.keys())
|
1391 |
|
1392 |
-
# #
|
1393 |
# perfect_bonus = 1.0
|
1394 |
-
#
|
1395 |
-
#
|
1396 |
-
#
|
1397 |
-
# perfect_bonus += 0.2
|
1398 |
-
# if perfect_conditions['experience_match']:
|
1399 |
-
# perfect_bonus += 0.2
|
1400 |
# if perfect_conditions['general_match']:
|
1401 |
# perfect_bonus += 0.2
|
1402 |
|
1403 |
# # 品種特性加成
|
1404 |
-
# breed_bonus = calculate_breed_bonus(breed_info, user_prefs) * 1.5
|
1405 |
|
1406 |
# # 計算最終分數
|
1407 |
# final_score = (base_score * 0.7 + breed_bonus * 0.3) * perfect_bonus
|
1408 |
|
|
|
|
|
|
|
1409 |
# return min(1.0, final_score)
|
1410 |
|
|
|
1411 |
def calculate_breed_compatibility_score(scores: dict, user_prefs: UserPreferences, breed_info: dict) -> float:
|
1412 |
"""
|
1413 |
-
|
1414 |
-
|
|
|
|
|
|
|
1415 |
"""
|
1416 |
def evaluate_perfect_conditions():
|
1417 |
-
"""
|
1418 |
perfect_matches = {
|
1419 |
'size_match': 0,
|
1420 |
'exercise_match': 0,
|
1421 |
'experience_match': 0,
|
1422 |
-
'
|
1423 |
}
|
1424 |
|
1425 |
-
#
|
1426 |
-
|
1427 |
-
|
1428 |
-
|
1429 |
-
|
1430 |
-
|
1431 |
-
|
1432 |
-
|
1433 |
-
|
1434 |
-
|
1435 |
-
|
1436 |
-
|
1437 |
-
|
1438 |
-
|
1439 |
-
|
1440 |
-
|
1441 |
-
|
1442 |
-
|
1443 |
-
|
1444 |
-
|
1445 |
-
|
1446 |
-
|
1447 |
-
|
1448 |
-
#
|
1449 |
exercise_needs = breed_info.get('Exercise Needs', 'MODERATE').upper()
|
1450 |
exercise_time = user_prefs.exercise_time
|
|
|
1451 |
|
1452 |
-
|
1453 |
-
|
1454 |
-
|
1455 |
-
|
1456 |
-
|
1457 |
-
|
1458 |
-
|
1459 |
-
|
1460 |
-
|
1461 |
-
|
1462 |
-
|
1463 |
-
|
1464 |
-
|
1465 |
-
|
1466 |
-
|
1467 |
-
|
1468 |
-
|
1469 |
-
|
1470 |
-
|
1471 |
-
|
1472 |
-
|
1473 |
-
|
1474 |
-
|
1475 |
-
|
1476 |
-
|
1477 |
-
|
1478 |
-
|
1479 |
-
|
1480 |
-
|
1481 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1482 |
else:
|
1483 |
-
|
1484 |
|
1485 |
-
#
|
1486 |
-
|
1487 |
-
|
1488 |
-
|
1489 |
-
|
1490 |
-
|
1491 |
-
|
1492 |
-
|
1493 |
-
|
1494 |
-
|
1495 |
-
if user_prefs.experience_level == 'advanced':
|
1496 |
-
perfect_matches['experience_match'] = 0.9
|
1497 |
-
elif user_prefs.experience_level == 'intermediate':
|
1498 |
-
perfect_matches['experience_match'] = 1.0
|
1499 |
-
else:
|
1500 |
-
perfect_matches['experience_match'] = 0.7
|
1501 |
-
elif care_level == 'Low':
|
1502 |
-
if user_prefs.experience_level == 'beginner':
|
1503 |
-
perfect_matches['experience_match'] = 1.0
|
1504 |
-
else:
|
1505 |
-
perfect_matches['experience_match'] = 0.9
|
1506 |
-
|
1507 |
-
# 一般條件匹配
|
1508 |
-
if all(score >= 0.85 for score in scores.values()):
|
1509 |
-
perfect_matches['general_match'] = True
|
1510 |
-
|
1511 |
return perfect_matches
|
1512 |
|
1513 |
def calculate_weights():
|
1514 |
-
"""
|
1515 |
base_weights = {
|
1516 |
'space': 0.20,
|
1517 |
'exercise': 0.20,
|
@@ -1521,22 +1646,16 @@ def calculate_breed_compatibility_score(scores: dict, user_prefs: UserPreference
|
|
1521 |
'noise': 0.10
|
1522 |
}
|
1523 |
|
1524 |
-
# 極端條件權重調整
|
1525 |
multipliers = {}
|
1526 |
|
1527 |
-
#
|
1528 |
-
if user_prefs.
|
1529 |
-
|
1530 |
-
|
1531 |
-
|
1532 |
-
multipliers['
|
1533 |
-
|
1534 |
-
|
1535 |
-
multipliers['experience'] = 2.8
|
1536 |
-
else:
|
1537 |
-
multipliers['experience'] = 2.5
|
1538 |
-
|
1539 |
-
# 運動需求更細緻的調整
|
1540 |
exercise_needs = breed_info.get('Exercise Needs', 'MODERATE').upper()
|
1541 |
if exercise_needs == 'VERY HIGH':
|
1542 |
if user_prefs.exercise_time < 90:
|
@@ -1546,40 +1665,62 @@ def calculate_breed_compatibility_score(scores: dict, user_prefs: UserPreference
|
|
1546 |
elif user_prefs.exercise_time < 30:
|
1547 |
multipliers['exercise'] = 3.5
|
1548 |
|
1549 |
-
#
|
1550 |
-
if user_prefs.
|
1551 |
-
|
1552 |
-
|
1553 |
-
|
|
|
|
|
1554 |
# 噪音敏感度調整
|
1555 |
if user_prefs.noise_tolerance == 'low':
|
1556 |
-
multipliers['noise'] = multipliers.get('noise', 1.0) *
|
|
|
|
|
|
|
|
|
1557 |
|
1558 |
-
#
|
1559 |
for key, multiplier in multipliers.items():
|
1560 |
base_weights[key] *= multiplier
|
1561 |
|
1562 |
return base_weights
|
1563 |
|
1564 |
def apply_special_case_adjustments(score):
|
1565 |
-
"""
|
1566 |
-
#
|
1567 |
if user_prefs.experience_level == 'beginner':
|
1568 |
-
if
|
1569 |
-
breed_info.get('Exercise Needs') == 'VERY HIGH'
|
1570 |
-
|
1571 |
-
|
1572 |
-
|
|
|
|
|
1573 |
exercise_needs = breed_info.get('Exercise Needs', 'MODERATE').upper()
|
1574 |
if exercise_needs == 'VERY HIGH' and user_prefs.exercise_time < 60:
|
1575 |
-
score *= 0.
|
1576 |
-
|
1577 |
-
|
1578 |
-
if (user_prefs.noise_tolerance == 'low' and
|
1579 |
-
breed_info.get('Breed') in breed_noise_info and
|
1580 |
-
breed_noise_info[breed_info['Breed']]['noise_level'].lower() == 'high'):
|
1581 |
-
score *= 0.7
|
1582 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1583 |
return score
|
1584 |
|
1585 |
# 評估完美匹配條件
|
@@ -1595,19 +1736,18 @@ def calculate_breed_compatibility_score(scores: dict, user_prefs: UserPreference
|
|
1595 |
# 計算基礎分數
|
1596 |
base_score = sum(scores[k] * normalized_weights[k] for k in scores.keys())
|
1597 |
|
1598 |
-
#
|
1599 |
perfect_bonus = 1.0
|
1600 |
-
perfect_bonus += 0.
|
1601 |
-
perfect_bonus += 0.
|
1602 |
-
perfect_bonus += 0.
|
1603 |
-
|
1604 |
-
|
1605 |
-
|
1606 |
# 品種特性加成
|
1607 |
-
breed_bonus = calculate_breed_bonus(breed_info, user_prefs) * 1.
|
1608 |
|
1609 |
# 計算最終分數
|
1610 |
-
final_score = (base_score * 0.
|
1611 |
|
1612 |
# 應用特殊情況調整
|
1613 |
final_score = apply_special_case_adjustments(final_score)
|
|
|
1294 |
|
1295 |
return min(0.2, adaptability_score)
|
1296 |
|
|
|
1297 |
# def calculate_breed_compatibility_score(scores: dict, user_prefs: UserPreferences, breed_info: dict) -> float:
|
1298 |
# """
|
1299 |
# 重構的品種相容性評分系統
|
1300 |
+
# 目標:實現更大的分數差異和更高的頂部分數,更精確的條件匹配
|
1301 |
# """
|
1302 |
# def evaluate_perfect_conditions():
|
1303 |
+
# """評估完美條件匹配度,允許部分匹配"""
|
1304 |
# perfect_matches = {
|
1305 |
+
# 'size_match': 0,
|
1306 |
+
# 'exercise_match': 0,
|
1307 |
+
# 'experience_match': 0,
|
1308 |
# 'general_match': False
|
1309 |
# }
|
1310 |
|
1311 |
+
# # 體型與空間匹配更細緻化
|
1312 |
# if user_prefs.living_space == 'apartment':
|
1313 |
+
# if breed_info['Size'] == 'Small':
|
1314 |
+
# perfect_matches['size_match'] = 1.0
|
1315 |
+
# elif breed_info['Size'] == 'Medium':
|
1316 |
+
# perfect_matches['size_match'] = 0.5
|
1317 |
+
# else:
|
1318 |
+
# perfect_matches['size_match'] = 0
|
1319 |
+
# elif user_prefs.living_space == 'house_small':
|
1320 |
+
# if breed_info['Size'] in ['Small', 'Medium']:
|
1321 |
+
# perfect_matches['size_match'] = 1.0
|
1322 |
+
# elif breed_info['Size'] == 'Large':
|
1323 |
+
# perfect_matches['size_match'] = 0.6
|
1324 |
+
# else:
|
1325 |
+
# perfect_matches['size_match'] = 0.3
|
1326 |
# elif user_prefs.living_space == 'house_large':
|
1327 |
+
# if breed_info['Size'] in ['Medium', 'Large']:
|
1328 |
+
# perfect_matches['size_match'] = 1.0
|
1329 |
+
# elif breed_info['Size'] == 'Small':
|
1330 |
+
# perfect_matches['size_match'] = 0.7
|
1331 |
+
# else:
|
1332 |
+
# perfect_matches['size_match'] = 0.8
|
1333 |
|
1334 |
+
# # 運動需求匹配更精確
|
1335 |
# exercise_needs = breed_info.get('Exercise Needs', 'MODERATE').upper()
|
1336 |
+
# exercise_time = user_prefs.exercise_time
|
1337 |
+
|
1338 |
+
# if exercise_needs == 'VERY HIGH':
|
1339 |
+
# if exercise_time >= 150:
|
1340 |
+
# perfect_matches['exercise_match'] = 1.0
|
1341 |
+
# elif exercise_time >= 120:
|
1342 |
+
# perfect_matches['exercise_match'] = 0.7
|
1343 |
+
# elif exercise_time >= 90:
|
1344 |
+
# perfect_matches['exercise_match'] = 0.4
|
1345 |
+
# else:
|
1346 |
+
# perfect_matches['exercise_match'] = 0
|
1347 |
+
# elif exercise_needs == 'HIGH':
|
1348 |
+
# if 120 <= exercise_time <= 150:
|
1349 |
+
# perfect_matches['exercise_match'] = 1.0
|
1350 |
+
# elif exercise_time >= 90:
|
1351 |
+
# perfect_matches['exercise_match'] = 0.8
|
1352 |
+
# elif exercise_time >= 60:
|
1353 |
+
# perfect_matches['exercise_match'] = 0.5
|
1354 |
+
# else:
|
1355 |
+
# perfect_matches['exercise_match'] = 0.2
|
1356 |
+
# elif exercise_needs == 'MODERATE':
|
1357 |
+
# if 60 <= exercise_time <= 120:
|
1358 |
+
# perfect_matches['exercise_match'] = 1.0
|
1359 |
+
# elif exercise_time > 120:
|
1360 |
+
# perfect_matches['exercise_match'] = 0.8
|
1361 |
+
# else:
|
1362 |
+
# perfect_matches['exercise_match'] = 0.6
|
1363 |
+
# elif exercise_needs == 'LOW':
|
1364 |
+
# if 30 <= exercise_time <= 90:
|
1365 |
+
# perfect_matches['exercise_match'] = 1.0
|
1366 |
+
# elif exercise_time > 90:
|
1367 |
+
# perfect_matches['exercise_match'] = 0.7
|
1368 |
+
# else:
|
1369 |
+
# perfect_matches['exercise_match'] = 0.5
|
1370 |
+
|
1371 |
+
# # 經驗匹配更細緻
|
1372 |
# care_level = breed_info.get('Care Level', 'MODERATE')
|
1373 |
+
# if care_level == 'High':
|
1374 |
+
# if user_prefs.experience_level == 'advanced':
|
1375 |
+
# perfect_matches['experience_match'] = 1.0
|
1376 |
+
# elif user_prefs.experience_level == 'intermediate':
|
1377 |
+
# perfect_matches['experience_match'] = 0.6
|
1378 |
+
# else:
|
1379 |
+
# perfect_matches['experience_match'] = 0.2
|
1380 |
+
# elif care_level == 'Moderate':
|
1381 |
+
# if user_prefs.experience_level == 'advanced':
|
1382 |
+
# perfect_matches['experience_match'] = 0.9
|
1383 |
+
# elif user_prefs.experience_level == 'intermediate':
|
1384 |
+
# perfect_matches['experience_match'] = 1.0
|
1385 |
+
# else:
|
1386 |
+
# perfect_matches['experience_match'] = 0.7
|
1387 |
+
# elif care_level == 'Low':
|
1388 |
+
# if user_prefs.experience_level == 'beginner':
|
1389 |
+
# perfect_matches['experience_match'] = 1.0
|
1390 |
+
# else:
|
1391 |
+
# perfect_matches['experience_match'] = 0.9
|
1392 |
|
1393 |
# # 一般條件匹配
|
1394 |
# if all(score >= 0.85 for score in scores.values()):
|
|
|
1397 |
# return perfect_matches
|
1398 |
|
1399 |
# def calculate_weights():
|
1400 |
+
# """計算更動態的權重"""
|
1401 |
# base_weights = {
|
1402 |
# 'space': 0.20,
|
1403 |
# 'exercise': 0.20,
|
1404 |
+
# 'experience': 0.20,
|
1405 |
# 'grooming': 0.15,
|
1406 |
# 'health': 0.15,
|
1407 |
# 'noise': 0.10
|
|
|
1410 |
# # 極端條件權重調整
|
1411 |
# multipliers = {}
|
1412 |
|
1413 |
+
# # 經驗權重更細緻的調整
|
1414 |
# if user_prefs.experience_level == 'beginner':
|
1415 |
+
# if breed_info.get('Care Level') == 'High':
|
1416 |
+
# multipliers['experience'] = 3.5
|
1417 |
+
# else:
|
1418 |
+
# multipliers['experience'] = 3.0
|
1419 |
# elif user_prefs.experience_level == 'advanced':
|
1420 |
+
# if breed_info.get('Care Level') == 'High':
|
1421 |
+
# multipliers['experience'] = 2.8
|
1422 |
+
# else:
|
1423 |
+
# multipliers['experience'] = 2.5
|
1424 |
+
|
1425 |
+
# # 運動需求更細緻的調整
|
1426 |
+
# exercise_needs = breed_info.get('Exercise Needs', 'MODERATE').upper()
|
1427 |
+
# if exercise_needs == 'VERY HIGH':
|
1428 |
+
# if user_prefs.exercise_time < 90:
|
1429 |
+
# multipliers['exercise'] = 4.0
|
1430 |
+
# elif user_prefs.exercise_time > 150:
|
1431 |
+
# multipliers['exercise'] = 3.0
|
1432 |
# elif user_prefs.exercise_time < 30:
|
1433 |
# multipliers['exercise'] = 3.5
|
1434 |
|
|
|
1437 |
# multipliers['space'] = 2.5
|
1438 |
# multipliers['noise'] = 2.0
|
1439 |
|
1440 |
+
# # 噪音敏感度調整
|
1441 |
+
# if user_prefs.noise_tolerance == 'low':
|
1442 |
+
# multipliers['noise'] = multipliers.get('noise', 1.0) * 2.5
|
1443 |
+
|
1444 |
# # 應用乘數
|
1445 |
# for key, multiplier in multipliers.items():
|
1446 |
# base_weights[key] *= multiplier
|
1447 |
|
1448 |
# return base_weights
|
1449 |
|
1450 |
+
# def apply_special_case_adjustments(score):
|
1451 |
+
# """處理特殊情況"""
|
1452 |
+
# # 新手不適合的特殊情況
|
1453 |
+
# if user_prefs.experience_level == 'beginner':
|
1454 |
+
# if (breed_info.get('Care Level') == 'High' and
|
1455 |
+
# breed_info.get('Exercise Needs') == 'VERY HIGH'):
|
1456 |
+
# score *= 0.7
|
1457 |
+
|
1458 |
+
# # 運動時間極端不匹配的情況
|
1459 |
+
# exercise_needs = breed_info.get('Exercise Needs', 'MODERATE').upper()
|
1460 |
+
# if exercise_needs == 'VERY HIGH' and user_prefs.exercise_time < 60:
|
1461 |
+
# score *= 0.6
|
1462 |
+
|
1463 |
+
# # 噪音敏感度極端情況
|
1464 |
+
# if (user_prefs.noise_tolerance == 'low' and
|
1465 |
+
# breed_info.get('Breed') in breed_noise_info and
|
1466 |
+
# breed_noise_info[breed_info['Breed']]['noise_level'].lower() == 'high'):
|
1467 |
+
# score *= 0.7
|
1468 |
+
|
1469 |
+
# return score
|
1470 |
+
|
1471 |
# # 評估完美匹配條件
|
1472 |
# perfect_conditions = evaluate_perfect_conditions()
|
1473 |
|
|
|
1481 |
# # 計算基礎分數
|
1482 |
# base_score = sum(scores[k] * normalized_weights[k] for k in scores.keys())
|
1483 |
|
1484 |
+
# # 完美匹配獎勵更動態
|
1485 |
# perfect_bonus = 1.0
|
1486 |
+
# perfect_bonus += 0.2 * perfect_conditions['size_match']
|
1487 |
+
# perfect_bonus += 0.2 * perfect_conditions['exercise_match']
|
1488 |
+
# perfect_bonus += 0.2 * perfect_conditions['experience_match']
|
|
|
|
|
|
|
1489 |
# if perfect_conditions['general_match']:
|
1490 |
# perfect_bonus += 0.2
|
1491 |
|
1492 |
# # 品種特性加成
|
1493 |
+
# breed_bonus = calculate_breed_bonus(breed_info, user_prefs) * 1.5
|
1494 |
|
1495 |
# # 計算最終分數
|
1496 |
# final_score = (base_score * 0.7 + breed_bonus * 0.3) * perfect_bonus
|
1497 |
|
1498 |
+
# # 應用特殊情況調整
|
1499 |
+
# final_score = apply_special_case_adjustments(final_score)
|
1500 |
+
|
1501 |
# return min(1.0, final_score)
|
1502 |
|
1503 |
+
|
1504 |
def calculate_breed_compatibility_score(scores: dict, user_prefs: UserPreferences, breed_info: dict) -> float:
|
1505 |
"""
|
1506 |
+
優化後的品種相容性評分系統
|
1507 |
+
主要改進:
|
1508 |
+
1. 更精確的條件匹配度評估
|
1509 |
+
2. 更動態的權重分配
|
1510 |
+
3. 更嚴格的特殊情況處理
|
1511 |
"""
|
1512 |
def evaluate_perfect_conditions():
|
1513 |
+
"""評估完美條件匹配度,重點優化不同條件組合的評估邏輯"""
|
1514 |
perfect_matches = {
|
1515 |
'size_match': 0,
|
1516 |
'exercise_match': 0,
|
1517 |
'experience_match': 0,
|
1518 |
+
'living_condition_match': 0
|
1519 |
}
|
1520 |
|
1521 |
+
# 體型與居住空間匹配評估
|
1522 |
+
size_living_matrix = {
|
1523 |
+
'apartment': {
|
1524 |
+
'Small': 1.0,
|
1525 |
+
'Medium': 0.4,
|
1526 |
+
'Large': 0.1,
|
1527 |
+
'Giant': 0.05
|
1528 |
+
},
|
1529 |
+
'house_small': {
|
1530 |
+
'Small': 0.9,
|
1531 |
+
'Medium': 1.0,
|
1532 |
+
'Large': 0.5,
|
1533 |
+
'Giant': 0.3
|
1534 |
+
},
|
1535 |
+
'house_large': {
|
1536 |
+
'Small': 0.7,
|
1537 |
+
'Medium': 0.9,
|
1538 |
+
'Large': 1.0,
|
1539 |
+
'Giant': 0.9
|
1540 |
+
}
|
1541 |
+
}
|
1542 |
+
perfect_matches['size_match'] = size_living_matrix.get(user_prefs.living_space, {}).get(breed_info['Size'], 0.5)
|
1543 |
+
|
1544 |
+
# 運動需求匹配評估
|
1545 |
exercise_needs = breed_info.get('Exercise Needs', 'MODERATE').upper()
|
1546 |
exercise_time = user_prefs.exercise_time
|
1547 |
+
exercise_type = user_prefs.exercise_type
|
1548 |
|
1549 |
+
# 建立運動時間範圍對照表
|
1550 |
+
exercise_ranges = {
|
1551 |
+
'VERY HIGH': {'ideal': (150, 180), 'acceptable': (120, 200)},
|
1552 |
+
'HIGH': {'ideal': (120, 150), 'acceptable': (90, 180)},
|
1553 |
+
'MODERATE': {'ideal': (60, 120), 'acceptable': (45, 150)},
|
1554 |
+
'LOW': {'ideal': (30, 60), 'acceptable': (20, 90)}
|
1555 |
+
}
|
1556 |
+
|
1557 |
+
# 評估運動時間匹配度
|
1558 |
+
breed_range = exercise_ranges.get(exercise_needs, exercise_ranges['MODERATE'])
|
1559 |
+
if breed_range['ideal'][0] <= exercise_time <= breed_range['ideal'][1]:
|
1560 |
+
time_match = 1.0
|
1561 |
+
elif breed_range['acceptable'][0] <= exercise_time <= breed_range['acceptable'][1]:
|
1562 |
+
time_match = 0.7
|
1563 |
+
else:
|
1564 |
+
time_match = 0.3
|
1565 |
+
|
1566 |
+
# 運動類型匹配評估
|
1567 |
+
exercise_type_matrix = {
|
1568 |
+
'VERY HIGH': {
|
1569 |
+
'light_walks': 0.2,
|
1570 |
+
'moderate_activity': 0.5,
|
1571 |
+
'active_training': 1.0
|
1572 |
+
},
|
1573 |
+
'HIGH': {
|
1574 |
+
'light_walks': 0.3,
|
1575 |
+
'moderate_activity': 0.8,
|
1576 |
+
'active_training': 1.0
|
1577 |
+
},
|
1578 |
+
'MODERATE': {
|
1579 |
+
'light_walks': 0.7,
|
1580 |
+
'moderate_activity': 1.0,
|
1581 |
+
'active_training': 0.8
|
1582 |
+
},
|
1583 |
+
'LOW': {
|
1584 |
+
'light_walks': 1.0,
|
1585 |
+
'moderate_activity': 0.7,
|
1586 |
+
'active_training': 0.4
|
1587 |
+
}
|
1588 |
+
}
|
1589 |
+
|
1590 |
+
type_match = exercise_type_matrix.get(exercise_needs, {}).get(exercise_type, 0.5)
|
1591 |
+
perfect_matches['exercise_match'] = (time_match * 0.7) + (type_match * 0.3)
|
1592 |
+
|
1593 |
+
# 經驗匹配度評估
|
1594 |
+
care_level = breed_info.get('Care Level', 'MODERATE').upper()
|
1595 |
+
experience_matrix = {
|
1596 |
+
'HIGH': {
|
1597 |
+
'beginner': 0.1,
|
1598 |
+
'intermediate': 0.6,
|
1599 |
+
'advanced': 1.0
|
1600 |
+
},
|
1601 |
+
'MODERATE': {
|
1602 |
+
'beginner': 0.5,
|
1603 |
+
'intermediate': 1.0,
|
1604 |
+
'advanced': 0.9
|
1605 |
+
},
|
1606 |
+
'LOW': {
|
1607 |
+
'beginner': 1.0,
|
1608 |
+
'intermediate': 0.9,
|
1609 |
+
'advanced': 0.8
|
1610 |
+
}
|
1611 |
+
}
|
1612 |
+
perfect_matches['experience_match'] = experience_matrix.get(care_level, {}).get(user_prefs.experience_level, 0.5)
|
1613 |
+
|
1614 |
+
# 生活條件整體匹配評估
|
1615 |
+
living_factors = []
|
1616 |
+
|
1617 |
+
# 院子可用性評估
|
1618 |
+
if breed_info.get('Exercise Needs', 'MODERATE').upper() in ['HIGH', 'VERY HIGH']:
|
1619 |
+
if user_prefs.yard_access == 'no_yard':
|
1620 |
+
living_factors.append(0.3)
|
1621 |
+
elif user_prefs.yard_access == 'shared_yard':
|
1622 |
+
living_factors.append(0.7)
|
1623 |
else:
|
1624 |
+
living_factors.append(1.0)
|
1625 |
|
1626 |
+
# 時間可用性評估
|
1627 |
+
time_availability_scores = {
|
1628 |
+
'limited': 0.4,
|
1629 |
+
'moderate': 0.7,
|
1630 |
+
'flexible': 1.0
|
1631 |
+
}
|
1632 |
+
living_factors.append(time_availability_scores.get(user_prefs.time_availability, 0.7))
|
1633 |
+
|
1634 |
+
perfect_matches['living_condition_match'] = sum(living_factors) / len(living_factors) if living_factors else 0.5
|
1635 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1636 |
return perfect_matches
|
1637 |
|
1638 |
def calculate_weights():
|
1639 |
+
"""計算動態權重,根據使用者條件調整各項評分的重要性"""
|
1640 |
base_weights = {
|
1641 |
'space': 0.20,
|
1642 |
'exercise': 0.20,
|
|
|
1646 |
'noise': 0.10
|
1647 |
}
|
1648 |
|
|
|
1649 |
multipliers = {}
|
1650 |
|
1651 |
+
# 居住空間權重調整
|
1652 |
+
if user_prefs.living_space == 'apartment':
|
1653 |
+
multipliers['space'] = 3.0
|
1654 |
+
multipliers['noise'] = 2.5
|
1655 |
+
if breed_info['Size'] in ['Large', 'Giant']:
|
1656 |
+
multipliers['space'] = 4.0
|
1657 |
+
|
1658 |
+
# 運動需求權重調整
|
|
|
|
|
|
|
|
|
|
|
1659 |
exercise_needs = breed_info.get('Exercise Needs', 'MODERATE').upper()
|
1660 |
if exercise_needs == 'VERY HIGH':
|
1661 |
if user_prefs.exercise_time < 90:
|
|
|
1665 |
elif user_prefs.exercise_time < 30:
|
1666 |
multipliers['exercise'] = 3.5
|
1667 |
|
1668 |
+
# 經驗需求權重調整
|
1669 |
+
if user_prefs.experience_level == 'beginner':
|
1670 |
+
if breed_info.get('Care Level', 'MODERATE').upper() == 'HIGH':
|
1671 |
+
multipliers['experience'] = 4.0
|
1672 |
+
else:
|
1673 |
+
multipliers['experience'] = 3.0
|
1674 |
+
|
1675 |
# 噪音敏感度調整
|
1676 |
if user_prefs.noise_tolerance == 'low':
|
1677 |
+
multipliers['noise'] = multipliers.get('noise', 1.0) * 3.0
|
1678 |
+
|
1679 |
+
# 有小孩的情況特別注重經驗需求
|
1680 |
+
if user_prefs.has_children and user_prefs.children_age == 'toddler':
|
1681 |
+
multipliers['experience'] = multipliers.get('experience', 1.0) * 2.0
|
1682 |
|
1683 |
+
# 應用權重調整
|
1684 |
for key, multiplier in multipliers.items():
|
1685 |
base_weights[key] *= multiplier
|
1686 |
|
1687 |
return base_weights
|
1688 |
|
1689 |
def apply_special_case_adjustments(score):
|
1690 |
+
"""處理特殊情況,給予更嚴格的分數調整"""
|
1691 |
+
# 極端不匹配情況的嚴格懲罰
|
1692 |
if user_prefs.experience_level == 'beginner':
|
1693 |
+
if breed_info.get('Care Level') == 'HIGH':
|
1694 |
+
if breed_info.get('Exercise Needs') == 'VERY HIGH':
|
1695 |
+
score *= 0.4
|
1696 |
+
else:
|
1697 |
+
score *= 0.6
|
1698 |
+
|
1699 |
+
# 運動需求極端不匹配
|
1700 |
exercise_needs = breed_info.get('Exercise Needs', 'MODERATE').upper()
|
1701 |
if exercise_needs == 'VERY HIGH' and user_prefs.exercise_time < 60:
|
1702 |
+
score *= 0.4
|
1703 |
+
elif exercise_needs == 'LOW' and user_prefs.exercise_time > 150:
|
1704 |
+
score *= 0.5
|
|
|
|
|
|
|
|
|
1705 |
|
1706 |
+
# 居住空間極端不匹配
|
1707 |
+
if user_prefs.living_space == 'apartment':
|
1708 |
+
if breed_info['Size'] == 'Giant':
|
1709 |
+
score *= 0.3
|
1710 |
+
elif breed_info['Size'] == 'Large':
|
1711 |
+
score *= 0.5
|
1712 |
+
|
1713 |
+
# 噪音敏感度極端不匹配
|
1714 |
+
if user_prefs.noise_tolerance == 'low':
|
1715 |
+
if breed_info.get('Breed') in breed_noise_info:
|
1716 |
+
if breed_noise_info[breed_info['Breed']]['noise_level'].lower() == 'high':
|
1717 |
+
score *= 0.4
|
1718 |
+
|
1719 |
+
# 時間限制的影響
|
1720 |
+
if user_prefs.time_availability == 'limited':
|
1721 |
+
if breed_info.get('Exercise Needs').upper() in ['HIGH', 'VERY HIGH']:
|
1722 |
+
score *= 0.6
|
1723 |
+
|
1724 |
return score
|
1725 |
|
1726 |
# 評估完美匹配條件
|
|
|
1736 |
# 計算基礎分數
|
1737 |
base_score = sum(scores[k] * normalized_weights[k] for k in scores.keys())
|
1738 |
|
1739 |
+
# 完美匹配獎勵計算
|
1740 |
perfect_bonus = 1.0
|
1741 |
+
perfect_bonus += 0.15 * perfect_conditions['size_match']
|
1742 |
+
perfect_bonus += 0.15 * perfect_conditions['exercise_match']
|
1743 |
+
perfect_bonus += 0.15 * perfect_conditions['experience_match']
|
1744 |
+
perfect_bonus += 0.05 * perfect_conditions['living_condition_match']
|
1745 |
+
|
|
|
1746 |
# 品種特性加成
|
1747 |
+
breed_bonus = calculate_breed_bonus(breed_info, user_prefs) * 1.2
|
1748 |
|
1749 |
# 計算最終分數
|
1750 |
+
final_score = (base_score * 0.8 + breed_bonus * 0.2) * perfect_bonus
|
1751 |
|
1752 |
# 應用特殊情況調整
|
1753 |
final_score = apply_special_case_adjustments(final_score)
|