File size: 107,840 Bytes
1e4c9bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
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
1450
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
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
import random
import hashlib
import numpy as np
import sqlite3
import re
import traceback
from typing import List, Dict, Tuple, Optional, Any
from dataclasses import dataclass
from sentence_transformers import SentenceTransformer
import torch
from sklearn.metrics.pairwise import cosine_similarity
from dog_database import get_dog_description
from breed_health_info import breed_health_info
from breed_noise_info import breed_noise_info
from scoring_calculation_system import UserPreferences, calculate_compatibility_score, UnifiedScoringSystem, calculate_unified_breed_scores
from query_understanding import QueryUnderstandingEngine, analyze_user_query
from constraint_manager import ConstraintManager, apply_breed_constraints
from multi_head_scorer import MultiHeadScorer, score_breed_candidates, BreedScore
from score_calibrator import ScoreCalibrator, calibrate_breed_scores
from config_manager import get_config_manager, get_standardized_breed_data

@dataclass
class BreedDescriptionVector:
    """Data structure for breed description vectorization"""
    breed_name: str
    description_text: str
    embedding: np.ndarray
    characteristics: Dict[str, Any]

class SemanticBreedRecommender:
    """
    Enhanced SBERT-based semantic breed recommendation system
    Provides multi-dimensional natural language understanding for dog breed recommendations
    """

    def __init__(self):
        """Initialize the semantic recommender"""
        self.model_name = 'all-MiniLM-L6-v2'  # Efficient SBERT model
        self.sbert_model = None
        self.breed_vectors = {}
        self.breed_list = self._get_breed_list()
        self.comparative_keywords = {
            'most': 1.0, 'love': 1.0, 'prefer': 0.9, 'like': 0.8,
            'then': 0.7, 'second': 0.7, 'followed': 0.6,
            'third': 0.5, 'least': 0.3, 'dislike': 0.2
        }
        # self.query_engine = QueryUnderstandingEngine()
        # self.constraint_manager = ConstraintManager()
        # self.multi_head_scorer = None  # Will be initialized with SBERT model
        # self.score_calibrator = ScoreCalibrator()
        # self.config_manager = get_config_manager()
        self._initialize_model()
        self._build_breed_vectors()

        # Initialize multi-head scorer with SBERT model if enhanced mode is enabled
        # if self.sbert_model:
        #     self.multi_head_scorer = MultiHeadScorer(self.sbert_model)
        #     print("Multi-head scorer initialized with SBERT model")

    def _get_breed_list(self) -> List[str]:
        """Get breed list from database"""
        try:
            conn = sqlite3.connect('animal_detector.db')
            cursor = conn.cursor()
            cursor.execute("SELECT DISTINCT Breed FROM AnimalCatalog")
            breeds = [row[0] for row in cursor.fetchall()]
            cursor.close()
            conn.close()
            return breeds
        except Exception as e:
            print(f"Error getting breed list: {str(e)}")
            # Backup breed list for Google Colab environment
            return ['Labrador_Retriever', 'German_Shepherd', 'Golden_Retriever',
                   'Bulldog', 'Poodle', 'Beagle', 'Rottweiler', 'Yorkshire_Terrier']

    def _initialize_model(self):
        """Initialize SBERT model with fallback"""
        try:
            print("Loading SBERT model...")
            # Try different model names if the primary one fails
            model_options = ['all-MiniLM-L6-v2', 'all-mpnet-base-v2', 'all-MiniLM-L12-v2']

            for model_name in model_options:
                try:
                    self.sbert_model = SentenceTransformer(model_name)
                    self.model_name = model_name
                    print(f"SBERT model {model_name} loaded successfully")
                    return
                except Exception as model_e:
                    print(f"Failed to load {model_name}: {str(model_e)}")
                    continue

            # If all models fail
            print("All SBERT models failed to load. Using basic text matching fallback.")
            self.sbert_model = None

        except Exception as e:
            print(f"Failed to initialize any SBERT model: {str(e)}")
            print(traceback.format_exc())
            print("Will provide basic text-based recommendations without embeddings")
            self.sbert_model = None

    def _create_breed_description(self, breed: str) -> str:
        """Create comprehensive natural language description for breed with all key characteristics"""
        try:
            # Get all information sources
            breed_info = get_dog_description(breed) or {}
            health_info = breed_health_info.get(breed, {}) if breed_health_info else {}
            noise_info = breed_noise_info.get(breed, {}) if breed_noise_info else {}

            breed_display_name = breed.replace('_', ' ')
            description_parts = []

            # 1. Basic size and physical characteristics
            size = breed_info.get('Size', 'medium').lower()
            description_parts.append(f"{breed_display_name} is a {size} sized dog breed")

            # 2. Temperament and personality (critical for matching)
            temperament = breed_info.get('Temperament', '')
            if temperament:
                description_parts.append(f"with a {temperament.lower()} temperament")

            # 3. Exercise and activity level (critical for apartment living)
            exercise_needs = breed_info.get('Exercise Needs', 'moderate').lower()
            if 'high' in exercise_needs or 'very high' in exercise_needs:
                description_parts.append("requiring high daily exercise and mental stimulation")
            elif 'low' in exercise_needs or 'minimal' in exercise_needs:
                description_parts.append("with minimal exercise requirements, suitable for apartment living")
            else:
                description_parts.append("with moderate exercise needs")

            # 4. Noise characteristics (critical for quiet requirements)
            noise_level = noise_info.get('noise_level', 'moderate').lower()
            if 'low' in noise_level or 'quiet' in noise_level:
                description_parts.append("known for being quiet and rarely barking")
            elif 'high' in noise_level or 'loud' in noise_level:
                description_parts.append("tends to be vocal and bark frequently")
            else:
                description_parts.append("with moderate barking tendencies")

            # 5. Living space compatibility
            if size in ['small', 'tiny']:
                description_parts.append("excellent for small apartments and limited spaces")
            elif size in ['large', 'giant']:
                description_parts.append("requiring large living spaces and preferably a yard")
            else:
                description_parts.append("adaptable to various living situations")

            # 6. Grooming and maintenance
            grooming_needs = breed_info.get('Grooming Needs', 'moderate').lower()
            if 'high' in grooming_needs:
                description_parts.append("requiring regular professional grooming")
            elif 'low' in grooming_needs:
                description_parts.append("with minimal grooming requirements")
            else:
                description_parts.append("with moderate grooming needs")

            # 7. Family compatibility
            good_with_children = breed_info.get('Good with Children', 'Yes')
            if good_with_children == 'Yes':
                description_parts.append("excellent with children and families")
            else:
                description_parts.append("better suited for adult households")

            # 8. Intelligence and trainability (from database description)
            intelligence_keywords = []
            description_text = breed_info.get('Description', '').lower()

            if description_text:
                # Extract intelligence indicators from description
                if any(word in description_text for word in ['intelligent', 'smart', 'clever', 'quick to learn']):
                    intelligence_keywords.extend(['highly intelligent', 'trainable', 'quick learner'])
                elif any(word in description_text for word in ['stubborn', 'independent', 'difficult to train']):
                    intelligence_keywords.extend(['independent minded', 'requires patience', 'challenging to train'])
                else:
                    intelligence_keywords.extend(['moderate intelligence', 'trainable with consistency'])

                # Extract working/purpose traits from description
                if any(word in description_text for word in ['working', 'herding', 'guard', 'hunting']):
                    intelligence_keywords.extend(['working breed', 'purpose-driven', 'task-oriented'])
                elif any(word in description_text for word in ['companion', 'lap', 'toy', 'decorative']):
                    intelligence_keywords.extend(['companion breed', 'affectionate', 'people-focused'])

                # Add intelligence context to description
                if intelligence_keywords:
                    description_parts.append(f"characterized as {', '.join(intelligence_keywords[:2])}")

            # 9. Special characteristics and purpose (enhanced with database mining)
            if breed_info.get('Description'):
                desc = breed_info.get('Description', '')[:150]  # Increased to 150 chars for more context
                if desc:
                    # Extract key traits from description for better semantic matching
                    desc_lower = desc.lower()
                    key_traits = []

                    # Extract key behavioral traits from description
                    if 'friendly' in desc_lower:
                        key_traits.append('friendly')
                    if 'gentle' in desc_lower:
                        key_traits.append('gentle')
                    if 'energetic' in desc_lower or 'active' in desc_lower:
                        key_traits.append('energetic')
                    if 'calm' in desc_lower or 'peaceful' in desc_lower:
                        key_traits.append('calm')
                    if 'protective' in desc_lower or 'guard' in desc_lower:
                        key_traits.append('protective')

                    trait_text = f" and {', '.join(key_traits)}" if key_traits else ""
                    description_parts.append(f"Known for: {desc.lower()}{trait_text}")

            # 9. Care level requirements
            try:
                care_level = breed_info.get('Care Level', 'moderate')
                if isinstance(care_level, str):
                    description_parts.append(f"requiring {care_level.lower()} overall care level")
                else:
                    description_parts.append("requiring moderate overall care level")
            except Exception as e:
                print(f"Error processing care level for {breed}: {str(e)}")
                description_parts.append("requiring moderate overall care level")

            # 10. Lifespan information
            try:
                lifespan = breed_info.get('Lifespan', '10-12 years')
                if lifespan and isinstance(lifespan, str) and lifespan.strip():
                    description_parts.append(f"with a typical lifespan of {lifespan}")
                else:
                    description_parts.append("with a typical lifespan of 10-12 years")
            except Exception as e:
                print(f"Error processing lifespan for {breed}: {str(e)}")
                description_parts.append("with a typical lifespan of 10-12 years")

            # Create comprehensive description
            full_description = '. '.join(description_parts) + '.'

            # Add comprehensive keywords for better semantic matching
            keywords = []

            # Basic breed name keywords
            keywords.extend([word.lower() for word in breed_display_name.split()])

            # Temperament keywords
            if temperament:
                keywords.extend([word.lower().strip(',') for word in temperament.split()])

            # Size-based keywords
            if 'small' in size or 'tiny' in size:
                keywords.extend(['small', 'tiny', 'compact', 'little', 'apartment', 'indoor', 'lap'])
            elif 'large' in size or 'giant' in size:
                keywords.extend(['large', 'big', 'giant', 'huge', 'yard', 'space', 'outdoor'])
            else:
                keywords.extend(['medium', 'moderate', 'average', 'balanced'])

            # Activity level keywords
            exercise_needs = breed_info.get('Exercise Needs', 'moderate').lower()
            if 'high' in exercise_needs:
                keywords.extend(['active', 'energetic', 'exercise', 'outdoor', 'hiking', 'running', 'athletic'])
            elif 'low' in exercise_needs:
                keywords.extend(['calm', 'low-energy', 'indoor', 'relaxed', 'couch', 'sedentary'])
            else:
                keywords.extend(['moderate', 'balanced', 'walks', 'regular'])

            # Noise level keywords
            noise_level = noise_info.get('noise_level', 'moderate').lower()
            if 'quiet' in noise_level or 'low' in noise_level:
                keywords.extend(['quiet', 'silent', 'calm', 'peaceful', 'low-noise'])
            elif 'high' in noise_level or 'loud' in noise_level:
                keywords.extend(['vocal', 'barking', 'loud', 'alert', 'watchdog'])

            # Living situation keywords
            if size in ['small', 'tiny'] and 'low' in exercise_needs:
                keywords.extend(['apartment', 'city', 'urban', 'small-space'])
            if size in ['large', 'giant'] or 'high' in exercise_needs:
                keywords.extend(['house', 'yard', 'suburban', 'rural', 'space'])

            # Family keywords
            good_with_children = breed_info.get('Good with Children', 'Yes')
            if good_with_children == 'Yes':
                keywords.extend(['family', 'children', 'kids', 'friendly', 'gentle'])

            # Intelligence and trainability keywords (from database description mining)
            if intelligence_keywords:
                keywords.extend([word.lower() for phrase in intelligence_keywords for word in phrase.split()])

            # Grooming-based keywords (enhanced)
            grooming_needs = breed_info.get('Grooming Needs', 'moderate').lower()
            if 'high' in grooming_needs:
                keywords.extend(['high-maintenance', 'professional-grooming', 'daily-brushing', 'coat-care'])
            elif 'low' in grooming_needs:
                keywords.extend(['low-maintenance', 'minimal-grooming', 'easy-care', 'wash-and-go'])
            else:
                keywords.extend(['moderate-grooming', 'weekly-brushing', 'regular-care'])

            # Lifespan-based keywords
            lifespan = breed_info.get('Lifespan', '10-12 years')
            if lifespan and isinstance(lifespan, str):
                try:
                    # Extract years from lifespan string (e.g., "10-12 years" or "12-15 years")
                    import re
                    years = re.findall(r'\d+', lifespan)
                    if years:
                        avg_years = sum(int(y) for y in years) / len(years)
                        if avg_years >= 14:
                            keywords.extend(['long-lived', 'longevity', 'durable', 'healthy-lifespan'])
                        elif avg_years <= 8:
                            keywords.extend(['shorter-lifespan', 'health-considerations', 'special-care'])
                        else:
                            keywords.extend(['average-lifespan', 'moderate-longevity'])
                except:
                    keywords.extend(['average-lifespan'])

            # Add keywords to description for better semantic matching
            unique_keywords = list(set(keywords))
            keyword_text = ' '.join(unique_keywords)
            full_description += f" Additional context: {keyword_text}"

            return full_description

        except Exception as e:
            print(f"Error creating description for {breed}: {str(e)}")
            return f"{breed.replace('_', ' ')} is a dog breed with unique characteristics."

    def _build_breed_vectors(self):
        """Build vector representations for all breeds"""
        try:
            print("Building breed vector database...")

            # Skip if model is not available
            if self.sbert_model is None:
                print("SBERT model not available, skipping vector building")
                return

            for breed in self.breed_list:
                description = self._create_breed_description(breed)

                # Generate embedding vector
                embedding = self.sbert_model.encode(description, convert_to_tensor=False)

                # Get breed characteristics
                breed_info = get_dog_description(breed)
                characteristics = {
                    'size': breed_info.get('Size', 'Medium') if breed_info else 'Medium',
                    'exercise_needs': breed_info.get('Exercise Needs', 'Moderate') if breed_info else 'Moderate',
                    'grooming_needs': breed_info.get('Grooming Needs', 'Moderate') if breed_info else 'Moderate',
                    'good_with_children': breed_info.get('Good with Children', 'Yes') if breed_info else 'Yes',
                    'temperament': breed_info.get('Temperament', '') if breed_info else ''
                }

                self.breed_vectors[breed] = BreedDescriptionVector(
                    breed_name=breed,
                    description_text=description,
                    embedding=embedding,
                    characteristics=characteristics
                )

            print(f"Successfully built {len(self.breed_vectors)} breed vectors")

        except Exception as e:
            print(f"Error building breed vectors: {str(e)}")
            print(traceback.format_exc())
            raise

    def _parse_comparative_preferences(self, user_input: str) -> Dict[str, float]:
        """Parse comparative preference expressions"""
        breed_scores = {}

        # Normalize input
        text = user_input.lower()

        # Find breed names and preference keywords
        for breed in self.breed_list:
            breed_display = breed.replace('_', ' ').lower()
            breed_words = breed_display.split()

            # Check if this breed is mentioned
            breed_mentioned = False
            for word in breed_words:
                if word in text:
                    breed_mentioned = True
                    break

            if breed_mentioned:
                # Find nearby preference keywords
                breed_score = 0.5  # Default score

                # Look for keywords within 50 characters of breed name
                breed_pos = text.find(breed_words[0])
                if breed_pos != -1:
                    # Check for keywords in context
                    context_start = max(0, breed_pos - 50)
                    context_end = min(len(text), breed_pos + 50)
                    context = text[context_start:context_end]

                    for keyword, score in self.comparative_keywords.items():
                        if keyword in context:
                            breed_score = max(breed_score, score)

                breed_scores[breed] = breed_score

        return breed_scores

    def _extract_lifestyle_keywords(self, user_input: str) -> Dict[str, List[str]]:
        """Enhanced lifestyle keyword extraction with better pattern matching"""
        keywords = {
            'living_space': [],
            'activity_level': [],
            'family_situation': [],
            'noise_preference': [],
            'size_preference': [],
            'care_level': [],
            'special_needs': [],
            'intelligence_preference': [],
            'grooming_preference': [],
            'lifespan_preference': [],
            'temperament_preference': [],
            'experience_level': []
        }

        text = user_input.lower()

        # Enhanced living space detection
        apartment_terms = ['apartment', 'flat', 'condo', 'small space', 'city living', 'urban', 'no yard', 'indoor']
        house_terms = ['house', 'yard', 'garden', 'backyard', 'large space', 'suburban', 'rural', 'farm']

        if any(term in text for term in apartment_terms):
            keywords['living_space'].append('apartment')
        if any(term in text for term in house_terms):
            keywords['living_space'].append('house')

        # Enhanced activity level detection
        high_activity = ['active', 'energetic', 'exercise', 'hiking', 'running', 'outdoor', 'sports', 'jogging',
                        'athletic', 'adventure', 'vigorous', 'high energy', 'workout']
        low_activity = ['calm', 'lazy', 'indoor', 'low energy', 'couch', 'sedentary', 'relaxed',
                       'peaceful', 'quiet lifestyle', 'minimal exercise']
        moderate_activity = ['moderate', 'walk', 'daily walks', 'light exercise']

        if any(term in text for term in high_activity):
            keywords['activity_level'].append('high')
        if any(term in text for term in low_activity):
            keywords['activity_level'].append('low')
        if any(term in text for term in moderate_activity):
            keywords['activity_level'].append('moderate')

        # Enhanced family situation detection
        children_terms = ['children', 'kids', 'family', 'child', 'toddler', 'baby', 'teenage', 'school age']
        elderly_terms = ['elderly', 'senior', 'old', 'retirement', 'aged', 'mature']
        single_terms = ['single', 'alone', 'individual', 'solo', 'myself']

        if any(term in text for term in children_terms):
            keywords['family_situation'].append('children')
        if any(term in text for term in elderly_terms):
            keywords['family_situation'].append('elderly')
        if any(term in text for term in single_terms):
            keywords['family_situation'].append('single')

        # Enhanced noise preference detection
        quiet_terms = ['quiet', 'silent', 'noise-sensitive', 'peaceful', 'no barking', 'minimal noise',
                      'soft-spoken', 'calm', 'tranquil']
        noise_ok_terms = ['loud', 'barking ok', 'noise tolerant', 'vocal', 'doesn\'t matter']

        if any(term in text for term in quiet_terms):
            keywords['noise_preference'].append('low')
        if any(term in text for term in noise_ok_terms):
            keywords['noise_preference'].append('high')

        # Enhanced size preference detection
        small_terms = ['small', 'tiny', 'little', 'compact', 'miniature', 'toy', 'lap dog']
        large_terms = ['large', 'big', 'giant', 'huge', 'massive', 'great']
        medium_terms = ['medium', 'moderate size', 'average', 'mid-sized']

        if any(term in text for term in small_terms):
            keywords['size_preference'].append('small')
        if any(term in text for term in large_terms):
            keywords['size_preference'].append('large')
        if any(term in text for term in medium_terms):
            keywords['size_preference'].append('medium')

        # Enhanced care level detection
        low_care = ['low maintenance', 'easy care', 'simple', 'minimal grooming', 'wash and go']
        high_care = ['high maintenance', 'grooming', 'care intensive', 'professional grooming', 'daily brushing']

        if any(term in text for term in low_care):
            keywords['care_level'].append('low')
        if any(term in text for term in high_care):
            keywords['care_level'].append('high')

        # Intelligence preference detection (NEW)
        smart_terms = ['smart', 'intelligent', 'clever', 'bright', 'quick learner', 'easy to train', 'trainable', 'genius', 'brilliant']
        independent_terms = ['independent', 'stubborn', 'strong-willed', 'less trainable', 'thinks for themselves']

        if any(term in text for term in smart_terms):
            keywords['intelligence_preference'].append('high')
        if any(term in text for term in independent_terms):
            keywords['intelligence_preference'].append('independent')

        # Grooming preference detection (NEW)
        low_grooming_terms = ['low grooming', 'minimal grooming', 'easy care', 'wash and wear', 'no grooming', 'simple coat']
        high_grooming_terms = ['high grooming', 'professional grooming', 'lots of care', 'high maintenance coat', 'daily brushing', 'regular grooming']

        if any(term in text for term in low_grooming_terms):
            keywords['grooming_preference'].append('low')
        if any(term in text for term in high_grooming_terms):
            keywords['grooming_preference'].append('high')

        # Lifespan preference detection (NEW)
        long_lived_terms = ['long lived', 'long lifespan', 'live long', 'many years', '15+ years', 'longevity']
        healthy_terms = ['healthy breed', 'few health issues', 'robust', 'hardy', 'strong constitution']

        if any(term in text for term in long_lived_terms):
            keywords['lifespan_preference'].append('long')
        if any(term in text for term in healthy_terms):
            keywords['lifespan_preference'].append('healthy')

        # Temperament preference detection (NEW)
        gentle_terms = ['gentle', 'calm', 'peaceful', 'laid back', 'chill', 'mellow', 'docile']
        playful_terms = ['playful', 'energetic', 'fun', 'active personality', 'lively', 'spirited', 'bouncy']
        protective_terms = ['protective', 'guard', 'watchdog', 'alert', 'vigilant', 'defensive']
        friendly_terms = ['friendly', 'social', 'outgoing', 'loves people', 'sociable', 'gregarious']

        if any(term in text for term in gentle_terms):
            keywords['temperament_preference'].append('gentle')
        if any(term in text for term in playful_terms):
            keywords['temperament_preference'].append('playful')
        if any(term in text for term in protective_terms):
            keywords['temperament_preference'].append('protective')
        if any(term in text for term in friendly_terms):
            keywords['temperament_preference'].append('friendly')

        # Experience level detection (NEW)
        beginner_terms = ['first time', 'beginner', 'new to dogs', 'never had', 'novice', 'inexperienced']
        advanced_terms = ['experienced', 'advanced', 'dog expert', 'many dogs before', 'professional', 'seasoned']

        if any(term in text for term in beginner_terms):
            keywords['experience_level'].append('beginner')
        if any(term in text for term in advanced_terms):
            keywords['experience_level'].append('advanced')

        # Enhanced special needs detection
        guard_terms = ['guard', 'protection', 'security', 'watchdog', 'protective', 'defender']
        companion_terms = ['therapy', 'emotional support', 'companion', 'comfort', 'lap dog', 'cuddly']
        hypoallergenic_terms = ['hypoallergenic', 'allergies', 'non-shedding', 'allergy-friendly', 'no shed']
        multi_pet_terms = ['good with cats', 'cat friendly', 'multi-pet', 'other animals']

        if any(term in text for term in guard_terms):
            keywords['special_needs'].append('guard')
        if any(term in text for term in companion_terms):
            keywords['special_needs'].append('companion')
        if any(term in text for term in hypoallergenic_terms):
            keywords['special_needs'].append('hypoallergenic')
        if any(term in text for term in multi_pet_terms):
            keywords['special_needs'].append('multi_pet')

        return keywords

    def _apply_size_distribution_correction(self, recommendations: List[Dict]) -> List[Dict]:
        """Apply size distribution correction to prevent large breed bias"""
        if len(recommendations) < 10:
            return recommendations

        # Analyze size distribution
        size_counts = {'toy': 0, 'small': 0, 'medium': 0, 'large': 0, 'giant': 0}

        for rec in recommendations:
            breed_info = get_dog_description(rec['breed'])
            if breed_info:
                size = self._normalize_breed_size(breed_info.get('Size', 'Medium'))
                size_counts[size] += 1

        total_recs = len(recommendations)
        large_giant_ratio = (size_counts['large'] + size_counts['giant']) / total_recs

        # If more than 70% are large/giant breeds, apply correction
        if large_giant_ratio > 0.7:
            corrected_recommendations = []
            size_quotas = {'toy': 2, 'small': 4, 'medium': 6, 'large': 2, 'giant': 1}
            current_counts = {'toy': 0, 'small': 0, 'medium': 0, 'large': 0, 'giant': 0}

            # First pass: add breeds within quotas
            for rec in recommendations:
                breed_info = get_dog_description(rec['breed'])
                if breed_info:
                    size = self._normalize_breed_size(breed_info.get('Size', 'Medium'))
                    if current_counts[size] < size_quotas[size]:
                        corrected_recommendations.append(rec)
                        current_counts[size] += 1

            # Second pass: fill remaining slots with best remaining candidates
            remaining_slots = 15 - len(corrected_recommendations)
            remaining_breeds = [rec for rec in recommendations if rec not in corrected_recommendations]

            corrected_recommendations.extend(remaining_breeds[:remaining_slots])
            return corrected_recommendations

        return recommendations

    def _normalize_breed_size(self, size: str) -> str:
        """Normalize breed size to standard categories"""
        if not isinstance(size, str):
            return 'medium'

        size_lower = size.lower()
        if any(term in size_lower for term in ['toy', 'tiny']):
            return 'toy'
        elif 'small' in size_lower:
            return 'small'
        elif 'medium' in size_lower:
            return 'medium'
        elif 'large' in size_lower:
            return 'large'
        elif any(term in size_lower for term in ['giant', 'extra large']):
            return 'giant'
        else:
            return 'medium'

    def _parse_user_requirements(self, user_input: str) -> Dict[str, Any]:
        """Parse user requirements more accurately"""
        requirements = {
            'living_space': None,
            'exercise_level': None,
            'preferred_size': None,
            'noise_tolerance': None
        }

        input_lower = user_input.lower()

        # Living space detection
        if 'apartment' in input_lower or 'small' in input_lower:
            requirements['living_space'] = 'apartment'
        elif 'large house' in input_lower or 'big' in input_lower:
            requirements['living_space'] = 'large_house'
        elif 'medium' in input_lower:
            requirements['living_space'] = 'medium_house'

        # Exercise level detection
        if "don't exercise" in input_lower or 'low exercise' in input_lower:
            requirements['exercise_level'] = 'low'
        elif any(term in input_lower for term in ['hiking', 'running', 'active']):
            requirements['exercise_level'] = 'high'
        elif '30 minutes' in input_lower or 'moderate' in input_lower:
            requirements['exercise_level'] = 'moderate'

        # Size preference detection
        if any(term in input_lower for term in ['small dog', 'tiny', 'toy']):
            requirements['preferred_size'] = 'small'
        elif any(term in input_lower for term in ['large dog', 'big dog']):
            requirements['preferred_size'] = 'large'
        elif 'medium' in input_lower:
            requirements['preferred_size'] = 'medium'

        return requirements

    def _apply_hard_constraints(self, breed: str, user_input: str, breed_characteristics: Dict[str, Any]) -> float:
        """Enhanced hard constraints with stricter penalties"""
        penalty = 0.0
        user_text_lower = user_input.lower()

        # Get breed information
        breed_info = get_dog_description(breed)
        if not breed_info:
            return 0.0

        breed_size = breed_info.get('Size', '').lower()
        exercise_needs = breed_info.get('Exercise Needs', '').lower()

        # Apartment living constraints - MUCH STRICTER
        if any(term in user_text_lower for term in ['apartment', 'flat', 'studio', 'small space']):
            if 'giant' in breed_size:
                return -2.0  # Complete elimination
            elif 'large' in breed_size:
                if any(term in exercise_needs for term in ['high', 'very high']):
                    return -2.0  # Complete elimination
                else:
                    penalty -= 0.5  # Still significant penalty
            elif 'medium' in breed_size and 'very high' in exercise_needs:
                penalty -= 0.6

        # Exercise mismatch constraints
        if "don't exercise much" in user_text_lower or "low exercise" in user_text_lower:
            if any(term in exercise_needs for term in ['very high', 'extreme', 'intense']):
                return -2.0  # Complete elimination
            elif 'high' in exercise_needs:
                penalty -= 0.8

        # Moderate lifestyle detection
        if any(term in user_text_lower for term in ['moderate', 'balanced', '30 minutes', 'half hour']):
            # Penalize extremes
            if 'giant' in breed_size:
                penalty -= 0.7  # Strong penalty for giants
            elif 'very high' in exercise_needs:
                penalty -= 0.5

        # Children safety (existing logic remains but enhanced)
        if any(term in user_text_lower for term in ['child', 'kids', 'family', 'baby']):
            good_with_children = breed_info.get('Good with Children', '').lower()
            if good_with_children == 'no':
                return -2.0  # Complete elimination for safety

        return penalty

    def get_enhanced_semantic_recommendations(self, user_input: str, top_k: int = 15) -> List[Dict[str, Any]]:
        """
        Enhanced multi-dimensional semantic breed recommendation

        Args:
            user_input: User's natural language description
            top_k: Number of recommendations to return

        Returns:
            List of recommended breeds with enhanced scoring
        """
        try:
            # Stage 1: Query Understanding
            dimensions = self.query_engine.analyze_query(user_input)
            print(f"Query dimensions detected: {len(dimensions.spatial_constraints + dimensions.activity_level + dimensions.noise_preferences + dimensions.size_preferences + dimensions.family_context + dimensions.maintenance_level + dimensions.special_requirements)} total dimensions")

            # Stage 2: Apply Constraints
            filter_result = self.constraint_manager.apply_constraints(dimensions, min_candidates=max(8, top_k))
            print(f"Constraint filtering: {len(self.breed_list)} -> {len(filter_result.passed_breeds)} candidates")

            if not filter_result.passed_breeds:
                error_msg = f"No dog breeds match your requirements after applying constraints. Applied constraints: {filter_result.applied_constraints}. Consider relaxing some requirements."
                print(f"ERROR: {error_msg}")
                raise ValueError(error_msg)

            # Stage 3: Multi-head Scoring
            if self.multi_head_scorer:
                breed_scores = self.multi_head_scorer.score_breeds(filter_result.passed_breeds, dimensions)
                print(f"Multi-head scoring completed for {len(breed_scores)} breeds")
            else:
                print("Multi-head scorer not available, using fallback scoring")
                return self.get_semantic_recommendations(user_input, top_k)

            # Stage 4: Score Calibration
            breed_score_tuples = [(score.breed_name, score.final_score) for score in breed_scores]
            calibration_result = self.score_calibrator.calibrate_scores(breed_score_tuples)
            print(f"Score calibration: method={calibration_result.calibration_method}")

            # Stage 5: Generate Final Recommendations
            final_recommendations = []

            for i, breed_score in enumerate(breed_scores[:top_k]):
                breed_name = breed_score.breed_name

                # Get calibrated score
                calibrated_score = calibration_result.score_mapping.get(breed_name, breed_score.final_score)

                # Get standardized breed info
                standardized_info = get_standardized_breed_data(breed_name.replace(' ', '_'))
                if standardized_info:
                    breed_info = self._get_breed_info_from_standardized(standardized_info)
                else:
                    breed_info = get_dog_description(breed_name.replace(' ', '_')) or {}

                recommendation = {
                    'breed': breed_name,
                    'rank': i + 1,
                    'overall_score': calibrated_score,
                    'final_score': calibrated_score,
                    'semantic_score': breed_score.semantic_component,
                    'attribute_score': breed_score.attribute_component,
                    'bidirectional_bonus': breed_score.bidirectional_bonus,
                    'confidence_score': breed_score.confidence_score,
                    'dimensional_breakdown': breed_score.dimensional_breakdown,
                    'explanation': breed_score.explanation,
                    'size': breed_info.get('Size', 'Unknown'),
                    'temperament': breed_info.get('Temperament', ''),
                    'exercise_needs': breed_info.get('Exercise Needs', 'Moderate'),
                    'grooming_needs': breed_info.get('Grooming Needs', 'Moderate'),
                    'good_with_children': breed_info.get('Good with Children', 'Yes'),
                    'lifespan': breed_info.get('Lifespan', '10-12 years'),
                    'description': breed_info.get('Description', ''),
                    'search_type': 'enhanced_description',
                    'calibration_method': calibration_result.calibration_method,
                    'applied_constraints': filter_result.applied_constraints,
                    'relaxed_constraints': filter_result.relaxed_constraints,
                    'warnings': filter_result.warnings
                }

                final_recommendations.append(recommendation)

            # Apply size distribution correction before returning
            corrected_recommendations = self._apply_size_distribution_correction(final_recommendations)

            # Stage 6: Apply Intelligent Trait Matching Enhancement
            intelligence_enhanced_recommendations = self._apply_intelligent_trait_matching(corrected_recommendations, user_input)

            print(f"Generated {len(intelligence_enhanced_recommendations)} enhanced semantic recommendations with intelligent trait matching")
            return intelligence_enhanced_recommendations

        except Exception as e:
            print(f"Error in enhanced semantic recommendations: {str(e)}")
            print(traceback.format_exc())
            # Fallback to original method
            return self.get_semantic_recommendations(user_input, top_k)

    def _apply_intelligent_trait_matching(self, recommendations: List[Dict], user_input: str) -> List[Dict]:
        """Apply intelligent trait matching based on enhanced keyword extraction and database mining"""
        try:
            # Extract enhanced keywords from user input
            extracted_keywords = self._extract_lifestyle_keywords(user_input)

            # Apply intelligent trait matching to each recommendation
            enhanced_recommendations = []

            for rec in recommendations:
                breed_name = rec['breed'].replace(' ', '_')

                # Get breed database information
                breed_info = get_dog_description(breed_name) or {}

                # Calculate intelligent trait bonuses
                intelligence_bonus = 0.0
                trait_match_details = {}

                # 1. Intelligence Matching
                if extracted_keywords.get('intelligence_preference'):
                    intelligence_pref = extracted_keywords['intelligence_preference'][0]
                    breed_desc = breed_info.get('Description', '').lower()

                    if intelligence_pref == 'high':
                        if any(word in breed_desc for word in ['intelligent', 'smart', 'clever', 'quick to learn', 'trainable']):
                            intelligence_bonus += 0.05
                            trait_match_details['intelligence_match'] = 'High intelligence match detected'
                        elif any(word in breed_desc for word in ['stubborn', 'independent', 'difficult']):
                            intelligence_bonus -= 0.02
                            trait_match_details['intelligence_warning'] = 'May be challenging to train'

                    elif intelligence_pref == 'independent':
                        if any(word in breed_desc for word in ['independent', 'stubborn', 'strong-willed']):
                            intelligence_bonus += 0.03
                            trait_match_details['independence_match'] = 'Independent nature match'

                # 2. Grooming Preference Matching
                if extracted_keywords.get('grooming_preference'):
                    grooming_pref = extracted_keywords['grooming_preference'][0]
                    breed_grooming = breed_info.get('Grooming Needs', '').lower()

                    if grooming_pref == 'low' and 'low' in breed_grooming:
                        intelligence_bonus += 0.03
                        trait_match_details['grooming_match'] = 'Low maintenance grooming match'
                    elif grooming_pref == 'high' and 'high' in breed_grooming:
                        intelligence_bonus += 0.03
                        trait_match_details['grooming_match'] = 'High maintenance grooming match'
                    elif grooming_pref == 'low' and 'high' in breed_grooming:
                        intelligence_bonus -= 0.04
                        trait_match_details['grooming_mismatch'] = 'High grooming needs may not suit preferences'

                # 3. Temperament Preference Matching
                if extracted_keywords.get('temperament_preference'):
                    temp_prefs = extracted_keywords['temperament_preference']
                    breed_temperament = breed_info.get('Temperament', '').lower()
                    breed_desc = breed_info.get('Description', '').lower()

                    temp_text = (breed_temperament + ' ' + breed_desc).lower()

                    for temp_pref in temp_prefs:
                        if temp_pref == 'gentle' and any(word in temp_text for word in ['gentle', 'calm', 'peaceful', 'mild']):
                            intelligence_bonus += 0.04
                            trait_match_details['temperament_match'] = f'Gentle temperament match: {temp_pref}'
                        elif temp_pref == 'playful' and any(word in temp_text for word in ['playful', 'energetic', 'lively', 'fun']):
                            intelligence_bonus += 0.04
                            trait_match_details['temperament_match'] = f'Playful temperament match: {temp_pref}'
                        elif temp_pref == 'protective' and any(word in temp_text for word in ['protective', 'guard', 'alert', 'watchful']):
                            intelligence_bonus += 0.04
                            trait_match_details['temperament_match'] = f'Protective temperament match: {temp_pref}'
                        elif temp_pref == 'friendly' and any(word in temp_text for word in ['friendly', 'social', 'outgoing', 'people']):
                            intelligence_bonus += 0.04
                            trait_match_details['temperament_match'] = f'Friendly temperament match: {temp_pref}'

                # 4. Experience Level Matching
                if extracted_keywords.get('experience_level'):
                    exp_level = extracted_keywords['experience_level'][0]
                    breed_desc = breed_info.get('Description', '').lower()

                    if exp_level == 'beginner':
                        # Favor easy-to-handle breeds for beginners
                        if any(word in breed_desc for word in ['easy', 'gentle', 'good for beginners', 'family', 'calm']):
                            intelligence_bonus += 0.06
                            trait_match_details['beginner_friendly'] = 'Good choice for first-time owners'
                        elif any(word in breed_desc for word in ['challenging', 'dominant', 'requires experience', 'strong-willed']):
                            intelligence_bonus -= 0.08
                            trait_match_details['experience_warning'] = 'May be challenging for first-time owners'

                    elif exp_level == 'advanced':
                        # Advanced users can handle more challenging breeds
                        if any(word in breed_desc for word in ['working', 'requires experience', 'intelligent', 'strong']):
                            intelligence_bonus += 0.03
                            trait_match_details['advanced_suitable'] = 'Good match for experienced owners'

                # 5. Lifespan Preference Matching
                if extracted_keywords.get('lifespan_preference'):
                    lifespan_pref = extracted_keywords['lifespan_preference'][0]
                    breed_lifespan = breed_info.get('Lifespan', '10-12 years')

                    try:
                        import re
                        years = re.findall(r'\d+', breed_lifespan)
                        if years:
                            avg_years = sum(int(y) for y in years) / len(years)
                            if lifespan_pref == 'long' and avg_years >= 13:
                                intelligence_bonus += 0.02
                                trait_match_details['longevity_match'] = f'Long lifespan match: {breed_lifespan}'
                            elif lifespan_pref == 'healthy' and avg_years >= 12:
                                intelligence_bonus += 0.02
                                trait_match_details['health_match'] = f'Healthy lifespan: {breed_lifespan}'
                    except:
                        pass

                # Apply the intelligence bonus to the overall score
                original_score = rec['overall_score']
                enhanced_score = min(1.0, original_score + intelligence_bonus)

                # Create enhanced recommendation with trait matching details
                enhanced_rec = rec.copy()
                enhanced_rec['overall_score'] = enhanced_score
                enhanced_rec['intelligence_bonus'] = intelligence_bonus
                enhanced_rec['trait_match_details'] = trait_match_details

                # Add detailed explanation if significant enhancement occurred
                if abs(intelligence_bonus) > 0.02:
                    enhancement_explanation = []
                    for detail_key, detail_value in trait_match_details.items():
                        enhancement_explanation.append(detail_value)

                    if enhancement_explanation:
                        current_explanation = enhanced_rec.get('explanation', '')
                        enhanced_explanation = current_explanation + f" Enhanced matching: {'; '.join(enhancement_explanation)}"
                        enhanced_rec['explanation'] = enhanced_explanation

                enhanced_recommendations.append(enhanced_rec)

            # Re-sort by enhanced overall score
            enhanced_recommendations.sort(key=lambda x: x['overall_score'], reverse=True)

            # Update ranks
            for i, rec in enumerate(enhanced_recommendations):
                rec['rank'] = i + 1

            print(f"Applied intelligent trait matching with average bonus: {sum(r['intelligence_bonus'] for r in enhanced_recommendations) / len(enhanced_recommendations):.3f}")

            return enhanced_recommendations

        except Exception as e:
            print(f"Error in intelligent trait matching: {str(e)}")
            # Return original recommendations if trait matching fails
            return recommendations

    def get_semantic_recommendations(self, user_input: str, top_k: int = 15) -> List[Dict[str, Any]]:
        """
        Get breed recommendations based on natural language description

        Args:
            user_input: User's natural language description
            top_k: Number of recommendations to return

        Returns:
            List of recommended breeds
        """
        try:
            print(f"Processing user input: {user_input}")

            # Check if model is available - if not, raise error
            if self.sbert_model is None:
                error_msg = "SBERT model not available. This could be due to:\n• Model download failed\n• Insufficient memory\n• Network connectivity issues\n\nPlease check your environment and try again."
                print(f"ERROR: {error_msg}")
                raise RuntimeError(error_msg)

            # Generate user input embedding
            user_embedding = self.sbert_model.encode(user_input, convert_to_tensor=False)

            # Parse comparative preferences
            comparative_prefs = self._parse_comparative_preferences(user_input)

            # Extract lifestyle keywords
            lifestyle_keywords = self._extract_lifestyle_keywords(user_input)

            # Calculate similarity with all breeds and apply constraints
            similarities = []

            for breed, breed_vector in self.breed_vectors.items():
                # Apply hard constraints first
                constraint_penalty = self._apply_hard_constraints(breed, user_input, breed_vector.characteristics)

                # Skip breeds that violate critical constraints
                if constraint_penalty <= -1.0:  # Complete disqualification
                    continue

                # Basic semantic similarity
                semantic_score = cosine_similarity(
                    [user_embedding],
                    [breed_vector.embedding]
                )[0][0]

                # Comparative preference weighting
                comparative_bonus = comparative_prefs.get(breed, 0.0)

                # Lifestyle matching bonus
                lifestyle_bonus = self._calculate_lifestyle_bonus(
                    breed_vector.characteristics,
                    lifestyle_keywords
                )

                # Apply constraint penalties
                lifestyle_bonus += constraint_penalty

                # Enhanced combined score with better distribution
                # Apply exponential scaling to create more natural score spread
                base_semantic = semantic_score ** 0.8  # Slightly compress high scores
                enhanced_lifestyle = lifestyle_bonus * 2.0  # Amplify lifestyle matching
                enhanced_comparative = comparative_bonus * 1.5  # Amplify breed preferences

                final_score = (
                    base_semantic * 0.55 +
                    enhanced_comparative * 0.30 +
                    enhanced_lifestyle * 0.15
                )

                # Add small random variation to break ties naturally
                random.seed(hash(breed))  # Consistent for same breed
                final_score += random.uniform(-0.03, 0.03)

                # Ensure final score doesn't exceed 1.0
                final_score = min(1.0, final_score)

                similarities.append({
                    'breed': breed,
                    'score': final_score,
                    'semantic_score': semantic_score,
                    'comparative_bonus': comparative_bonus,
                    'lifestyle_bonus': lifestyle_bonus
                })

            # Calculate standardized display scores with balanced distribution
            breed_display_scores = []

            # First, collect all semantic scores for normalization
            all_semantic_scores = [breed_data['semantic_score'] for breed_data in similarities]
            semantic_mean = np.mean(all_semantic_scores)
            semantic_std = np.std(all_semantic_scores) if len(all_semantic_scores) > 1 else 1.0

            for breed_data in similarities:
                breed = breed_data['breed']
                base_semantic = breed_data['semantic_score']

                # Normalize semantic score to prevent extreme outliers
                if semantic_std > 0:
                    normalized_semantic = (base_semantic - semantic_mean) / semantic_std
                    normalized_semantic = max(-2.0, min(2.0, normalized_semantic))  # Cap at 2 standard deviations
                    scaled_semantic = 0.5 + (normalized_semantic * 0.1)  # Map to 0.3-0.7 range
                else:
                    scaled_semantic = 0.5

                # Get breed characteristics
                breed_info = get_dog_description(breed) if breed != 'Unknown' else {}
                breed_size = breed_info.get('Size', '').lower() if breed_info else ''
                exercise_needs = breed_info.get('Exercise Needs', '').lower() if breed_info else ''

                # Calculate feature matching score (more important than pure semantic similarity)
                feature_score = 0.0
                user_text = user_input.lower()

                # Size and space requirements (high weight)
                if any(term in user_text for term in ['apartment', 'small', 'limited space']):
                    if 'small' in breed_size:
                        feature_score += 0.25
                    elif 'medium' in breed_size:
                        feature_score += 0.05
                    elif 'large' in breed_size or 'giant' in breed_size:
                        feature_score -= 0.30

                # Exercise requirements (high weight)
                if any(term in user_text for term in ['low exercise', 'minimal exercise', "doesn't need", 'not much']):
                    if 'low' in exercise_needs or 'minimal' in exercise_needs:
                        feature_score += 0.20
                    elif 'high' in exercise_needs or 'very high' in exercise_needs:
                        feature_score -= 0.25
                elif any(term in user_text for term in ['active', 'high exercise', 'running', 'hiking']):
                    if 'high' in exercise_needs:
                        feature_score += 0.20
                    elif 'low' in exercise_needs:
                        feature_score -= 0.15

                # Family compatibility
                if any(term in user_text for term in ['children', 'kids', 'family']):
                    good_with_children = breed_info.get('Good with Children', '') if breed_info else ''
                    if good_with_children == 'Yes':
                        feature_score += 0.10
                    elif good_with_children == 'No':
                        feature_score -= 0.20

                # Combine scores with balanced weights
                final_score = (
                    scaled_semantic * 0.35 +  # Reduced semantic weight
                    feature_score * 0.45 +    # Increased feature matching weight
                    breed_data['lifestyle_bonus'] * 0.15 +
                    breed_data['comparative_bonus'] * 0.05
                )

                # Calculate base compatibility score
                base_compatibility = final_score

                # Apply dynamic scoring with natural distribution
                if base_compatibility >= 0.9:  # Exceptional matches
                    score_range = (0.92, 0.98)
                    position = (base_compatibility - 0.9) / 0.1
                elif base_compatibility >= 0.75:  # Excellent matches
                    score_range = (0.85, 0.91)
                    position = (base_compatibility - 0.75) / 0.15
                elif base_compatibility >= 0.6:  # Good matches
                    score_range = (0.75, 0.84)
                    position = (base_compatibility - 0.6) / 0.15
                elif base_compatibility >= 0.45:  # Fair matches
                    score_range = (0.65, 0.74)
                    position = (base_compatibility - 0.45) / 0.15
                elif base_compatibility >= 0.3:  # Poor matches
                    score_range = (0.55, 0.64)
                    position = (base_compatibility - 0.3) / 0.15
                else:  # Very poor matches
                    score_range = (0.45, 0.54)
                    position = max(0, base_compatibility / 0.3)

                # Calculate final score with natural variation
                score_span = score_range[1] - score_range[0]
                base_score = score_range[0] + (position * score_span)

                # Add controlled random variation for natural ranking
                random.seed(hash(breed + user_input[:15]))
                variation = random.uniform(-0.015, 0.015)
                display_score = round(max(0.45, min(0.98, base_score + variation)), 3)

                breed_display_scores.append({
                    'breed': breed,
                    'display_score': display_score,
                    'semantic_score': base_semantic,
                    'comparative_bonus': breed_data['comparative_bonus'],
                    'lifestyle_bonus': breed_data['lifestyle_bonus']
                })

            # Sort by display score to ensure ranking consistency
            breed_display_scores.sort(key=lambda x: x['display_score'], reverse=True)
            top_breeds = breed_display_scores[:top_k]

            # Convert to standard recommendation format
            recommendations = []
            for i, breed_data in enumerate(top_breeds):
                breed = breed_data['breed']
                display_score = breed_data['display_score']

                # Get detailed information
                breed_info = get_dog_description(breed)

                recommendation = {
                    'breed': breed.replace('_', ' '),
                    'rank': i + 1,
                    'overall_score': display_score,  # Use display score for consistency
                    'final_score': display_score,    # Ensure final_score matches overall_score
                    'semantic_score': breed_data['semantic_score'],
                    'comparative_bonus': breed_data['comparative_bonus'],
                    'lifestyle_bonus': breed_data['lifestyle_bonus'],
                    'size': breed_info.get('Size', 'Unknown') if breed_info else 'Unknown',
                    'temperament': breed_info.get('Temperament', '') if breed_info else '',
                    'exercise_needs': breed_info.get('Exercise Needs', 'Moderate') if breed_info else 'Moderate',
                    'grooming_needs': breed_info.get('Grooming Needs', 'Moderate') if breed_info else 'Moderate',
                    'good_with_children': breed_info.get('Good with Children', 'Yes') if breed_info else 'Yes',
                    'lifespan': breed_info.get('Lifespan', '10-12 years') if breed_info else '10-12 years',
                    'description': breed_info.get('Description', '') if breed_info else '',
                    'search_type': 'description'
                }

                recommendations.append(recommendation)

            print(f"Generated {len(recommendations)} semantic recommendations")
            return recommendations

        except Exception as e:
            print(f"Failed to generate semantic recommendations: {str(e)}")
            print(traceback.format_exc())
            return []

    def _calculate_lifestyle_bonus(self, breed_characteristics: Dict[str, Any],
                                 lifestyle_keywords: Dict[str, List[str]]) -> float:
        """Enhanced lifestyle matching bonus calculation"""
        bonus = 0.0
        penalties = 0.0

        # Enhanced size matching
        breed_size = breed_characteristics.get('size', '').lower()
        size_prefs = lifestyle_keywords.get('size_preference', [])
        for pref in size_prefs:
            if pref in breed_size:
                bonus += 0.25  # Strong reward for size match
            elif (pref == 'small' and 'large' in breed_size) or \
                 (pref == 'large' and 'small' in breed_size):
                penalties += 0.15  # Penalty for size mismatch

        # Enhanced activity level matching
        breed_exercise = breed_characteristics.get('exercise_needs', '').lower()
        activity_prefs = lifestyle_keywords.get('activity_level', [])

        if 'high' in activity_prefs:
            if 'high' in breed_exercise or 'very high' in breed_exercise:
                bonus += 0.2
            elif 'low' in breed_exercise:
                penalties += 0.2
        elif 'low' in activity_prefs:
            if 'low' in breed_exercise:
                bonus += 0.2
            elif 'high' in breed_exercise or 'very high' in breed_exercise:
                penalties += 0.25
        elif 'moderate' in activity_prefs:
            if 'moderate' in breed_exercise:
                bonus += 0.15

        # Enhanced family situation matching
        good_with_children = breed_characteristics.get('good_with_children', 'Yes')
        family_prefs = lifestyle_keywords.get('family_situation', [])

        if 'children' in family_prefs:
            if good_with_children == 'Yes':
                bonus += 0.15
            else:
                penalties += 0.3  # Strong penalty for non-child-friendly breeds

        # Enhanced living space matching
        living_prefs = lifestyle_keywords.get('living_space', [])
        if 'apartment' in living_prefs:
            if 'small' in breed_size:
                bonus += 0.2
            elif 'medium' in breed_size and 'low' in breed_exercise:
                bonus += 0.1
            elif 'large' in breed_size or 'giant' in breed_size:
                penalties += 0.2  # Penalty for large dogs in apartments

        # Noise preference matching
        noise_prefs = lifestyle_keywords.get('noise_preference', [])
        temperament = breed_characteristics.get('temperament', '').lower()

        if 'low' in noise_prefs:
            # Reward quiet breeds
            if any(term in temperament for term in ['gentle', 'calm', 'quiet']):
                bonus += 0.1

        # Care level matching
        grooming_needs = breed_characteristics.get('grooming_needs', '').lower()
        care_prefs = lifestyle_keywords.get('care_level', [])

        if 'low' in care_prefs and 'low' in grooming_needs:
            bonus += 0.1
        elif 'high' in care_prefs and 'high' in grooming_needs:
            bonus += 0.1
        elif 'low' in care_prefs and 'high' in grooming_needs:
            penalties += 0.15

        # Special needs matching
        special_needs = lifestyle_keywords.get('special_needs', [])

        if 'guard' in special_needs:
            if any(term in temperament for term in ['protective', 'alert', 'watchful']):
                bonus += 0.1
        elif 'companion' in special_needs:
            if any(term in temperament for term in ['affectionate', 'gentle', 'loyal']):
                bonus += 0.1

        # Calculate final bonus with penalties
        final_bonus = bonus - penalties
        return max(-0.3, min(0.5, final_bonus))  # Allow negative bonus but limit range

    def _get_breed_info_from_standardized(self, standardized_info) -> Dict[str, Any]:
        """Convert standardized breed info to dictionary format"""
        try:
            size_map = {1: 'Tiny', 2: 'Small', 3: 'Medium', 4: 'Large', 5: 'Giant'}
            exercise_map = {1: 'Low', 2: 'Moderate', 3: 'High', 4: 'Very High'}
            care_map = {1: 'Low', 2: 'Moderate', 3: 'High'}

            return {
                'Size': size_map.get(standardized_info.size_category, 'Medium'),
                'Exercise Needs': exercise_map.get(standardized_info.exercise_level, 'Moderate'),
                'Grooming Needs': care_map.get(standardized_info.care_complexity, 'Moderate'),
                'Good with Children': 'Yes' if standardized_info.child_compatibility >= 0.8 else
                                     'No' if standardized_info.child_compatibility <= 0.2 else 'Unknown',
                'Temperament': 'Varies by individual',
                'Lifespan': '10-12 years',
                'Description': f'A {size_map.get(standardized_info.size_category, "medium")} sized breed'
            }
        except Exception as e:
            print(f"Error converting standardized info: {str(e)}")
            return {}

    def _get_fallback_recommendations(self, top_k: int = 15) -> List[Dict[str, Any]]:
        """Get fallback recommendations when enhanced system fails"""
        try:
            safe_breeds = [
                ('Labrador Retriever', 0.85),
                ('Golden Retriever', 0.82),
                ('Cavalier King Charles Spaniel', 0.80),
                ('French Bulldog', 0.78),
                ('Boston Terrier', 0.76),
                ('Bichon Frise', 0.74),
                ('Pug', 0.72),
                ('Cocker Spaniel', 0.70)
            ]

            recommendations = []
            for i, (breed, score) in enumerate(safe_breeds[:top_k]):
                breed_info = get_dog_description(breed.replace(' ', '_')) or {}

                recommendation = {
                    'breed': breed,
                    'rank': i + 1,
                    'overall_score': score,
                    'final_score': score,
                    'semantic_score': score * 0.8,
                    'comparative_bonus': 0.0,
                    'lifestyle_bonus': 0.0,
                    'size': breed_info.get('Size', 'Unknown'),
                    'temperament': breed_info.get('Temperament', ''),
                    'exercise_needs': breed_info.get('Exercise Needs', 'Moderate'),
                    'grooming_needs': breed_info.get('Grooming Needs', 'Moderate'),
                    'good_with_children': breed_info.get('Good with Children', 'Yes'),
                    'lifespan': breed_info.get('Lifespan', '10-12 years'),
                    'description': breed_info.get('Description', ''),
                    'search_type': 'fallback'
                }
                recommendations.append(recommendation)

            return recommendations

        except Exception as e:
            print(f"Error generating fallback recommendations: {str(e)}")
            return []

    def get_enhanced_recommendations_with_unified_scoring(self, user_input: str, top_k: int = 15) -> List[Dict[str, Any]]:
        """簡化的增強推薦方法"""
        try:
            print(f"Processing enhanced recommendation: {user_input[:50]}...")

            # 使用基本語意匹配
            return self.get_semantic_recommendations(user_input, top_k)

        except Exception as e:
            error_msg = f"Enhanced recommendation error: {str(e)}. Please check your description."
            print(f"ERROR: {error_msg}")
            print(traceback.format_exc())
            raise RuntimeError(error_msg) from e

    def _analyze_user_description_enhanced(self, user_description: str) -> Dict[str, Any]:
        """增強用戶描述分析"""
        text = user_description.lower()
        analysis = {
            'mentioned_breeds': [],
            'lifestyle_keywords': {},
            'preference_strength': {},
            'constraint_requirements': [],
            'user_context': {}
        }

        # 提取提及的品種
        for breed in self.breed_list:
            breed_display = breed.replace('_', ' ').lower()
            if breed_display in text or any(word in text for word in breed_display.split()):
                analysis['mentioned_breeds'].append(breed)
                # 簡單偏好強度分析
                if any(word in text for word in ['love', 'prefer', 'like', '喜歡', '最愛']):
                    analysis['preference_strength'][breed] = 0.8
                else:
                    analysis['preference_strength'][breed] = 0.5

        # 提取約束要求
        if any(word in text for word in ['quiet', 'silent', 'no barking', '安靜']):
            analysis['constraint_requirements'].append('low_noise')
        if any(word in text for word in ['apartment', 'small space', '公寓']):
            analysis['constraint_requirements'].append('apartment_suitable')
        if any(word in text for word in ['children', 'kids', 'family', '小孩']):
            analysis['constraint_requirements'].append('child_friendly')

        # 提取用戶背景
        analysis['user_context'] = {
            'has_children': any(word in text for word in ['children', 'kids', '小孩']),
            'living_space': 'apartment' if any(word in text for word in ['apartment', '公寓']) else 'house',
            'activity_level': 'high' if any(word in text for word in ['active', 'energetic', '活躍']) else 'moderate',
            'noise_sensitive': any(word in text for word in ['quiet', 'silent', '安靜']),
            'experience_level': 'beginner' if any(word in text for word in ['first time', 'beginner', '新手']) else 'intermediate'
        }

        return analysis

    def _create_user_preferences_from_analysis_enhanced(self, analysis: Dict[str, Any]) -> UserPreferences:
        """從分析結果創建用戶偏好物件"""
        context = analysis['user_context']

        # 推斷居住空間類型
        living_space = 'apartment' if context.get('living_space') == 'apartment' else 'house_small'

        # 推斷院子權限
        yard_access = 'no_yard' if living_space == 'apartment' else 'shared_yard'

        # 推斷運動時間
        activity_level = context.get('activity_level', 'moderate')
        exercise_time_map = {'high': 120, 'moderate': 60, 'low': 30}
        exercise_time = exercise_time_map.get(activity_level, 60)

        # 推斷運動類型
        exercise_type_map = {'high': 'active_training', 'moderate': 'moderate_activity', 'low': 'light_walks'}
        exercise_type = exercise_type_map.get(activity_level, 'moderate_activity')

        # 推斷噪音容忍度
        noise_tolerance = 'low' if context.get('noise_sensitive', False) else 'medium'

        return UserPreferences(
            living_space=living_space,
            yard_access=yard_access,
            exercise_time=exercise_time,
            exercise_type=exercise_type,
            grooming_commitment='medium',
            experience_level=context.get('experience_level', 'intermediate'),
            time_availability='moderate',
            has_children=context.get('has_children', False),
            children_age='school_age' if context.get('has_children', False) else None,
            noise_tolerance=noise_tolerance,
            space_for_play=(living_space != 'apartment'),
            other_pets=False,
            climate='moderate',
            health_sensitivity='medium',
            barking_acceptance=noise_tolerance,
            size_preference='no_preference'
        )

    def _get_candidate_breeds_enhanced(self, analysis: Dict[str, Any]) -> List[str]:
        """獲取候選品種列表"""
        candidate_breeds = set()

        # 如果提及特定品種,優先包含
        if analysis['mentioned_breeds']:
            candidate_breeds.update(analysis['mentioned_breeds'])

        # 根據約束要求過濾品種
        if 'apartment_suitable' in analysis['constraint_requirements']:
            apartment_suitable = [
                'French_Bulldog', 'Cavalier_King_Charles_Spaniel', 'Boston_Terrier',
                'Pug', 'Bichon_Frise', 'Cocker_Spaniel', 'Yorkshire_Terrier', 'Shih_Tzu'
            ]
            candidate_breeds.update(breed for breed in apartment_suitable if breed in self.breed_list)

        if 'child_friendly' in analysis['constraint_requirements']:
            child_friendly = [
                'Labrador_Retriever', 'Golden_Retriever', 'Beagle', 'Cavalier_King_Charles_Spaniel',
                'Bichon_Frise', 'Poodle', 'Cocker_Spaniel'
            ]
            candidate_breeds.update(breed for breed in child_friendly if breed in self.breed_list)

        # 如果候選品種不足,添加更多通用品種
        if len(candidate_breeds) < 20:
            general_breeds = [
                'Labrador_Retriever', 'German_Shepherd', 'Golden_Retriever', 'French_Bulldog',
                'Bulldog', 'Poodle', 'Beagle', 'Rottweiler', 'Yorkshire_Terrier', 'Boston_Terrier',
                'Border_Collie', 'Siberian_Husky', 'Cavalier_King_Charles_Spaniel', 'Boxer',
                'Bichon_Frise', 'Cocker_Spaniel', 'Shih_Tzu', 'Pug', 'Chihuahua'
            ]
            candidate_breeds.update(breed for breed in general_breeds if breed in self.breed_list)

        return list(candidate_breeds)[:30]  # 限制候選數量以提高效率

    def _apply_constraint_filtering_enhanced(self, breed: str, analysis: Dict[str, Any]) -> float:
        """應用約束過濾,返回調整分數"""
        penalty = 0.0

        breed_info = get_dog_description(breed)
        if not breed_info:
            return penalty

        # 低噪音要求
        if 'low_noise' in analysis['constraint_requirements']:
            noise_info = breed_noise_info.get(breed, {})
            noise_level = noise_info.get('noise_level', 'moderate').lower()
            if 'high' in noise_level:
                penalty -= 0.3  # 嚴重扣分
            elif 'low' in noise_level:
                penalty += 0.1  # 輕微加分

        # 公寓適合性
        if 'apartment_suitable' in analysis['constraint_requirements']:
            size = breed_info.get('Size', '').lower()
            exercise_needs = breed_info.get('Exercise Needs', '').lower()

            if size in ['large', 'giant']:
                penalty -= 0.2
            elif size in ['small', 'tiny']:
                penalty += 0.1

            if 'high' in exercise_needs:
                penalty -= 0.15

        # 兒童友善性
        if 'child_friendly' in analysis['constraint_requirements']:
            good_with_children = breed_info.get('Good with Children', 'Unknown')
            if good_with_children == 'Yes':
                penalty += 0.15
            elif good_with_children == 'No':
                penalty -= 0.4  # 嚴重扣分

        return penalty

    def _get_breed_characteristics_enhanced(self, breed: str) -> Dict[str, Any]:
        """獲取品種特徵"""
        breed_info = get_dog_description(breed)
        if not breed_info:
            return {}

        characteristics = {
            'size': breed_info.get('Size', 'Unknown'),
            'temperament': breed_info.get('Temperament', ''),
            'exercise_needs': breed_info.get('Exercise Needs', 'Moderate'),
            'grooming_needs': breed_info.get('Grooming Needs', 'Moderate'),
            'good_with_children': breed_info.get('Good with Children', 'Unknown'),
            'lifespan': breed_info.get('Lifespan', '10-12 years'),
            'description': breed_info.get('Description', '')
        }

        # 添加噪音資訊
        noise_info = breed_noise_info.get(breed, {})
        characteristics['noise_level'] = noise_info.get('noise_level', 'moderate')

        return characteristics

    def get_hybrid_recommendations(self, user_description: str,
                                 user_preferences: Optional[Any] = None,
                                 top_k: int = 15) -> List[Dict[str, Any]]:
        """
        Hybrid recommendations: Combine semantic matching with traditional scoring

        Args:
            user_description: User's natural language description
            user_preferences: Optional structured preference settings
            top_k: Number of recommendations to return

        Returns:
            Hybrid recommendation results
        """
        try:
            # Get semantic recommendations
            semantic_recommendations = self.get_semantic_recommendations(user_description, top_k * 2)

            if not user_preferences:
                return semantic_recommendations[:top_k]

            # Combine with traditional scoring
            hybrid_results = []

            for semantic_rec in semantic_recommendations:
                breed_name = semantic_rec['breed'].replace(' ', '_')

                # Calculate traditional compatibility score
                traditional_score = calculate_compatibility_score(user_preferences, breed_name)

                # Hybrid score (semantic 40% + traditional 60%)
                hybrid_score = (
                    semantic_rec['overall_score'] * 0.4 +
                    traditional_score * 0.6
                )

                semantic_rec['hybrid_score'] = hybrid_score
                semantic_rec['traditional_score'] = traditional_score
                hybrid_results.append(semantic_rec)

            # Re-sort by hybrid score
            hybrid_results.sort(key=lambda x: x['hybrid_score'], reverse=True)

            # Update rankings
            for i, result in enumerate(hybrid_results[:top_k]):
                result['rank'] = i + 1
                result['overall_score'] = result['hybrid_score']

            return hybrid_results[:top_k]

        except Exception as e:
            print(f"Hybrid recommendation failed: {str(e)}")
            print(traceback.format_exc())
            return self.get_semantic_recommendations(user_description, top_k)

def get_breed_recommendations_by_description(user_description: str,
                                           user_preferences: Optional[Any] = None,
                                           top_k: int = 15) -> List[Dict[str, Any]]:
    """Main interface function for getting breed recommendations by description"""
    try:
        print("Initializing Enhanced SemanticBreedRecommender...")
        recommender = SemanticBreedRecommender()

        # 優先使用整合統一評分系統的增強推薦
        print("Using enhanced recommendation system with unified scoring")
        results = recommender.get_enhanced_recommendations_with_unified_scoring(user_description, top_k)

        if results and len(results) > 0:
            print(f"Generated {len(results)} enhanced recommendations successfully")
            return results
        else:
            # 如果增強系統無結果,嘗試原有增強系統
            print("Enhanced unified system returned no results, trying original enhanced system")
            results = recommender.get_enhanced_semantic_recommendations(user_description, top_k)

            if results and len(results) > 0:
                return results
            else:
                # 最後回退到標準系統
                print("All enhanced systems failed, using standard system")
                if user_preferences:
                    results = recommender.get_hybrid_recommendations(user_description, user_preferences, top_k)
                else:
                    results = recommender.get_semantic_recommendations(user_description, top_k)

                if not results:
                    error_msg = f"All recommendation systems failed to generate results. Please check your input description and try again. Error details may be in the console."
                    print(f"ERROR: {error_msg}")
                    raise RuntimeError(error_msg)
                return results

    except Exception as e:
        error_msg = f"Critical error in recommendation system: {str(e)}. Please check your input and system configuration."
        print(f"ERROR: {error_msg}")
        print(traceback.format_exc())
        raise RuntimeError(error_msg) from e


def get_enhanced_recommendations_with_unified_scoring(user_description: str, top_k: int = 15) -> List[Dict[str, Any]]:
    """簡化版本:基本語意推薦功能"""
    try:
        print(f"Processing description-based recommendation: {user_description[:50]}...")

        # 創建基本推薦器實例
        recommender = SemanticBreedRecommender()

        if not recommender.sbert_model:
            print("SBERT model not available, using basic text matching...")
            # 使用基本文字匹配邏輯
            return _get_basic_text_matching_recommendations(user_description, top_k)

        # 使用語意相似度推薦
        recommendations = []
        user_embedding = recommender.sbert_model.encode(user_description)

        # 計算所有品種的增強分數
        all_breed_scores = []
        for breed_name, breed_vector in recommender.breed_vectors.items():
            breed_embedding = breed_vector.embedding
            similarity = cosine_similarity([user_embedding], [breed_embedding])[0][0]

            # 獲取品種資料
            breed_info = get_dog_description(breed_name) or {}

            # 計算增強的匹配分數
            enhanced_score = _calculate_enhanced_matching_score(
                breed_name, breed_info, user_description, similarity
            )

            all_breed_scores.append((breed_name, enhanced_score, breed_info, similarity))

        # 按 final_score 排序(而不是語意相似度)
        all_breed_scores.sort(key=lambda x: x[1]['final_score'], reverse=True)
        top_breeds = all_breed_scores[:top_k]

        for i, (breed, enhanced_score, breed_info, similarity) in enumerate(top_breeds):
            recommendation = {
                'breed': breed.replace('_', ' '),
                'rank': i + 1,  # 正確的排名
                'overall_score': enhanced_score['final_score'],
                'final_score': enhanced_score['final_score'],
                'semantic_score': similarity,
                'comparative_bonus': enhanced_score['lifestyle_bonus'],
                'lifestyle_bonus': enhanced_score['lifestyle_bonus'],
                'size': breed_info.get('Size', 'Unknown'),
                'temperament': breed_info.get('Temperament', 'Unknown'),
                'exercise_needs': breed_info.get('Exercise Needs', 'Moderate'),
                'grooming_needs': breed_info.get('Grooming Needs', 'Moderate'),
                'good_with_children': breed_info.get('Good with Children', 'Unknown'),
                'lifespan': breed_info.get('Lifespan', '10-12 years'),
                'description': breed_info.get('Description', 'No description available'),
                'search_type': 'description',
                'scores': enhanced_score['dimension_scores']
            }
            recommendations.append(recommendation)

        print(f"Generated {len(recommendations)} semantic recommendations")
        return recommendations

    except Exception as e:
        error_msg = f"Error in semantic recommendation system: {str(e)}. Please check your input and try again."
        print(f"ERROR: {error_msg}")
        print(traceback.format_exc())
        raise RuntimeError(error_msg) from e

def _calculate_enhanced_matching_score(breed: str, breed_info: dict, user_description: str, base_similarity: float) -> dict:
    """計算增強的匹配分數,基於用戶描述和品種特性"""
    try:
        user_desc = user_description.lower()

        # 分析用戶需求
        space_requirements = _analyze_space_requirements(user_desc)
        exercise_requirements = _analyze_exercise_requirements(user_desc)
        noise_requirements = _analyze_noise_requirements(user_desc)
        size_requirements = _analyze_size_requirements(user_desc)
        family_requirements = _analyze_family_requirements(user_desc)

        # 獲取品種特性
        breed_size = breed_info.get('Size', '').lower()
        breed_exercise = breed_info.get('Exercise Needs', '').lower()
        breed_noise = breed_noise_info.get(breed, {}).get('noise_level', 'moderate').lower()
        breed_temperament = breed_info.get('Temperament', '').lower()
        breed_good_with_children = breed_info.get('Good with Children', '').lower()

        # 計算各維度匹配分數
        dimension_scores = {}

        # 空間匹配 (30% 權重)
        space_score = _calculate_space_compatibility(space_requirements, breed_size, breed_exercise)
        dimension_scores['space'] = space_score

        # 運動需求匹配 (25% 權重)
        exercise_score = _calculate_exercise_compatibility(exercise_requirements, breed_exercise)
        dimension_scores['exercise'] = exercise_score

        # 噪音匹配 (20% 權重)
        noise_score = _calculate_noise_compatibility(noise_requirements, breed_noise)
        dimension_scores['noise'] = noise_score

        # 體型匹配 (15% 權重)
        size_score = _calculate_size_compatibility(size_requirements, breed_size)
        dimension_scores['grooming'] = min(0.9, base_similarity + 0.1)  # 美容需求基於語意相似度

        # 家庭相容性 (10% 權重)
        family_score = _calculate_family_compatibility(family_requirements, breed_good_with_children, breed_temperament)
        dimension_scores['family'] = family_score
        dimension_scores['experience'] = min(0.9, base_similarity + 0.05)  # 經驗需求基於語意相似度

        # 應用硬約束過濾
        constraint_penalty = _apply_hard_constraints_enhanced(user_desc, breed_info)

        # 計算加權總分 - 精確化維度權重配置
        # 根據指導建議重新平衡維度權重
        weighted_score = (
            space_score * 0.30 +      # 空間相容性(降低5%)
            exercise_score * 0.28 +   # 運動需求匹配(降低2%)
            noise_score * 0.18 +      # 噪音控制(提升3%)
            family_score * 0.12 +     # 家庭相容性(提升2%)
            size_score * 0.08 +       # 體型匹配(降低2%)
            min(0.9, base_similarity + 0.1) * 0.04  # 護理需求(新增獨立權重)
        )

        # 優化完美匹配獎勵機制 - 降低觸發門檻並增加層次
        perfect_match_bonus = 0.0
        if space_score >= 0.88 and exercise_score >= 0.88 and noise_score >= 0.85:
            perfect_match_bonus = 0.08  # 卓越匹配獎勵
        elif space_score >= 0.82 and exercise_score >= 0.82 and noise_score >= 0.75:
            perfect_match_bonus = 0.04  # 優秀匹配獎勵
        elif space_score >= 0.75 and exercise_score >= 0.75:
            perfect_match_bonus = 0.02  # 良好匹配獎勵

        # 結合語意相似度與維度匹配 - 調整為75%維度匹配 25%語義相似度
        base_combined_score = (weighted_score * 0.75 + base_similarity * 0.25) + perfect_match_bonus

        # 應用漸進式約束懲罰,但確保基礎分數保障
        raw_final_score = base_combined_score + constraint_penalty

        # 實施動態分數保障機制 - 提升至40-42%基礎分數
        # 根據品種特性動態調整基礎分數
        base_guaranteed_score = 0.42  # 提升基礎保障分數

        # 特殊品種基礎分數調整
        high_adaptability_breeds = ['French_Bulldog', 'Pug', 'Golden_Retriever', 'Labrador_Retriever']
        if any(breed in breed for breed in high_adaptability_breeds):
            base_guaranteed_score = 0.45  # 高適應性品種更高基礎分數

        # 動態分數分佈優化
        if raw_final_score >= base_guaranteed_score:
            # 對於高分品種,實施適度壓縮避免過度集中
            if raw_final_score > 0.85:
                compression_factor = 0.92  # 輕度壓縮高分
                final_score = 0.85 + (raw_final_score - 0.85) * compression_factor
            else:
                final_score = raw_final_score
            final_score = min(0.93, final_score)  # 降低最高分數限制
        else:
            # 對於低分品種,使用改進的保障機制
            normalized_raw_score = max(0.15, raw_final_score)
            # 基礎保障75% + 實際計算25%,保持一定區分度
            final_score = base_guaranteed_score * 0.75 + normalized_raw_score * 0.25
            final_score = max(base_guaranteed_score, min(0.93, final_score))

        lifestyle_bonus = max(0.0, weighted_score - base_similarity)

        return {
            'final_score': final_score,
            'weighted_score': weighted_score,
            'lifestyle_bonus': lifestyle_bonus,
            'dimension_scores': dimension_scores,
            'constraint_penalty': constraint_penalty
        }

    except Exception as e:
        print(f"Error in enhanced matching calculation for {breed}: {str(e)}")
        return {
            'final_score': base_similarity,
            'weighted_score': base_similarity,
            'lifestyle_bonus': 0.0,
            'dimension_scores': {
                'space': base_similarity * 0.9,
                'exercise': base_similarity * 0.85,
                'grooming': base_similarity * 0.8,
                'experience': base_similarity * 0.75,
                'noise': base_similarity * 0.7,
                'family': base_similarity * 0.65
            },
            'constraint_penalty': 0.0
        }

def _analyze_space_requirements(user_desc: str) -> dict:
    """分析空間需求 - 增強中等活動量識別"""
    requirements = {'type': 'unknown', 'size': 'medium', 'importance': 0.5}

    if any(word in user_desc for word in ['apartment', 'small apartment', 'small space', 'condo', 'flat']):
        requirements['type'] = 'apartment'
        requirements['size'] = 'small'
        requirements['importance'] = 0.95  # 提高重要性
    elif any(word in user_desc for word in ['medium-sized house', 'medium house', 'townhouse']):
        requirements['type'] = 'medium_house'
        requirements['size'] = 'medium'
        requirements['importance'] = 0.8  # 中等活動量用戶的特殊標記
    elif any(word in user_desc for word in ['large house', 'big house', 'yard', 'garden', 'large space', 'backyard']):
        requirements['type'] = 'house'
        requirements['size'] = 'large'
        requirements['importance'] = 0.7

    return requirements

def _analyze_exercise_requirements(user_desc: str) -> dict:
    """分析運動需求 - 增強中等活動量識別"""
    requirements = {'level': 'moderate', 'importance': 0.5}

    # 低運動量識別
    if any(word in user_desc for word in ["don't exercise", "don't exercise much", "low exercise", "minimal", "lazy", "not active"]):
        requirements['level'] = 'low'
        requirements['importance'] = 0.95
    # 中等運動量的精確識別
    elif any(phrase in user_desc for phrase in ['30 minutes', 'half hour', 'moderate', 'balanced', 'walk about']):
        if 'walk' in user_desc or 'daily' in user_desc:
            requirements['level'] = 'moderate'
            requirements['importance'] = 0.85  # 中等活動量的特殊標記
    # 高運動量識別
    elif any(word in user_desc for word in ['active', 'hiking', 'outdoor activities', 'running', 'outdoors', 'love hiking']):
        requirements['level'] = 'high'
        requirements['importance'] = 0.9

    return requirements

def _analyze_noise_requirements(user_desc: str) -> dict:
    """分析噪音需求"""
    requirements = {'tolerance': 'medium', 'importance': 0.5}

    if any(word in user_desc for word in ['quiet', 'no bark', "won't bark", "doesn't bark", 'silent', 'peaceful']):
        requirements['tolerance'] = 'low'
        requirements['importance'] = 0.9
    elif any(word in user_desc for word in ['loud', 'barking ok', 'noise ok']):
        requirements['tolerance'] = 'high'
        requirements['importance'] = 0.7

    return requirements

def _analyze_size_requirements(user_desc: str) -> dict:
    """分析體型需求"""
    requirements = {'preferred': 'any', 'importance': 0.5}

    if any(word in user_desc for word in ['small', 'tiny', 'little', 'lap dog', 'compact']):
        requirements['preferred'] = 'small'
        requirements['importance'] = 0.8
    elif any(word in user_desc for word in ['large', 'big', 'giant']):
        requirements['preferred'] = 'large'
        requirements['importance'] = 0.8

    return requirements

def _analyze_family_requirements(user_desc: str) -> dict:
    """分析家庭需求"""
    requirements = {'children': False, 'importance': 0.3}

    if any(word in user_desc for word in ['children', 'kids', 'family', 'child']):
        requirements['children'] = True
        requirements['importance'] = 0.8

    return requirements

def _calculate_space_compatibility(space_req: dict, breed_size: str, breed_exercise: str) -> float:
    """計算空間相容性分數 - 增強中等活動量處理"""
    if space_req['type'] == 'apartment':
        if 'small' in breed_size or 'toy' in breed_size:
            base_score = 0.95
        elif 'medium' in breed_size:
            if 'low' in breed_exercise:
                base_score = 0.75
            else:
                base_score = 0.45  # 降低中型犬在公寓的分數
        elif 'large' in breed_size:
            base_score = 0.05  # 大型犬極度不適合公寓
        elif 'giant' in breed_size:
            base_score = 0.01  # 超大型犬完全不適合公寓
        else:
            base_score = 0.7
    elif space_req['type'] == 'medium_house':
        # 中型房屋的特殊處理 - 適合中等活動量用戶
        if 'small' in breed_size or 'toy' in breed_size:
            base_score = 0.9
        elif 'medium' in breed_size:
            base_score = 0.95  # 中型犬在中型房屋很適合
        elif 'large' in breed_size:
            if 'moderate' in breed_exercise or 'low' in breed_exercise:
                base_score = 0.8  # 低運動量大型犬還可以
            else:
                base_score = 0.6  # 高運動量大型犬不太適合
        elif 'giant' in breed_size:
            base_score = 0.3  # 超大型犬在中型房屋不太適合
        else:
            base_score = 0.85
    else:
        # 大型房屋的情況
        if 'small' in breed_size or 'toy' in breed_size:
            base_score = 0.85
        elif 'medium' in breed_size:
            base_score = 0.9
        elif 'large' in breed_size or 'giant' in breed_size:
            base_score = 0.95
        else:
            base_score = 0.8

    return min(0.95, base_score)

def _calculate_exercise_compatibility(exercise_req: dict, breed_exercise: str) -> float:
    """計算運動需求相容性分數 - 增強中等活動量處理"""
    if exercise_req['level'] == 'low':
        if 'low' in breed_exercise or 'minimal' in breed_exercise:
            return 0.95
        elif 'moderate' in breed_exercise:
            return 0.5  # 降低不匹配分數
        elif 'high' in breed_exercise:
            return 0.1  # 進一步降低高運動需求的匹配
        else:
            return 0.7
    elif exercise_req['level'] == 'high':
        if 'high' in breed_exercise:
            return 0.95
        elif 'moderate' in breed_exercise:
            return 0.8
        elif 'low' in breed_exercise:
            return 0.6
        else:
            return 0.7
    else:  # moderate - 中等活動量的精確處理
        if 'moderate' in breed_exercise:
            return 0.95  # 完美匹配
        elif 'low' in breed_exercise:
            return 0.85  # 低運動需求的品種對中等活動量用戶也不錯
        elif 'high' in breed_exercise:
            return 0.5  # 中等活動量用戶不太適合高運動需求品種
        else:
            return 0.75

    return 0.6

def _calculate_noise_compatibility(noise_req: dict, breed_noise: str) -> float:
    """計算噪音相容性分數,更好處理複合等級"""
    breed_noise_lower = breed_noise.lower()

    if noise_req['tolerance'] == 'low':
        if 'low' in breed_noise_lower and 'moderate' not in breed_noise_lower:
            return 0.95  # 純低噪音
        elif 'low-moderate' in breed_noise_lower or 'low to moderate' in breed_noise_lower:
            return 0.8   # 低到中等噪音,還可接受
        elif breed_noise_lower in ['moderate']:
            return 0.4   # 中等噪音有些問題
        elif 'high' in breed_noise_lower:
            return 0.1   # 高噪音不適合
        else:
            return 0.6   # 未知噪音水平,保守估計
    elif noise_req['tolerance'] == 'high':
        if 'high' in breed_noise_lower:
            return 0.9
        elif 'moderate' in breed_noise_lower:
            return 0.85
        elif 'low' in breed_noise_lower:
            return 0.8   # 安靜犬對高容忍度的人也很好
        else:
            return 0.8
    else:  # moderate tolerance
        if 'moderate' in breed_noise_lower:
            return 0.9
        elif 'low' in breed_noise_lower:
            return 0.85
        elif 'high' in breed_noise_lower:
            return 0.6
        else:
            return 0.75

    return 0.7

def _calculate_size_compatibility(size_req: dict, breed_size: str) -> float:
    """計算體型相容性分數"""
    if size_req['preferred'] == 'small':
        if any(word in breed_size for word in ['small', 'toy', 'tiny']):
            return 0.9
        elif 'medium' in breed_size:
            return 0.6
        else:
            return 0.3
    elif size_req['preferred'] == 'large':
        if any(word in breed_size for word in ['large', 'giant']):
            return 0.9
        elif 'medium' in breed_size:
            return 0.7
        else:
            return 0.4

    return 0.7  # 無特別偏好

def _calculate_family_compatibility(family_req: dict, good_with_children: str, temperament: str) -> float:
    """計算家庭相容性分數"""
    if family_req['children']:
        if 'yes' in good_with_children.lower():
            return 0.9
        elif any(word in temperament for word in ['gentle', 'patient', 'friendly']):
            return 0.8
        elif 'no' in good_with_children.lower():
            return 0.2
        else:
            return 0.6

    return 0.7

def _apply_hard_constraints_enhanced(user_desc: str, breed_info: dict) -> float:
    """應用品種特性感知的動態懲罰機制"""
    penalty = 0.0

    # 建立懲罰衰減係數和補償機制
    penalty_decay_factor = 0.7
    breed_adaptability_bonus = 0.0
    breed_size = breed_info.get('Size', '').lower()
    breed_exercise = breed_info.get('Exercise Needs', '').lower()
    breed_name = breed_info.get('Breed', '').replace(' ', '_')

    # 公寓空間約束 - 品種特性感知懲罰機制
    if 'apartment' in user_desc or 'small apartment' in user_desc:
        if 'giant' in breed_size:
            base_penalty = -0.35  # 減少基礎懲罰
            # 特定品種適應性補償
            adaptable_giants = ['Mastiff', 'Great Dane']  # 相對安靜的巨型犬
            if any(adapt_breed in breed_name for adapt_breed in adaptable_giants):
                breed_adaptability_bonus += 0.08
            penalty += base_penalty * penalty_decay_factor
        elif 'large' in breed_size:
            base_penalty = -0.25  # 減少大型犬懲罰
            # 適合公寓的大型犬補償
            apartment_friendly_large = ['Greyhound', 'Great_Dane']
            if any(apt_breed in breed_name for apt_breed in apartment_friendly_large):
                breed_adaptability_bonus += 0.06
            penalty += base_penalty * penalty_decay_factor
        elif 'medium' in breed_size and 'high' in breed_exercise:
            penalty += -0.15 * penalty_decay_factor  # 進一步減少懲罰

    # 運動需求不匹配 - 品種特性感知懲罰機制
    if any(phrase in user_desc for phrase in ["don't exercise", "not active", "low exercise", "don't exercise much"]):
        if 'high' in breed_exercise:
            base_penalty = -0.28  # 減少基礎懲罰
            # 低維護高運動犬種補償
            adaptable_high_energy = ['Greyhound', 'Whippet']  # 運動爆發型,平時安靜
            if any(adapt_breed in breed_name for adapt_breed in adaptable_high_energy):
                breed_adaptability_bonus += 0.10
            penalty += base_penalty * penalty_decay_factor
        elif 'moderate' in breed_exercise:
            penalty += -0.08 * penalty_decay_factor  # 進一步減少懲罰

    # 噪音控制需求不匹配 - 品種特性感知懲罰機制
    if any(phrase in user_desc for phrase in ['quiet', "won't bark", "doesn't bark", "silent"]):
        breed_noise = breed_noise_info.get(breed_name, {}).get('noise_level', 'moderate').lower()
        if 'high' in breed_noise:
            base_penalty = -0.18  # 減少基礎懲罰
            # 訓練性良好的高噪音品種補償
            trainable_vocal_breeds = ['German_Shepherd', 'Golden_Retriever']
            if any(train_breed in breed_name for train_breed in trainable_vocal_breeds):
                breed_adaptability_bonus += 0.05
            penalty += base_penalty * penalty_decay_factor
        elif 'moderate' in breed_noise and 'low' not in breed_noise:
            penalty += -0.05 * penalty_decay_factor

    # 體型偏好不匹配 - 漸進式懲罰
    if any(phrase in user_desc for phrase in ['small', 'tiny', 'little']):
        if 'giant' in breed_size:
            penalty -= 0.35  # 超大型犬懲罰
        elif 'large' in breed_size:
            penalty -= 0.20  # 大型犬懲罰

    # 中等活動量用戶的特殊約束處理 - 漸進式懲罰
    moderate_activity_terms = ['30 minutes', 'half hour', 'moderate', 'balanced', 'medium-sized house']
    if any(term in user_desc for term in moderate_activity_terms):
        # 超大型犬對中等活動量用戶的適度懲罰
        giant_breeds = ['Saint Bernard', 'Tibetan Mastiff', 'Great Dane', 'Mastiff', 'Newfoundland']
        if any(giant in breed_name for giant in giant_breeds) or 'giant' in breed_size:
            penalty -= 0.35  # 適度懲罰,不完全排除

        # 中型房屋 + 超大型犬的額外考量
        if 'medium-sized house' in user_desc and any(giant in breed_name for giant in giant_breeds):
            if not any(high_activity in user_desc for high_activity in ['hiking', 'running', 'active', 'outdoor activities']):
                penalty -= 0.15  # 輕度額外懲罰

    # 30分鐘散步對極高運動需求品種的懲罰
    if any(term in user_desc for term in ['30 minutes', 'half hour']) and 'walk' in user_desc:
        high_energy_breeds = ['Siberian Husky', 'Border Collie', 'Jack Russell Terrier', 'Weimaraner']
        if any(he_breed in breed_name for he_breed in high_energy_breeds) and 'high' in breed_exercise:
            penalty -= 0.25  # 適度懲罰極高運動需求品種

    # 添加特殊品種適應性補償機制
    # 對於邊界適配品種,給予適度補償
    boundary_adaptable_breeds = {
        'Italian_Greyhound': 0.08,  # 安靜、低維護的小型犬
        'Boston_Bull': 0.06,        # 適應性強的小型犬
        'Havanese': 0.05,           # 友好適應的小型犬
        'Silky_terrier': 0.04,      # 安靜的玩具犬
        'Basset': 0.07              # 低能量但友好的中型犬
    }

    if breed_name in boundary_adaptable_breeds:
        breed_adaptability_bonus += boundary_adaptable_breeds[breed_name]

    # 應用品種適應性補償並設置懲罰上限
    final_penalty = penalty + breed_adaptability_bonus
    # 限制最大懲罰,避免單一約束主導評分
    final_penalty = max(-0.4, final_penalty)

    return final_penalty

def _get_basic_text_matching_recommendations(user_description: str, top_k: int = 15) -> List[Dict[str, Any]]:
    """基本文字匹配推薦(SBERT 不可用時的後備方案)"""
    try:
        print("Using basic text matching as fallback...")

        # 基本關鍵字匹配
        keywords = user_description.lower().split()
        breed_scores = []

        # 從數據庫獲取品種清單
        try:
            conn = sqlite3.connect('animal_detector.db')
            cursor = conn.cursor()
            cursor.execute("SELECT DISTINCT Breed FROM AnimalCatalog LIMIT 50")
            basic_breeds = [row[0] for row in cursor.fetchall()]
            cursor.close()
            conn.close()
        except Exception as e:
            print(f"Could not load breed list from database: {str(e)}")
            # 後備品種清單
            basic_breeds = [
                'Labrador_Retriever', 'Golden_Retriever', 'German_Shepherd', 'French_Bulldog',
                'Border_Collie', 'Poodle', 'Beagle', 'Rottweiler', 'Yorkshire_Terrier',
                'Dachshund', 'Boxer', 'Siberian_Husky', 'Great_Dane', 'Pomeranian', 'Shih-Tzu',
                'Maltese_Dog', 'Chihuahua', 'Cavalier_King_Charles_Spaniel', 'Boston_Terrier',
                'Japanese_Spaniel', 'Toy_Terrier', 'Affenpinscher', 'Pekingese', 'Lhasa'
            ]

        for breed in basic_breeds:
            breed_info = get_dog_description(breed) or {}
            breed_text = f"{breed} {breed_info.get('Temperament', '')} {breed_info.get('Size', '')} {breed_info.get('Description', '')}".lower()

            # 計算關鍵字匹配分數
            matches = sum(1 for keyword in keywords if keyword in breed_text)
            base_score = min(0.95, 0.3 + (matches / len(keywords)) * 0.6)

            # 應用增強匹配邏輯
            enhanced_score = _calculate_enhanced_matching_score(
                breed, breed_info, user_description, base_score
            )

            breed_scores.append((breed, enhanced_score['final_score'], breed_info, enhanced_score))

        # 按分數排序
        breed_scores.sort(key=lambda x: x[1], reverse=True)

        recommendations = []
        for i, (breed, final_score, breed_info, enhanced_score) in enumerate(breed_scores[:top_k]):
            recommendation = {
                'breed': breed.replace('_', ' '),
                'rank': i + 1,
                'overall_score': final_score,
                'final_score': final_score,
                'semantic_score': enhanced_score.get('weighted_score', final_score),
                'comparative_bonus': enhanced_score.get('lifestyle_bonus', 0.0),
                'lifestyle_bonus': enhanced_score.get('lifestyle_bonus', 0.0),
                'size': breed_info.get('Size', 'Unknown'),
                'temperament': breed_info.get('Temperament', 'Unknown'),
                'exercise_needs': breed_info.get('Exercise Needs', 'Moderate'),
                'grooming_needs': breed_info.get('Grooming Needs', 'Moderate'),
                'good_with_children': breed_info.get('Good with Children', 'Unknown'),
                'lifespan': breed_info.get('Lifespan', '10-12 years'),
                'description': breed_info.get('Description', 'No description available'),
                'search_type': 'description',
                'scores': enhanced_score.get('dimension_scores', {
                    'space': final_score * 0.9,
                    'exercise': final_score * 0.85,
                    'grooming': final_score * 0.8,
                    'experience': final_score * 0.75,
                    'noise': final_score * 0.7,
                    'family': final_score * 0.65
                })
            }
            recommendations.append(recommendation)

        return recommendations

    except Exception as e:
        error_msg = f"Error in basic text matching: {str(e)}"
        print(f"ERROR: {error_msg}")
        raise RuntimeError(error_msg) from e