anno-eval 0.8.0

Evaluation harnesses, datasets, and muxer-backed sampling for anno
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
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
8036
8037
8038
8039
8040
8041
8042
8043
8044
8045
8046
8047
8048
8049
8050
8051
8052
8053
8054
8055
8056
8057
8058
8059
8060
8061
8062
8063
8064
8065
8066
8067
8068
8069
8070
8071
8072
8073
8074
8075
8076
8077
8078
8079
8080
8081
8082
8083
8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
8094
8095
8096
8097
8098
8099
8100
8101
8102
8103
8104
8105
8106
8107
8108
8109
8110
8111
8112
8113
8114
8115
8116
8117
8118
8119
8120
8121
8122
8123
8124
8125
8126
8127
8128
8129
8130
8131
8132
8133
8134
8135
8136
8137
8138
8139
8140
8141
8142
8143
8144
8145
8146
8147
8148
8149
8150
8151
8152
8153
8154
8155
8156
8157
8158
8159
8160
8161
8162
8163
8164
8165
8166
8167
8168
8169
8170
8171
8172
8173
8174
8175
8176
8177
8178
8179
8180
8181
8182
8183
8184
8185
8186
8187
8188
8189
8190
8191
8192
8193
8194
8195
8196
8197
8198
8199
8200
8201
8202
8203
8204
8205
8206
8207
8208
8209
8210
8211
8212
8213
8214
8215
8216
8217
8218
8219
8220
8221
8222
8223
8224
8225
8226
8227
8228
8229
8230
8231
8232
8233
8234
8235
8236
8237
8238
8239
8240
8241
8242
8243
8244
8245
8246
8247
8248
8249
8250
8251
8252
8253
8254
8255
8256
8257
8258
8259
8260
8261
8262
8263
8264
8265
8266
8267
8268
8269
8270
8271
8272
8273
8274
8275
8276
8277
8278
8279
8280
8281
8282
8283
8284
8285
8286
8287
8288
8289
8290
8291
8292
8293
8294
8295
8296
8297
8298
8299
8300
8301
8302
8303
8304
8305
8306
8307
8308
8309
8310
8311
8312
8313
8314
8315
8316
8317
8318
8319
8320
8321
8322
8323
8324
8325
8326
8327
8328
8329
8330
8331
8332
8333
8334
8335
8336
8337
8338
8339
8340
8341
8342
8343
8344
8345
8346
8347
8348
8349
8350
8351
8352
8353
8354
8355
8356
8357
8358
8359
8360
8361
8362
8363
8364
8365
8366
8367
8368
8369
8370
8371
8372
8373
8374
8375
8376
8377
8378
8379
8380
8381
8382
8383
8384
8385
8386
8387
8388
8389
8390
8391
8392
8393
8394
8395
8396
8397
8398
8399
8400
8401
8402
8403
8404
8405
8406
8407
8408
8409
8410
8411
8412
8413
8414
8415
8416
8417
8418
8419
8420
8421
8422
8423
8424
8425
8426
8427
8428
8429
8430
8431
8432
8433
8434
8435
8436
8437
8438
8439
8440
8441
8442
8443
8444
8445
8446
8447
8448
8449
8450
8451
8452
8453
8454
8455
8456
8457
8458
8459
8460
8461
8462
8463
8464
8465
8466
8467
8468
8469
8470
8471
8472
8473
8474
8475
8476
8477
8478
8479
8480
8481
8482
8483
8484
8485
8486
8487
8488
8489
8490
8491
8492
8493
8494
8495
8496
8497
8498
8499
8500
8501
8502
8503
8504
8505
8506
8507
8508
8509
8510
8511
8512
8513
8514
8515
8516
8517
8518
8519
8520
8521
8522
8523
8524
8525
8526
8527
8528
8529
8530
8531
8532
8533
8534
8535
8536
8537
8538
8539
8540
8541
8542
8543
8544
8545
8546
8547
8548
8549
8550
8551
8552
8553
8554
8555
8556
8557
8558
8559
8560
8561
8562
8563
8564
8565
8566
8567
8568
8569
8570
8571
8572
8573
8574
8575
8576
8577
8578
8579
8580
8581
8582
8583
8584
8585
8586
8587
8588
8589
8590
8591
8592
8593
8594
8595
8596
8597
8598
8599
8600
8601
8602
8603
8604
8605
8606
8607
8608
8609
8610
8611
8612
8613
8614
8615
8616
8617
8618
8619
8620
8621
8622
8623
8624
8625
8626
8627
8628
8629
8630
8631
8632
8633
8634
8635
8636
8637
8638
8639
8640
8641
8642
8643
8644
8645
8646
8647
8648
8649
8650
8651
8652
8653
8654
8655
8656
8657
8658
8659
8660
8661
8662
8663
8664
8665
8666
8667
8668
8669
8670
8671
8672
8673
8674
8675
8676
8677
8678
8679
8680
8681
8682
8683
8684
8685
8686
8687
8688
8689
8690
8691
8692
8693
8694
8695
8696
8697
8698
8699
8700
8701
8702
8703
8704
8705
8706
8707
8708
8709
8710
8711
8712
8713
8714
8715
8716
8717
8718
8719
8720
8721
8722
8723
8724
8725
8726
8727
8728
8729
8730
8731
8732
8733
8734
8735
8736
8737
8738
8739
8740
8741
8742
8743
8744
8745
8746
8747
8748
8749
8750
8751
8752
8753
8754
8755
8756
8757
8758
8759
8760
8761
8762
8763
8764
8765
8766
8767
8768
8769
8770
8771
8772
8773
8774
8775
8776
8777
8778
8779
8780
8781
8782
8783
8784
8785
8786
8787
8788
8789
8790
8791
8792
8793
8794
8795
8796
8797
8798
8799
8800
8801
8802
8803
8804
8805
8806
8807
8808
8809
8810
8811
8812
8813
8814
8815
8816
8817
8818
8819
8820
8821
8822
8823
8824
8825
8826
8827
8828
8829
8830
8831
8832
8833
8834
8835
8836
8837
8838
8839
8840
8841
8842
8843
8844
8845
8846
8847
8848
8849
8850
8851
8852
8853
8854
8855
8856
8857
8858
8859
8860
8861
8862
8863
8864
8865
8866
8867
8868
8869
8870
8871
8872
8873
8874
8875
8876
8877
8878
8879
8880
8881
8882
8883
8884
8885
8886
8887
8888
8889
8890
8891
8892
8893
8894
8895
8896
8897
8898
8899
8900
8901
8902
8903
8904
8905
8906
8907
8908
8909
8910
8911
8912
8913
8914
8915
8916
8917
8918
8919
8920
8921
8922
8923
8924
8925
8926
8927
8928
8929
8930
8931
8932
8933
8934
8935
8936
8937
8938
8939
8940
8941
8942
8943
8944
8945
8946
8947
8948
8949
8950
8951
8952
8953
8954
8955
8956
8957
8958
8959
8960
8961
8962
8963
8964
8965
8966
8967
8968
8969
8970
8971
8972
8973
8974
8975
8976
8977
8978
8979
8980
8981
8982
8983
8984
8985
8986
8987
8988
8989
8990
8991
8992
8993
8994
8995
8996
8997
8998
8999
9000
9001
9002
9003
9004
9005
9006
9007
9008
9009
9010
9011
9012
9013
9014
9015
9016
9017
9018
9019
9020
9021
9022
9023
9024
9025
9026
9027
9028
9029
9030
9031
9032
9033
9034
9035
9036
9037
9038
9039
9040
9041
9042
9043
9044
9045
9046
9047
9048
9049
9050
9051
9052
9053
9054
9055
9056
9057
9058
9059
9060
9061
9062
9063
9064
9065
9066
9067
9068
9069
9070
9071
9072
9073
9074
9075
9076
9077
9078
9079
9080
9081
9082
9083
9084
9085
9086
9087
9088
9089
9090
9091
9092
9093
9094
9095
9096
9097
9098
9099
9100
9101
9102
9103
9104
9105
9106
9107
9108
9109
9110
9111
9112
9113
9114
9115
9116
9117
9118
9119
9120
9121
9122
9123
9124
9125
9126
9127
9128
9129
9130
9131
9132
9133
9134
9135
9136
9137
9138
9139
9140
9141
9142
9143
9144
9145
9146
9147
9148
9149
9150
9151
9152
9153
9154
9155
9156
9157
9158
9159
9160
9161
9162
9163
9164
9165
9166
9167
9168
9169
9170
9171
9172
9173
9174
9175
9176
9177
9178
9179
9180
9181
9182
9183
9184
9185
9186
9187
9188
9189
9190
9191
9192
9193
9194
9195
9196
9197
9198
9199
9200
9201
9202
9203
9204
9205
9206
9207
9208
9209
9210
9211
9212
9213
9214
9215
9216
9217
9218
9219
9220
9221
9222
9223
9224
9225
9226
9227
9228
9229
9230
9231
9232
9233
9234
9235
9236
9237
9238
9239
9240
9241
9242
9243
9244
9245
9246
9247
9248
9249
9250
9251
9252
9253
9254
9255
9256
9257
9258
9259
9260
9261
9262
9263
9264
9265
9266
9267
9268
9269
9270
9271
9272
9273
9274
9275
9276
9277
9278
9279
9280
9281
9282
9283
9284
9285
9286
9287
9288
9289
9290
9291
9292
9293
9294
9295
9296
9297
9298
9299
9300
9301
9302
9303
9304
9305
9306
9307
9308
9309
9310
9311
9312
9313
9314
9315
9316
9317
9318
9319
9320
9321
9322
9323
9324
9325
9326
9327
9328
9329
9330
9331
9332
9333
9334
9335
9336
9337
9338
9339
9340
9341
9342
9343
9344
9345
9346
9347
9348
9349
9350
9351
9352
9353
9354
9355
9356
9357
9358
9359
9360
9361
9362
9363
9364
9365
9366
9367
9368
9369
9370
9371
9372
9373
9374
9375
9376
9377
9378
9379
9380
9381
9382
9383
9384
9385
9386
9387
9388
9389
9390
9391
9392
9393
9394
9395
9396
9397
9398
9399
9400
9401
9402
9403
9404
9405
9406
9407
9408
9409
9410
9411
9412
9413
9414
9415
9416
9417
9418
9419
9420
9421
9422
9423
9424
9425
9426
9427
9428
9429
9430
9431
9432
9433
9434
9435
9436
9437
9438
9439
9440
9441
9442
9443
9444
9445
9446
9447
9448
9449
9450
9451
9452
9453
9454
9455
9456
9457
9458
9459
9460
9461
9462
9463
9464
9465
9466
9467
9468
9469
9470
9471
9472
9473
9474
9475
9476
9477
9478
9479
9480
9481
9482
9483
9484
9485
9486
9487
9488
9489
9490
9491
9492
9493
9494
9495
9496
9497
9498
9499
9500
9501
9502
9503
9504
9505
9506
9507
9508
9509
9510
9511
9512
9513
9514
9515
9516
9517
9518
9519
9520
9521
9522
9523
9524
9525
9526
9527
9528
9529
9530
9531
9532
9533
9534
9535
9536
9537
9538
9539
9540
9541
9542
9543
9544
9545
9546
9547
9548
9549
9550
9551
9552
9553
9554
9555
9556
9557
9558
9559
9560
9561
9562
9563
9564
9565
9566
9567
9568
9569
9570
9571
9572
9573
9574
9575
9576
9577
9578
9579
9580
9581
9582
9583
9584
9585
9586
9587
9588
9589
9590
9591
9592
9593
9594
9595
9596
9597
9598
9599
9600
9601
9602
9603
9604
9605
9606
9607
9608
9609
9610
9611
9612
9613
9614
9615
9616
9617
9618
9619
9620
9621
9622
9623
9624
9625
9626
9627
9628
9629
9630
9631
9632
9633
9634
9635
9636
9637
9638
9639
9640
9641
9642
9643
9644
9645
9646
9647
9648
9649
9650
9651
9652
9653
9654
9655
9656
9657
9658
9659
9660
9661
9662
9663
9664
9665
9666
9667
9668
9669
9670
9671
9672
9673
9674
9675
9676
9677
9678
9679
9680
9681
9682
9683
9684
9685
9686
9687
9688
9689
9690
9691
9692
9693
9694
9695
9696
9697
9698
9699
9700
9701
9702
9703
9704
9705
9706
9707
9708
9709
9710
9711
9712
9713
9714
9715
9716
9717
9718
9719
9720
9721
9722
9723
9724
9725
9726
9727
9728
9729
9730
9731
9732
9733
9734
9735
9736
9737
9738
9739
9740
9741
9742
9743
9744
9745
9746
9747
9748
9749
9750
9751
9752
9753
9754
9755
9756
9757
9758
9759
9760
9761
9762
9763
9764
9765
9766
9767
9768
9769
9770
9771
9772
9773
9774
9775
9776
9777
9778
9779
9780
9781
9782
9783
9784
9785
9786
9787
9788
9789
9790
9791
9792
9793
9794
9795
9796
9797
9798
9799
9800
9801
9802
9803
9804
9805
9806
9807
9808
9809
9810
9811
9812
9813
9814
9815
9816
9817
9818
9819
9820
9821
9822
9823
9824
9825
9826
9827
9828
9829
9830
9831
9832
9833
9834
9835
9836
9837
9838
9839
9840
9841
9842
9843
9844
9845
9846
9847
9848
9849
9850
9851
9852
9853
9854
9855
9856
9857
9858
9859
9860
9861
9862
9863
9864
9865
//! Dataset downloading and caching for NER evaluation.
//!
//! Downloads, caches, and parses real NER datasets from public sources.
//! Follows burntsushi's philosophy: real-world data, not toy examples.
//!
//! # Quick Start
//!
//! ```rust,ignore
//! use anno::eval::{DatasetLoader, LoadableDatasetId};
//!
//! let loader = DatasetLoader::new()?;
//! let dataset = loader.load(LoadableDatasetId::WikiGold)?;
//! println!("Loaded {} sentences", dataset.len());
//! ```
//!
//! # Dataset IDs: catalog vs loadable
//!
//! The full dataset catalog lives in [`dataset_registry::DatasetId`]. Not every dataset
//! in the catalog can be downloaded/parsed by this crate (some require licenses or have
//! unimplemented formats).
//!
//! This module provides [`LoadableDatasetId`], a wrapper that guarantees a dataset has
//! a loading implementation. Use it for `DatasetLoader::{load, load_or_download}`.
//!
//! # Supported Datasets
//!
//! | Dataset | Source | License | Entities |
//! |---------|--------|---------|----------|
//! | WikiGold | Wikipedia | CC-BY | PER, LOC, ORG, MISC |
//! | WNUT-17 | Social Media | Open | person, location, corporation, etc. |
//! | MIT Movie | MIT | Research | actor, director, genre, title, etc. |
//! | MIT Restaurant | MIT | Research | amenity, cuisine, dish, etc. |
//! | CoNLL-2003 Sample | Public | Research | PER, LOC, ORG, MISC |
//! | OntoNotes Sample | Public | Research | 18 entity types |
//! | BC5CDR | PubMed | Research | Disease, Chemical |
//! | NCBI Disease | PubMed | Research | Disease |
//!
//! # Design Philosophy
//!
//! - **Lazy downloading**: Only fetch what's needed
//! - **Persistent caching**: Never re-download unchanged data
//! - **Integrity verification**: SHA256 checksums for all downloads
//! - **Graceful degradation**: Work offline with cached data
//! - **Clear errors**: Explain exactly what went wrong
//!
//! # Extended Example
//!
//! ```rust,ignore
//! use anno::eval::{DatasetLoader, LoadableDatasetId};
//!
//! let loader = DatasetLoader::new()?;
//!
//! // Check cache status before loading
//! if loader.is_cached(LoadableDatasetId::WikiGold) {
//!     println!("WikiGold is cached, will load from disk");
//! }
//!
//! // Load dataset (downloads if not cached, verifies checksum)
//! let dataset = loader.load(LoadableDatasetId::WikiGold)?;
//! println!("Loaded {} sentences with {} entities",
//!     dataset.len(), dataset.entity_count());
//!
//! // Iterate over examples
//! for example in dataset.iter() {
//!     println!("Text: {}", example.text);
//!     for entity in &example.entities {
//!         println!("  {} [{}]", entity.text, entity.entity_type);
//!     }
//! }
//! ```
//!
//! [`dataset_registry::DatasetId`]: super::dataset_registry::DatasetId

use anno::{Error, Result};
#[cfg(test)]
use anno_core::EntityType;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fs;
use std::path::PathBuf;

// =============================================================================
// Dataset Identification
// =============================================================================

/// Dataset identifier (full catalog).
///
/// This is the single source of truth for dataset metadata. Not all datasets are
/// loadable by `DatasetLoader`. Use [`LoadableDatasetId`] when you want that guarantee.
///
/// # Usage
///
/// ```rust,ignore
/// use anno::eval::{DatasetLoader, LoadableDatasetId};
///
/// let loader = DatasetLoader::new()?;
/// let dataset = loader.load(LoadableDatasetId::WikiGold)?;
/// ```
///
/// # Available Datasets
///
/// ## NER Datasets
///
/// | Dataset | Size | Domain | Entity Types |
/// |---------|------|--------|--------------|
/// | `WikiGold` | ~3.5k entities | Wikipedia | PER, LOC, ORG, MISC |
/// | `Wnut17` | ~2k entities | Social media | person, location, etc. |
/// | `MitMovie` | ~10k entities | Movies | actor, director, genre |
/// | `MitRestaurant` | ~8k entities | Restaurants | cuisine, dish, etc. |
/// | `CoNLL2003Sample` | ~20k entities | News | PER, LOC, ORG, MISC |
/// | `OntoNotesSample` | ~18k entities | Mixed | 18 types |
/// | `BC5CDR` | ~28k entities | Biomedical | Disease, Chemical |
/// | `NCBIDisease` | ~6k entities | Biomedical | Disease |
///
/// ## Coreference Datasets
///
/// | Dataset | Size | Domain | Features |
/// |---------|------|--------|----------|
/// | `GAP` | 8,908 pairs | Wikipedia | Gender-balanced pronouns |
/// | `PreCo` | 38k docs | Reading | Includes singletons |
/// | `LitBank` | 100 works | Literature | Literary coreference |
///
/// # Extending
///
/// To add a new loadable dataset:
/// 1. Add the variant here
/// 2. Implement loading in `DatasetLoader::load()`
/// 3. Add to `DatasetId::all()` iterator
/// 4. Ensure it exists in `dataset_registry::DatasetId` (metadata catalog)
///
pub use super::dataset_registry::DatasetId;

/// Dataset identifier guaranteed to be loadable by [`DatasetLoader`].
///
/// This is a thin wrapper over [`DatasetId`] that enforces loader support via
/// `TryFrom<DatasetId>` / `FromStr`.
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub struct LoadableDatasetId(DatasetId);

#[derive(Debug, Copy, Clone, PartialEq, Eq)]
enum DatasetParsePlan {
    Conll,
    JsonlNer,
    WikiannJson,
    TweetNer7,
    DocredJson,
    GoogleReCorpus,
    ChisiecJson,
    CadecHybrid,
    Bc5cdr,
    NcbiDisease,
    GapTsv,
    PrecoJsonl,
    Litbank,
    EcbPlus,
    AfriSenti,
    AfriQa,
    MasakhaNews,
    Conllu,
    AgNews,
    Dbpedia14,
    YahooAnswers,
    Trec,
    TweetTopic,
    Maven,
    MavenArg,
    Casie,
    Rams,
    HfApiResponse,
    /// TSV-based NER format (HIPE-2022 style)
    TsvNer,
    /// CSV-based NER format (E-NER/EDGAR-NER style: Token,Tag)
    CsvNer,
}

impl LoadableDatasetId {
    /// All datasets that have loading implementations.
    #[must_use]
    pub fn all() -> Vec<Self> {
        DatasetId::all()
            .iter()
            .copied()
            .filter(|id| Self::is_loadable_dataset(*id))
            .map(Self)
            .collect()
    }

    /// Access the underlying catalog ID.
    #[must_use]
    pub fn into_inner(self) -> DatasetId {
        self.0
    }

    #[must_use]
    fn is_loadable_dataset(id: DatasetId) -> bool {
        // Loader truth: some catalog entries are intentionally non-loadable (blocked, deprecated,
        // or require manual access) even if they have a parse plan.
        //
        // Keep this list small and motivated. Prefer expressing “non-loadable” in the registry
        // metadata first, then enforcing it here so CLI counts and `LoadableDatasetId` remain
        // honest.
        if matches!(
            id,
            DatasetId::CoNLL2002 | DatasetId::CoNLL2002Spanish | DatasetId::CoNLL2002Dutch
        ) {
            return false;
        }

        Self::parse_plan(id).is_some()
    }

    #[must_use]
    fn parse_plan(id: DatasetId) -> Option<DatasetParsePlan> {
        if let Some(hint) = Self::registry_hint_plan(id) {
            return Some(hint);
        }

        Some(match id {
            // ===================================================================
            // CoNLL/BIO format datasets
            // ===================================================================
            // These datasets use standard CoNLL-2003 or BIO tagging format.
            // The CoNLL parser handles: space/tab-separated columns, IOB2/BIO schemes,
            // and standard entity type mappings (PER, ORG, LOC, etc.).
            //
            // Note: Some datasets listed here are also handled by registry_hint_plan()
            // but are kept for explicit control or historical reasons.
            //
            // To add more: If a dataset has format="CoNLL" or format="BIO" in the registry
            // and is NER-only, it should be auto-detected. Add here only if auto-detection
            // fails (e.g., multi-task datasets, ambiguous metadata).
            DatasetId::WikiGold
            | DatasetId::Wnut17
            | DatasetId::MitMovie
            | DatasetId::MitRestaurant
            | DatasetId::CoNLL2003Sample
            | DatasetId::OntoNotesSample
            | DatasetId::UniversalNERBench
            | DatasetId::LegNER
            | DatasetId::TwiConv
            | DatasetId::AIDA
            | DatasetId::TACKBP
            | DatasetId::BroadTwitterCorpus
            | DatasetId::BioMNER
            | DatasetId::MasakhaNER
            | DatasetId::MasakhaNER2
            | DatasetId::OntoNotes50
            | DatasetId::GermEval2014
            | DatasetId::HAREM
            | DatasetId::SemEval2013Task91
            | DatasetId::MUC6
            | DatasetId::MUC7
            | DatasetId::JNLPBA
            | DatasetId::BC2GMFull
            | DatasetId::CRAFT
            | DatasetId::FinNER
            | DatasetId::LegalNER
            | DatasetId::AGRONER
            | DatasetId::ATISFlightBooking
            | DatasetId::AerospaceNERDataset
            | DatasetId::AstrologyNER
            | DatasetId::AutomotiveNER
            | DatasetId::AviationProductsNER
            | DatasetId::BeekeepingNER
            | DatasetId::BrewingNER
            | DatasetId::ChineseEngineeringGeologyNER
            | DatasetId::CocktailNER
            | DatasetId::ConstructionNER
            | DatasetId::CropDiseaseNER
            | DatasetId::CryptoNER
            | DatasetId::CyNERAptner
            | DatasetId::DnDNERBenchmark
            | DatasetId::EFGC
            | DatasetId::EnergyNER
            | DatasetId::EquestrianNER
            | DatasetId::EsportsNER
            | DatasetId::FINERFood
            | DatasetId::FitnessNER
            | DatasetId::FourRegionsGeologyNER
            | DatasetId::GNERGeoscience
            | DatasetId::GardeningNER
            | DatasetId::HealthcareAdminNER
            | DatasetId::HindiEnglishSocialMediaNER
            | DatasetId::JobPostingNER
            | DatasetId::LogisticsNER
            | DatasetId::ArabicEventCoref
            | DatasetId::FrenchFullLengthFictionCoref
            | DatasetId::LongtoNotes
            | DatasetId::ManufacturingNER
            | DatasetId::MaritimeNER
            | DatasetId::MovieCoref
            | DatasetId::MusicNER
            | DatasetId::NDNER
            | DatasetId::OrigamiNER
            | DatasetId::PaleontologyNER
            | DatasetId::PharmaNER
            | DatasetId::PhotographyNER
            | DatasetId::RealEstateNER
            | DatasetId::RitterTwitterNER
            | DatasetId::SECFilingsNER
            | DatasetId::SanskritNERBhagavadGita
            | DatasetId::ScubaNER
            | DatasetId::SportsNERGeneral
            | DatasetId::TVShowMultilingualCoref
            | DatasetId::TarotNER
            | DatasetId::TelecomNER
            | DatasetId::TelenovelaNER
            | DatasetId::TourismNER
            | DatasetId::VeterinaryNER
            | DatasetId::WaterResourceNER
            | DatasetId::WeatherNER
            | DatasetId::WineNER
            | DatasetId::WoodworkingNER
            // Added 2025-12: More CoNLL-format datasets with direct download URLs
            | DatasetId::QxoRef
            | DatasetId::GICoref
            | DatasetId::WNUT16
            | DatasetId::NoiseBench
            | DatasetId::CrossWeigh
            | DatasetId::ZELDA
            | DatasetId::GENIANested => DatasetParsePlan::Conll,

            // ===================================================================
            // JSONL format (HuggingFace style)
            // ===================================================================
            // These datasets use JSON Lines format with standard fields:
            // - `tokens`: Array of token strings
            // - `ner_tags`: Array of BIO/IOB2 tags
            // - Optional: `text` (original sentence), `entities` (structured)
            //
            // The JsonlNer parser is flexible and handles variations in field names.
            DatasetId::MultiNERD
            | DatasetId::ACORD
            | DatasetId::ARFFiction
            | DatasetId::AgMNER
            | DatasetId::AgriNER
            | DatasetId::AnimeMangaNER
            | DatasetId::AntiquesNER
            | DatasetId::AstronomicalTelegramKEE
            | DatasetId::BirdwatchingNER
            | DatasetId::BoardGameNER
            | DatasetId::BookCorefBamman
            | DatasetId::CUAD
            | DatasetId::ChessNER
            | DatasetId::CoMTA
            | DatasetId::DeepFashion2
            | DatasetId::FIREBALL
            | DatasetId::FashionIQ
            | DatasetId::FragranceNER
            | DatasetId::IMDbSemiStructuredRE
            | DatasetId::InsuranceNER
            | DatasetId::KnittingNER
            | DatasetId::LLMRocMinNER
            | DatasetId::MOFDataset
            | DatasetId::MathDial
            | DatasetId::NHKRecipeDataset
            | DatasetId::NaturalProductsRE
            | DatasetId::NumismaticsNER
            | DatasetId::PhilatelyNER
            | DatasetId::PolyIE
            | DatasetId::RecipeDBAnnotated
            | DatasetId::ResumeNER
            | DatasetId::RetailInventoryNER
            | DatasetId::SPoRC
            | DatasetId::Saraga
            | DatasetId::SolidStateDoping
            | DatasetId::SpotifyPodcastsDataset
            | DatasetId::TattooNER
            | DatasetId::ThemeParkNER
            | DatasetId::VREN
            | DatasetId::VisDialCoref
            // Added 2025-12: More JSONL-format datasets with direct download URLs
            | DatasetId::REBEL
            | DatasetId::BBQ
            | DatasetId::RealToxicityPrompts
            | DatasetId::BookCoref
            | DatasetId::BookCorefSplit
            | DatasetId::WIESP2022NER
            | DatasetId::FewRel
            | DatasetId::PIIMasking200k
            | DatasetId::B2NERD
            | DatasetId::OpenNER
            | DatasetId::FictionNER750M => DatasetParsePlan::JsonlNer,

            // WikiANN-ish JSON format (from download_hf_datasets.py)
            DatasetId::UNER | DatasetId::MSNER => DatasetParsePlan::WikiannJson,

            // TweetNER7 uses JSON format with integer tags
            DatasetId::TweetNER7 => DatasetParsePlan::TweetNer7,

            // ===================================================================
            // JSON format (Relation extraction datasets)
            // ===================================================================
            // Relation extraction datasets use JSON format with entity pairs and relations.
            // Common structure: {entities: [...], relations: [...], text: "..."}
            //
            // The DocredJson parser handles DocRED, ReTACRED, and similar formats.
            // Some datasets (SciERC, PolyIE, EnzChemRED) are auto-detected via
            // registry_hint_plan() but listed here for explicit control.
            //
            // To add more: RE datasets with format="JSON" or format="JSONL" and is_re=true
            // should be auto-detected. Add here only for special cases.
            DatasetId::DocRED
            | DatasetId::ReTACRED
            | DatasetId::NYTFB
            | DatasetId::WEBNLG
            | DatasetId::GoogleRE
            | DatasetId::BioRED
            | DatasetId::SciER
            | DatasetId::MixRED
            | DatasetId::CovEReD
            | DatasetId::ACE2005
            | DatasetId::MuDoCo
            | DatasetId::SciERCNER
            // Added 2025-01-27: RE datasets with public URLs but missing hints
            // Note: SciERC, PolyIE, EnzChemRED are now handled by registry_hint_plan() auto-detection
            => DatasetParsePlan::DocredJson,

            // CHisIEC: Ancient Chinese historical NER+RE (custom JSON format)
            DatasetId::CHisIEC => DatasetParsePlan::ChisiecJson,

            // Discontinuous NER (HF datasets-server API or JSONL format)
            DatasetId::CADEC | DatasetId::ShARe13 | DatasetId::ShARe14 => {
                DatasetParsePlan::CadecHybrid
            }

            // Biomedical formats
            DatasetId::BC5CDR => DatasetParsePlan::Bc5cdr,
            DatasetId::NCBIDisease => DatasetParsePlan::NcbiDisease,

            // Coreference formats
            // Note: GUM uses CoNLL format and is handled by registry_hint_plan()
            DatasetId::GAP | DatasetId::WikiCoref => DatasetParsePlan::GapTsv,
            DatasetId::WinoBias => DatasetParsePlan::GapTsv,
            DatasetId::PreCo | DatasetId::SciCo => DatasetParsePlan::PrecoJsonl,
            DatasetId::LitBank => DatasetParsePlan::Litbank,
            DatasetId::ECBPlus => DatasetParsePlan::EcbPlus,

            // African language datasets
            DatasetId::AfriSenti => DatasetParsePlan::AfriSenti,
            DatasetId::AfriQA => DatasetParsePlan::AfriQa,
            DatasetId::MasakhaNEWS => DatasetParsePlan::MasakhaNews,
            DatasetId::MasakhaPOS => DatasetParsePlan::Conllu,

            // Text classification datasets
            DatasetId::AGNews => DatasetParsePlan::AgNews,
            DatasetId::DBPedia14 => DatasetParsePlan::Dbpedia14,
            DatasetId::YahooAnswers => DatasetParsePlan::YahooAnswers,
            DatasetId::TREC => DatasetParsePlan::Trec,
            DatasetId::TweetTopic => DatasetParsePlan::TweetTopic,

            // Event extraction datasets
            DatasetId::MAVEN => DatasetParsePlan::Maven,
            DatasetId::MAVENArg => DatasetParsePlan::MavenArg,
            DatasetId::CASIE => DatasetParsePlan::Casie,
            DatasetId::RAMS => DatasetParsePlan::Rams,

            // HF datasets-server API responses
            DatasetId::GENIA
            | DatasetId::AnatEM
            | DatasetId::BC2GM
            | DatasetId::BC4CHEMD
            | DatasetId::FewNERD
            | DatasetId::CrossNER
            | DatasetId::FabNER
            | DatasetId::WikiNeural
            | DatasetId::WikiANN
            | DatasetId::MultiCoNER
            | DatasetId::MultiCoNERv2
            | DatasetId::PolyglotNER
            | DatasetId::UniversalNER => DatasetParsePlan::HfApiResponse,

            _ => return None,
        })
    }

    /// Best-effort "hint" derived from registry metadata.
    ///
    /// This is intentionally conservative: it returns `None` if the registry
    /// doesn't provide enough signal to choose the same plan as `parse_plan`.
    ///
    /// The long-term goal is to have this cover most datasets so `parse_plan`
    /// shrinks to a small exception table.
    ///
    /// # Architecture Notes
    ///
    /// This function enables automatic format detection for datasets with:
    /// - Clear format metadata (`format:` field in registry)
    /// - Single primary task (NER, RE, coref, etc.)
    /// - Standard annotation schemes (BIO, IOB2, CoNLL, JSONL)
    ///
    /// Datasets with multiple tasks (e.g., NER+RE) or ambiguous formats
    /// should be added to explicit matches in `parse_plan()` instead.
    ///
    /// # Adding New Datasets
    ///
    /// 1. **Common formats (CoNLL, JSONL, TSV)**: Add format metadata to registry,
    ///    ensure single primary task, and this function will auto-detect.
    /// 2. **Special formats**: Add explicit match in `parse_plan()` match statement.
    /// 3. **Multi-task datasets**: Add explicit match (auto-detection is conservative).
    #[must_use]
    fn registry_hint_plan(id: DatasetId) -> Option<DatasetParsePlan> {
        // Hybrids / special cases first.
        if id == DatasetId::CHisIEC {
            // CHisIEC is multi-task and uses a custom JSON array format (not DocRED/CrossRE).
            // It MUST not be auto-detected as DocredJson.
            return Some(DatasetParsePlan::ChisiecJson);
        }
        if matches!(
            id,
            DatasetId::CADEC | DatasetId::ShARe13 | DatasetId::ShARe14
        ) {
            return Some(DatasetParsePlan::CadecHybrid);
        }
        if id == DatasetId::GoogleRE {
            // The Google relation-extraction-corpus files are not DocRED/CrossRE.
            // They are JSONL records with fields like {pred, sub, obj, evidences[], judgments[]}.
            return Some(DatasetParsePlan::GoogleReCorpus);
        }
        if id == DatasetId::TweetNER7 {
            return Some(DatasetParsePlan::TweetNer7);
        }

        // HuggingFace-hosted datasets with bespoke (non-generic) parse plans.
        //
        // These are marked `access_status: HuggingFace` in the registry, so they must be
        // hintable; but their parse plans are not derivable from `format` alone (or use
        // specialized parsing despite being hosted on the Hub).
        if id == DatasetId::BC5CDR {
            return Some(DatasetParsePlan::Bc5cdr);
        }
        if id == DatasetId::NCBIDisease {
            return Some(DatasetParsePlan::NcbiDisease);
        }
        if id == DatasetId::AfriSenti {
            return Some(DatasetParsePlan::AfriSenti);
        }
        if id == DatasetId::AfriQA {
            return Some(DatasetParsePlan::AfriQa);
        }
        if id == DatasetId::MasakhaNEWS {
            return Some(DatasetParsePlan::MasakhaNews);
        }
        if id == DatasetId::AGNews {
            return Some(DatasetParsePlan::AgNews);
        }
        if id == DatasetId::DBPedia14 {
            return Some(DatasetParsePlan::Dbpedia14);
        }
        if id == DatasetId::YahooAnswers {
            return Some(DatasetParsePlan::YahooAnswers);
        }
        if id == DatasetId::TREC {
            return Some(DatasetParsePlan::Trec);
        }
        if id == DatasetId::TweetTopic {
            return Some(DatasetParsePlan::TweetTopic);
        }

        // Coref datasets in CoNLL format (not GAP TSV): parse as regular CoNLL.
        // These have NER annotations alongside coref, so the CoNLL parser works.
        if matches!(
            id,
            DatasetId::QxoRef
                | DatasetId::GICoref
                | DatasetId::WNUT16
                | DatasetId::NoiseBench
                | DatasetId::CrossWeigh
                | DatasetId::ZELDA
                | DatasetId::GENIANested
                // Added 2025-12: More CoNLL NER datasets
                | DatasetId::HistNERo
                | DatasetId::DutchArchaeology
                | DatasetId::FINER
                | DatasetId::CALCS2018
                | DatasetId::MedievalCharterNER
                | DatasetId::RockNER
                | DatasetId::AIDACoNLL
                | DatasetId::NNE
                | DatasetId::GermEvalDiscontinuous
                | DatasetId::PubMedDiscontinuous
                | DatasetId::IndicNER
                | DatasetId::NorNE
                | DatasetId::CHEMDNER
                | DatasetId::GeoWebNews
                | DatasetId::TASTEset
                | DatasetId::RecipeNER
                | DatasetId::AstroNER
                | DatasetId::FinanceNER
                | DatasetId::TechNER
                | DatasetId::CALCS
                | DatasetId::LinCE
                | DatasetId::FinTechPatent
                | DatasetId::WaterAgriNER
                | DatasetId::NERsocialFood
                | DatasetId::RussianCulturalNER
                | DatasetId::EighteenthCenturyNER
                | DatasetId::GuaraniNER
                | DatasetId::ShipiboKoniboNER
                | DatasetId::BASHI
                | DatasetId::ENER
                // Added 2025-01-27: CoNLL NER datasets with public URLs but missing hints
                | DatasetId::OntoNotes50
                | DatasetId::GUM
                // CoNLL04RE is CoNLL format (works with CoNLL parser even though it's for RE)
                | DatasetId::CoNLL04RE
        ) {
            return Some(DatasetParsePlan::Conll);
        }

        // JSONL datasets that should use JsonlNer despite having coref/RE tasks.
        // The JsonlNer parser handles tokens + ner_tags fields generically.
        if matches!(
            id,
            DatasetId::REBEL
                | DatasetId::BBQ
                | DatasetId::RealToxicityPrompts
                | DatasetId::BookCoref
                | DatasetId::BookCorefSplit
                | DatasetId::WIESP2022NER
                | DatasetId::FewRel
                | DatasetId::PIIMasking200k
                | DatasetId::B2NERD
                | DatasetId::OpenNER
                | DatasetId::FictionNER750M
                // Added 2025-12: More JSONL NER datasets
                | DatasetId::MultiWOZNER
                | DatasetId::HinglishNER
                | DatasetId::ChineseNestedNER
                | DatasetId::AgCNER
                | DatasetId::LongDocNER
                | DatasetId::MultiBioNERLong
                | DatasetId::ReasoningNER
                | DatasetId::BioNERLLaMA
                | DatasetId::LexGLUENER
                | DatasetId::FinBenNER
                | DatasetId::FiNER139
                | DatasetId::SciNER
                | DatasetId::CharacterCodex
                | DatasetId::AIONER
                | DatasetId::WIESPAstro
                | DatasetId::CEREC
                | DatasetId::DELICATE
                | DatasetId::CSN
                // Added 2025-01-27: JSONL NER datasets with public URLs but missing hints
                | DatasetId::SCINERNested
                | DatasetId::AgriNER
                | DatasetId::MOFDataset
                | DatasetId::SolidStateDoping
        ) {
            return Some(DatasetParsePlan::JsonlNer);
        }

        // CoNLLU datasets (Universal Dependencies treebanks with NER annotations)
        if matches!(
            id,
            DatasetId::AncientGreekUD
                | DatasetId::LatinUD
                | DatasetId::SanskritUD
                | DatasetId::OldEnglishUD
                | DatasetId::OldNorseUD
                | DatasetId::UDEsperantoCairo
                // Added 2025-12: More UD treebanks (ancient/historical/constructed)
                | DatasetId::CopticScriptorium
                | DatasetId::TaggedPBCEsperanto
                | DatasetId::TaggedPBCKlingon
                | DatasetId::AkkadianUD
                | DatasetId::AncientHebrewUD
                | DatasetId::ClassicalChineseUD
                | DatasetId::CopticUD
                | DatasetId::GothicUD
                | DatasetId::HittiteUD
                | DatasetId::OldChurchSlavonicUD
                | DatasetId::LatinITTB
                | DatasetId::LatinPROIEL
                | DatasetId::EsperantoUD
                | DatasetId::NavajoMorph
        ) {
            return Some(DatasetParsePlan::Conllu);
        }

        // TSV NER datasets (HIPE-2022 style)
        if matches!(id, DatasetId::HIPE2022) {
            return Some(DatasetParsePlan::TsvNer);
        }

        // CSV NER datasets
        if matches!(id, DatasetId::ENer) {
            return Some(DatasetParsePlan::CsvNer);
        }

        // Some datasets are fetched via HF datasets-server (JSON response), regardless of their
        // “canonical” source format in the registry.
        if matches!(
            id,
            DatasetId::GENIA
                | DatasetId::AnatEM
                | DatasetId::BC2GM
                | DatasetId::BC4CHEMD
                | DatasetId::FewNERD
                | DatasetId::CrossNER
                | DatasetId::FabNER
                | DatasetId::WikiNeural
                | DatasetId::WikiANN
                | DatasetId::MultiCoNER
                | DatasetId::MultiCoNERv2
                | DatasetId::PolyglotNER
                | DatasetId::UniversalNER
        ) {
            return Some(DatasetParsePlan::HfApiResponse);
        }

        // Use tasks_or_inferred() to support datasets that have task-like categories
        // (e.g., `categories: [ner, ancient]`) but no explicit `tasks` field yet.
        // This enables automatic loading for datasets with correct format + category.
        let inferred_tasks = id.tasks_or_inferred();
        let is_ner = inferred_tasks.contains(&"ner");
        let is_coref = inferred_tasks.contains(&"coref");
        let is_re = inferred_tasks.contains(&"re");
        let is_event = inferred_tasks.contains(&"event_extraction");

        let annotation_scheme = id.annotation_scheme().unwrap_or("");

        // Format string is about the *source* dataset, not necessarily our cached
        // representation; still useful for many direct-download datasets.
        if let Some(format) = id.format() {
            let plan = match format {
                "CoNLL" | "BIO" | "IOB2" if is_ner && !is_coref && !is_re && !is_event => {
                    Some(DatasetParsePlan::Conll)
                }
                "CoNLL-U" | "CoNLLU" if is_ner && !is_coref && !is_re && !is_event => {
                    Some(DatasetParsePlan::Conllu)
                }
                "JSONL" if is_ner && !is_coref && !is_re && !is_event => {
                    Some(DatasetParsePlan::JsonlNer)
                }
                // Coreference: we only treat these as unambiguous when the dataset is *only* coref.
                // Check both format field and annotation_scheme for CoNLLCoref datasets.
                "TSV" | "ConllCoref" | "CoNLLCoref"
                    if is_coref && !is_ner && !is_re && !is_event =>
                {
                    // GAP-style TSV and similar "coref-only" exports.
                    Some(DatasetParsePlan::GapTsv)
                }
                // CoNLL format with CoNLLCoref annotation scheme (e.g., GICoref, qxoRef)
                "CoNLL"
                    if annotation_scheme == "CoNLLCoref"
                        && is_coref
                        && !is_ner
                        && !is_re
                        && !is_event =>
                {
                    Some(DatasetParsePlan::GapTsv)
                }
                "JSONL" if is_coref && !is_ner && !is_re && !is_event => {
                    // PreCo/SciCo style JSONL exports.
                    Some(DatasetParsePlan::PrecoJsonl)
                }

                // Relation extraction: unambiguous only when the dataset is *only* RE.
                // Also handle RE datasets that might have NER as secondary task (e.g., SciERCNER).
                "JSON" | "JSONL" if is_re && !is_coref && !is_event => {
                    Some(DatasetParsePlan::DocredJson)
                }

                // Event extraction: treat JSONL as MAVEN-ish only when the dataset is event-only.
                "JSONL" if is_event && !is_ner && !is_coref && !is_re => {
                    Some(DatasetParsePlan::Maven)
                }

                // TSV NER format (HIPE-2022 style)
                "TSV" if is_ner && !is_coref && !is_re && !is_event => {
                    Some(DatasetParsePlan::TsvNer)
                }

                // CSV NER format (E-NER/EDGAR-NER style: Token,Tag)
                "CSV" if is_ner && !is_coref && !is_re && !is_event => {
                    Some(DatasetParsePlan::CsvNer)
                }

                // These are too ambiguous across sources; treat as unknown for now.
                _ => None,
            };

            if plan.is_some() {
                return plan;
            }
        }

        // HuggingFace datasets: if the registry provides an HF id/config and marks the
        // dataset as HuggingFace-accessible, we can treat it as an HfApiResponse plan.
        //
        // Keep this as a *fallback*: registry format/task signals (and explicit hint lists
        // above) take precedence, so we don't override e.g. CoNLL/JSONL datasets that are
        // hosted on HuggingFace but should be parsed as raw files.
        if id.hf_id().is_some()
            && id.access_status()
                == crate::eval::dataset_registry::DatasetAccessibility::HuggingFace
        {
            return Some(DatasetParsePlan::HfApiResponse);
        }

        None
    }
}

impl std::ops::Deref for LoadableDatasetId {
    type Target = DatasetId;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl From<LoadableDatasetId> for DatasetId {
    fn from(value: LoadableDatasetId) -> Self {
        value.0
    }
}

impl std::str::FromStr for LoadableDatasetId {
    type Err = Error;

    fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
        let id: DatasetId = s
            .parse()
            .map_err(|e| Error::InvalidInput(format!("Invalid dataset id '{}': {}", s, e)))?;
        Self::try_from(id)
    }
}

impl TryFrom<DatasetId> for LoadableDatasetId {
    type Error = Error;

    fn try_from(value: DatasetId) -> std::result::Result<Self, Self::Error> {
        if Self::is_loadable_dataset(value) {
            Ok(Self(value))
        } else {
            Err(Error::InvalidInput(format!(
                "Dataset {:?} is in the registry but has no loading implementation",
                value
            )))
        }
    }
}

// =============================================================================
// Loader-specific extensions for `DatasetId`
// =============================================================================
//
// `DatasetId` is defined in `dataset_registry` (the full catalog of datasets).
// This module is intentionally split:
// - `DatasetId`: metadata/catalog (can include datasets that are not loadable here)
// - `LoadableDatasetId`: the subset that this loader+parsers support

// Extension methods for DatasetId that delegate to registry
impl DatasetId {
    /// Get default metadata for this dataset.
    ///
    /// Delegates to registry methods.
    #[must_use]
    pub fn default_metadata(&self) -> DatasetMetadata {
        DatasetMetadata {
            description: Some(self.description().to_string()),
            license: self.license().map(|s| s.to_string()),
            citation: self.citation().map(|s| s.to_string()),
            split: Some("test".to_string()), // Default to test split
            language: Some(self.language().to_string()),
            domain: Some(self.domain().to_string()),
            original_source: Some(self.download_url().to_string()),
            version: None,
            annotators: None,
            guidelines_url: None,
        }
    }
}

// =============================================================================
// Cache Manifest
// =============================================================================

/// Entry in the cache manifest tracking a downloaded dataset file.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CacheManifestEntry {
    /// Dataset identifier.
    pub dataset_id: String,
    /// Source URL from which the data was downloaded.
    ///
    /// This is the *logical* registry URL (what the dataset entry says).
    pub source_url: String,
    /// The URL that was actually fetched (after any rewrites / mirrors).
    ///
    /// This is helpful for reproducibility (and debugging) when the registry URL is a
    /// homepage (e.g., HuggingFace dataset page) but we fetched via datasets-server
    /// or `resolve/main/...`.
    #[serde(default)]
    pub resolved_url: Option<String>,
    /// SHA-256 checksum of the downloaded file.
    pub sha256: String,
    /// File size in bytes.
    pub file_size: u64,
    /// ISO 8601 timestamp of when the file was downloaded.
    pub downloaded_at: String,
    /// Number of sentences parsed from the dataset.
    pub sentence_count: usize,
    /// Number of entities parsed from the dataset.
    pub entity_count: usize,
    /// Anno version that downloaded this file.
    pub anno_version: String,
}

/// Cache manifest tracking all downloaded datasets.
///
/// Stored as `manifest.json` in the cache directory.
/// Enables:
/// - Reproducibility: know exactly what was downloaded
/// - Validation: verify checksums match
/// - Debugging: trace when data was fetched
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct CacheManifest {
    /// Version of the manifest format.
    pub version: u32,
    /// Entries for each cached dataset.
    pub entries: HashMap<String, CacheManifestEntry>,
}

impl CacheManifest {
    /// Current manifest format version.
    pub const CURRENT_VERSION: u32 = 1;

    /// Create a new empty manifest.
    #[must_use]
    pub fn new() -> Self {
        Self {
            version: Self::CURRENT_VERSION,
            entries: HashMap::new(),
        }
    }

    /// Load manifest from a cache directory.
    ///
    /// Returns empty manifest if file doesn't exist.
    pub fn load(cache_dir: &std::path::Path) -> Result<Self> {
        let manifest_path = cache_dir.join("manifest.json");
        if !manifest_path.exists() {
            return Ok(Self::new());
        }

        let content = fs::read_to_string(&manifest_path)
            .map_err(|e| Error::InvalidInput(format!("Failed to read manifest: {}", e)))?;

        serde_json::from_str(&content)
            .map_err(|e| Error::InvalidInput(format!("Failed to parse manifest: {}", e)))
    }

    /// Save manifest to a cache directory.
    pub fn save(&self, cache_dir: &std::path::Path) -> Result<()> {
        let manifest_path = cache_dir.join("manifest.json");
        let content = serde_json::to_string_pretty(self)
            .map_err(|e| Error::InvalidInput(format!("Failed to serialize manifest: {}", e)))?;

        fs::write(&manifest_path, content)
            .map_err(|e| Error::InvalidInput(format!("Failed to write manifest: {}", e)))
    }

    /// Add or update an entry in the manifest.
    pub fn update_entry(&mut self, entry: CacheManifestEntry) {
        self.entries.insert(entry.dataset_id.clone(), entry);
    }

    /// Get entry for a dataset.
    #[must_use]
    pub fn get(&self, dataset_id: &str) -> Option<&CacheManifestEntry> {
        self.entries.get(dataset_id)
    }

    /// Check if a dataset's cached file matches the manifest.
    ///
    /// Returns `true` if the file exists and checksum matches.
    #[must_use]
    pub fn verify_entry(&self, dataset_id: &str, cache_dir: &std::path::Path) -> bool {
        let Some(entry) = self.entries.get(dataset_id) else {
            return false;
        };

        // Check file exists with expected size
        let file_path = cache_dir.join(&entry.dataset_id);
        let Ok(metadata) = fs::metadata(&file_path) else {
            return false;
        };

        metadata.len() == entry.file_size
        // Note: Full SHA-256 verification is expensive; size check is usually sufficient
    }
}

// =============================================================================
// Temporal Metadata
// =============================================================================

/// Temporal metadata for datasets (optional).
///
/// Used for temporal stratification of evaluation metrics.
/// Most datasets don't have temporal metadata, so this is optional.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct TemporalMetadata {
    /// KB version used for entity linking (if applicable)
    pub kb_version: Option<String>,
    /// Temporal cutoff date (entities before this date are "old", after are "new")
    pub temporal_cutoff: Option<String>, // ISO 8601 date string
    /// Entity creation dates (if available)
    pub entity_creation_dates: Option<HashMap<String, String>>, // entity_id -> ISO 8601 date
}

// =============================================================================
// Annotated Structures
// =============================================================================

/// Annotated token with NER tag.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AnnotatedToken {
    /// Token text
    pub text: String,
    /// NER tag (BIO format)
    pub ner_tag: String,
}

/// Annotated sentence with tokens.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AnnotatedSentence {
    /// Tokens in the sentence
    pub tokens: Vec<AnnotatedToken>,
    /// Source dataset
    pub source_dataset: DatasetId,
}

impl AnnotatedSentence {
    /// Get text of the sentence.
    #[must_use]
    pub fn text(&self) -> String {
        self.tokens
            .iter()
            .map(|t| t.text.as_str())
            .collect::<Vec<_>>()
            .join(" ")
    }

    /// Get entities from this sentence.
    #[must_use]
    pub fn entities(&self) -> Vec<super::datasets::GoldEntity> {
        // Precompute character offsets for each token in `self.text()` (tokens joined by a
        // single ASCII space). Offsets are in *character* units, not bytes.
        let mut token_starts: Vec<usize> = Vec::with_capacity(self.tokens.len());
        let mut pos: usize = 0;
        for (i, tok) in self.tokens.iter().enumerate() {
            token_starts.push(pos);
            pos += tok.text.chars().count();
            if i + 1 < self.tokens.len() {
                pos += 1; // the space inserted by `text()`
            }
        }

        let mut entities = Vec::new();
        let mut i = 0;
        while i < self.tokens.len() {
            let tag = &self.tokens[i].ner_tag;

            // Check for BIO format (B-TYPE, I-TYPE)
            if tag.starts_with("B-") {
                let entity_type = tag.trim_start_matches("B-");
                let mut end = i + 1;
                // Only continue with I-tags that match the same entity type
                while end < self.tokens.len()
                    && self.tokens[end].ner_tag.starts_with("I-")
                    && self.tokens[end].ner_tag.trim_start_matches("I-") == entity_type
                {
                    end += 1;
                }
                let entity_text: String = self.tokens[i..end]
                    .iter()
                    .map(|t| t.text.as_str())
                    .collect::<Vec<_>>()
                    .join(" ");

                let start = token_starts.get(i).copied().unwrap_or(0);
                let end_char = if end <= i {
                    start
                } else {
                    let last = end - 1;
                    token_starts.get(last).copied().unwrap_or(start)
                        + self.tokens[last].text.chars().count()
                };

                entities.push(super::datasets::GoldEntity {
                    text: entity_text,
                    original_label: entity_type.to_string(),
                    entity_type: anno_core::EntityType::from_label(entity_type),
                    start,
                    end: end_char,
                });
                i = end;
            }
            // Fallback: non-BIO format (simple type names like "person", "organization")
            // Used by datasets like FewNERD that use ClassLabels
            else if tag != "O" && !tag.starts_with("I-") && !tag.starts_with("TAG_") {
                let entity_type = tag.as_str();
                let mut end = i + 1;
                // Continue while same entity type (treating consecutive same labels as one entity)
                while end < self.tokens.len() && self.tokens[end].ner_tag == *tag {
                    end += 1;
                }
                let entity_text: String = self.tokens[i..end]
                    .iter()
                    .map(|t| t.text.as_str())
                    .collect::<Vec<_>>()
                    .join(" ");

                let start = token_starts.get(i).copied().unwrap_or(0);
                let end_char = if end <= i {
                    start
                } else {
                    let last = end - 1;
                    token_starts.get(last).copied().unwrap_or(start)
                        + self.tokens[last].text.chars().count()
                };

                entities.push(super::datasets::GoldEntity {
                    text: entity_text,
                    original_label: entity_type.to_string(),
                    entity_type: anno_core::EntityType::from_label(entity_type),
                    start,
                    end: end_char,
                });
                i = end;
            } else {
                i += 1;
            }
        }
        entities
    }
}

/// Dataset metadata for provenance and reproducibility.
///
/// Captures all information needed to understand where data came from
/// and how to attribute/license it correctly.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct DatasetMetadata {
    /// Human-readable description.
    pub description: Option<String>,
    /// License (e.g., "CC-BY-4.0", "Apache-2.0", "Research-only").
    pub license: Option<String>,
    /// Citation reference (BibTeX key or paper title).
    pub citation: Option<String>,
    /// Split type (e.g., "train", "dev", "test").
    pub split: Option<String>,
    /// Language code (e.g., "en", "de", "zh").
    pub language: Option<String>,
    /// Domain (e.g., "news", "biomedical", "social-media").
    pub domain: Option<String>,
    /// Original source repository or homepage.
    pub original_source: Option<String>,
    /// Version string if the dataset has versions.
    pub version: Option<String>,
    /// Number of annotators (for IAA reporting).
    pub annotators: Option<u32>,
    /// Annotation guidelines URL if available.
    pub guidelines_url: Option<String>,
}

/// Where the dataset was loaded from.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
pub enum DataSource {
    /// Loaded from S3 cache (fast, reliable).
    S3Cache,
    /// Loaded from local file cache.
    LocalCache,
    /// Downloaded from original URL (may be slow or unavailable).
    OriginalUrl,
    /// Dataset was skipped (URL broken or unavailable).
    #[default]
    Skipped,
    /// Embedded in binary (test fixtures).
    Embedded,
}

impl DataSource {
    /// Human-readable description of the source.
    #[must_use]
    pub fn description(&self) -> &'static str {
        match self {
            Self::S3Cache => "S3 cache",
            Self::LocalCache => "local cache",
            Self::OriginalUrl => "original URL",
            Self::Skipped => "skipped (unavailable)",
            Self::Embedded => "embedded",
        }
    }

    /// Whether this source is cached (fast).
    #[must_use]
    pub fn is_cached(&self) -> bool {
        matches!(self, Self::S3Cache | Self::LocalCache | Self::Embedded)
    }

    /// Whether this source indicates the data is unavailable.
    #[must_use]
    pub fn is_unavailable(&self) -> bool {
        matches!(self, Self::Skipped)
    }
}

impl std::fmt::Display for DataSource {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.description())
    }
}

/// A loaded dataset with metadata.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LoadedDataset {
    /// Dataset identifier.
    pub id: DatasetId,
    /// Annotated sentences.
    pub sentences: Vec<AnnotatedSentence>,
    /// When the dataset was loaded.
    pub loaded_at: String, // ISO 8601
    /// Source URL.
    pub source_url: String,
    /// Where the data was actually loaded from.
    #[serde(default)]
    pub data_source: DataSource,
    /// Optional temporal metadata for temporal stratification
    #[serde(default)]
    pub temporal_metadata: Option<TemporalMetadata>,
    /// Dataset provenance and attribution metadata.
    #[serde(default)]
    pub metadata: DatasetMetadata,
}

impl LoadedDataset {
    /// Total number of sentences.
    #[must_use]
    pub fn len(&self) -> usize {
        self.sentences.len()
    }

    /// Check if empty.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.sentences.is_empty()
    }

    /// Total number of entities.
    #[must_use]
    pub fn entity_count(&self) -> usize {
        self.sentences.iter().map(|s| s.entities().len()).sum()
    }

    /// Count entities by type.
    #[must_use]
    pub fn entity_counts_by_type(&self) -> HashMap<String, usize> {
        let mut counts = HashMap::new();
        for sentence in &self.sentences {
            for entity in sentence.entities() {
                *counts.entry(entity.original_label.clone()).or_insert(0) += 1;
            }
        }
        counts
    }

    /// Get statistics summary.
    #[must_use]
    pub fn stats(&self) -> DatasetStats {
        DatasetStats {
            name: self.id.name().to_string(),
            sentences: self.len(),
            tokens: self.sentences.iter().map(|s| s.tokens.len()).sum(),
            entities: self.entity_count(),
            entities_by_type: self.entity_counts_by_type(),
        }
    }

    /// Convert to test cases format for evaluation.
    #[must_use]
    pub fn to_test_cases(&self) -> Vec<(String, Vec<super::datasets::GoldEntity>)> {
        self.sentences
            .iter()
            .map(|s| (s.text(), s.entities()))
            .collect()
    }
}

// =============================================================================
// Dataset Statistics
// =============================================================================

/// Dataset statistics.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DatasetStats {
    /// Dataset name.
    pub name: String,
    /// Number of sentences.
    pub sentences: usize,
    /// Total token count.
    pub tokens: usize,
    /// Total entity count.
    pub entities: usize,
    /// Entities by type.
    pub entities_by_type: HashMap<String, usize>,
}

/// A document with text and gold relations for relation extraction evaluation.
#[derive(Debug, Clone)]
pub struct RelationDocument {
    /// Document text
    pub text: String,
    /// Gold standard relations
    pub relations: Vec<super::relation::RelationGold>,
}

impl std::fmt::Display for DatasetStats {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        writeln!(f, "Dataset: {}", self.name)?;
        writeln!(f, "  Sentences: {}", self.sentences)?;
        writeln!(f, "  Tokens: {}", self.tokens)?;
        writeln!(f, "  Entities: {}", self.entities)?;
        writeln!(f, "  Entity types:")?;
        let mut types: Vec<_> = self.entities_by_type.iter().collect();
        types.sort_by(|a, b| b.1.cmp(a.1));
        for (etype, count) in types {
            writeln!(f, "    {}: {}", etype, count)?;
        }
        Ok(())
    }
}

// =============================================================================
// Dataset Loader
// =============================================================================

/// Loads and caches NER datasets.
///
/// # Caching Strategy (Tiered)
///
/// 1. **Local cache** (`~/.cache/anno/datasets/`): Checked first, fastest
/// 2. **S3 cache** (`s3://anno-data/`): Checked if `ANNO_S3_CACHE=1` and AWS credentials available
/// 3. **URL download**: Original source URL, last resort
///
/// This tiered approach:
/// - Maximizes speed (local cache hit is instant)
/// - Provides redundancy (S3 backup if original URLs go down)
/// - Allows sharing datasets across machines via S3
///
/// # Environment Variables
///
/// - `ANNO_CACHE_DIR`: Override default cache location
/// - `ANNO_S3_CACHE`: Set to "1" to enable S3 fallback (requires AWS credentials)
/// - `ANNO_S3_BUCKET`: Override S3 bucket name (default: `arc-anno-data`)
///
/// # Example
///
/// ```bash
/// # Enable S3 caching
/// export ANNO_S3_CACHE=1
/// export AWS_PROFILE=default  # or set AWS_ACCESS_KEY_ID, etc.
///
/// # Now load_or_download() will try S3 before URL
/// ```
pub struct DatasetLoader {
    cache_dir: PathBuf,
    /// S3 bucket name for fallback caching (if enabled)
    s3_bucket: Option<String>,
    /// Cache manifest for tracking downloads.
    ///
    /// This is shared across evaluation threads, so it must be thread-safe.
    manifest: std::sync::RwLock<CacheManifest>,
}

impl DatasetLoader {
    // Default download cap when `ANNO_MAX_DOWNLOAD_BYTES` is unset.
    //
    // Rationale: unset should be *usable* but *safe* by default. This cap is meant to prevent
    // accidental multi-GB downloads while still allowing many evaluation datasets.
    #[cfg(feature = "eval")]
    const DEFAULT_MAX_DOWNLOAD_BYTES: u64 = 50 * 1024 * 1024; // 50 MiB

    #[cfg(feature = "eval")]
    fn max_download_bytes() -> Option<u64> {
        match std::env::var("ANNO_MAX_DOWNLOAD_BYTES").ok() {
            Some(s) => {
                let s = s.trim();
                if s.is_empty() {
                    return Some(Self::DEFAULT_MAX_DOWNLOAD_BYTES);
                }
                let Ok(v) = s.parse::<u64>() else {
                    return Some(Self::DEFAULT_MAX_DOWNLOAD_BYTES);
                };
                if v == 0 {
                    None // explicit opt-out
                } else {
                    Some(v)
                }
            }
            None => Some(Self::DEFAULT_MAX_DOWNLOAD_BYTES),
        }
    }

    #[cfg(feature = "eval")]
    fn enforce_max_download_bytes(content_len: usize, source: &str) -> Result<()> {
        let Some(limit) = Self::max_download_bytes() else {
            return Ok(());
        };
        let len = content_len as u64;
        if len > limit {
            return Err(Error::InvalidInput(format!(
                "Download rejected ({} bytes > ANNO_MAX_DOWNLOAD_BYTES={} bytes) from {}",
                len, limit, source
            )));
        }
        Ok(())
    }

    #[cfg(feature = "eval")]
    fn hf_dataset_from_rows_url(url: &str) -> Option<String> {
        // Example:
        // https://datasets-server.huggingface.co/rows?dataset=masakhane%2Fmasakhaner2&config=bam&split=test...
        let (_, query) = url.split_once('?')?;
        for part in query.split('&') {
            let (k, v) = part.split_once('=')?;
            if k == "dataset" {
                // Minimal percent-decoding sufficient for HF dataset ids.
                let decoded = v.replace("%2F", "/").replace("%2f", "/");
                return Some(decoded);
            }
        }
        None
    }

    /// Create a new loader with default cache directory.
    ///
    /// Default location: `~/.cache/anno/datasets` (platform cache via `dirs` crate)
    /// Falls back to `.anno/datasets` in current directory if `dirs` crate unavailable.
    ///
    /// S3 fallback is enabled if `ANNO_S3_CACHE=1` environment variable is set.
    pub fn new() -> Result<Self> {
        // Load workspace `.env` (idempotent, does not override existing env vars).
        // This keeps eval tooling usable without manual exporting of env vars.
        anno::env::load_dotenv();

        // Check for custom cache directory
        let cache_dir = if let Ok(custom_dir) = std::env::var("ANNO_CACHE_DIR") {
            PathBuf::from(custom_dir).join("datasets")
        } else {
            let base_dir = dirs::cache_dir().unwrap_or_else(|| PathBuf::from("."));
            base_dir.join("anno").join("datasets")
        };

        // Check for S3 caching
        let s3_bucket = if std::env::var("ANNO_S3_CACHE").unwrap_or_default() == "1" {
            Some(std::env::var("ANNO_S3_BUCKET").unwrap_or_else(|_| "arc-anno-data".to_string()))
        } else {
            None
        };

        fs::create_dir_all(&cache_dir).map_err(|e| {
            Error::InvalidInput(format!("Failed to create cache dir {:?}: {}", cache_dir, e))
        })?;

        let manifest = CacheManifest::load(&cache_dir)?;

        Ok(Self {
            cache_dir,
            s3_bucket,
            manifest: std::sync::RwLock::new(manifest),
        })
    }

    /// Update the cache manifest with a new entry and save it.
    #[cfg(feature = "eval")]
    fn update_manifest(&self, entry: CacheManifestEntry) -> Result<()> {
        let mut manifest = self
            .manifest
            .write()
            .map_err(|_| Error::InvalidInput("cache manifest lock poisoned".to_string()))?;
        manifest.update_entry(entry);
        manifest.save(&self.cache_dir)?;
        Ok(())
    }

    /// Create a loader with a custom cache directory.
    pub fn with_cache_dir(cache_dir: impl Into<PathBuf>) -> Result<Self> {
        let cache_dir = cache_dir.into();
        let s3_bucket = if std::env::var("ANNO_S3_CACHE").unwrap_or_default() == "1" {
            Some(std::env::var("ANNO_S3_BUCKET").unwrap_or_else(|_| "arc-anno-data".to_string()))
        } else {
            None
        };

        fs::create_dir_all(&cache_dir).map_err(|e| {
            Error::InvalidInput(format!("Failed to create cache dir {:?}: {}", cache_dir, e))
        })?;

        let manifest = CacheManifest::load(&cache_dir)?;

        Ok(Self {
            cache_dir,
            s3_bucket,
            manifest: std::sync::RwLock::new(manifest),
        })
    }

    /// Base directory for cached datasets.
    #[must_use]
    pub fn cache_dir(&self) -> &std::path::Path {
        &self.cache_dir
    }

    /// Whether S3 fallback caching is enabled.
    #[must_use]
    pub fn s3_enabled(&self) -> bool {
        self.s3_bucket.is_some()
    }

    /// The configured S3 bucket name (if enabled).
    #[must_use]
    pub fn s3_bucket(&self) -> Option<&str> {
        self.s3_bucket.as_deref()
    }

    /// Snapshot of cached dataset manifest entries (cloned).
    ///
    /// This is intended for tooling (CLI/scripts) that wants to iterate cached datasets
    /// without holding the manifest lock for a long time.
    #[must_use]
    pub fn cached_manifest_entries(&self) -> Vec<CacheManifestEntry> {
        let Ok(manifest) = self.manifest.read() else {
            return Vec::new();
        };
        let mut out: Vec<CacheManifestEntry> = manifest.entries.values().cloned().collect();
        out.sort_by(|a, b| a.dataset_id.cmp(&b.dataset_id));
        out
    }

    /// Upload a locally cached dataset (by `DatasetId`) to S3, using its manifest entry.
    ///
    /// This uses the same object layout as `load_or_download()`'s best-effort upload:
    /// - `datasets/<cache_filename>` (legacy/mutable key)
    /// - `datasets/by-sha256/<sha>/<cache_filename>` (immutable snapshot)
    /// - `datasets/<cache_filename>.latest.json` (pointer)
    /// - `datasets/<cache_filename>.manifest.json` (sidecar metadata)
    #[cfg(feature = "eval")]
    pub fn upload_cached_dataset_to_s3(&self, bucket: &str, id: DatasetId) -> Result<()> {
        let key = id.cache_filename();
        let entry = {
            let guard = self
                .manifest
                .read()
                .map_err(|_| Error::InvalidInput("cache manifest lock poisoned".to_string()))?;
            guard
                .get(key)
                .cloned()
                .ok_or_else(|| Error::InvalidInput(format!("No manifest entry for {}", key)))?
        };
        let path = self.cache_path_for(id);
        let content = std::fs::read_to_string(&path).map_err(|e| {
            Error::InvalidInput(format!(
                "Failed to read cached dataset {}: {}",
                path.display(),
                e
            ))
        })?;
        Self::enforce_max_download_bytes(content.len(), "local cache (sync-s3)")?;
        self.upload_to_s3(bucket, id, &content, &entry)
    }

    #[must_use]
    fn cache_path_for(&self, id: DatasetId) -> PathBuf {
        self.cache_dir.join(id.cache_filename())
    }

    #[must_use]
    fn is_cached_for(&self, id: DatasetId) -> bool {
        if !self.cache_path_for(id).exists() {
            return false;
        }

        // Best-effort cache invalidation: if the registry URL changes, the on-disk cached
        // file may no longer correspond to the dataset id. Prefer re-download over silently
        // using stale/incorrect data.
        //
        // This is intentionally lightweight (no checksum re-hash here).
        if let Ok(manifest) = self.manifest.read() {
            if let Some(entry) = manifest.get(id.cache_filename()) {
                if entry.source_url != id.download_url() {
                    return false;
                }
                // Treat “cached but empty” as invalid: this is almost always a bad download
                // (HTML, auth wall, or format mismatch) and should be re-fetched.
                if entry.sentence_count == 0 {
                    return false;
                }
            }
        }

        true
    }

    /// Get the cache path for a dataset.
    #[must_use]
    pub fn cache_path(&self, id: LoadableDatasetId) -> PathBuf {
        self.cache_path_for(id.0)
    }

    /// Check if a dataset is cached locally.
    #[must_use]
    pub fn is_cached(&self, id: LoadableDatasetId) -> bool {
        self.is_cached_for(id.0)
    }

    /// Load a dataset from cache.
    ///
    /// Returns an error if the dataset is not cached.
    pub fn load(&self, id: LoadableDatasetId) -> Result<LoadedDataset> {
        let dataset_id = id.0;
        let cache_path = self.cache_path(id);
        if !cache_path.exists() {
            return Err(Error::InvalidInput(format!(
                "Dataset {:?} not cached at {:?}",
                dataset_id, cache_path
            )));
        }

        let content = fs::read_to_string(&cache_path).map_err(|e| {
            Error::InvalidInput(format!("Failed to read cache {:?}: {}", cache_path, e))
        })?;

        let mut dataset = self.parse_content_impl(&content, dataset_id)?;
        if dataset.sentences.is_empty() {
            return Err(Error::InvalidInput(format!(
                "Cached dataset '{}' parsed to 0 sentences (cache_path={:?})",
                dataset_id.name(),
                cache_path
            )));
        }
        dataset.data_source = DataSource::LocalCache;
        Ok(dataset)
    }

    /// Load or download a dataset.
    ///
    /// Tries cache first, then S3 (if enabled), then downloads from URL.
    #[cfg(feature = "eval")]
    pub fn load_or_download(&self, id: LoadableDatasetId) -> Result<LoadedDataset> {
        let dataset_id = id.0;
        // 1. Check local cache first
        if self.is_cached(id) {
            return self.load(id);
        }

        // 2. Try S3 cache if enabled
        if let Some(ref bucket) = self.s3_bucket {
            if let Ok((content, manifest_entry)) = self.download_from_s3(bucket, dataset_id) {
                Self::enforce_max_download_bytes(content.len(), "S3")?;
                // Cache locally for future use
                let cache_path = self.cache_path(id);
                fs::write(&cache_path, &content).map_err(|e| {
                    Error::InvalidInput(format!("Failed to write cache {:?}: {}", cache_path, e))
                })?;

                let mut dataset = self.parse_content_impl(&content, dataset_id)?;
                dataset.data_source = DataSource::S3Cache;

                // Best-effort: if S3 provides a manifest entry, record it locally.
                if let Some(entry) = manifest_entry {
                    let _ = self.update_manifest(entry);
                }
                return Ok(dataset);
            }
        }

        // 3. Download from original URL
        let (content, resolved_url) = self.download_with_resolved_url(dataset_id)?;
        Self::enforce_max_download_bytes(content.len(), &resolved_url)?;
        let file_size = content.len() as u64;
        let sha256 = self.compute_sha256(&content);

        // 4. Cache the downloaded content locally
        let cache_path = self.cache_path(id);
        fs::write(&cache_path, &content).map_err(|e| {
            Error::InvalidInput(format!("Failed to write cache {:?}: {}", cache_path, e))
        })?;

        // 5. Parse the content
        let mut dataset = self.parse_content_impl(&content, dataset_id)?;
        dataset.data_source = DataSource::OriginalUrl;

        // 6. Update manifest with download metadata
        let entry = CacheManifestEntry {
            dataset_id: dataset_id.cache_filename().to_string(),
            source_url: dataset_id.download_url().to_string(),
            resolved_url: Some(resolved_url.clone()),
            sha256,
            file_size,
            downloaded_at: chrono::Utc::now().to_rfc3339(),
            sentence_count: dataset.sentences.len(),
            entity_count: dataset.entity_count(),
            anno_version: env!("CARGO_PKG_VERSION").to_string(),
        };
        let _ = self.update_manifest(entry); // Best effort

        // 7. Optionally upload to S3 for future use (best effort)
        if let Some(ref bucket) = self.s3_bucket {
            let entry = CacheManifestEntry {
                dataset_id: dataset_id.cache_filename().to_string(),
                source_url: dataset_id.download_url().to_string(),
                resolved_url: Some(resolved_url),
                sha256: self.compute_sha256(&content),
                file_size: content.len() as u64,
                downloaded_at: chrono::Utc::now().to_rfc3339(),
                sentence_count: dataset.sentences.len(),
                entity_count: dataset.entity_count(),
                anno_version: env!("CARGO_PKG_VERSION").to_string(),
            };
            let _ = self.upload_to_s3(bucket, dataset_id, &content, &entry);
        }

        Ok(dataset)
    }

    /// Load-or-download shim when `eval` is disabled.
    ///
    /// This method will load from cache if present, otherwise return a helpful error.
    #[cfg(not(feature = "eval"))]
    pub fn load_or_download(&self, id: LoadableDatasetId) -> Result<LoadedDataset> {
        if self.is_cached(id) {
            return self.load(id);
        }

        Err(Error::InvalidInput(
            "Dataset is not cached. Rebuild with feature `eval` to enable downloading.".to_string(),
        ))
    }

    /// Download dataset from S3 cache bucket.
    ///
    /// Uses AWS CLI under the hood (requires `aws` command in PATH and valid credentials).
    #[cfg(feature = "eval")]
    fn download_from_s3(
        &self,
        bucket: &str,
        id: DatasetId,
    ) -> Result<(String, Option<CacheManifestEntry>)> {
        use std::process::Command;

        // Prefer a content-addressed snapshot if the pointer exists; fall back to the legacy key.
        let pointer_key = format!("datasets/{}.latest.json", id.cache_filename());
        let pointer_uri = format!("s3://{}/{}", bucket, pointer_key);

        let mut content: Option<String> = None;

        // Attempt: pointer -> by-sha256 snapshot
        let pointer = Command::new("aws")
            .args(["s3", "cp", &pointer_uri, "-"])
            .output()
            .ok()
            .and_then(|o| {
                if !o.status.success() {
                    // Pointer missing or unreadable; fall back to legacy key.
                    return None;
                }
                String::from_utf8(o.stdout).ok()
            })
            .and_then(|json| serde_json::from_str::<serde_json::Value>(&json).ok());

        if let Some(pointer) = pointer {
            if let Some(sha) = pointer.get("sha256").and_then(|v| v.as_str()) {
                let by_sha_key = format!("datasets/by-sha256/{}/{}", sha, id.cache_filename());
                let by_sha_uri = format!("s3://{}/{}", bucket, by_sha_key);
                let output = Command::new("aws")
                    .args(["s3", "cp", &by_sha_uri, "-"])
                    .output()
                    .ok();

                if let Some(output) = output {
                    if output.status.success() {
                        content = String::from_utf8(output.stdout).ok();
                    }
                }
            }
        }

        // Fall back: legacy key (mutable)
        if content.is_none() {
            let s3_key = format!("datasets/{}", id.cache_filename());
            let s3_uri = format!("s3://{}/{}", bucket, s3_key);

            // Use aws s3 cp to download to stdout
            let output = Command::new("aws")
                .args(["s3", "cp", &s3_uri, "-"])
                .output()
                .map_err(|e| Error::InvalidInput(format!("Failed to run aws s3 cp: {}", e)))?;

            if output.status.success() {
                content = Some(String::from_utf8(output.stdout).map_err(|e| {
                    Error::InvalidInput(format!("S3 content not valid UTF-8: {}", e))
                })?);
            } else {
                return Err(Error::InvalidInput(format!(
                    "S3 download failed: {}",
                    String::from_utf8_lossy(&output.stderr)
                )));
            }
        }

        let Some(content) = content else {
            return Err(Error::InvalidInput("S3 download failed".to_string()));
        };

        // Best-effort: attempt to fetch per-dataset manifest entry.
        let manifest = self.download_manifest_entry_from_s3(bucket, id).ok();

        Ok((content, manifest))
    }

    /// Upload dataset to S3 cache bucket.
    ///
    /// Best effort - failures are logged but don't stop execution.
    #[cfg(feature = "eval")]
    fn upload_to_s3(
        &self,
        bucket: &str,
        id: DatasetId,
        content: &str,
        manifest_entry: &CacheManifestEntry,
    ) -> Result<()> {
        use std::io::Write;
        use std::process::{Command, Stdio};

        let s3_key = format!("datasets/{}", id.cache_filename());
        let s3_uri = format!("s3://{}/{}", bucket, s3_key);

        // Use aws s3 cp to upload from stdin
        let mut child = Command::new("aws")
            .args(["s3", "cp", "-", &s3_uri])
            .stdin(Stdio::piped())
            .stdout(Stdio::null())
            .stderr(Stdio::null())
            .spawn()
            .map_err(|e| Error::InvalidInput(format!("Failed to spawn aws s3 cp: {}", e)))?;

        if let Some(ref mut stdin) = child.stdin {
            let _ = stdin.write_all(content.as_bytes());
        }

        let status = child
            .wait()
            .map_err(|e| Error::InvalidInput(format!("Failed to wait for aws s3 cp: {}", e)))?;

        if !status.success() {
            return Err(Error::InvalidInput("S3 upload failed".to_string()));
        }

        // Also upload a content-addressed snapshot. This supports the "durable mirror" use case:
        // once uploaded, snapshots are stable even if upstream changes.
        let by_sha_key = format!(
            "datasets/by-sha256/{}/{}",
            manifest_entry.sha256,
            id.cache_filename()
        );
        let by_sha_uri = format!("s3://{}/{}", bucket, by_sha_key);

        let mut child = Command::new("aws")
            .args(["s3", "cp", "-", &by_sha_uri])
            .stdin(Stdio::piped())
            .stdout(Stdio::null())
            .stderr(Stdio::null())
            .spawn()
            .map_err(|e| Error::InvalidInput(format!("Failed to spawn aws s3 cp: {}", e)))?;

        if let Some(ref mut stdin) = child.stdin {
            let _ = stdin.write_all(content.as_bytes());
        }

        let _ = child.wait();

        // Update a pointer file for the latest snapshot.
        let pointer_key = format!("datasets/{}.latest.json", id.cache_filename());
        let pointer_uri = format!("s3://{}/{}", bucket, pointer_key);
        let pointer_json = serde_json::json!({
            "dataset": id.cache_filename(),
            "sha256": manifest_entry.sha256,
            "updated_at": chrono::Utc::now().to_rfc3339(),
            "source_url": manifest_entry.source_url,
            "resolved_url": manifest_entry.resolved_url,
            "file_size": manifest_entry.file_size,
        })
        .to_string();

        let mut child = Command::new("aws")
            .args(["s3", "cp", "-", &pointer_uri])
            .stdin(Stdio::piped())
            .stdout(Stdio::null())
            .stderr(Stdio::null())
            .spawn()
            .map_err(|e| Error::InvalidInput(format!("Failed to spawn aws s3 cp: {}", e)))?;

        if let Some(ref mut stdin) = child.stdin {
            let _ = stdin.write_all(pointer_json.as_bytes());
        }

        let _ = child.wait();

        // Upload a sidecar manifest entry (JSON). This makes the S3 cache self-describing.
        let manifest_key = format!("datasets/{}.manifest.json", id.cache_filename());
        let manifest_uri = format!("s3://{}/{}", bucket, manifest_key);
        let json = serde_json::to_string_pretty(manifest_entry)
            .map_err(|e| Error::InvalidInput(format!("Failed to serialize S3 manifest: {}", e)))?;

        let mut child = Command::new("aws")
            .args(["s3", "cp", "-", &manifest_uri])
            .stdin(Stdio::piped())
            .stdout(Stdio::null())
            .stderr(Stdio::null())
            .spawn()
            .map_err(|e| Error::InvalidInput(format!("Failed to spawn aws s3 cp: {}", e)))?;

        if let Some(ref mut stdin) = child.stdin {
            let _ = stdin.write_all(json.as_bytes());
        }

        let status = child
            .wait()
            .map_err(|e| Error::InvalidInput(format!("Failed to wait for aws s3 cp: {}", e)))?;

        if status.success() {
            Ok(())
        } else {
            Err(Error::InvalidInput("S3 manifest upload failed".to_string()))
        }
    }

    /// Best-effort: download the sidecar manifest entry for a dataset from S3.
    #[cfg(feature = "eval")]
    fn download_manifest_entry_from_s3(
        &self,
        bucket: &str,
        id: DatasetId,
    ) -> Result<CacheManifestEntry> {
        use std::process::Command;

        let manifest_key = format!("datasets/{}.manifest.json", id.cache_filename());
        let manifest_uri = format!("s3://{}/{}", bucket, manifest_key);

        let output = Command::new("aws")
            .args(["s3", "cp", &manifest_uri, "-"])
            .output()
            .map_err(|e| Error::InvalidInput(format!("Failed to run aws s3 cp: {}", e)))?;

        if !output.status.success() {
            return Err(Error::InvalidInput(
                "S3 manifest download failed".to_string(),
            ));
        }

        let json = String::from_utf8(output.stdout)
            .map_err(|e| Error::InvalidInput(format!("S3 manifest not valid UTF-8: {}", e)))?;

        serde_json::from_str(&json)
            .map_err(|e| Error::InvalidInput(format!("Failed to parse S3 manifest JSON: {}", e)))
    }

    /// Download dataset from source with retry logic and pagination support.
    ///
    /// Implements exponential backoff retry strategy:
    /// - 3 retries maximum
    /// - Initial delay: 1 second
    /// - Exponential backoff: 2^attempt seconds
    /// - Max delay: 10 seconds
    ///
    /// For HuggingFace datasets-server API, automatically paginates to download full dataset.
    #[cfg(feature = "eval")]
    fn download_with_resolved_url(&self, id: DatasetId) -> Result<(String, String)> {
        let url = id.download_url().to_string();

        fn env_single_csv(keys: &[&str]) -> Option<String> {
            for &k in keys {
                let Ok(raw) = std::env::var(k) else {
                    continue;
                };
                let mut parts = raw
                    .split(',')
                    .map(|s| s.trim().to_ascii_lowercase())
                    .filter(|s| !s.is_empty())
                    .collect::<Vec<_>>();
                parts.sort();
                parts.dedup();
                if parts.len() == 1 {
                    return Some(parts[0].clone());
                }
            }
            None
        }

        // Check if URL is empty (dataset not available for download)
        if url.is_empty() {
            let access_status = id.access_status();
            let notes = id.notes();
            let mirror_url = id.mirror_url();

            let mut error_msg = format!(
                "Dataset '{}' ({:?}) has no download URL available.",
                id.name(),
                id
            );

            // Add access status information
            match access_status {
                super::dataset_registry::DatasetAccessibility::Public => {
                    error_msg.push_str("\n\nStatus: Public (but URL not configured)");
                }
                super::dataset_registry::DatasetAccessibility::HuggingFace => {
                    if let Some(hf_id) = id.hf_id() {
                        error_msg.push_str(&format!(
                            "\n\nStatus: Available on HuggingFace\nDataset ID: {}",
                            hf_id
                        ));
                    } else {
                        error_msg
                            .push_str("\n\nStatus: Available on HuggingFace (ID not configured)");
                    }
                }
                super::dataset_registry::DatasetAccessibility::Local => {
                    error_msg.push_str("\n\nStatus: Available locally (check testdata/ or cache)");
                }
                super::dataset_registry::DatasetAccessibility::Registration => {
                    error_msg
                        .push_str("\n\nStatus: Requires registration (e.g., LDC for academics)");
                }
                super::dataset_registry::DatasetAccessibility::ContactAuthors => {
                    error_msg.push_str("\n\nStatus: Contact dataset authors for access");
                }
                super::dataset_registry::DatasetAccessibility::NotYetReleased => {
                    error_msg.push_str("\n\nStatus: Not yet publicly released");
                }
                super::dataset_registry::DatasetAccessibility::DependsOnOther => {
                    if let Some(dep) = id.depends_on() {
                        error_msg
                            .push_str(&format!("\n\nStatus: Depends on another dataset: {}", dep));
                    } else {
                        error_msg.push_str("\n\nStatus: Depends on another dataset");
                    }
                }
                super::dataset_registry::DatasetAccessibility::Deprecated => {
                    error_msg.push_str("\n\nStatus: Deprecated or no longer available");
                }
            }

            if let Some(note) = notes {
                error_msg.push_str(&format!("\n\nNotes: {}", note));
            }

            if let Some(mirror) = mirror_url {
                error_msg.push_str(&format!("\n\nMirror URL: {}", mirror));
            }

            return Err(Error::InvalidInput(error_msg));
        }

        // For multilingual datasets hosted on HuggingFace with per-language configs (e.g. WikiANN),
        // let callers bias the HF "config" selection via `ANNO_MUXER_PIN_LANG`.
        //
        // This keeps muxer facet pins honest: a `.lang=de` run should ideally download a German
        // config, not a random/first config.
        let preferred_hf_config: Option<String> = if id.language().eq_ignore_ascii_case("mul") {
            // Explicit override wins if set.
            env_single_csv(&["ANNO_HF_DATASET_CONFIG"])
                .or_else(|| env_single_csv(&["ANNO_MUXER_PIN_LANG", "ANNO_MUXER_FILTER_LANG"]))
        } else {
            env_single_csv(&["ANNO_HF_DATASET_CONFIG"])
        };

        // If the registry marks this dataset as HuggingFace-accessible and provides an HF id,
        // prefer fetching via datasets-server (even if the registry `url` is a paper/GitHub homepage).
        if id.access_status() == super::dataset_registry::DatasetAccessibility::HuggingFace {
            if let Some(hf_ds) = id.hf_id().map(|s| s.to_string()) {
                if let Ok((config, split)) =
                    self.resolve_hf_config_split_prefer(&hf_ds, preferred_hf_config.as_deref())
                {
                    let base = Self::hf_rows_url(&hf_ds, &config, &split);
                    if std::env::var("ANNO_MUXER_VERBOSE")
                        .ok()
                        .is_some_and(|v| v == "1" || v.eq_ignore_ascii_case("true"))
                    {
                        eprintln!(
                            "dataset-loader: hf dataset={} config={} split={} (preferred={:?})",
                            hf_ds, config, split, preferred_hf_config
                        );
                    }
                    match self.download_hf_dataset_paginated(id, &base) {
                        Ok(content) => return Ok((content, base)),
                        Err(_e) => {
                            // Fall back to downloading a raw file from the dataset repo.
                            if let Ok((content, resolved)) =
                                self.download_hf_dataset_file_from_hub(&hf_ds)
                            {
                                return Ok((content, resolved));
                            }
                        }
                    }
                }
            }
        }

        // HuggingFace dataset pages are common in the registry. Convert them into a
        // datasets-server API download so we can actually fetch data (and avoid caching HTML).
        //
        // Example:
        // - page:  https://huggingface.co/datasets/KevinSpaghetti/cadec
        // - API:   https://datasets-server.huggingface.co/rows?dataset=KevinSpaghetti%2Fcadec&...
        if url.contains("huggingface.co/datasets/") {
            // Prefer the registry's `hf_id` when available (more reliable than parsing the URL).
            let hf_ds = id
                .hf_id()
                .map(|s| s.to_string())
                .or_else(|| Self::extract_hf_dataset_name(&url));

            if let Some(hf_ds) = hf_ds {
                match self.resolve_hf_config_split_prefer(&hf_ds, preferred_hf_config.as_deref()) {
                    Ok((config, split)) => {
                        let base = Self::hf_rows_url(&hf_ds, &config, &split);
                        if std::env::var("ANNO_MUXER_VERBOSE")
                            .ok()
                            .is_some_and(|v| v == "1" || v.eq_ignore_ascii_case("true"))
                        {
                            eprintln!(
                                "dataset-loader: hf dataset={} config={} split={} (preferred={:?})",
                                hf_ds, config, split, preferred_hf_config
                            );
                        }
                        match self.download_hf_dataset_paginated(id, &base) {
                            Ok(content) => return Ok((content, base)),
                            Err(e) => {
                                // Some HF datasets don't support datasets-server row export.
                                // Fall back to downloading a raw file from the dataset repo.
                                if let Ok((content, resolved)) =
                                    self.download_hf_dataset_file_from_hub(&hf_ds)
                                {
                                    return Ok((content, resolved));
                                }
                                return Err(e);
                            }
                        }
                    }
                    Err(_e) => {
                        // If the datasets-server endpoint is unavailable (rate limits, transient issues),
                        // prefer falling back to the hub-file path rather than downloading HTML.
                        if let Ok((content, resolved)) =
                            self.download_hf_dataset_file_from_hub(&hf_ds)
                        {
                            return Ok((content, resolved));
                        }
                    }
                }
            }
        }

        // Check if this is a HuggingFace datasets-server API URL
        if url.contains("datasets-server.huggingface.co/rows") {
            match self.download_hf_dataset_paginated(id, &url) {
                Ok(content) => return Ok((content, url)),
                Err(e) => {
                    // Some datasets fail row export (422). If we can infer the HF dataset id,
                    // fall back to the hub file download path.
                    if let Some(hf_ds) = Self::hf_dataset_from_rows_url(&url) {
                        if let Ok((content, resolved)) =
                            self.download_hf_dataset_file_from_hub(&hf_ds)
                        {
                            return Ok((content, resolved));
                        }
                    }
                    return Err(e);
                }
            }
        }

        // Regular download with retry logic
        const MAX_RETRIES: u32 = 3;
        const INITIAL_DELAY_SECS: u64 = 1;

        let mut last_error = None;

        for attempt in 0..=MAX_RETRIES {
            match self.download_attempt(&url) {
                Ok(content) => {
                    // Checksum validation removed - registry doesn't provide expected_checksum
                    // Future: Add checksum validation if registry provides it

                    return Ok((content, url.clone()));
                }
                Err(e) => {
                    last_error = Some(e);
                    if attempt < MAX_RETRIES {
                        let delay_secs = (INITIAL_DELAY_SECS * (1 << attempt)).min(10);
                        log::warn!(
                            "Download attempt {} failed for {}, retrying in {}s...",
                            attempt + 1,
                            &url,
                            delay_secs
                        );
                        std::thread::sleep(std::time::Duration::from_secs(delay_secs));
                    }
                }
            }
        }

        Err(last_error.unwrap_or_else(|| {
            Error::InvalidInput(format!(
                "Failed to download {} after {} retries. \
                 Check network connection and try again. \
                 URL: {}",
                id.name(),
                MAX_RETRIES + 1,
                &url
            ))
        }))
    }

    /// Extract `<org>/<dataset>` from `https://huggingface.co/datasets/<org>/<dataset>(/...)`.
    #[cfg(feature = "eval")]
    fn extract_hf_dataset_name(url: &str) -> Option<String> {
        let marker = "huggingface.co/datasets/";
        let idx = url.find(marker)? + marker.len();
        let rest = &url[idx..];
        let rest = rest.split('?').next().unwrap_or(rest);
        let rest = rest.trim_matches('/');
        let mut parts = rest.split('/');
        let org = parts.next()?;
        let name = parts.next()?;
        Some(format!("{}/{}", org, name))
    }

    #[cfg(feature = "eval")]
    fn url_encode_component(s: &str) -> String {
        // Minimal encoding for HF datasets-server query parameters.
        // In practice we mainly need `/` -> `%2F`.
        s.replace('/', "%2F").replace(' ', "%20")
    }

    #[cfg(feature = "eval")]
    fn hf_rows_url(dataset: &str, config: &str, split: &str) -> String {
        format!(
            "https://datasets-server.huggingface.co/rows?dataset={}&config={}&split={}&offset=0&length=100",
            Self::url_encode_component(dataset),
            Self::url_encode_component(config),
            Self::url_encode_component(split),
        )
    }

    /// Resolve a usable (config, split) pair for a HF dataset via datasets-server, with an optional
    /// preferred config.
    #[cfg(feature = "eval")]
    fn resolve_hf_config_split_prefer(
        &self,
        dataset: &str,
        preferred_config: Option<&str>,
    ) -> Result<(String, String)> {
        let url = format!(
            "https://datasets-server.huggingface.co/splits?dataset={}",
            Self::url_encode_component(dataset)
        );
        let response = ureq::get(&url)
            .timeout(std::time::Duration::from_secs(30))
            .call()
            .map_err(|e| Error::InvalidInput(format!("Failed to query HF splits: {}", e)))?;

        if response.status() != 200 {
            return Err(Error::InvalidInput(format!(
                "HF splits query returned HTTP {} for dataset {}",
                response.status(),
                dataset
            )));
        }

        let body = response.into_string().map_err(|e| {
            Error::InvalidInput(format!("Failed to read HF splits response: {}", e))
        })?;

        let json: serde_json::Value = serde_json::from_str(&body).map_err(|e| {
            Error::InvalidInput(format!("Invalid JSON from HF splits endpoint: {}", e))
        })?;

        let splits = json
            .get("splits")
            .and_then(|v| v.as_array())
            .ok_or_else(|| {
                Error::InvalidInput("HF splits response missing `splits`".to_string())
            })?;

        // Prefer smaller/eval splits if present, otherwise pick the first split.
        // If `preferred_config` is provided, try to pick a split within that config first.
        let mut chosen = None;
        let prefer = preferred_config.map(|s| s.trim()).filter(|s| !s.is_empty());

        for pass in 0..2 {
            for s in splits {
                let config = s.get("config").and_then(|v| v.as_str());
                let split = s.get("split").and_then(|v| v.as_str());
                if let (Some(config), Some(split)) = (config, split) {
                    // Pass 0: only consider preferred config. Pass 1: consider any config.
                    if pass == 0 {
                        if let Some(p) = prefer {
                            if config != p {
                                continue;
                            }
                        } else {
                            // No preference: skip pass 0.
                            break;
                        }
                    }

                    if split == "test" {
                        chosen = Some((config.to_string(), split.to_string()));
                        break;
                    }
                    if chosen.is_none() {
                        chosen = Some((config.to_string(), split.to_string()));
                    }
                }
            }
            if chosen.is_some() {
                break;
            }
        }

        chosen.ok_or_else(|| {
            Error::InvalidInput(format!(
                "HF splits endpoint returned no usable (config, split) for dataset {}",
                dataset
            ))
        })
    }

    /// Best-effort fallback: download a raw dataset file from the HuggingFace Hub.
    ///
    /// This is a fallback for datasets that do not support the datasets-server row export.
    #[cfg(feature = "eval")]
    fn download_hf_dataset_file_from_hub(&self, dataset: &str) -> Result<(String, String)> {
        // Example API: https://huggingface.co/api/datasets/coref-data/preco_raw
        let api_url = format!("https://huggingface.co/api/datasets/{}", dataset);
        let response = ureq::get(&api_url)
            .timeout(std::time::Duration::from_secs(30))
            .call()
            .map_err(|e| {
                Error::InvalidInput(format!(
                    "Failed to query HuggingFace dataset metadata for {}: {}",
                    dataset, e
                ))
            })?;

        if response.status() != 200 {
            return Err(Error::InvalidInput(format!(
                "HuggingFace dataset metadata request returned HTTP {} for {}",
                response.status(),
                dataset
            )));
        }

        let body = response.into_string().map_err(|e| {
            Error::InvalidInput(format!(
                "Failed to read HuggingFace dataset metadata: {}",
                e
            ))
        })?;
        let json: serde_json::Value = serde_json::from_str(&body).map_err(|e| {
            Error::InvalidInput(format!(
                "Invalid JSON from HuggingFace dataset metadata: {}",
                e
            ))
        })?;

        let siblings = json
            .get("siblings")
            .and_then(|v| v.as_array())
            .ok_or_else(|| {
                Error::InvalidInput("HF dataset metadata missing `siblings`".to_string())
            })?;

        // Prefer smaller / eval-friendly splits, but fall back to whatever exists.
        //
        // Many HF dataset repos store raw source files (CoNLL/TSV/etc.) instead of JSONL,
        // often under subdirectories. We handle both.
        let preferred = [
            // JSONL (common)
            "dev.jsonl",
            "validation.jsonl",
            "test.jsonl",
            "train.jsonl",
        ];
        let mut chosen: Option<String> = None;

        for want in preferred {
            if siblings.iter().any(|s| {
                s.get("rfilename")
                    .and_then(|v| v.as_str())
                    .is_some_and(|f| f == want)
            }) {
                chosen = Some(want.to_string());
                break;
            }
        }

        if chosen.is_none() {
            // Otherwise, pick a file we can plausibly parse (allow subdirectories).
            //
            // Priority:
            // - contains split hint: test > dev/valid > train
            // - extension: jsonl > conll/bio/iob/tsv/txt
            //
            // This is still best-effort; the parser selection is handled elsewhere via the dataset id.
            fn split_rank(name: &str) -> u8 {
                let n = name.to_lowercase();
                if n.contains("test") {
                    0
                } else if n.contains("dev") || n.contains("valid") || n.contains("validation") {
                    1
                } else if n.contains("train") {
                    2
                } else {
                    3
                }
            }
            fn ext_rank(name: &str) -> u8 {
                let n = name.to_lowercase();
                if n.ends_with(".jsonl") {
                    0
                } else if n.ends_with(".conll")
                    || n.ends_with(".bio")
                    || n.ends_with(".iob")
                    || n.ends_with(".iob2")
                {
                    1
                } else if n.ends_with(".tsv") {
                    2
                } else if n.ends_with(".txt") {
                    3
                } else {
                    9
                }
            }

            let mut candidates: Vec<String> = siblings
                .iter()
                .filter_map(|s| {
                    s.get("rfilename")
                        .and_then(|v| v.as_str())
                        .map(|f| f.to_string())
                })
                .filter(|f| ext_rank(f) < 9)
                .collect();

            candidates.sort_by(|a, b| {
                (split_rank(a), ext_rank(a), a.len()).cmp(&(split_rank(b), ext_rank(b), b.len()))
            });

            chosen = candidates.first().cloned();
        }

        let Some(filename) = chosen else {
            return Err(Error::InvalidInput(format!(
                "No downloadable .jsonl file discovered for HF dataset {}",
                dataset
            )));
        };

        // Download file via resolve (use main; can be made content-addressed later via `sha`).
        let file_url = format!(
            "https://huggingface.co/datasets/{}/resolve/main/{}",
            dataset, filename
        );

        // Best-effort: avoid downloading huge files if a max byte limit is configured.
        if let Some(limit) = Self::max_download_bytes() {
            if let Ok(resp) = ureq::head(&file_url)
                .timeout(std::time::Duration::from_secs(30))
                .call()
            {
                if resp.status() == 200 {
                    if let Some(len) = resp
                        .header("Content-Length")
                        .and_then(|s| s.parse::<u64>().ok())
                    {
                        if len > limit {
                            return Err(Error::InvalidInput(format!(
                                "Download rejected ({} bytes > ANNO_MAX_DOWNLOAD_BYTES={} bytes) from {}",
                                len, limit, file_url
                            )));
                        }
                    }
                }
            }
        }

        let content = self.download_attempt(&file_url)?;
        Ok((content, file_url))
    }

    /// Placeholder when `eval` is disabled.
    #[cfg(not(feature = "eval"))]
    #[allow(dead_code)]
    fn download_hf_dataset_file_from_hub(&self, _dataset: &str) -> Result<(String, String)> {
        Err(Error::InvalidInput(
            "HuggingFace file fallback requires feature `eval`".to_string(),
        ))
    }

    /// Download HuggingFace dataset with pagination support.
    ///
    /// HF datasets-server API limits responses to 100 rows by default.
    /// This function automatically paginates to download the full dataset.
    ///
    /// For datasets available on HuggingFace Hub, can also use hf-hub crate
    /// for direct file downloads (faster, no pagination needed).
    #[cfg(feature = "eval")]
    fn download_hf_dataset_paginated(&self, id: DatasetId, base_url: &str) -> Result<String> {
        // Try hf-hub direct download first (if available and dataset is on HF)
        #[cfg(feature = "onnx")] // hf-hub is available with onnx feature
        {
            if let Ok(content) = self.try_hf_hub_download(id) {
                return Ok(content);
            }
        }

        // Fall back to paginated API download
        // HuggingFace datasets-server API limits to 100 rows per request
        const PAGE_SIZE: usize = 100;
        let mut all_rows = Vec::new();
        let mut features = None;
        let mut offset: usize = 0;
        let mut total_rows = None;

        log::info!(
            "Downloading {} with pagination (page size: {})",
            id.name(),
            PAGE_SIZE
        );

        loop {
            // Build paginated URL
            let url = if base_url.contains("offset=") {
                // Replace existing offset parameter
                let prev_offset = offset.saturating_sub(PAGE_SIZE);
                base_url
                    .replace(
                        &format!("offset={}", prev_offset),
                        &format!("offset={}", offset),
                    )
                    .replace("length=100", &format!("length={}", PAGE_SIZE))
            } else {
                // Add pagination parameters
                let separator = if base_url.contains('?') { "&" } else { "?" };
                format!(
                    "{}{}offset={}&length={}",
                    base_url, separator, offset, PAGE_SIZE
                )
            };

            match self.download_attempt(&url) {
                Ok(content) => {
                    let parsed: serde_json::Value =
                        serde_json::from_str(&content).map_err(|e| {
                            Error::InvalidInput(format!("Invalid JSON response: {}", e))
                        })?;

                    // Extract features (only from first page)
                    if features.is_none() {
                        features = parsed.get("features").cloned();
                    }

                    // Extract total number of rows (if available)
                    if total_rows.is_none() {
                        total_rows = parsed
                            .get("num_rows_total")
                            .and_then(|v| v.as_u64())
                            .map(|n| n as usize);
                    }

                    // Extract rows from this page
                    if let Some(rows) = parsed.get("rows").and_then(|v| v.as_array()) {
                        if rows.is_empty() {
                            break; // No more rows
                        }
                        all_rows.extend_from_slice(rows);
                        log::debug!(
                            "Downloaded {} rows (total so far: {})",
                            rows.len(),
                            all_rows.len()
                        );

                        // Check if we've got all rows
                        if let Some(total) = total_rows {
                            if all_rows.len() >= total {
                                break;
                            }
                        } else if rows.len() < PAGE_SIZE {
                            // No total available, but got fewer rows than requested = last page
                            break;
                        }

                        offset += PAGE_SIZE;
                    } else {
                        // No rows in response, might be error or empty dataset
                        break;
                    }
                }
                Err(e) => {
                    // If we got some rows, return partial dataset with warning
                    if !all_rows.is_empty() {
                        log::warn!(
                            "Failed to download full {} dataset (got {} rows before error: {}). \
                             Returning partial dataset.",
                            id.name(),
                            all_rows.len(),
                            e
                        );
                        break;
                    } else {
                        return Err(e);
                    }
                }
            }

            // Safety limit: prevent infinite loops
            if offset > 1_000_000 {
                log::warn!(
                    "Reached safety limit (1M rows) for {}. Returning partial dataset ({} rows).",
                    id.name(),
                    all_rows.len()
                );
                break;
            }
        }

        // Reconstruct full API response format
        let mut response: serde_json::Value = serde_json::json!({
            "rows": all_rows,
        });

        if let Some(features_val) = features {
            response["features"] = features_val;
        }

        if let Some(total) = total_rows {
            response["num_rows_total"] = serde_json::json!(total);
        }

        serde_json::to_string(&response).map_err(|e| {
            Error::InvalidInput(format!("Failed to serialize paginated response: {}", e))
        })
    }

    /// Try downloading dataset directly from HuggingFace Hub using hf-hub crate.
    ///
    /// This is faster than paginated API calls for datasets available on HF Hub.
    /// Returns Ok(content) if successful, Err if not available or hf-hub not enabled.
    ///
    /// Uses `HF_TOKEN` environment variable if set for accessing gated datasets.
    #[cfg(all(feature = "eval", feature = "onnx"))]
    fn try_hf_hub_download(&self, id: DatasetId) -> Result<String> {
        use hf_hub::api::sync::{Api, ApiBuilder};

        // Map dataset IDs to HuggingFace dataset names and file paths
        let (dataset_name, file_path) = match id {
            DatasetId::MultiNERD => ("Babelscape/multinerd", "test/test_en.jsonl"),
            DatasetId::TweetNER7 => ("tner/tweetner7", "dataset/2020.dev.json"),
            DatasetId::BroadTwitterCorpus => ("GateNLP/broad_twitter_corpus", "test/a.conll"),
            DatasetId::CADEC => ("KevinSpaghetti/cadec", "data/test.jsonl"),
            DatasetId::PreCo => ("coref-data/preco", "data/test.jsonl"),
            // Gated datasets that require HF_TOKEN
            DatasetId::MultiCoNER => ("MultiCoNER/multiconer_v1", "en/test.conll"),
            DatasetId::MultiCoNERv2 => ("MultiCoNER/multiconer_v2", "en/test.conll"),
            _ => {
                return Err(Error::InvalidInput(
                    "Dataset not available via hf-hub".to_string(),
                ))
            }
        };

        // Load .env for HF_TOKEN if not already set
        anno::env::load_dotenv();

        // Use HF_TOKEN from environment if available (for gated datasets)
        let api = if let Some(token) = anno::env::hf_token() {
            ApiBuilder::new()
                .with_token(Some(token))
                .build()
                .map_err(|e| {
                    Error::InvalidInput(format!(
                        "Failed to initialize HuggingFace API with token: {}",
                        e
                    ))
                })?
        } else {
            Api::new().map_err(|e| {
                Error::InvalidInput(format!("Failed to initialize HuggingFace API: {}", e))
            })?
        };

        let repo = api.dataset(dataset_name.to_string());
        let file_path_buf = repo.get(file_path).map_err(|e| {
            Error::InvalidInput(format!(
                "Failed to download {} from HuggingFace Hub: {}. \
                 Falling back to HTTP download.",
                file_path, e
            ))
        })?;

        std::fs::read_to_string(&file_path_buf)
            .map_err(|e| Error::InvalidInput(format!("Failed to read downloaded file: {}", e)))
    }

    /// Placeholder for when hf-hub is not available.
    #[cfg(not(all(feature = "eval", feature = "onnx")))]
    #[allow(dead_code)] // Part of trait interface, may be unused in some feature combinations
    fn try_hf_hub_download(&self, _id: DatasetId) -> Result<String> {
        Err(Error::InvalidInput("hf-hub not available".to_string()))
    }

    /// Single download attempt with retry logic.
    ///
    /// Retries up to 3 times with exponential backoff for transient failures (timeouts, 5xx errors).
    #[cfg(feature = "eval")]
    fn download_attempt(&self, url: &str) -> Result<String> {
        const MAX_RETRIES: usize = 3;
        const INITIAL_TIMEOUT_SECS: u64 = 30;
        const MAX_TIMEOUT_SECS: u64 = 120;

        let mut last_error = None;

        for attempt in 0..=MAX_RETRIES {
            let timeout_secs = (INITIAL_TIMEOUT_SECS * (1 << attempt.min(2))).min(MAX_TIMEOUT_SECS);

            match ureq::get(url)
                .timeout(std::time::Duration::from_secs(timeout_secs))
                .call()
            {
                Ok(response) => {
                    if response.status() == 200 {
                        // Success - read content
                        let content = response.into_string().map_err(|e| {
                            Error::InvalidInput(format!(
                                "Failed to read response from {}: {}. \
                                 Response may be too large or corrupted.",
                                url, e
                            ))
                        })?;

                        // Heuristic guardrail: many registry URLs are dataset homepages. If we fetch HTML,
                        // treat it as a download failure so we don't silently cache garbage and then parse
                        // "0 sentences".
                        let lower = content
                            .chars()
                            .take(2048)
                            .collect::<String>()
                            .to_lowercase();
                        if lower.contains("<html") || lower.contains("<!doctype html") {
                            return Err(Error::InvalidInput(format!(
                                "Downloaded HTML from {}. This URL looks like a webpage, not a raw dataset file.",
                                url
                            )));
                        }

                        return Ok(content);
                    }

                    let status = response.status();
                    let body = response.into_string().unwrap_or_default();
                    let body = body.trim();
                    let body_preview = if body.len() > 800 {
                        format!("{}", &body[..800])
                    } else {
                        body.to_string()
                    };

                    // Retry on 5xx server errors (transient)
                    if (500..600).contains(&status) && attempt < MAX_RETRIES {
                        let wait_ms = 1000 * (1 << attempt); // Exponential backoff: 1s, 2s, 4s
                        log::debug!(
                            "Server error {} downloading {} (attempt {}/{}), retrying in {}ms...",
                            status,
                            url,
                            attempt + 1,
                            MAX_RETRIES + 1,
                            wait_ms
                        );
                        std::thread::sleep(std::time::Duration::from_millis(wait_ms));
                        last_error = Some(format!(
                            "HTTP {} downloading {}. Server returned error status. {}{}",
                            status,
                            url,
                            if body_preview.is_empty() {
                                ""
                            } else {
                                "Response body: "
                            },
                            body_preview
                        ));
                        continue;
                    }

                    // Non-retryable error (4xx client errors)
                    return Err(Error::InvalidInput(format!(
                        "HTTP {} downloading {}. \
                         Server returned error status. \
                         Dataset may be temporarily unavailable or URL changed. {}{}",
                        status,
                        url,
                        if body_preview.is_empty() {
                            ""
                        } else {
                            "Response body: "
                        },
                        body_preview
                    )));
                }
                Err(ureq::Error::Transport(e)) => {
                    // Network errors (timeouts, connection failures) - retry
                    let error_msg = format!("{}", e);
                    let is_timeout =
                        error_msg.contains("timeout") || error_msg.contains("timed out");

                    if is_timeout && attempt < MAX_RETRIES {
                        let wait_ms = 1000 * (1 << attempt); // Exponential backoff
                        log::debug!(
                            "Timeout downloading {} (attempt {}/{}), retrying in {}ms...",
                            url,
                            attempt + 1,
                            MAX_RETRIES + 1,
                            wait_ms
                        );
                        std::thread::sleep(std::time::Duration::from_millis(wait_ms));
                        last_error = Some(error_msg);
                        continue;
                    }

                    // Final attempt failed or non-timeout error
                    return Err(Error::InvalidInput(format!(
                        "Network error downloading {}: {}. \
                         Check your internet connection and try again. \
                         {}",
                        url,
                        error_msg,
                        if attempt > 0 {
                            format!("(Failed after {} retries)", attempt)
                        } else {
                            String::new()
                        }
                    )));
                }
                Err(e) => {
                    // Other errors (non-retryable)
                    let error_msg = format!("{}", e);
                    return Err(Error::InvalidInput(format!(
                        "Error downloading {}: {}. \
                         Check your internet connection and try again.",
                        url, error_msg
                    )));
                }
            }
        }

        // All retries exhausted
        Err(Error::InvalidInput(format!(
            "Failed to download {} after {} attempts. Last error: {}",
            url,
            MAX_RETRIES + 1,
            last_error.unwrap_or_else(|| "unknown error".to_string())
        )))
    }

    /// Single download attempt returning raw bytes (for binary formats like ZIP).
    ///
    /// Same retry/backoff logic as `download_attempt` but reads response as bytes.
    #[cfg(feature = "eval")]
    fn download_attempt_bytes(&self, url: &str) -> Result<Vec<u8>> {
        const MAX_RETRIES: usize = 3;
        const INITIAL_TIMEOUT_SECS: u64 = 30;
        const MAX_TIMEOUT_SECS: u64 = 120;
        const MAX_BYTES: usize = 50 * 1024 * 1024; // 50 MB limit

        let mut last_error = None;

        for attempt in 0..=MAX_RETRIES {
            let timeout_secs = (INITIAL_TIMEOUT_SECS * (1 << attempt.min(2))).min(MAX_TIMEOUT_SECS);

            match ureq::get(url)
                .timeout(std::time::Duration::from_secs(timeout_secs))
                .call()
            {
                Ok(response) => {
                    if response.status() == 200 {
                        use std::io::Read as _;
                        let mut bytes = Vec::new();
                        response
                            .into_reader()
                            .take(MAX_BYTES as u64)
                            .read_to_end(&mut bytes)
                            .map_err(|e| {
                                Error::InvalidInput(format!(
                                    "Failed to read bytes from {}: {}",
                                    url, e
                                ))
                            })?;
                        return Ok(bytes);
                    }

                    let status = response.status();
                    if (500..600).contains(&status) && attempt < MAX_RETRIES {
                        let wait_ms = 1000 * (1 << attempt);
                        std::thread::sleep(std::time::Duration::from_millis(wait_ms));
                        last_error = Some(format!("HTTP {} from {}", status, url));
                        continue;
                    }

                    return Err(Error::InvalidInput(format!(
                        "HTTP {} downloading binary from {}",
                        status, url
                    )));
                }
                Err(ureq::Error::Transport(e)) => {
                    let msg = format!("{}", e);
                    if (msg.contains("timeout") || msg.contains("timed out"))
                        && attempt < MAX_RETRIES
                    {
                        let wait_ms = 1000 * (1 << attempt);
                        std::thread::sleep(std::time::Duration::from_millis(wait_ms));
                        last_error = Some(msg);
                        continue;
                    }
                    return Err(Error::InvalidInput(format!(
                        "Network error downloading {}: {}",
                        url, msg
                    )));
                }
                Err(e) => {
                    return Err(Error::InvalidInput(format!(
                        "Error downloading {}: {}",
                        url, e
                    )));
                }
            }
        }

        Err(Error::InvalidInput(format!(
            "Failed to download {} after {} attempts. Last error: {}",
            url,
            MAX_RETRIES + 1,
            last_error.unwrap_or_else(|| "unknown".to_string())
        )))
    }

    /// Compute SHA256 checksum of content.
    #[cfg(feature = "eval")]
    fn compute_sha256(&self, content: &str) -> String {
        #[cfg(feature = "eval")]
        {
            use sha2::{Digest, Sha256};
            let mut hasher = Sha256::new();
            hasher.update(content.as_bytes());
            format!("{:x}", hasher.finalize())
        }
        #[cfg(not(feature = "eval"))]
        {
            // Fallback if sha2 not available
            use std::collections::hash_map::DefaultHasher;
            use std::hash::{Hash, Hasher};
            let mut hasher = DefaultHasher::new();
            content.hash(&mut hasher);
            format!("{:x}", hasher.finish())
        }
    }

    /// Get temporal metadata for a dataset if available.
    fn get_temporal_metadata(id: DatasetId) -> Option<TemporalMetadata> {
        match id {
            DatasetId::TweetNER7 => {
                // TweetNER7 has temporal entity recognition - use dataset creation date as cutoff
                Some(TemporalMetadata {
                    kb_version: None,                                // No KB linking in TweetNER7
                    temporal_cutoff: Some("2017-01-01".to_string()), // Approximate dataset creation
                    entity_creation_dates: None,                     // Would need entity linking
                })
            }
            DatasetId::BroadTwitterCorpus => {
                // BroadTwitterCorpus is stratified across times - use approximate cutoff
                Some(TemporalMetadata {
                    kb_version: None,
                    temporal_cutoff: Some("2018-01-01".to_string()), // Approximate
                    entity_creation_dates: None,
                })
            }
            DatasetId::BC5CDR
            | DatasetId::NCBIDisease
            | DatasetId::GENIA
            | DatasetId::AnatEM
            | DatasetId::BC2GM
            | DatasetId::BC4CHEMD => {
                // Biomedical datasets might have KB versions (UMLS, etc.)
                Some(TemporalMetadata {
                    kb_version: None,
                    temporal_cutoff: None,
                    entity_creation_dates: None,
                })
            }
            _ => None, // Most datasets don't have temporal metadata
        }
    }

    /// Parse content based on dataset format.
    ///
    /// This is intentionally `pub` (behind the `eval` module) to enable offline evaluation
    /// workflows and integration tests that want to exercise parsing without network I/O.
    ///
    /// It does **not** download or read from cache. For typical usage, prefer
    /// [`DatasetLoader::load_or_download`] / [`DatasetLoader::load`].
    pub fn parse_content_str(&self, content: &str, id: DatasetId) -> Result<LoadedDataset> {
        self.parse_content_impl(content, id)
    }

    /// Internal implementation for parsing a dataset payload.
    fn parse_content_impl(&self, content: &str, id: DatasetId) -> Result<LoadedDataset> {
        if content.trim().is_empty() {
            return Err(Error::InvalidInput(format!(
                "Dataset {:?} file is empty",
                id
            )));
        }

        let plan = LoadableDatasetId::parse_plan(id).ok_or_else(|| {
            Error::InvalidInput(format!("No parser configured for dataset {:?}", id))
        })?;

        let result = match plan {
            DatasetParsePlan::Conll => self.parse_conll(content, id),
            DatasetParsePlan::JsonlNer => self.parse_jsonl_ner(content, id),
            DatasetParsePlan::WikiannJson => self.parse_wikiann_json(content, id),
            DatasetParsePlan::TweetNer7 => self.parse_tweetner7(content, id),
            DatasetParsePlan::DocredJson => self.parse_docred(content, id),
            DatasetParsePlan::GoogleReCorpus => self.parse_google_re_corpus(content, id),
            DatasetParsePlan::ChisiecJson => self.parse_chisiec(content, id),
            DatasetParsePlan::CadecHybrid => {
                if self.is_hf_api_response(content) {
                    self.parse_cadec_hf_api(content, id)
                } else {
                    self.parse_cadec_jsonl(content, id)
                }
            }
            DatasetParsePlan::Bc5cdr => self.parse_bc5cdr(content, id),
            DatasetParsePlan::NcbiDisease => self.parse_ncbi_disease(content, id),
            DatasetParsePlan::GapTsv => self.parse_gap(content, id),
            DatasetParsePlan::PrecoJsonl => self.parse_preco_jsonl(content, id),
            DatasetParsePlan::Litbank => self.parse_litbank(content, id),
            DatasetParsePlan::EcbPlus => self.parse_ecb_plus(content, id),
            DatasetParsePlan::AfriSenti => self.parse_afrisenti(content, id),
            DatasetParsePlan::AfriQa => self.parse_afriqa(content, id),
            DatasetParsePlan::MasakhaNews => self.parse_masakhanews(content, id),
            DatasetParsePlan::Conllu => self.parse_conllu(content, id),
            DatasetParsePlan::AgNews => self.parse_agnews(content, id),
            DatasetParsePlan::Dbpedia14 => self.parse_dbpedia14(content, id),
            DatasetParsePlan::YahooAnswers => self.parse_yahoo_answers(content, id),
            DatasetParsePlan::Trec => self.parse_trec(content, id),
            DatasetParsePlan::TweetTopic => self.parse_tweettopic(content, id),
            DatasetParsePlan::Maven => self.parse_maven(content, id),
            DatasetParsePlan::MavenArg => self.parse_maven_arg(content, id),
            DatasetParsePlan::Casie => self.parse_casie(content, id),
            DatasetParsePlan::Rams => self.parse_rams(content, id),
            DatasetParsePlan::HfApiResponse => self.parse_hf_api_response(content, id),
            DatasetParsePlan::TsvNer => self.parse_tsv_ner(content, id),
            DatasetParsePlan::CsvNer => self.parse_csv_ner(content, id),
        }?;

        // Validate parsed dataset is not empty
        if result.sentences.is_empty() {
            return Err(Error::InvalidInput(format!(
                "Dataset {:?} parsed successfully but contains no sentences. \
                 This may indicate a parsing issue or empty dataset file.",
                id
            )));
        }

        Ok(result)
    }

    /// Check if content is HuggingFace datasets-server API response.
    fn is_hf_api_response(&self, content: &str) -> bool {
        // Check for HF datasets-server API response structure
        let trimmed = content.trim_start();
        trimmed.starts_with("{\"rows\":")
            || trimmed.starts_with("{\"features\":")
            || (trimmed.starts_with("{")
                && trimmed.contains("\"rows\":[")
                && trimmed.contains("\"features\":["))
    }

    /// Parse CoNLL/BIO format content.
    fn parse_conll(&self, content: &str, id: DatasetId) -> Result<LoadedDataset> {
        let mut sentences = Vec::new();
        let mut current_tokens = Vec::new();

        // Detect format: MIT datasets use TAB separator with TAG first
        let is_mit_format = matches!(id, DatasetId::MitMovie | DatasetId::MitRestaurant);

        for line in content.lines() {
            let line = line.trim();

            // Empty line = sentence boundary
            if line.is_empty() {
                if !current_tokens.is_empty() {
                    sentences.push(AnnotatedSentence {
                        tokens: std::mem::take(&mut current_tokens),
                        source_dataset: id,
                    });
                }
                continue;
            }

            // Skip document markers
            if line.starts_with("-DOCSTART-") {
                continue;
            }

            // Parse based on format
            let (text, ner_tag) = if is_mit_format {
                // MIT format: TAG\tword (tab-separated)
                let parts: Vec<&str> = line.split('\t').collect();
                if parts.len() >= 2 {
                    (parts[1].to_string(), parts[0].to_string())
                } else {
                    continue;
                }
            } else {
                // Standard CoNLL/BIO format (space-separated)
                let parts: Vec<&str> = line.split_whitespace().collect();
                if parts.is_empty() {
                    continue;
                }

                if parts.len() >= 4 {
                    // CoNLL-2003 format: word POS chunk NER
                    (parts[0].to_string(), parts[3].to_string())
                } else if parts.len() >= 2 {
                    // BIO format: word NER
                    (parts[0].to_string(), parts[parts.len() - 1].to_string())
                } else {
                    // Single column - assume O tag
                    (parts[0].to_string(), "O".to_string())
                }
            };

            // Normalize BIO tags: some corpora (e.g., WikiGold) use `I-XXX` even at the
            // beginning of an entity. Treat such `I-` tags as `B-` when they don't
            // continue an entity of the same type.
            let ner_tag = if let Some(label) = ner_tag.strip_prefix("I-") {
                let continues_same = current_tokens
                    .last()
                    .map(|t| t.ner_tag.as_str())
                    .is_some_and(|prev| {
                        (prev.starts_with("B-") || prev.starts_with("I-"))
                            && prev.get(2..).is_some_and(|prev_label| prev_label == label)
                    });

                if continues_same {
                    ner_tag
                } else {
                    format!("B-{}", label)
                }
            } else {
                ner_tag
            };

            current_tokens.push(AnnotatedToken { text, ner_tag });
        }

        // Don't forget last sentence
        if !current_tokens.is_empty() {
            sentences.push(AnnotatedSentence {
                tokens: current_tokens,
                source_dataset: id,
            });
        }

        if sentences.is_empty() {
            return Err(Error::InvalidInput(format!(
                "CoNLL file for {:?} contains no valid sentences",
                id
            )));
        }

        let now = chrono::Utc::now().to_rfc3339();

        Ok(LoadedDataset {
            id,
            sentences,
            loaded_at: now,
            source_url: id.download_url().to_string(),
            data_source: DataSource::LocalCache,
            temporal_metadata: Self::get_temporal_metadata(id),
            metadata: id.default_metadata(),
        })
    }

    /// Parse JSONL NER format (HuggingFace style, e.g., MultiNERD).
    ///
    /// Expected format: `{"tokens": ["word1", "word2"], "ner_tags": [0, 1, 0]}`
    fn parse_jsonl_ner(&self, content: &str, id: DatasetId) -> Result<LoadedDataset> {
        let mut sentences = Vec::new();

        // MultiNERD tag mapping (index -> label)
        let tag_labels = [
            "O", "B-PER", "I-PER", "B-ORG", "I-ORG", "B-LOC", "I-LOC", "B-ANIM", "I-ANIM", "B-BIO",
            "I-BIO", "B-CEL", "I-CEL", "B-DIS", "I-DIS", "B-EVE", "I-EVE", "B-FOOD", "I-FOOD",
            "B-INST", "I-INST", "B-MEDIA", "I-MEDIA", "B-MYTH", "I-MYTH", "B-PLANT", "I-PLANT",
            "B-TIME", "I-TIME", "B-VEHI", "I-VEHI",
        ];

        for line in content.lines() {
            let line = line.trim();
            if line.is_empty() {
                continue;
            }

            // Parse JSON line
            let parsed: serde_json::Value = match serde_json::from_str(line) {
                Ok(v) => v,
                Err(_) => continue, // Skip malformed lines
            };

            let tokens = match parsed.get("tokens").and_then(|v| v.as_array()) {
                Some(t) => t,
                None => continue,
            };

            let ner_tags = match parsed.get("ner_tags").and_then(|v| v.as_array()) {
                Some(t) => t,
                None => continue,
            };

            if tokens.len() != ner_tags.len() {
                continue; // Skip malformed entries
            }

            let mut annotated_tokens = Vec::new();
            for (token, tag) in tokens.iter().zip(ner_tags.iter()) {
                let text = token.as_str().unwrap_or("").to_string();
                let tag_idx = tag.as_u64().unwrap_or(0) as usize;
                let ner_tag = tag_labels.get(tag_idx).unwrap_or(&"O").to_string();
                annotated_tokens.push(AnnotatedToken { text, ner_tag });
            }

            if !annotated_tokens.is_empty() {
                sentences.push(AnnotatedSentence {
                    tokens: annotated_tokens,
                    source_dataset: id,
                });
            }
        }

        if sentences.is_empty() {
            return Err(Error::InvalidInput(format!(
                "JSONL NER file for {:?} contains no valid sentences",
                id
            )));
        }

        let now = chrono::Utc::now().to_rfc3339();
        Ok(LoadedDataset {
            id,
            sentences,
            loaded_at: now,
            source_url: id.download_url().to_string(),
            data_source: DataSource::LocalCache,
            temporal_metadata: Self::get_temporal_metadata(id),
            metadata: id.default_metadata(),
        })
    }

    /// Parse HuggingFace datasets-server API response.
    ///
    /// Expected format:
    /// ```json
    /// {
    ///   "features": [{"name": "tokens", ...}, {"name": "ner_tags", ...}],
    ///   "rows": [{"row_idx": 0, "row": {"tokens": [...], "ner_tags": [...]}}, ...]
    /// }
    /// ```
    fn parse_hf_api_response(&self, content: &str, id: DatasetId) -> Result<LoadedDataset> {
        let parsed: serde_json::Value = serde_json::from_str(content)
            .map_err(|e| Error::InvalidInput(format!("Failed to parse HF API response: {}", e)))?;

        let mut sentences = Vec::new();

        // Extract tag names from features if available (for integer tag mapping)
        let tag_names = self.extract_tag_names_from_features(&parsed);
        let class_names = self.extract_class_names_from_features(&parsed);

        let rows = parsed
            .get("rows")
            .and_then(|v| v.as_array())
            .ok_or_else(|| Error::InvalidInput("No 'rows' array in HF API response".to_string()))?;

        for row_obj in rows {
            let row = match row_obj.get("row") {
                Some(r) => r,
                None => continue,
            };

            // Primary path: token-level NER rows (`tokens` + `ner_tags`)
            if let (Some(tokens), Some(ner_tags)) = (
                row.get("tokens").and_then(|v| v.as_array()),
                row.get("ner_tags").and_then(|v| v.as_array()),
            ) {
                if tokens.len() != ner_tags.len() {
                    continue;
                }

                let mut annotated_tokens = Vec::new();
                for (token, tag) in tokens.iter().zip(ner_tags.iter()) {
                    let text = token.as_str().unwrap_or("").to_string();

                    // Handle both integer and string tags
                    let ner_tag = if let Some(tag_idx) = tag.as_u64() {
                        // Integer tag - map using feature names or default
                        tag_names
                            .get(tag_idx as usize)
                            .cloned()
                            .unwrap_or_else(|| format!("TAG_{}", tag_idx))
                    } else if let Some(tag_str) = tag.as_str() {
                        // String tag - use directly
                        tag_str.to_string()
                    } else {
                        "O".to_string()
                    };

                    annotated_tokens.push(AnnotatedToken { text, ner_tag });
                }

                if !annotated_tokens.is_empty() {
                    sentences.push(AnnotatedSentence {
                        tokens: annotated_tokens,
                        source_dataset: id,
                    });
                }
                continue;
            }

            // Fallback: temporal standoff rows (`text` + `*_expressions` with char offsets).
            //
            // Some HF datasets expose TIMEX/EVENT spans as character ranges in lists like:
            // - `time_expressions`: [{start_char, end_char, ...}, ...]
            // - `event_expressions`: ...
            // - `signal_expressions`: ...
            //
            // We tokenize `text` by whitespace and assign BIO tags based on overlap with any span.
            if let Some(text) = row.get("text").and_then(|v| v.as_str()).map(str::trim) {
                let has_temporal_spans = row
                    .get("time_expressions")
                    .and_then(|v| v.as_array())
                    .is_some()
                    || row
                        .get("event_expressions")
                        .and_then(|v| v.as_array())
                        .is_some()
                    || row
                        .get("signal_expressions")
                        .and_then(|v| v.as_array())
                        .is_some();

                if has_temporal_spans && !text.is_empty() {
                    fn spans_from_array(
                        arr: Option<&Vec<serde_json::Value>>,
                    ) -> Vec<(usize, usize)> {
                        let mut out = Vec::new();
                        let Some(arr) = arr else { return out };
                        for item in arr {
                            let Some(s) = item.get("start_char").and_then(|v| v.as_u64()) else {
                                continue;
                            };
                            let Some(e) = item.get("end_char").and_then(|v| v.as_u64()) else {
                                continue;
                            };
                            let s = s as usize;
                            let e = e as usize;
                            if e > s {
                                out.push((s, e));
                            }
                        }
                        out
                    }

                    fn overlaps(token_s: usize, token_e: usize, spans: &[(usize, usize)]) -> bool {
                        spans.iter().any(|(s, e)| token_s < *e && token_e > *s)
                    }

                    // Tokenize by whitespace, tracking char offsets (Rust `char` count).
                    let mut tokens: Vec<(String, usize, usize)> = Vec::new();
                    let mut cur = String::new();
                    let mut cur_start: Option<usize> = None;

                    for (i, ch) in text.chars().enumerate() {
                        if ch.is_whitespace() {
                            if let Some(s) = cur_start.take() {
                                let e = i;
                                if !cur.is_empty() {
                                    tokens.push((std::mem::take(&mut cur), s, e));
                                } else {
                                    cur.clear();
                                }
                            }
                        } else {
                            if cur_start.is_none() {
                                cur_start = Some(i);
                            }
                            cur.push(ch);
                        }
                    }
                    if let Some(s) = cur_start.take() {
                        let e = text.chars().count();
                        if !cur.is_empty() {
                            tokens.push((cur, s, e));
                        }
                    }

                    if tokens.is_empty() {
                        continue;
                    }

                    let timex_spans =
                        spans_from_array(row.get("time_expressions").and_then(|v| v.as_array()));
                    let event_spans =
                        spans_from_array(row.get("event_expressions").and_then(|v| v.as_array()));
                    let signal_spans =
                        spans_from_array(row.get("signal_expressions").and_then(|v| v.as_array()));

                    let mut annotated_tokens = Vec::with_capacity(tokens.len());
                    let mut prev_label: Option<&'static str> = None;
                    for (tok, s, e) in tokens {
                        let label = if overlaps(s, e, &timex_spans) {
                            Some("TIMEX")
                        } else if overlaps(s, e, &event_spans) {
                            Some("EVENT")
                        } else if overlaps(s, e, &signal_spans) {
                            Some("SIGNAL")
                        } else {
                            None
                        };

                        let ner_tag = match (label, prev_label) {
                            (None, _) => {
                                prev_label = None;
                                "O".to_string()
                            }
                            (Some(l), Some(p)) if l == p => format!("I-{}", l),
                            (Some(l), _) => {
                                prev_label = Some(l);
                                format!("B-{}", l)
                            }
                        };

                        annotated_tokens.push(AnnotatedToken { text: tok, ner_tag });
                    }

                    sentences.push(AnnotatedSentence {
                        tokens: annotated_tokens,
                        source_dataset: id,
                    });
                    continue;
                }
            }

            // Fallback: DISRPT-style CoNLL-U rows for discourse segmentation.
            //
            // DISRPT `*.conllu` configs are exported via HF datasets-server as arrays:
            // - `form`: token surface strings
            // - `misc`: per-token features, including `Seg=B-seg` (segment boundary) or `Seg=O`
            //
            // We convert boundaries into BIO tags of a single entity type (`SEG`), treating each
            // segment (EDU) as an entity span.
            if let (Some(forms), Some(misc)) = (
                row.get("form").and_then(|v| v.as_array()),
                row.get("misc").and_then(|v| v.as_array()),
            ) {
                if !forms.is_empty() && forms.len() == misc.len() {
                    let mut annotated_tokens = Vec::with_capacity(forms.len());
                    let mut in_seg = false;
                    for (i, (f, m)) in forms.iter().zip(misc.iter()).enumerate() {
                        let tok = f.as_str().unwrap_or("").to_string();
                        let misc_s = m.as_str().unwrap_or("");
                        let start = misc_s.contains("Seg=B-seg");
                        let ner_tag = if i == 0 || start {
                            in_seg = true;
                            "B-SEG".to_string()
                        } else if in_seg {
                            "I-SEG".to_string()
                        } else {
                            "O".to_string()
                        };
                        annotated_tokens.push(AnnotatedToken { text: tok, ner_tag });
                    }

                    sentences.push(AnnotatedSentence {
                        tokens: annotated_tokens,
                        source_dataset: id,
                    });
                    continue;
                }
            }

            // Fallback: classification-ish rows (`<text>` + `label`).
            //
            // We encode the gold label as `B-<LABEL>` on a single token, matching the loader's
            // convention for other classification datasets.
            let text = if let Some(s) = row.get("text").and_then(|v| v.as_str()) {
                s.trim().to_string()
            } else if let (Some(a), Some(b)) = (
                row.get("unit1_txt").and_then(|v| v.as_str()),
                row.get("unit2_txt").and_then(|v| v.as_str()),
            ) {
                format!("{} [SEP] {}", a.trim(), b.trim())
            } else if let (Some(a), Some(b)) = (
                row.get("sentence1").and_then(|v| v.as_str()),
                row.get("sentence2").and_then(|v| v.as_str()),
            ) {
                format!("{} [SEP] {}", a.trim(), b.trim())
            } else if let (Some(a), Some(b)) = (
                row.get("premise").and_then(|v| v.as_str()),
                row.get("hypothesis").and_then(|v| v.as_str()),
            ) {
                format!("{} [SEP] {}", a.trim(), b.trim())
            } else {
                continue;
            };
            if text.trim().is_empty() {
                continue;
            }

            let label_value = row.get("label").or_else(|| row.get("labels"));
            let label = match label_value {
                Some(v) if v.is_string() => v.as_str().unwrap_or("").to_string(),
                Some(v) if v.is_number() => {
                    let idx = v.as_u64().unwrap_or(0) as usize;
                    class_names
                        .get(idx)
                        .cloned()
                        .unwrap_or_else(|| format!("LABEL_{}", idx))
                }
                _ => "label".to_string(),
            };
            if label.trim().is_empty() {
                continue;
            }

            sentences.push(AnnotatedSentence {
                tokens: vec![AnnotatedToken {
                    text,
                    ner_tag: format!("B-{}", label),
                }],
                source_dataset: id,
            });
        }

        if sentences.is_empty() {
            return Err(Error::InvalidInput(format!(
                "HF API response for {:?} contains no valid sentences",
                id
            )));
        }

        let now = chrono::Utc::now().to_rfc3339();
        Ok(LoadedDataset {
            id,
            sentences,
            loaded_at: now,
            source_url: id.download_url().to_string(),
            data_source: DataSource::LocalCache,
            temporal_metadata: Self::get_temporal_metadata(id),
            metadata: id.default_metadata(),
        })
    }

    /// Extract tag names from HF API features metadata.
    fn extract_tag_names_from_features(&self, parsed: &serde_json::Value) -> Vec<String> {
        let mut tag_names = Vec::new();

        if let Some(features) = parsed.get("features").and_then(|v| v.as_array()) {
            for feature in features {
                let name = feature.get("name").and_then(|v| v.as_str());
                if name == Some("ner_tags") {
                    // Look for ClassLabel names
                    if let Some(names) = feature
                        .get("type")
                        .and_then(|t| t.get("feature"))
                        .and_then(|f| f.get("names"))
                        .and_then(|n| n.as_array())
                    {
                        for name in names {
                            if let Some(s) = name.as_str() {
                                tag_names.push(s.to_string());
                            }
                        }
                    }
                    break;
                }
            }
        }

        tag_names
    }

    /// Extract class label names for `label` fields in HF API features metadata.
    fn extract_class_names_from_features(&self, parsed: &serde_json::Value) -> Vec<String> {
        let mut names = Vec::new();
        if let Some(features) = parsed.get("features").and_then(|v| v.as_array()) {
            for feature in features {
                let name = feature.get("name").and_then(|v| v.as_str());
                if name == Some("label") {
                    if let Some(label_names) = feature
                        .get("type")
                        .and_then(|t| t.get("names"))
                        .and_then(|n| n.as_array())
                    {
                        for n in label_names {
                            if let Some(s) = n.as_str() {
                                names.push(s.to_string());
                            }
                        }
                    }
                    break;
                }
            }
        }
        names
    }

    /// Parse TweetNER7 JSON format.
    ///
    /// TweetNER7 is JSONL with each line: {"tokens": [...], "tags": [...]}
    /// Tag mapping from label.json (tag -> id format, we need id -> tag):
    fn parse_tweetner7(&self, content: &str, id: DatasetId) -> Result<LoadedDataset> {
        // TweetNER7 tag mapping from label.json (index order!)
        // {"B-corporation": 0, "B-creative_work": 1, "B-event": 2, "B-group": 3,
        //  "B-location": 4, "B-person": 5, "B-product": 6, "I-corporation": 7,
        //  "I-creative_work": 8, "I-event": 9, "I-group": 10, "I-location": 11,
        //  "I-person": 12, "I-product": 13, "O": 14}
        let tag_labels = [
            "B-corporation",   // 0
            "B-creative_work", // 1
            "B-event",         // 2
            "B-group",         // 3
            "B-location",      // 4
            "B-person",        // 5
            "B-product",       // 6
            "I-corporation",   // 7
            "I-creative_work", // 8
            "I-event",         // 9
            "I-group",         // 10
            "I-location",      // 11
            "I-person",        // 12
            "I-product",       // 13
            "O",               // 14
        ];

        let mut sentences = Vec::new();

        // Parse as JSONL (one JSON object per line)
        for line in content.lines() {
            let line = line.trim();
            if line.is_empty() {
                continue;
            }

            let parsed: serde_json::Value = match serde_json::from_str(line) {
                Ok(v) => v,
                Err(_) => continue, // Skip malformed lines
            };

            let tokens = match parsed.get("tokens").and_then(|v| v.as_array()) {
                Some(t) => t,
                None => continue,
            };

            let tags = match parsed.get("tags").and_then(|v| v.as_array()) {
                Some(t) => t,
                None => continue,
            };

            if tokens.len() != tags.len() {
                continue;
            }

            let mut annotated_tokens = Vec::new();
            for (token, tag) in tokens.iter().zip(tags.iter()) {
                let text = token.as_str().unwrap_or("").to_string();
                let tag_idx = tag.as_u64().unwrap_or(0) as usize;
                let ner_tag = tag_labels.get(tag_idx).unwrap_or(&"O").to_string();
                annotated_tokens.push(AnnotatedToken { text, ner_tag });
            }

            if !annotated_tokens.is_empty() {
                sentences.push(AnnotatedSentence {
                    tokens: annotated_tokens,
                    source_dataset: id,
                });
            }
        }

        let now = chrono::Utc::now().to_rfc3339();
        Ok(LoadedDataset {
            id,
            sentences,
            loaded_at: now,
            source_url: id.download_url().to_string(),
            data_source: DataSource::LocalCache,
            temporal_metadata: Self::get_temporal_metadata(id),
            metadata: id.default_metadata(),
        })
    }

    /// Parse HIPE-2022 style TSV NER format.
    ///
    /// Expected format:
    /// ```text
    /// TOKEN    NE-COARSE-LIT   NE-COARSE-METO   NE-FINE-LIT   ...
    /// # hipe2022:document_id = doc123
    /// word1    B-PER           _                B-pers.author ...
    /// word2    I-PER           _                I-pers.author ...
    /// word3    O               _                O             ...
    /// ```
    ///
    /// - First line is header (starts with TOKEN)
    /// - Lines starting with `#` are metadata comments
    /// - Data lines are tab-separated with token in first column
    /// - NE-COARSE-LIT (column 2) contains BIO-tagged NER labels
    /// - `_` means no annotation
    fn parse_tsv_ner(&self, content: &str, id: DatasetId) -> Result<LoadedDataset> {
        let mut sentences = Vec::new();
        let mut current_tokens = Vec::new();

        for line in content.lines() {
            let line = line.trim();

            // Skip empty lines - they indicate sentence boundaries
            if line.is_empty() {
                if !current_tokens.is_empty() {
                    sentences.push(AnnotatedSentence {
                        tokens: std::mem::take(&mut current_tokens),
                        source_dataset: id,
                    });
                }
                continue;
            }

            // Skip header line
            if line.starts_with("TOKEN\t") || line.starts_with("TOKEN ") {
                continue;
            }

            // Skip metadata comments
            if line.starts_with('#') {
                // Document boundary comment can also serve as sentence boundary
                if line.contains("document_id") && !current_tokens.is_empty() {
                    sentences.push(AnnotatedSentence {
                        tokens: std::mem::take(&mut current_tokens),
                        source_dataset: id,
                    });
                }
                continue;
            }

            // Parse data line
            let parts: Vec<&str> = line.split('\t').collect();
            if parts.len() < 2 {
                continue; // Malformed line
            }

            let token_text = parts[0].to_string();
            let ner_label = parts.get(1).unwrap_or(&"O");

            // Convert underscore to O (no annotation)
            let ner_tag = if *ner_label == "_" || ner_label.is_empty() {
                "O".to_string()
            } else {
                ner_label.to_string()
            };

            current_tokens.push(AnnotatedToken {
                text: token_text,
                ner_tag,
            });
        }

        // Don't forget the last sentence
        if !current_tokens.is_empty() {
            sentences.push(AnnotatedSentence {
                tokens: current_tokens,
                source_dataset: id,
            });
        }

        if sentences.is_empty() {
            return Err(Error::InvalidInput(format!(
                "TSV NER file for {:?} contains no valid sentences",
                id
            )));
        }

        let now = chrono::Utc::now().to_rfc3339();
        Ok(LoadedDataset {
            id,
            sentences,
            loaded_at: now,
            source_url: id.download_url().to_string(),
            data_source: DataSource::LocalCache,
            temporal_metadata: Self::get_temporal_metadata(id),
            metadata: id.default_metadata(),
        })
    }

    /// Parse CSV NER format (E-NER/EDGAR-NER style).
    ///
    /// Expected format: `Token,Tag` (comma-separated)
    /// Uses `-DOCSTART-` for document boundaries and empty lines for sentence boundaries.
    /// Tags use BIO scheme (e.g., O, B-PERSON, I-BUSINESS).
    ///
    /// # Errors
    ///
    /// Returns error if CSV is malformed or has no valid sentences.
    fn parse_csv_ner(&self, content: &str, id: DatasetId) -> Result<LoadedDataset> {
        let mut sentences = Vec::new();
        let mut current_tokens = Vec::new();

        for line in content.lines() {
            let line = line.trim();

            // Empty lines indicate sentence boundaries
            if line.is_empty() {
                if !current_tokens.is_empty() {
                    sentences.push(AnnotatedSentence {
                        tokens: std::mem::take(&mut current_tokens),
                        source_dataset: id,
                    });
                }
                continue;
            }

            // Handle -DOCSTART- markers (document boundary, also a sentence boundary)
            if line.starts_with("-DOCSTART-") {
                if !current_tokens.is_empty() {
                    sentences.push(AnnotatedSentence {
                        tokens: std::mem::take(&mut current_tokens),
                        source_dataset: id,
                    });
                }
                continue;
            }

            // Skip header line if present
            if line.eq_ignore_ascii_case("token,tag")
                || line.eq_ignore_ascii_case("text,label")
                || line.eq_ignore_ascii_case("word,ner")
            {
                continue;
            }

            // Parse comma-separated line
            // Handle cases where the token itself might be a comma: ",O" means token is comma
            let (token_text, ner_tag) = if let Some(rest) = line.strip_prefix(',') {
                // Token is empty or the comma itself
                if let Some(idx) = rest.find(',') {
                    // Format: ,tag (empty token) or ,,tag (comma token)
                    if idx == 0 {
                        // ",,tag" -> token is comma, tag follows
                        (",".to_string(), rest[1..].to_string())
                    } else {
                        // ",tag" -> empty token
                        (String::new(), rest[..idx].to_string())
                    }
                } else {
                    // ",tag" with no more commas
                    (String::new(), rest.to_string())
                }
            } else if let Some(idx) = line.rfind(',') {
                // Normal case: Token,Tag
                let token = line[..idx].to_string();
                let tag = line[idx + 1..].to_string();
                (token, tag)
            } else {
                // Malformed line, skip
                continue;
            };

            // Skip if we got empty results
            if token_text.is_empty() && ner_tag.is_empty() {
                continue;
            }

            // Convert empty tag to O
            let ner_tag = if ner_tag.is_empty() || ner_tag == "_" {
                "O".to_string()
            } else {
                ner_tag
            };

            current_tokens.push(AnnotatedToken {
                text: token_text,
                ner_tag,
            });
        }

        // Don't forget the last sentence
        if !current_tokens.is_empty() {
            sentences.push(AnnotatedSentence {
                tokens: current_tokens,
                source_dataset: id,
            });
        }

        if sentences.is_empty() {
            return Err(Error::InvalidInput(format!(
                "CSV NER file for {:?} contains no valid sentences",
                id
            )));
        }

        let now = chrono::Utc::now().to_rfc3339();
        Ok(LoadedDataset {
            id,
            sentences,
            loaded_at: now,
            source_url: id.download_url().to_string(),
            data_source: DataSource::LocalCache,
            temporal_metadata: Self::get_temporal_metadata(id),
            metadata: id.default_metadata(),
        })
    }

    /// Parse WikiANN-style JSON array format.
    ///
    /// Expected format: `[{"text": "...", "tokens": ["word1", "word2"], "ner_tags": ["O", "B-LOC"]}, ...]`
    /// Used by UNER and MSNER datasets converted via download_hf_datasets.py.
    fn parse_wikiann_json(&self, content: &str, id: DatasetId) -> Result<LoadedDataset> {
        let parsed: serde_json::Value = serde_json::from_str(content)
            .map_err(|e| Error::InvalidInput(format!("Failed to parse JSON: {}", e)))?;

        let mut sentences = Vec::new();

        // Handle JSON array format
        if let Some(items) = parsed.as_array() {
            for item in items {
                let tokens = match item.get("tokens").and_then(|v| v.as_array()) {
                    Some(t) => t,
                    None => continue,
                };

                let ner_tags = match item.get("ner_tags").and_then(|v| v.as_array()) {
                    Some(t) => t,
                    None => continue,
                };

                if tokens.len() != ner_tags.len() {
                    continue;
                }

                let mut annotated_tokens = Vec::new();
                for (token, tag) in tokens.iter().zip(ner_tags.iter()) {
                    let text = token.as_str().unwrap_or("").to_string();
                    let ner_tag = tag.as_str().unwrap_or("O").to_string();
                    annotated_tokens.push(AnnotatedToken { text, ner_tag });
                }

                if !annotated_tokens.is_empty() {
                    sentences.push(AnnotatedSentence {
                        tokens: annotated_tokens,
                        source_dataset: id,
                    });
                }
            }
        }

        let now = chrono::Utc::now().to_rfc3339();
        Ok(LoadedDataset {
            id,
            sentences,
            loaded_at: now,
            source_url: id.download_url().to_string(),
            data_source: DataSource::LocalCache,
            temporal_metadata: Self::get_temporal_metadata(id),
            metadata: id.default_metadata(),
        })
    }

    /// Parse DocRED document-level relation extraction JSON format.
    ///
    /// DocRED is primarily for relation extraction but includes NER annotations.
    /// Parse DocRED/CrossRE JSON format for relation extraction.
    ///
    /// CrossRE format: {"doc_key": "...", "sentence": [...], "ner": [[start, end, type], ...], "relations": [...]}
    fn parse_docred(&self, content: &str, id: DatasetId) -> Result<LoadedDataset> {
        let mut sentences = Vec::new();

        // Parse as JSONL (one JSON object per line)
        for line in content.lines() {
            let line = line.trim();
            if line.is_empty() {
                continue;
            }

            let doc: serde_json::Value = match serde_json::from_str(line) {
                Ok(v) => v,
                Err(_) => continue,
            };

            // CrossRE format: sentence array + ner array
            let tokens_arr = match doc.get("sentence").and_then(|v| v.as_array()) {
                Some(t) => t,
                None => continue,
            };

            let ner_spans = doc.get("ner").and_then(|v| v.as_array());

            // Build token list with entity annotations
            let mut tokens: Vec<AnnotatedToken> = tokens_arr
                .iter()
                .filter_map(|t| t.as_str())
                .map(|word| AnnotatedToken {
                    text: word.to_string(),
                    ner_tag: "O".to_string(),
                })
                .collect();

            // Apply NER annotations: [start, end, type]
            if let Some(ner) = ner_spans {
                for span in ner {
                    if let Some(arr) = span.as_array() {
                        if arr.len() >= 3 {
                            let start = arr[0].as_u64().unwrap_or(0) as usize;
                            let end = arr[1].as_u64().unwrap_or(0) as usize;
                            let ent_type = arr[2].as_str().unwrap_or("ENTITY");

                            // Apply BIO tags
                            for idx in start..=end {
                                if idx < tokens.len() {
                                    tokens[idx].ner_tag = if idx == start {
                                        format!("B-{}", ent_type.to_uppercase())
                                    } else {
                                        format!("I-{}", ent_type.to_uppercase())
                                    };
                                }
                            }
                        }
                    }
                }
            }

            if !tokens.is_empty() {
                sentences.push(AnnotatedSentence {
                    tokens,
                    source_dataset: id,
                });
            }
        }

        if sentences.is_empty() {
            return Err(Error::InvalidInput(format!(
                "DocRED/CrossRE JSON for {:?} contains no valid sentences",
                id
            )));
        }

        let now = chrono::Utc::now().to_rfc3339();
        Ok(LoadedDataset {
            id,
            sentences,
            loaded_at: now,
            source_url: id.download_url().to_string(),
            data_source: DataSource::LocalCache,
            temporal_metadata: Self::get_temporal_metadata(id),
            metadata: id.default_metadata(),
        })
    }

    /// Parse Google's relation-extraction-corpus JSONL format.
    ///
    /// Example record (one per line):
    /// ```json
    /// {"pred":"/people/person/place_of_birth","sub":"/m/...","obj":"/m/...","evidences":[{"url":"...","snippet":"..."}],"judgments":[{"rater":"...","judgment":"yes"}]}
    /// ```
    ///
    /// This dataset does **not** include token-level entity spans. For now we treat each
    /// evidence snippet as a plain sentence with all tokens tagged as `O`, which is
    /// sufficient for sanity evaluation plumbing (and avoids failing dataset loads).
    fn parse_google_re_corpus(&self, content: &str, id: DatasetId) -> Result<LoadedDataset> {
        let mut sentences: Vec<AnnotatedSentence> = Vec::new();

        for line in content.lines() {
            let line = line.trim();
            if line.is_empty() {
                continue;
            }

            let rec: serde_json::Value = match serde_json::from_str(line) {
                Ok(v) => v,
                Err(_) => continue,
            };

            let snippet = rec
                .get("evidences")
                .and_then(|v| v.as_array())
                .and_then(|arr| arr.first())
                .and_then(|ev| ev.get("snippet"))
                .and_then(|s| s.as_str());
            let Some(snippet) = snippet else {
                continue;
            };

            let tokens: Vec<AnnotatedToken> = snippet
                .split_whitespace()
                .filter(|t| !t.is_empty())
                .map(|t| AnnotatedToken {
                    text: t.to_string(),
                    ner_tag: "O".to_string(),
                })
                .collect();

            if tokens.is_empty() {
                continue;
            }

            sentences.push(AnnotatedSentence {
                tokens,
                source_dataset: id,
            });
        }

        if sentences.is_empty() {
            return Err(Error::InvalidInput(format!(
                "Google relation-extraction-corpus file for {:?} contains no usable evidence snippets",
                id
            )));
        }

        let now = chrono::Utc::now().to_rfc3339();
        Ok(LoadedDataset {
            id,
            sentences,
            loaded_at: now,
            source_url: id.download_url().to_string(),
            data_source: DataSource::LocalCache,
            temporal_metadata: Self::get_temporal_metadata(id),
            metadata: id.default_metadata(),
        })
    }

    /// Parse CADEC from HuggingFace datasets-server API.
    ///
    /// CADEC HF API format: {"text": "...", "ade": "...", "term_PT": "..."}
    /// Each row is a text-ADE pair (one sentence per ADE mention).
    /// The `ade` field contains the adverse drug event mention within `text`.
    fn parse_cadec_hf_api(&self, content: &str, id: DatasetId) -> Result<LoadedDataset> {
        let parsed: serde_json::Value = serde_json::from_str(content).map_err(|e| {
            Error::InvalidInput(format!("Failed to parse CADEC HF API response: {}", e))
        })?;

        let mut sentences = Vec::new();

        let rows = parsed
            .get("rows")
            .and_then(|v| v.as_array())
            .ok_or_else(|| {
                Error::InvalidInput("No 'rows' array in CADEC HF API response".to_string())
            })?;

        for row_obj in rows {
            let row = match row_obj.get("row") {
                Some(r) => r,
                None => continue,
            };

            let text = match row.get("text").and_then(|v| v.as_str()) {
                Some(t) => t,
                None => continue,
            };

            let ade_text = match row.get("ade").and_then(|v| v.as_str()) {
                Some(a) => a,
                None => continue,
            };

            // Find ADE span in text.
            //
            // IMPORTANT: anno uses **character offsets** globally, but this parser assigns BIO tags
            // token-by-token and uses byte spans internally. We must preserve indices and avoid
            // Unicode casefolding (which can change string length).
            //
            // Strategy:
            // 1) Try exact match (fast, preserves byte indices).
            // 2) Fall back to ASCII case-insensitive match (ADE strings are ASCII-centric).
            let ade_start_byte = text.find(ade_text).or_else(|| {
                let needle_len = ade_text.len();
                if needle_len == 0 {
                    return None;
                }
                for (b, _) in text.char_indices() {
                    if let Some(hay) = text.get(b..b + needle_len) {
                        if hay.eq_ignore_ascii_case(ade_text) {
                            return Some(b);
                        }
                    }
                }
                None
            });
            let Some(ade_start_byte) = ade_start_byte else {
                continue; // ADE not found in text
            };
            let ade_end_byte = ade_start_byte + ade_text.len();

            // Tokenize text preserving byte offsets.
            let mut tokens: Vec<AnnotatedToken> = Vec::new();
            let mut byte_idx = 0;
            let words: Vec<&str> = text.split_whitespace().collect();

            for word in words {
                let word_start =
                    text.get(byte_idx..).and_then(|s| s.find(word)).unwrap_or(0) + byte_idx;
                let word_end = word_start + word.len();

                // Check if this word overlaps with ADE span (byte spans)
                let ner_tag = if word_start >= ade_start_byte && word_end <= ade_end_byte {
                    // Check if this is the first word of the ADE
                    if word_start == ade_start_byte
                        || tokens.is_empty()
                        || !tokens
                            .last()
                            .expect("tokens.is_empty() checked above")
                            .ner_tag
                            .starts_with("I-")
                    {
                        "B-adverse_drug_event".to_string()
                    } else {
                        "I-adverse_drug_event".to_string()
                    }
                } else {
                    "O".to_string()
                };

                tokens.push(AnnotatedToken {
                    text: word.to_string(),
                    ner_tag,
                });

                // Update byte position to after this word (including trailing space)
                byte_idx = word_end;
                if byte_idx < text.len() && text.as_bytes().get(byte_idx) == Some(&b' ') {
                    byte_idx += 1;
                }
            }

            if !tokens.is_empty() {
                sentences.push(AnnotatedSentence {
                    tokens,
                    source_dataset: id,
                });
            }
        }

        let now = chrono::Utc::now().to_rfc3339();
        Ok(LoadedDataset {
            id,
            sentences,
            loaded_at: now,
            source_url: id.download_url().to_string(),
            data_source: DataSource::LocalCache,
            temporal_metadata: Self::get_temporal_metadata(id),
            metadata: id.default_metadata(),
        })
    }

    /// Parse CADEC JSONL format with support for discontinuous entities.
    ///
    /// CADEC format can include:
    /// - Standard BIO tags: `{"tokens": [...], "ner_tags": [...]}`
    /// - Entity spans: `{"tokens": [...], "entities": [{"text": "...", "label": "...", "start": 0, "end": 10}]}`
    /// - Discontinuous entities: `{"entities": [{"text": "...", "label": "...", "spans": [[0, 5], [10, 15]]}]}`
    ///
    /// For discontinuous entities, we convert them to BIO tags by marking all tokens
    /// within any span as part of the entity.
    fn parse_cadec_jsonl(&self, content: &str, id: DatasetId) -> Result<LoadedDataset> {
        let mut sentences = Vec::new();

        for line in content.lines() {
            let line = line.trim();
            if line.is_empty() {
                continue;
            }

            // Parse JSON line
            let parsed: serde_json::Value = match serde_json::from_str(line) {
                Ok(v) => v,
                Err(_) => continue, // Skip malformed lines
            };

            // Try to get tokens
            let tokens = match parsed.get("tokens").and_then(|v| v.as_array()) {
                Some(t) => t,
                None => continue,
            };

            let mut annotated_tokens = Vec::new();
            let mut char_offset = 0;

            // Build token list with character offsets
            let mut token_offsets = Vec::new();
            for token in tokens {
                let text = token.as_str().unwrap_or("").to_string();
                let start = char_offset;
                char_offset += text.chars().count() + 1; // +1 for space
                let end = char_offset - 1;
                token_offsets.push((text, start, end));
            }

            // Initialize all tokens as "O"
            for (text, _, _) in &token_offsets {
                annotated_tokens.push(AnnotatedToken {
                    text: text.clone(),
                    ner_tag: "O".to_string(),
                });
            }

            // Try to parse entities (for discontinuous support)
            if let Some(entities) = parsed.get("entities").and_then(|v| v.as_array()) {
                for entity in entities {
                    let label = entity
                        .get("label")
                        .or_else(|| entity.get("entity_type"))
                        .and_then(|v| v.as_str())
                        .unwrap_or("UNKNOWN")
                        .to_string();

                    // Check for discontinuous spans
                    if let Some(spans) = entity.get("spans").and_then(|v| v.as_array()) {
                        // Discontinuous entity with multiple spans
                        for span in spans {
                            if let Some(span_array) = span.as_array() {
                                if span_array.len() >= 2 {
                                    let start = span_array[0].as_u64().unwrap_or(0) as usize;
                                    let end = span_array[1].as_u64().unwrap_or(0) as usize;

                                    // Mark tokens within this span
                                    for (idx, (_, token_start, token_end)) in
                                        token_offsets.iter().enumerate()
                                    {
                                        if *token_start >= start && *token_end <= end {
                                            if idx > 0
                                                && annotated_tokens[idx - 1]
                                                    .ner_tag
                                                    .starts_with(&format!("I-{}", label))
                                                || annotated_tokens[idx - 1]
                                                    .ner_tag
                                                    .starts_with(&format!("B-{}", label))
                                            {
                                                annotated_tokens[idx].ner_tag =
                                                    format!("I-{}", label);
                                            } else {
                                                annotated_tokens[idx].ner_tag =
                                                    format!("B-{}", label);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    } else if let (Some(start_val), Some(end_val)) = (
                        entity.get("start").and_then(|v| v.as_u64()),
                        entity.get("end").and_then(|v| v.as_u64()),
                    ) {
                        // Contiguous entity
                        let start = start_val as usize;
                        let end = end_val as usize;

                        // Mark tokens within this span
                        for (idx, (_, token_start, token_end)) in token_offsets.iter().enumerate() {
                            if *token_start >= start && *token_end <= end {
                                if idx > 0
                                    && (annotated_tokens[idx - 1]
                                        .ner_tag
                                        .starts_with(&format!("I-{}", label))
                                        || annotated_tokens[idx - 1]
                                            .ner_tag
                                            .starts_with(&format!("B-{}", label)))
                                {
                                    annotated_tokens[idx].ner_tag = format!("I-{}", label);
                                } else {
                                    annotated_tokens[idx].ner_tag = format!("B-{}", label);
                                }
                            }
                        }
                    }
                }
            } else if let Some(ner_tags) = parsed.get("ner_tags").and_then(|v| v.as_array()) {
                // Fallback to standard BIO tags
                let tag_labels = [
                    "O",
                    "B-PER",
                    "I-PER",
                    "B-ORG",
                    "I-ORG",
                    "B-LOC",
                    "I-LOC",
                    "B-MISC",
                    "I-MISC",
                    "B-DRUG",
                    "I-DRUG",
                    "B-ADR",
                    "I-ADR",
                    "B-DISEASE",
                    "I-DISEASE",
                ];

                for (idx, (text, _, _)) in token_offsets.iter().enumerate() {
                    if let Some(tag_val) = ner_tags.get(idx) {
                        let tag_idx = tag_val.as_u64().unwrap_or(0) as usize;
                        let ner_tag = tag_labels.get(tag_idx).unwrap_or(&"O").to_string();
                        annotated_tokens[idx] = AnnotatedToken {
                            text: text.clone(),
                            ner_tag,
                        };
                    }
                }
            }

            if !annotated_tokens.is_empty() {
                sentences.push(AnnotatedSentence {
                    tokens: annotated_tokens,
                    source_dataset: id,
                });
            }
        }

        if sentences.is_empty() {
            return Err(Error::InvalidInput(format!(
                "CADEC JSONL file for {:?} contains no valid sentences",
                id
            )));
        }

        let now = chrono::Utc::now().to_rfc3339();
        Ok(LoadedDataset {
            id,
            sentences,
            loaded_at: now,
            source_url: id.download_url().to_string(),
            data_source: DataSource::LocalCache,
            temporal_metadata: Self::get_temporal_metadata(id),
            metadata: id.default_metadata(),
        })
    }

    /// Parse BC5CDR BioC XML format.
    ///
    /// Note: This is a simplified parser that extracts text passages.
    /// Full annotation extraction would require proper XML parsing.
    /// Parse BC5CDR dataset in CoNLL format from BioFLAIR.
    ///
    /// Format: WORD\tPOS\tCHUNK\tNER_TAG
    fn parse_bc5cdr(&self, content: &str, id: DatasetId) -> Result<LoadedDataset> {
        let mut sentences = Vec::new();
        let mut current_tokens = Vec::new();

        for line in content.lines() {
            let line = line.trim();

            // Skip DOCSTART lines
            if line.starts_with("-DOCSTART-") {
                continue;
            }

            if line.is_empty() {
                // End of sentence
                if !current_tokens.is_empty() {
                    sentences.push(AnnotatedSentence {
                        tokens: std::mem::take(&mut current_tokens),
                        source_dataset: id,
                    });
                }
                continue;
            }

            // Parse CoNLL line: WORD\tPOS\tCHUNK\tNER_TAG
            let parts: Vec<&str> = line.split('\t').collect();
            if parts.len() >= 4 {
                let word = parts[0].to_string();
                let ner_tag = parts[3].to_string();

                // Map Entity tags to BIO format
                let normalized_tag = if ner_tag.contains("Entity")
                    || ner_tag.contains("CHEMICAL")
                    || ner_tag.contains("DISEASE")
                {
                    // Convert I-Entity to B-CHEMICAL or I-CHEMICAL based on context
                    if ner_tag.starts_with("B-") {
                        "B-CHEMICAL".to_string()
                    } else if ner_tag.starts_with("I-") {
                        "I-CHEMICAL".to_string()
                    } else {
                        "O".to_string()
                    }
                } else {
                    ner_tag
                };

                current_tokens.push(AnnotatedToken {
                    text: word,
                    ner_tag: normalized_tag,
                });
            }
        }

        // Don't forget the last sentence
        if !current_tokens.is_empty() {
            sentences.push(AnnotatedSentence {
                tokens: current_tokens,
                source_dataset: id,
            });
        }

        let now = chrono::Utc::now().to_rfc3339();
        Ok(LoadedDataset {
            id,
            sentences,
            loaded_at: now,
            source_url: id.download_url().to_string(),
            data_source: DataSource::LocalCache,
            temporal_metadata: Self::get_temporal_metadata(id),
            metadata: id.default_metadata(),
        })
    }

    /// Parse NCBI Disease corpus format.
    ///
    /// Format: PMID|t|Title or PMID|a|Abstract, followed by annotation lines.
    /// Parse NCBI Disease dataset in CoNLL format from BioFLAIR.
    ///
    /// Format: WORD\tPOS\tCHUNK\tNER_TAG
    fn parse_ncbi_disease(&self, content: &str, id: DatasetId) -> Result<LoadedDataset> {
        let mut sentences = Vec::new();
        let mut current_tokens = Vec::new();

        for line in content.lines() {
            let line = line.trim();

            if line.is_empty() {
                // End of sentence
                if !current_tokens.is_empty() {
                    sentences.push(AnnotatedSentence {
                        tokens: std::mem::take(&mut current_tokens),
                        source_dataset: id,
                    });
                }
                continue;
            }

            // Parse CoNLL line: WORD\tPOS\tCHUNK\tNER_TAG
            let parts: Vec<&str> = line.split('\t').collect();
            if parts.len() >= 4 {
                let word = parts[0].to_string();
                let ner_tag = parts[3].to_string();

                current_tokens.push(AnnotatedToken {
                    text: word,
                    ner_tag,
                });
            }
        }

        // Don't forget the last sentence
        if !current_tokens.is_empty() {
            sentences.push(AnnotatedSentence {
                tokens: current_tokens,
                source_dataset: id,
            });
        }

        if sentences.is_empty() {
            return Err(Error::InvalidInput(format!(
                "NCBI Disease file for {:?} contains no valid sentences",
                id
            )));
        }

        let now = chrono::Utc::now().to_rfc3339();
        Ok(LoadedDataset {
            id,
            sentences,
            loaded_at: now,
            source_url: id.download_url().to_string(),
            data_source: DataSource::LocalCache,
            temporal_metadata: Self::get_temporal_metadata(id),
            metadata: id.default_metadata(),
        })
    }

    /// Parse GAP coreference TSV format.
    ///
    /// GAP format columns: ID, Text, Pronoun, Pronoun-offset, A, A-offset, A-coref, B, B-offset, B-coref, URL
    fn parse_gap(&self, content: &str, id: DatasetId) -> Result<LoadedDataset> {
        let mut sentences = Vec::new();
        let mut first_line = true;

        for line in content.lines() {
            // Skip header
            if first_line {
                first_line = false;
                continue;
            }

            let parts: Vec<&str> = line.split('\t').collect();
            if parts.len() < 10 {
                continue;
            }

            let text = parts[1];

            // Create tokens (whitespace tokenization for simplicity)
            let tokens: Vec<AnnotatedToken> = text
                .split_whitespace()
                .map(|w| AnnotatedToken {
                    text: w.to_string(),
                    ner_tag: "O".to_string(),
                })
                .collect();

            if !tokens.is_empty() {
                sentences.push(AnnotatedSentence {
                    tokens,
                    source_dataset: id,
                });
            }
        }

        if sentences.is_empty() {
            return Err(Error::InvalidInput(format!(
                "GAP TSV file for {:?} contains no valid sentences",
                id
            )));
        }

        let now = chrono::Utc::now().to_rfc3339();
        Ok(LoadedDataset {
            id,
            sentences,
            loaded_at: now,
            source_url: id.download_url().to_string(),
            data_source: DataSource::LocalCache,
            temporal_metadata: Self::get_temporal_metadata(id),
            metadata: id.default_metadata(),
        })
    }

    /// Parse PreCo JSONL format from HuggingFace.
    ///
    /// PreCo JSONL format: One JSON object per line with "sentences" array.
    /// Note: PreCo is a coreference dataset, not NER. This parser extracts sentences
    /// for NER evaluation (which will have 0 entities). Use `load_coref()` for coreference.
    fn parse_preco_jsonl(&self, content: &str, id: DatasetId) -> Result<LoadedDataset> {
        let mut sentences = Vec::new();
        let mut line_count = 0usize;
        let mut parsed_count = 0usize;

        for line in content.lines() {
            let line = line.trim();
            if line.is_empty() {
                continue;
            }
            line_count += 1;

            let parsed: serde_json::Value = match serde_json::from_str(line) {
                Ok(v) => v,
                Err(e) => {
                    // Log first few parse errors for debugging
                    if parsed_count < 3 {
                        log::warn!("PreCo JSONL parse error on line {}: {}", line_count, e);
                    }
                    continue; // Skip malformed lines
                }
            };

            // PreCo format: {"sentences": [[token1, token2, ...], ...]}
            if let Some(sents) = parsed.get("sentences").and_then(|v| v.as_array()) {
                parsed_count += 1;
                for sent_tokens in sents {
                    if let Some(token_array) = sent_tokens.as_array() {
                        let tokens: Vec<AnnotatedToken> = token_array
                            .iter()
                            .filter_map(|t| t.as_str())
                            .map(|t| AnnotatedToken {
                                text: t.to_string(),
                                ner_tag: "O".to_string(), // PreCo has no NER annotations
                            })
                            .collect();

                        if !tokens.is_empty() {
                            sentences.push(AnnotatedSentence {
                                tokens,
                                source_dataset: id,
                            });
                        }
                    }
                }
            }
        }

        if sentences.is_empty() {
            return Err(Error::InvalidInput(format!(
                "PreCo JSONL file contains no valid sentences (parsed {} of {} lines)",
                parsed_count, line_count
            )));
        }

        let now = chrono::Utc::now().to_rfc3339();
        Ok(LoadedDataset {
            id,
            sentences,
            loaded_at: now,
            source_url: id.download_url().to_string(),
            data_source: DataSource::LocalCache,
            temporal_metadata: Self::get_temporal_metadata(id),
            metadata: id.default_metadata(),
        })
    }

    /// Parse LitBank annotation format for NER.
    ///
    /// LitBank .ann format: T<id>\t<Type> <start> <end>\t<text>
    /// Note: LitBank is primarily a coreference dataset. This parser extracts
    /// entity mentions as NER annotations, but LitBank should be used with
    /// `load_coref()` for proper coreference evaluation.
    fn parse_litbank(&self, content: &str, id: DatasetId) -> Result<LoadedDataset> {
        // LitBank .ann format: each line is T<id>\t<Type> <start> <end>\t<text>
        // Extract entity mentions as NER annotations
        let now = chrono::Utc::now().to_rfc3339();
        let mut sentences = Vec::new();
        let mut entities: Vec<(usize, usize, String, String)> = Vec::new(); // (start, end, text, label)

        for line in content.lines() {
            let line = line.trim();
            if line.is_empty() {
                continue;
            }

            if line.starts_with('T') {
                // Entity annotation: T1\tPER 0 5\tAlice
                // LitBank uses ACE-style labels like PROP_PER, NOM_LOC, PRON_FAC etc.
                // We normalize to just the entity type (PER, LOC, ORG, GPE, FAC, VEH)
                // Note: LitBank also has coreference chain annotations like "character_name-ID"
                // which should be skipped for NER evaluation
                let parts: Vec<&str> = line.split('\t').collect();
                if parts.len() >= 3 {
                    let type_span: Vec<&str> = parts[1].split_whitespace().collect();
                    if type_span.len() >= 3 {
                        let raw_label = type_span[0];

                        // Valid entity types (with or without prefix)
                        const VALID_ENTITY_TYPES: &[&str] = &[
                            "PER",
                            "LOC",
                            "ORG",
                            "GPE",
                            "FAC",
                            "VEH",
                            "PERSON",
                            "LOCATION",
                            "ORGANIZATION",
                        ];

                        // Entity type annotations start with PROP_, NOM_, PRON_ OR are plain types
                        let is_prefixed_entity = raw_label.starts_with("PROP_")
                            || raw_label.starts_with("NOM_")
                            || raw_label.starts_with("PRON_");
                        let is_plain_entity = VALID_ENTITY_TYPES.contains(&raw_label);

                        if !is_prefixed_entity && !is_plain_entity {
                            // Skip coreference chain annotations (e.g., "jarndyce_2-73")
                            continue;
                        }

                        // Normalize: PROP_PER -> PER, NOM_LOC -> LOC, PRON_FAC -> FAC
                        let label = if is_prefixed_entity {
                            raw_label.split('_').next_back().unwrap_or(raw_label)
                        } else {
                            raw_label
                        };
                        let start: usize = type_span[1].parse().unwrap_or(0);
                        let end: usize = type_span[2].parse().unwrap_or(0);
                        let text = parts[2];

                        entities.push((start, end, text.to_string(), label.to_string()));
                    }
                }
            }
        }

        if entities.is_empty() {
            return Err(Error::InvalidInput(
                "LitBank .ann file contains no entity annotations (T lines)".to_string(),
            ));
        }

        // Sort entities by start position
        entities.sort_by_key(|(start, _, _, _)| *start);

        // Reconstruct text and create tokens with NER tags
        let max_end = entities
            .iter()
            .map(|(_, end, _, _)| *end)
            .max()
            .unwrap_or(0);
        let mut text_chars: Vec<char> = vec![' '; max_end.max(1)];
        let mut token_starts: Vec<usize> = Vec::new();

        // Fill in entity text
        for (start, _end, text, _) in &entities {
            let text_chars_vec: Vec<char> = text.chars().collect();
            let _actual_end = (*start + text_chars_vec.len()).min(text_chars.len());
            if *start < text_chars.len() {
                for (i, ch) in text_chars_vec.iter().enumerate() {
                    let pos = *start + i;
                    if pos < text_chars.len() {
                        text_chars[pos] = *ch;
                    }
                }
            }
            token_starts.push(*start);
        }

        // Note: text reconstruction is not used directly since we tokenize from entity text
        // but we keep it for potential future use (e.g., validation)
        let _text: String = text_chars
            .into_iter()
            .collect::<String>()
            .trim()
            .to_string();

        // Create tokens with NER tags
        // Improved approach: tokenize entity text into words and apply BIO tags
        let mut tokens: Vec<AnnotatedToken> = Vec::new();

        // Sort entities by start position for processing
        entities.sort_by_key(|(start, _, _, _)| *start);

        let mut last_end = 0usize;
        for (start, end, entity_text, label) in &entities {
            // Add "O" tokens for gaps between entities
            if *start > last_end {
                // For gaps, we can't reconstruct the actual text without the .txt file
                // But we can estimate based on character distance
                let gap_size = *start - last_end;
                if gap_size > 0 {
                    // Estimate number of words in gap (roughly 5 chars per word + space)
                    let estimated_words = (gap_size / 6).max(1);
                    for _ in 0..estimated_words.min(10) {
                        // Limit gap tokens to avoid excessive placeholders
                        tokens.push(AnnotatedToken {
                            text: "[...]".to_string(),
                            ner_tag: "O".to_string(),
                        });
                    }
                }
            }

            // Tokenize entity text into words and apply BIO tags
            let entity_words: Vec<&str> = entity_text.split_whitespace().collect();
            for (i, word) in entity_words.iter().enumerate() {
                let ner_tag = if i == 0 {
                    format!("B-{}", label)
                } else {
                    format!("I-{}", label)
                };
                tokens.push(AnnotatedToken {
                    text: word.to_string(),
                    ner_tag,
                });
            }

            last_end = *end;
        }

        if !tokens.is_empty() {
            sentences.push(AnnotatedSentence {
                tokens,
                source_dataset: id,
            });
        }

        if sentences.is_empty() {
            return Err(Error::InvalidInput(
                "LitBank file produced no sentences after parsing".to_string(),
            ));
        }

        Ok(LoadedDataset {
            id,
            sentences,
            loaded_at: now,
            source_url: id.download_url().to_string(),
            data_source: DataSource::LocalCache,
            temporal_metadata: Self::get_temporal_metadata(id),
            metadata: id.default_metadata(),
        })
    }

    /// Parse ECB+ CSV format for event coreference.
    ///
    /// ECB+ uses CSV format with columns for event mentions and coreference links.
    /// For now, extracts entities as NER annotations (event triggers).
    fn parse_ecb_plus(&self, content: &str, id: DatasetId) -> Result<LoadedDataset> {
        let mut sentences = Vec::new();
        let mut first_line = true;

        for line in content.lines() {
            // Skip header
            if first_line {
                first_line = false;
                continue;
            }

            let parts: Vec<&str> = line.split(',').collect();
            if parts.len() < 3 {
                continue;
            }

            // ECB+ CSV format: sentence_id, text, event_mention, ...
            // Extract text and create tokens
            let text = parts.get(1).unwrap_or(&"");
            let tokens: Vec<AnnotatedToken> = text
                .split_whitespace()
                .map(|w| AnnotatedToken {
                    text: w.to_string(),
                    ner_tag: "O".to_string(),
                })
                .collect();

            if !tokens.is_empty() {
                sentences.push(AnnotatedSentence {
                    tokens,
                    source_dataset: id,
                });
            }
        }

        if sentences.is_empty() {
            return Err(Error::InvalidInput(format!(
                "ECB+ CSV file for {:?} contains no valid sentences",
                id
            )));
        }

        let now = chrono::Utc::now().to_rfc3339();
        Ok(LoadedDataset {
            id,
            sentences,
            loaded_at: now,
            source_url: id.download_url().to_string(),
            data_source: DataSource::LocalCache,
            temporal_metadata: Self::get_temporal_metadata(id),
            metadata: id.default_metadata(),
        })
    }

    // =========================================================================
    // Coreference Loading
    // =========================================================================

    /// Load coreference dataset, returning documents with chains.
    ///
    /// Use this for GAP, PreCo, and LitBank datasets.
    pub fn load_coref(&self, id: DatasetId) -> Result<Vec<super::coref::CorefDocument>> {
        if !id.is_coreference() {
            return Err(Error::InvalidInput(format!(
                "{:?} is not a coreference dataset",
                id
            )));
        }

        let cache_path = self.cache_path_for(id);
        if !cache_path.exists() {
            return Err(Error::InvalidInput(format!(
                "Dataset {:?} not cached at {:?}. Download from {}",
                id,
                cache_path,
                id.download_url()
            )));
        }

        let content = std::fs::read_to_string(&cache_path)
            .map_err(|e| Error::InvalidInput(format!("Failed to read {:?}: {}", cache_path, e)))?;

        match id {
            DatasetId::CorefUD => super::coref_loader::parse_corefud_conllu(&content),
            DatasetId::GAP => {
                let examples = super::coref_loader::parse_gap_tsv(&content)?;
                Ok(examples
                    .into_iter()
                    .map(|ex| ex.to_coref_document())
                    .collect())
            }
            DatasetId::PreCo => {
                // PreCo can be JSONL (one JSON object per line) or JSON array
                // Try JSONL first (more common), then fall back to JSON array
                if content.trim().starts_with('[') {
                    // JSON array format
                    let docs = super::coref_loader::parse_preco_json(&content)?;
                    Ok(docs.into_iter().map(|d| d.to_coref_document()).collect())
                } else {
                    // JSONL format - parse each line and convert to JSON array format
                    let mut json_objects = Vec::new();
                    for line in content.lines() {
                        let line = line.trim();
                        if line.is_empty() {
                            continue;
                        }
                        // Validate it's valid JSON
                        if serde_json::from_str::<serde_json::Value>(line).is_ok() {
                            json_objects.push(line);
                        }
                    }
                    // Convert JSONL to JSON array format
                    let json_array = format!("[{}]", json_objects.join(","));
                    let docs = super::coref_loader::parse_preco_json(&json_array)?;
                    Ok(docs.into_iter().map(|d| d.to_coref_document()).collect())
                }
            }
            DatasetId::LitBank => {
                // LitBank coreference - parse .ann format for chains
                self.parse_litbank_coref(&content)
            }
            DatasetId::ECBPlus => {
                // ECB+ may be cached as either:
                // - ZIP binary (new: real XML annotations)
                // - CSV text (legacy: sentence index)
                let raw_bytes = std::fs::read(&cache_path).map_err(|e| {
                    Error::InvalidInput(format!("Failed to read {:?}: {}", cache_path, e))
                })?;
                if raw_bytes.starts_with(b"PK\x03\x04") {
                    return super::coref_loader::parse_ecb_plus_zip(&raw_bytes);
                }
                // Fall back to CSV parser
                super::coref_loader::parse_ecb_plus_coref(&content)
            }
            DatasetId::WikiCoref => {
                // WikiCoref uses a GAP-compatible TSV format.
                let examples = super::coref_loader::parse_gap_tsv(&content)?;
                Ok(examples
                    .into_iter()
                    .map(|ex| ex.to_coref_document())
                    .collect())
            }
            DatasetId::GUM
            | DatasetId::WinoBias
            | DatasetId::TwiConv
            | DatasetId::MuDoCo
            | DatasetId::SciCo => Err(Error::InvalidInput(format!(
                "{:?} coreference format is not yet supported (requires a dedicated parser)",
                id
            ))),
            DatasetId::BookCoref | DatasetId::BookCorefSplit => {
                // BOOKCOREF: Book-scale coreference (Martinelli et al. 2025)
                // Format: OntoNotes-style with character metadata
                // Note: Actual data requires HuggingFace datasets library to download
                // from Project Gutenberg. We support pre-downloaded JSONL.
                super::coref_loader::parse_bookcoref_json(&content)
            }
            _ => Err(Error::InvalidInput(format!(
                "No coreference parser for {:?}",
                id
            ))),
        }
    }

    /// Load coreference dataset, downloading if needed.
    #[cfg(feature = "eval")]
    pub fn load_or_download_coref(
        &self,
        id: DatasetId,
    ) -> Result<Vec<super::coref::CorefDocument>> {
        if !self.is_cached_for(id) {
            if matches!(id, DatasetId::CorefUD) {
                let cache_path = self.cache_path_for(id);
                return Err(Error::InvalidInput(format!(
                    "CorefUD is not downloadable via anno yet. Please provide a local CorefUD .conllu file.\n\
                     - Option A: copy it to the cache path {:?}\n\
                     - Option B: use CorefLoader::load_corefud_from_path(<path>)",
                    cache_path
                )));
            }

            let cache_path = self.cache_path_for(id);
            if matches!(id, DatasetId::ECBPlus) {
                // ECB+ is a ZIP file -- download as raw bytes
                let url = id.download_url();
                let bytes = self.download_attempt_bytes(url)?;
                std::fs::write(&cache_path, &bytes).map_err(|e| {
                    Error::InvalidInput(format!("Failed to cache {:?}: {}", cache_path, e))
                })?;
            } else {
                let (content, _) = self.download_with_resolved_url(id)?;
                std::fs::write(&cache_path, &content).map_err(|e| {
                    Error::InvalidInput(format!("Failed to cache {:?}: {}", cache_path, e))
                })?;
            }
        }
        self.load_coref(id)
    }

    /// Parse LitBank for coreference chains.
    ///
    /// LitBank format: .ann files with T lines (mentions) and R lines (coreference relations).
    /// T lines: T<id>\t<Type> <start> <end>\t<text>
    /// R lines: R<id>\tCoref Arg1:T<id1> Arg2:T<id2>
    ///
    /// Note: LitBank .ann files reference character offsets in corresponding .txt files.
    /// This parser reconstructs text from mentions, but ideally should read .txt file.
    fn parse_litbank_coref(&self, content: &str) -> Result<Vec<super::coref::CorefDocument>> {
        use super::coref::{CorefChain, CorefDocument, Mention};
        use std::collections::HashMap;

        // LitBank .ann format includes coreference with R lines
        // R1\tCoref Arg1:T1 Arg2:T2
        let mut mentions: HashMap<String, Mention> = HashMap::new();
        let mut coref_links: Vec<(String, String)> = Vec::new();
        let mut max_end = 0usize;

        for line in content.lines() {
            let line = line.trim();
            if line.is_empty() {
                continue;
            }

            if line.starts_with('T') {
                let parts: Vec<&str> = line.split('\t').collect();
                if parts.len() >= 3 {
                    let id = parts[0];
                    let type_span: Vec<&str> = parts[1].split_whitespace().collect();
                    if type_span.len() >= 3 {
                        let start: usize = type_span[1].parse().unwrap_or(0);
                        let end: usize = type_span[2].parse().unwrap_or(0);
                        let text = parts[2];
                        max_end = max_end.max(end);
                        mentions.insert(id.to_string(), Mention::new(text, start, end));
                    }
                }
            } else if line.starts_with('R') && line.contains("Coref") {
                // R1\tCoref Arg1:T1 Arg2:T2
                let parts: Vec<&str> = line.split_whitespace().collect();
                if parts.len() >= 3 {
                    let arg1 = parts[1].trim_start_matches("Arg1:");
                    let arg2 = parts[2].trim_start_matches("Arg2:");
                    coref_links.push((arg1.to_string(), arg2.to_string()));
                }
            }
        }

        if mentions.is_empty() {
            return Err(Error::InvalidInput(
                "LitBank file contains no mentions (T lines)".to_string(),
            ));
        }

        // Reconstruct text from mentions by sorting by start offset
        let mut sorted_mentions: Vec<(usize, &Mention)> =
            mentions.values().map(|m| (m.start, m)).collect();
        sorted_mentions.sort_by_key(|(start, _)| *start);

        // Build text by inserting mentions at their offsets
        let mut text_chars: Vec<char> = vec![' '; max_end.max(1)];
        for (start, mention) in &sorted_mentions {
            let mention_text: Vec<char> = mention.text.chars().collect();
            let _end = (*start + mention_text.len()).min(text_chars.len());
            if *start < text_chars.len() {
                for (i, ch) in mention_text.iter().enumerate() {
                    let pos = *start + i;
                    if pos < text_chars.len() {
                        text_chars[pos] = *ch;
                    }
                }
            }
        }
        let text: String = text_chars
            .into_iter()
            .collect::<String>()
            .trim()
            .to_string();

        // Build chains from links using union-find
        let mut chains: Vec<Vec<Mention>> = Vec::new();
        let mut mention_to_chain: HashMap<String, usize> = HashMap::new();

        // First, add all mentions as singletons if they're not in any chain
        for (id, mention) in &mentions {
            if !mention_to_chain.contains_key(id) {
                let idx = chains.len();
                chains.push(vec![mention.clone()]);
                mention_to_chain.insert(id.clone(), idx);
            }
        }

        // Then merge chains based on coref links
        for (id1, id2) in coref_links {
            let chain_idx = match (mention_to_chain.get(&id1), mention_to_chain.get(&id2)) {
                (Some(&idx1), Some(&idx2)) if idx1 != idx2 => {
                    // Merge chains
                    let to_merge = std::mem::take(&mut chains[idx2]);
                    chains[idx1].extend(to_merge);
                    // Update all mentions in merged chain to point to idx1
                    for m in &chains[idx1] {
                        // Find mention ID by matching text and position
                        for (mid, mref) in &mentions {
                            if mref.text == m.text && mref.start == m.start {
                                mention_to_chain.insert(mid.clone(), idx1);
                            }
                        }
                    }
                    idx1
                }
                (Some(&idx), None) => {
                    if let Some(m) = mentions.get(&id2) {
                        chains[idx].push(m.clone());
                        mention_to_chain.insert(id2, idx);
                    }
                    idx
                }
                (None, Some(&idx)) => {
                    if let Some(m) = mentions.get(&id1) {
                        chains[idx].push(m.clone());
                        mention_to_chain.insert(id1, idx);
                    }
                    idx
                }
                (None, None) => {
                    let idx = chains.len();
                    let mut chain = Vec::new();
                    if let Some(m) = mentions.get(&id1) {
                        chain.push(m.clone());
                        mention_to_chain.insert(id1.clone(), idx);
                    }
                    if let Some(m) = mentions.get(&id2) {
                        chain.push(m.clone());
                        mention_to_chain.insert(id2, idx);
                    }
                    if !chain.is_empty() {
                        chains.push(chain);
                    }
                    idx
                }
                (Some(&idx), Some(_)) => idx,
            };
            let _ = chain_idx; // Used above
        }

        // Filter empty chains and convert
        let coref_chains: Vec<CorefChain> = chains
            .into_iter()
            .filter(|c| !c.is_empty())
            .enumerate()
            .map(|(i, mentions)| CorefChain::with_id(mentions, i as u64))
            .collect();

        if coref_chains.is_empty() {
            return Err(Error::InvalidInput(
                "LitBank file contains no coreference chains".to_string(),
            ));
        }

        // Create document with reconstructed text
        let doc = CorefDocument::new(&text, coref_chains);
        Ok(vec![doc])
    }

    // =========================================================================
    // Relation Extraction Loading
    // =========================================================================

    /// Load relation extraction dataset, returning documents with relations.
    ///
    /// Use this for DocRED and ReTACRED datasets.
    pub fn load_relation(&self, id: DatasetId) -> Result<Vec<RelationDocument>> {
        if !id.is_relation_extraction() {
            return Err(Error::InvalidInput(format!(
                "{:?} is not a relation extraction dataset",
                id
            )));
        }

        let cache_path = self.cache_path_for(id);
        if !cache_path.exists() {
            return Err(Error::InvalidInput(format!(
                "Dataset {:?} not cached at {:?}. Download from {}",
                id,
                cache_path,
                id.download_url()
            )));
        }

        let content = std::fs::read_to_string(&cache_path)
            .map_err(|e| Error::InvalidInput(format!("Failed to read {:?}: {}", cache_path, e)))?;

        match id {
            DatasetId::DocRED
            | DatasetId::ReTACRED
            | DatasetId::NYTFB
            | DatasetId::WEBNLG
            | DatasetId::GoogleRE
            | DatasetId::BioRED
            | DatasetId::SciER
            | DatasetId::MixRED
            | DatasetId::CovEReD => {
                // All these datasets use the CrossRE format (same as DocRED)
                self.parse_docred_relations(&content)
            }
            DatasetId::CHisIEC => {
                // CHisIEC uses a different JSON format with entity indices
                self.parse_chisiec_relations(&content)
            }
            DatasetId::CADEC => {
                // CADEC is NER, not relation extraction
                Err(Error::InvalidInput(
                    "CADEC is a NER dataset, not relation extraction".to_string(),
                ))
            }
            _ => Err(Error::InvalidInput(format!(
                "No relation parser for {:?}",
                id
            ))),
        }
    }

    /// Load relation extraction dataset, downloading if needed.
    #[cfg(feature = "eval")]
    pub fn load_or_download_relation(&self, id: DatasetId) -> Result<Vec<RelationDocument>> {
        if !self.is_cached_for(id) {
            let (content, _) = self.download_with_resolved_url(id)?;
            let cache_path = self.cache_path_for(id);
            std::fs::write(&cache_path, &content).map_err(|e| {
                Error::InvalidInput(format!("Failed to cache {:?}: {}", cache_path, e))
            })?;
        }
        self.load_relation(id)
    }

    /// Parse DocRED/CrossRE format for relation extraction.
    ///
    /// Format: JSONL with {"sentence": [...], "ner": [[start, end, type], ...], "relations": [[id1-start, id1-end, id2-start, id2-end, rel-type, ...], ...]}
    fn parse_docred_relations(&self, content: &str) -> Result<Vec<RelationDocument>> {
        use super::relation::RelationGold;

        let mut documents = Vec::new();

        // Parse as JSONL (one JSON object per line)
        for line in content.lines() {
            let line = line.trim();
            if line.is_empty() {
                continue;
            }

            let doc: serde_json::Value = match serde_json::from_str(line) {
                Ok(v) => v,
                Err(_) => continue,
            };

            // Get sentence tokens
            let tokens_arr = match doc.get("sentence").and_then(|v| v.as_array()) {
                Some(t) => t,
                None => continue,
            };

            // Build text from tokens (with proper spacing)
            let text: String = tokens_arr
                .iter()
                .filter_map(|t| t.as_str())
                .collect::<Vec<_>>()
                .join(" ");

            // Build token-to-character offset mapping
            // This maps each token index to its character start position in the text
            let mut token_to_char: Vec<usize> = Vec::new();
            let mut char_pos = 0;
            for (i, token) in tokens_arr.iter().enumerate() {
                if let Some(tok_str) = token.as_str() {
                    token_to_char.push(char_pos);
                    // Add token length + 1 for space (except last token)
                    char_pos += tok_str.len();
                    if i < tokens_arr.len() - 1 {
                        char_pos += 1; // Space between tokens
                    }
                } else {
                    token_to_char.push(char_pos);
                }
            }

            // Get NER spans: [start, end, type]
            let ner_spans = doc.get("ner").and_then(|v| v.as_array());

            // Build entity map: (token_start, token_end) -> (type, text, char_start, char_end)
            let mut entity_map: std::collections::HashMap<
                (usize, usize),
                (String, String, usize, usize),
            > = std::collections::HashMap::new();
            if let Some(ner) = ner_spans {
                for span in ner {
                    if let Some(arr) = span.as_array() {
                        if arr.len() >= 3 {
                            let token_start = arr[0].as_u64().unwrap_or(0) as usize;
                            let token_end = arr[1].as_u64().unwrap_or(0) as usize;
                            let ent_type = arr[2].as_str().unwrap_or("ENTITY").to_string();

                            // Extract entity text from tokens
                            let entity_text: String = tokens_arr
                                .iter()
                                .skip(token_start)
                                .take(token_end - token_start + 1)
                                .filter_map(|t| t.as_str())
                                .collect::<Vec<_>>()
                                .join(" ");

                            // Calculate actual character offsets
                            let char_start = token_to_char.get(token_start).copied().unwrap_or(0);
                            let char_end = if token_end < token_to_char.len() {
                                // Get end position of last token
                                let last_token_char_start = token_to_char[token_end];
                                if let Some(last_token) =
                                    tokens_arr.get(token_end).and_then(|t| t.as_str())
                                {
                                    last_token_char_start + last_token.len()
                                } else {
                                    char_start + entity_text.len()
                                }
                            } else {
                                char_start + entity_text.len()
                            };

                            entity_map.insert(
                                (token_start, token_end),
                                (ent_type, entity_text, char_start, char_end),
                            );
                        }
                    }
                }
            }

            // Parse relations: [id1-start, id1-end, id2-start, id2-end, rel-type, ...]
            let relations_arr = doc.get("relations").and_then(|v| v.as_array());
            let mut relations = Vec::new();

            if let Some(rels) = relations_arr {
                for rel in rels {
                    if let Some(arr) = rel.as_array() {
                        if arr.len() >= 5 {
                            let head_token_start = arr[0].as_u64().unwrap_or(0) as usize;
                            let head_token_end = arr[1].as_u64().unwrap_or(0) as usize;
                            let tail_token_start = arr[2].as_u64().unwrap_or(0) as usize;
                            let tail_token_end = arr[3].as_u64().unwrap_or(0) as usize;
                            let rel_type = arr[4].as_str().unwrap_or("RELATION").to_string();

                            // Get entity info from map (including character offsets)
                            let (head_type, head_text, head_char_start, head_char_end) = entity_map
                                .get(&(head_token_start, head_token_end))
                                .cloned()
                                .unwrap_or_else(|| {
                                    // Fallback: compute from token positions
                                    let char_start =
                                        token_to_char.get(head_token_start).copied().unwrap_or(0);
                                    let char_end = if head_token_end < token_to_char.len() {
                                        let last_start = token_to_char[head_token_end];
                                        if let Some(last_tok) =
                                            tokens_arr.get(head_token_end).and_then(|t| t.as_str())
                                        {
                                            last_start + last_tok.len()
                                        } else {
                                            char_start
                                        }
                                    } else {
                                        char_start
                                    };
                                    ("ENTITY".to_string(), String::new(), char_start, char_end)
                                });

                            let (tail_type, tail_text, tail_char_start, tail_char_end) = entity_map
                                .get(&(tail_token_start, tail_token_end))
                                .cloned()
                                .unwrap_or_else(|| {
                                    // Fallback: compute from token positions
                                    let char_start =
                                        token_to_char.get(tail_token_start).copied().unwrap_or(0);
                                    let char_end = if tail_token_end < token_to_char.len() {
                                        let last_start = token_to_char[tail_token_end];
                                        if let Some(last_tok) =
                                            tokens_arr.get(tail_token_end).and_then(|t| t.as_str())
                                        {
                                            last_start + last_tok.len()
                                        } else {
                                            char_start
                                        }
                                    } else {
                                        char_start
                                    };
                                    ("ENTITY".to_string(), String::new(), char_start, char_end)
                                });

                            relations.push(RelationGold::new(
                                (head_char_start, head_char_end),
                                head_type,
                                head_text,
                                (tail_char_start, tail_char_end),
                                tail_type,
                                tail_text,
                                rel_type,
                            ));
                        }
                    }
                }
            }

            if !text.is_empty() {
                documents.push(RelationDocument { text, relations });
            }
        }

        Ok(documents)
    }

    /// Parse CHisIEC (Chinese Historical Information Extraction Corpus) NER format.
    ///
    /// # Research Background
    ///
    /// CHisIEC (Tang et al., LREC-COLING 2024) addresses the unique challenges of
    /// information extraction from ancient Chinese historical texts (文言文):
    ///
    /// - **No word boundaries**: Classical Chinese has no spaces; we tokenize by character
    /// - **Archaic vocabulary**: Official titles (官职) differ from modern equivalents
    /// - **Long time span**: Covers texts from multiple dynasties across 2000+ years
    /// - **Domain-specific entities**: Government positions, classical texts, historical figures
    ///
    /// Paper: <https://aclanthology.org/2024.lrec-main.283/>
    /// GitHub: <https://github.com/tangxuemei1995/CHisIEC>
    ///
    /// # Entity Types
    ///
    /// | Type | Chinese | Meaning | Example |
    /// |------|---------|---------|---------|
    /// | PER | 人物 | Historical figure | 司馬遷, 曹操 |
    /// | LOC | 地点 | Location/Place | 長安, 洛陽 |
    /// | OFI | 官职 | Official position | 丞相, 太守 |
    /// | BOOK | 书籍 | Classical text | 史記, 論語 |
    ///
    /// # Format
    ///
    /// JSON array where each object contains:
    /// - `tokens`: String of continuous text (no spaces between characters)
    /// - `entities`: Array of entity objects with `type`, `start`, `end`, `span`
    ///
    /// # Distinction from HistoricalChineseNER
    ///
    /// **CHisIEC** (this dataset):
    /// - Ancient Chinese (文言文, pre-modern)
    /// - Source: 24 dynastic histories (二十四史)
    /// - Tasks: NER + Relation Extraction
    ///
    /// **HistoricalChineseNER** (different dataset):
    /// - Modern Chinese transition (1872-1949)
    /// - Source: ShenBao newspapers
    /// - Tasks: NER + Entity Linking + Coreference
    ///
    /// # Implementation Notes
    ///
    /// Chinese text is tokenized by character for NER tagging. Character offsets
    /// are used directly (critical for Unicode correctness).
    fn parse_chisiec(&self, content: &str, id: DatasetId) -> Result<LoadedDataset> {
        let mut sentences = Vec::new();

        // CHisIEC RE data is a JSON array
        let docs: Vec<serde_json::Value> = serde_json::from_str(content)
            .map_err(|e| Error::InvalidInput(format!("Failed to parse CHisIEC JSON: {}", e)))?;

        for doc in docs {
            // Get tokens string (characters concatenated, no spaces in Chinese)
            let text = match doc.get("tokens").and_then(|v| v.as_str()) {
                Some(t) => t.to_string(),
                None => continue,
            };

            if text.is_empty() {
                continue;
            }

            // Chinese text: each character is a token
            let chars: Vec<char> = text.chars().collect();
            let mut tokens: Vec<AnnotatedToken> = chars
                .iter()
                .map(|c| AnnotatedToken {
                    text: c.to_string(),
                    ner_tag: "O".to_string(),
                })
                .collect();

            // Get entities array and build BIO tags
            let entities_arr = doc.get("entities").and_then(|v| v.as_array());

            if let Some(entities) = entities_arr {
                for entity in entities {
                    let ent_type = entity
                        .get("type")
                        .and_then(|v| v.as_str())
                        .unwrap_or("ENTITY");
                    let start = entity.get("start").and_then(|v| v.as_u64()).unwrap_or(0) as usize;
                    let end = entity.get("end").and_then(|v| v.as_u64()).unwrap_or(0) as usize;

                    // Apply BIO tags (CHisIEC uses character indices)
                    for idx in start..end {
                        if idx < tokens.len() {
                            tokens[idx].ner_tag = if idx == start {
                                format!("B-{}", ent_type)
                            } else {
                                format!("I-{}", ent_type)
                            };
                        }
                    }
                }
            }

            if !tokens.is_empty() {
                sentences.push(AnnotatedSentence {
                    tokens,
                    source_dataset: id,
                });
            }
        }

        if sentences.is_empty() {
            return Err(Error::InvalidInput(format!(
                "CHisIEC file for {:?} contains no valid sentences",
                id
            )));
        }

        let now = chrono::Utc::now().to_rfc3339();
        Ok(LoadedDataset {
            id,
            sentences,
            loaded_at: now,
            source_url: id.download_url().to_string(),
            data_source: DataSource::LocalCache,
            temporal_metadata: Self::get_temporal_metadata(id),
            metadata: id.default_metadata(),
        })
    }

    /// Parse CHisIEC relation extraction format.
    ///
    /// # Research Motivation
    ///
    /// Ancient Chinese historical texts encode complex socio-political relationships
    /// that require domain-specific relation types. The 12 relation types in CHisIEC
    /// reflect structures unique to dynastic China:
    ///
    /// - **Hierarchical**: 上下級 (superior-subordinate), 任職 (office-holding)
    /// - **Kinship**: 父母 (parents), 兄弟 (siblings)
    /// - **Political**: 政治奧援 (political support), 同僚 (colleagues)
    /// - **Military**: 敵對攻伐 (hostility/attack), 駐守 (defend/garrison)
    /// - **Spatial**: 到達 (arrival), 出生於某地 (birthplace), 管理 (governance)
    /// - **Identity**: 別名 (alias/alternative name)
    ///
    /// # Format Difference from CrossRE/DocRED
    ///
    /// **CHisIEC** uses entity indices:
    /// ```json
    /// "relations": [{"type": "上下級", "head": 0, "tail": 1, ...}]
    /// ```
    ///
    /// **CrossRE/DocRED** uses token spans:
    /// ```json
    /// "relations": [[0, 2, 3, 5, "relation_type"]]
    /// ```
    ///
    /// This difference requires a separate parser.
    ///
    /// # JSON Format
    ///
    /// ```json
    /// {
    ///   "tokens": "明日,召問其故...",
    ///   "entities": [
    ///     {"type": "PER", "start": 11, "end": 13, "span": "玉汝"},
    ///     {"type": "PER", "start": 14, "end": 16, "span": "嚴公"}
    ///   ],
    ///   "relations": [
    ///     {"type": "上下級", "head": 1, "tail": 0, "head_span": "嚴公", "tail_span": "玉汝"}
    ///   ],
    ///   "orig_id": 1260
    /// }
    /// ```
    ///
    /// # Relation Types (12 total)
    ///
    /// | Chinese | Pinyin | English | Category |
    /// |---------|--------|---------|----------|
    /// | 敵對攻伐 | dí duì gōng fá | Attack/Hostility | Military |
    /// | 任職 | rèn zhí | Office Holding | Political |
    /// | 上下級 | shàng xià jí | Superior-subordinate | Hierarchical |
    /// | 政治奧援 | zhèng zhì ào yuán | Political Support | Political |
    /// | 同僚 | tóng liáo | Colleague | Social |
    /// | 到達 | dào dá | Arrive | Spatial |
    /// | 管理 | guǎn lǐ | Manage/Govern | Administrative |
    /// | 出生於某地 | chū shēng yú mǒu dì | Birthplace | Spatial |
    /// | 駐守 | zhù shǒu | Defend/Garrison | Military |
    /// | 別名 | bié míng | Alias | Identity |
    /// | 父母 | fù mǔ | Parents | Kinship |
    /// | 兄弟 | xiōng dì | Siblings | Kinship |
    fn parse_chisiec_relations(&self, content: &str) -> Result<Vec<RelationDocument>> {
        use super::relation::RelationGold;

        let mut documents = Vec::new();

        // Parse as JSON array
        let docs: Vec<serde_json::Value> = serde_json::from_str(content)
            .map_err(|e| Error::InvalidInput(format!("Failed to parse CHisIEC JSON: {}", e)))?;

        for doc in docs {
            // Get tokens string
            let text = match doc.get("tokens").and_then(|v| v.as_str()) {
                Some(t) => t.to_string(),
                None => continue,
            };

            if text.is_empty() {
                continue;
            }

            // Build entity list: [(type, start, end, text), ...]
            let entities_arr = doc.get("entities").and_then(|v| v.as_array());
            let mut entity_list: Vec<(String, usize, usize, String)> = Vec::new();

            if let Some(entities) = entities_arr {
                for entity in entities {
                    let ent_type = entity
                        .get("type")
                        .and_then(|v| v.as_str())
                        .unwrap_or("ENTITY")
                        .to_string();
                    let start = entity.get("start").and_then(|v| v.as_u64()).unwrap_or(0) as usize;
                    let end = entity.get("end").and_then(|v| v.as_u64()).unwrap_or(0) as usize;
                    let span = entity
                        .get("span")
                        .and_then(|v| v.as_str())
                        .unwrap_or("")
                        .to_string();

                    // Fallback to extracting from text if span is empty
                    let span_text = if !span.is_empty() {
                        span
                    } else {
                        text.chars().skip(start).take(end - start).collect()
                    };

                    entity_list.push((ent_type, start, end, span_text));
                }
            }

            // Parse relations
            let relations_arr = doc.get("relations").and_then(|v| v.as_array());
            let mut relations = Vec::new();

            if let Some(rels) = relations_arr {
                for rel in rels {
                    let rel_type = rel
                        .get("type")
                        .and_then(|v| v.as_str())
                        .unwrap_or("RELATION")
                        .to_string();
                    // CHisIEC uses entity indices for head/tail
                    let head_idx = rel.get("head").and_then(|v| v.as_u64()).unwrap_or(0) as usize;
                    let tail_idx = rel.get("tail").and_then(|v| v.as_u64()).unwrap_or(0) as usize;

                    // Look up entities by index
                    if head_idx < entity_list.len() && tail_idx < entity_list.len() {
                        let (head_type, head_start, head_end, head_text) = &entity_list[head_idx];
                        let (tail_type, tail_start, tail_end, tail_text) = &entity_list[tail_idx];

                        relations.push(RelationGold::new(
                            (*head_start, *head_end),
                            head_type.clone(),
                            head_text.clone(),
                            (*tail_start, *tail_end),
                            tail_type.clone(),
                            tail_text.clone(),
                            rel_type,
                        ));
                    }
                }
            }

            if !text.is_empty() {
                documents.push(RelationDocument { text, relations });
            }
        }

        Ok(documents)
    }

    // =========================================================================
    // African Language Dataset Parsers (Masakhane Community)
    // =========================================================================

    /// Parse AfriSenti sentiment analysis format.
    ///
    /// AfriSenti uses TSV format: text\tlabel
    /// Labels: positive, neutral, negative
    ///
    /// Source: <https://github.com/afrisenti-semeval/afrisent-semeval-2023>
    fn parse_afrisenti(&self, content: &str, id: DatasetId) -> Result<LoadedDataset> {
        let mut sentences = Vec::new();
        let now = chrono::Utc::now().to_rfc3339();

        for line in content.lines() {
            let line = line.trim();
            if line.is_empty() || line.starts_with('#') {
                continue;
            }

            let parts: Vec<&str> = line.split('\t').collect();
            if parts.len() >= 2 {
                let text = parts[0].to_string();
                let label = parts[1].to_string();

                // For sentiment, we create a single token annotation with the sentiment label
                let tokens = vec![AnnotatedToken {
                    text: text.clone(),
                    ner_tag: format!("B-{}", label),
                }];

                sentences.push(AnnotatedSentence {
                    tokens,
                    source_dataset: id,
                });
            }
        }

        if sentences.is_empty() {
            return Err(Error::InvalidInput(format!(
                "AfriSenti file for {:?} contains no valid sentences",
                id
            )));
        }

        Ok(LoadedDataset {
            id,
            sentences,
            loaded_at: now,
            source_url: id.download_url().to_string(),
            data_source: DataSource::LocalCache,
            temporal_metadata: Self::get_temporal_metadata(id),
            metadata: id.default_metadata(),
        })
    }

    /// Parse AfriQA question answering format.
    ///
    /// AfriQA uses JSON format with fields: context, question, answers
    /// Each answer has text and answer_start
    ///
    /// Source: <https://github.com/masakhane-io/afriqa>
    fn parse_afriqa(&self, content: &str, id: DatasetId) -> Result<LoadedDataset> {
        let mut sentences = Vec::new();
        let now = chrono::Utc::now().to_rfc3339();

        // AfriQA data can be JSON array or JSONL
        let docs: Vec<serde_json::Value> = if content.trim().starts_with('[') {
            serde_json::from_str(content)
                .map_err(|e| Error::InvalidInput(format!("Failed to parse AfriQA JSON: {}", e)))?
        } else {
            // JSONL format
            content
                .lines()
                .filter(|l| !l.trim().is_empty())
                .filter_map(|l| serde_json::from_str(l).ok())
                .collect()
        };

        for doc in docs {
            // Get context text
            let context = doc.get("context").and_then(|v| v.as_str()).unwrap_or("");

            // Get answers
            if let Some(answers) = doc.get("answers").and_then(|v| v.as_object()) {
                let texts = answers.get("text").and_then(|v| v.as_array());
                let starts = answers.get("answer_start").and_then(|v| v.as_array());

                if let (Some(texts), Some(starts)) = (texts, starts) {
                    // Create tokens for context (word-level tokenization)
                    let words: Vec<&str> = context.split_whitespace().collect();
                    let mut tokens: Vec<AnnotatedToken> = words
                        .iter()
                        .map(|w| AnnotatedToken {
                            text: w.to_string(),
                            ner_tag: "O".to_string(),
                        })
                        .collect();

                    // Mark answer spans
                    for (text_val, start_val) in texts.iter().zip(starts.iter()) {
                        if let (Some(answer_text), Some(start)) =
                            (text_val.as_str(), start_val.as_u64())
                        {
                            let start = start as usize;
                            let answer_words: Vec<&str> = answer_text.split_whitespace().collect();

                            // Find word index by counting words before start position
                            let prefix: String = context.chars().take(start).collect();
                            let word_idx = prefix.split_whitespace().count();

                            // Apply BIO tags
                            for (i, _) in answer_words.iter().enumerate() {
                                let idx = word_idx + i;
                                if idx < tokens.len() {
                                    tokens[idx].ner_tag = if i == 0 {
                                        "B-ANSWER".to_string()
                                    } else {
                                        "I-ANSWER".to_string()
                                    };
                                }
                            }
                        }
                    }

                    if !tokens.is_empty() {
                        sentences.push(AnnotatedSentence {
                            tokens,
                            source_dataset: id,
                        });
                    }
                }
            }
        }

        if sentences.is_empty() {
            return Err(Error::InvalidInput(format!(
                "AfriQA file for {:?} contains no valid sentences",
                id
            )));
        }

        Ok(LoadedDataset {
            id,
            sentences,
            loaded_at: now,
            source_url: id.download_url().to_string(),
            data_source: DataSource::LocalCache,
            temporal_metadata: Self::get_temporal_metadata(id),
            metadata: id.default_metadata(),
        })
    }

    /// Parse MasakhaNEWS topic classification format.
    ///
    /// MasakhaNEWS uses TSV format: headline\tbody\tcategory
    /// Categories: business, entertainment, health, politics, religion, sports, technology
    ///
    /// Source: <https://github.com/masakhane-io/masakhane-news>
    fn parse_masakhanews(&self, content: &str, id: DatasetId) -> Result<LoadedDataset> {
        let mut sentences = Vec::new();
        let now = chrono::Utc::now().to_rfc3339();

        for line in content.lines() {
            let line = line.trim();
            if line.is_empty() || line.starts_with('#') || line.starts_with("headline\t") {
                continue;
            }

            let parts: Vec<&str> = line.split('\t').collect();
            if parts.len() >= 2 {
                // Use headline as text, category as label
                let text = parts[0].to_string();
                let category = if parts.len() >= 3 {
                    parts[2].to_string()
                } else {
                    parts[1].to_string()
                };

                // For classification, create single token with category
                let tokens = vec![AnnotatedToken {
                    text,
                    ner_tag: format!("B-{}", category),
                }];

                sentences.push(AnnotatedSentence {
                    tokens,
                    source_dataset: id,
                });
            }
        }

        if sentences.is_empty() {
            return Err(Error::InvalidInput(format!(
                "MasakhaNEWS file for {:?} contains no valid sentences",
                id
            )));
        }

        Ok(LoadedDataset {
            id,
            sentences,
            loaded_at: now,
            source_url: id.download_url().to_string(),
            data_source: DataSource::LocalCache,
            temporal_metadata: Self::get_temporal_metadata(id),
            metadata: id.default_metadata(),
        })
    }

    /// Parse TREC question classification format.
    ///
    /// TREC format: `COARSE:fine question text`
    /// E.g.: `NUM:dist How far is it from Denver to Aspen ?`
    ///
    /// 6 coarse classes: ABBR, DESC, ENTY, HUM, LOC, NUM
    /// 50 fine-grained classes.
    ///
    /// Source: <https://cogcomp.seas.upenn.edu/Data/QA/QC/>
    fn parse_trec(&self, content: &str, id: DatasetId) -> Result<LoadedDataset> {
        let mut sentences = Vec::new();
        let now = chrono::Utc::now().to_rfc3339();

        for line in content.lines() {
            let line = line.trim();
            if line.is_empty() {
                continue;
            }

            // Format: COARSE:fine question text
            // Find the first space to separate label from question
            if let Some(space_idx) = line.find(' ') {
                let label = &line[..space_idx];
                let question = line[space_idx + 1..].trim();

                // Extract coarse label (before colon)
                let coarse_label = label.split(':').next().unwrap_or(label);

                // Create single token with the question and classification label
                let tokens = vec![AnnotatedToken {
                    text: question.to_string(),
                    ner_tag: format!("B-{}", coarse_label),
                }];

                sentences.push(AnnotatedSentence {
                    tokens,
                    source_dataset: id,
                });
            }
        }

        if sentences.is_empty() {
            return Err(Error::InvalidInput(format!(
                "TREC file for {:?} contains no valid sentences",
                id
            )));
        }

        Ok(LoadedDataset {
            id,
            sentences,
            loaded_at: now,
            source_url: id.download_url().to_string(),
            data_source: DataSource::LocalCache,
            temporal_metadata: Self::get_temporal_metadata(id),
            metadata: id.default_metadata(),
        })
    }

    /// Parse AG News classification format (parquet or CSV).
    ///
    /// AG News has 4 classes: World (0), Sports (1), Business (2), Sci/Tech (3)
    ///
    /// Source: <https://huggingface.co/datasets/ag_news>
    fn parse_agnews(&self, content: &str, id: DatasetId) -> Result<LoadedDataset> {
        let mut sentences = Vec::new();
        let now = chrono::Utc::now().to_rfc3339();

        let label_map = ["World", "Sports", "Business", "Sci/Tech"];

        // Try parsing as JSON (converted from parquet)
        for line in content.lines() {
            let line = line.trim();
            if line.is_empty() || line.starts_with('#') {
                continue;
            }

            // Try JSON format
            if let Ok(obj) = serde_json::from_str::<serde_json::Value>(line) {
                let text = obj.get("text").and_then(|v| v.as_str()).unwrap_or_default();
                let label_idx = obj.get("label").and_then(|v| v.as_i64()).unwrap_or(0) as usize;

                let label = label_map.get(label_idx).unwrap_or(&"Unknown");

                let tokens = vec![AnnotatedToken {
                    text: text.to_string(),
                    ner_tag: format!("B-{}", label),
                }];

                sentences.push(AnnotatedSentence {
                    tokens,
                    source_dataset: id,
                });
            }
        }

        if sentences.is_empty() {
            return Err(Error::InvalidInput(format!(
                "AG News file for {:?} contains no valid sentences",
                id
            )));
        }

        Ok(LoadedDataset {
            id,
            sentences,
            loaded_at: now,
            source_url: id.download_url().to_string(),
            data_source: DataSource::LocalCache,
            temporal_metadata: Self::get_temporal_metadata(id),
            metadata: id.default_metadata(),
        })
    }

    /// Parse DBPedia-14 classification format.
    ///
    /// 14 classes from DBpedia ontology.
    ///
    /// Source: <https://huggingface.co/datasets/dbpedia_14>
    fn parse_dbpedia14(&self, content: &str, id: DatasetId) -> Result<LoadedDataset> {
        let mut sentences = Vec::new();
        let now = chrono::Utc::now().to_rfc3339();

        let label_map = [
            "Company",
            "EducationalInstitution",
            "Artist",
            "Athlete",
            "OfficeHolder",
            "MeanOfTransportation",
            "Building",
            "NaturalPlace",
            "Village",
            "Animal",
            "Plant",
            "Album",
            "Film",
            "WrittenWork",
        ];

        for line in content.lines() {
            let line = line.trim();
            if line.is_empty() {
                continue;
            }

            if let Ok(obj) = serde_json::from_str::<serde_json::Value>(line) {
                let content_text = obj
                    .get("content")
                    .and_then(|v| v.as_str())
                    .unwrap_or_default();
                let label_idx = obj.get("label").and_then(|v| v.as_i64()).unwrap_or(0) as usize;

                let label = label_map.get(label_idx).unwrap_or(&"Unknown");

                let tokens = vec![AnnotatedToken {
                    text: content_text.to_string(),
                    ner_tag: format!("B-{}", label),
                }];

                sentences.push(AnnotatedSentence {
                    tokens,
                    source_dataset: id,
                });
            }
        }

        if sentences.is_empty() {
            return Err(Error::InvalidInput(format!(
                "DBPedia-14 file for {:?} contains no valid sentences",
                id
            )));
        }

        Ok(LoadedDataset {
            id,
            sentences,
            loaded_at: now,
            source_url: id.download_url().to_string(),
            data_source: DataSource::LocalCache,
            temporal_metadata: Self::get_temporal_metadata(id),
            metadata: id.default_metadata(),
        })
    }

    /// Parse Yahoo Answers topic classification format.
    ///
    /// 10 topics: Society, Science, Health, Education, Computers,
    /// Sports, Business, Entertainment, Family, Politics
    ///
    /// Source: <https://huggingface.co/datasets/yahoo_answers_topics>
    fn parse_yahoo_answers(&self, content: &str, id: DatasetId) -> Result<LoadedDataset> {
        let mut sentences = Vec::new();
        let now = chrono::Utc::now().to_rfc3339();

        let label_map = [
            "Society",
            "Science",
            "Health",
            "Education",
            "Computers",
            "Sports",
            "Business",
            "Entertainment",
            "Family",
            "Politics",
        ];

        for line in content.lines() {
            let line = line.trim();
            if line.is_empty() {
                continue;
            }

            if let Ok(obj) = serde_json::from_str::<serde_json::Value>(line) {
                // Yahoo Answers has question_title, question_content, best_answer
                let question = obj
                    .get("question_title")
                    .and_then(|v| v.as_str())
                    .unwrap_or_default();
                let label_idx = obj.get("topic").and_then(|v| v.as_i64()).unwrap_or(0) as usize;

                let label = label_map.get(label_idx).unwrap_or(&"Unknown");

                let tokens = vec![AnnotatedToken {
                    text: question.to_string(),
                    ner_tag: format!("B-{}", label),
                }];

                sentences.push(AnnotatedSentence {
                    tokens,
                    source_dataset: id,
                });
            }
        }

        if sentences.is_empty() {
            return Err(Error::InvalidInput(format!(
                "Yahoo Answers file for {:?} contains no valid sentences",
                id
            )));
        }

        Ok(LoadedDataset {
            id,
            sentences,
            loaded_at: now,
            source_url: id.download_url().to_string(),
            data_source: DataSource::LocalCache,
            temporal_metadata: Self::get_temporal_metadata(id),
            metadata: id.default_metadata(),
        })
    }

    /// Parse MAVEN event detection format.
    ///
    /// MAVEN provides event triggers with 168 event types.
    /// Supports both:
    /// - Full MAVEN JSONL format (train.jsonl/valid.jsonl with events array)
    /// - Fallback: docid2topic.json mapping file
    ///
    /// Full format structure:
    /// ```json
    /// {
    ///   "id": "doc_id",
    ///   "content": [{"sentence": "...", "tokens": [...]}],
    ///   "events": [{
    ///     "type": "EventType",
    ///     "mention": [{"trigger_word": "...", "sent_id": 0, "offset": [3, 4]}]
    ///   }]
    /// }
    /// ```
    ///
    /// Source: <https://github.com/THU-KEG/MAVEN-dataset>
    fn parse_maven(&self, content: &str, id: DatasetId) -> Result<LoadedDataset> {
        let mut sentences = Vec::new();
        let now = chrono::Utc::now().to_rfc3339();

        // Try parsing as JSONL (full MAVEN format)
        let mut is_jsonl = false;
        for line in content.lines() {
            let line = line.trim();
            if line.is_empty() {
                continue;
            }

            if let Ok(doc) = serde_json::from_str::<serde_json::Value>(line) {
                // Check if this is full MAVEN format with events
                if let Some(events) = doc.get("events").and_then(|e| e.as_array()) {
                    is_jsonl = true;

                    // Get document content for context
                    let doc_content: Vec<String> = doc
                        .get("content")
                        .and_then(|c| c.as_array())
                        .map(|sents| {
                            sents
                                .iter()
                                .filter_map(|s| s.get("sentence").and_then(|v| v.as_str()))
                                .map(|s| s.to_string())
                                .collect()
                        })
                        .unwrap_or_default();

                    // Process each event
                    for event in events {
                        let event_type = event
                            .get("type")
                            .and_then(|t| t.as_str())
                            .unwrap_or("EVENT");

                        // Process each mention of this event
                        if let Some(mentions) = event.get("mention").and_then(|m| m.as_array()) {
                            for mention in mentions {
                                let trigger_word = mention
                                    .get("trigger_word")
                                    .and_then(|t| t.as_str())
                                    .unwrap_or("");

                                let sent_id =
                                    mention.get("sent_id").and_then(|s| s.as_u64()).unwrap_or(0)
                                        as usize;

                                // Get sentence context if available
                                let context = doc_content
                                    .get(sent_id)
                                    .cloned()
                                    .unwrap_or_else(|| trigger_word.to_string());

                                let tokens = vec![AnnotatedToken {
                                    text: context,
                                    ner_tag: format!("B-{}", event_type),
                                }];

                                sentences.push(AnnotatedSentence {
                                    tokens,
                                    source_dataset: id,
                                });
                            }
                        }
                    }
                }
            }
        }

        // Fallback: parse as docid2topic.json mapping
        if !is_jsonl {
            if let Ok(obj) = serde_json::from_str::<serde_json::Value>(content) {
                if let Some(map) = obj.as_object() {
                    for (doc_id, event_type) in map {
                        let event_type_str = event_type.as_str().unwrap_or("event");

                        let tokens = vec![AnnotatedToken {
                            text: doc_id.clone(),
                            ner_tag: format!(
                                "B-EVENT_{}",
                                event_type_str.to_uppercase().replace(' ', "_")
                            ),
                        }];

                        sentences.push(AnnotatedSentence {
                            tokens,
                            source_dataset: id,
                        });
                    }
                }
            }
        }

        Ok(LoadedDataset {
            id,
            sentences,
            loaded_at: now,
            source_url: id.download_url().to_string(),
            data_source: DataSource::LocalCache,
            temporal_metadata: Self::get_temporal_metadata(id),
            metadata: id.default_metadata(),
        })
    }

    /// Parse CASIE cybersecurity event extraction format.
    ///
    /// CASIE has 5 event types: Attack-Pattern, Vulnerability, Data-Breach, Malware, Patch
    ///
    /// Format structure:
    /// ```json
    /// {
    ///   "content": "document text...",
    ///   "cyberevent": {
    ///     "hopper": [{
    ///       "events": [{
    ///         "subtype": "Databreach",
    ///         "nugget": {"text": "trigger", "startOffset": 0, "endOffset": 10},
    ///         "argument": [{"text": "arg", "role": {"type": "RoleType"}}]
    ///       }]
    ///     }]
    ///   }
    /// }
    /// ```
    ///
    /// Source: <https://github.com/Ebiquity/CASIE>
    fn parse_casie(&self, content: &str, id: DatasetId) -> Result<LoadedDataset> {
        let mut sentences = Vec::new();
        let now = chrono::Utc::now().to_rfc3339();

        // Handle JSONL format (multiple documents)
        for line in content.lines() {
            let line = line.trim();
            if line.is_empty() {
                continue;
            }

            if let Ok(doc) = serde_json::from_str::<serde_json::Value>(line) {
                let content_text = doc
                    .get("content")
                    .and_then(|v| v.as_str())
                    .unwrap_or_default();

                if content_text.is_empty() {
                    continue;
                }

                // Extract events from cyberevent.hopper[].events[]
                let mut found_events = false;
                if let Some(hopper) = doc
                    .get("cyberevent")
                    .and_then(|ce| ce.get("hopper"))
                    .and_then(|h| h.as_array())
                {
                    for cluster in hopper {
                        if let Some(events) = cluster.get("events").and_then(|e| e.as_array()) {
                            for event in events {
                                found_events = true;

                                // Get event subtype
                                let subtype = event
                                    .get("subtype")
                                    .and_then(|s| s.as_str())
                                    .unwrap_or("Event");

                                // Get trigger (nugget)
                                let trigger_text = event
                                    .get("nugget")
                                    .and_then(|n| n.get("text"))
                                    .and_then(|t| t.as_str())
                                    .unwrap_or("");

                                // Create entry with trigger and event type
                                let tokens = vec![AnnotatedToken {
                                    text: trigger_text.to_string(),
                                    ner_tag: format!("B-{}", subtype),
                                }];

                                sentences.push(AnnotatedSentence {
                                    tokens,
                                    source_dataset: id,
                                });

                                // Also extract arguments if present
                                if let Some(args) = event.get("argument").and_then(|a| a.as_array())
                                {
                                    for arg in args {
                                        let arg_text =
                                            arg.get("text").and_then(|t| t.as_str()).unwrap_or("");
                                        let role = arg
                                            .get("role")
                                            .and_then(|r| r.get("type"))
                                            .and_then(|t| t.as_str())
                                            .unwrap_or("Argument");

                                        if !arg_text.is_empty() {
                                            let tokens = vec![AnnotatedToken {
                                                text: arg_text.to_string(),
                                                ner_tag: format!("B-ARG_{}", role),
                                            }];

                                            sentences.push(AnnotatedSentence {
                                                tokens,
                                                source_dataset: id,
                                            });
                                        }
                                    }
                                }
                            }
                        }
                    }
                }

                // If no events found, add document with O tag
                if !found_events {
                    let tokens = vec![AnnotatedToken {
                        text: content_text.chars().take(200).collect(),
                        ner_tag: "O".to_string(),
                    }];

                    sentences.push(AnnotatedSentence {
                        tokens,
                        source_dataset: id,
                    });
                }
            }
        }

        if sentences.is_empty() {
            return Err(Error::InvalidInput(format!(
                "MAVEN file for {:?} contains no valid sentences",
                id
            )));
        }

        Ok(LoadedDataset {
            id,
            sentences,
            loaded_at: now,
            source_url: id.download_url().to_string(),
            data_source: DataSource::LocalCache,
            temporal_metadata: Self::get_temporal_metadata(id),
            metadata: id.default_metadata(),
        })
    }

    /// Parse MAVEN-ARG event argument extraction format.
    ///
    /// MAVEN-ARG extends MAVEN with 612 argument roles across 162 event types.
    ///
    /// Format structure:
    /// ```json
    /// {
    ///   "id": "doc_id",
    ///   "document": "full document text...",
    ///   "events": [{
    ///     "type": "EventType",
    ///     "mention": [{"trigger_word": "...", "offset": [start, end]}],
    ///     "argument": {"Role": [{"content": "arg text", "offset": [s, e]}]}
    ///   }]
    /// }
    /// ```
    ///
    /// Source: <https://github.com/THU-KEG/MAVEN-Argument>
    fn parse_maven_arg(&self, content: &str, id: DatasetId) -> Result<LoadedDataset> {
        let mut sentences = Vec::new();
        let now = chrono::Utc::now().to_rfc3339();

        for line in content.lines() {
            let line = line.trim();
            if line.is_empty() {
                continue;
            }

            if let Ok(doc) = serde_json::from_str::<serde_json::Value>(line) {
                // Get document text
                let _doc_text = doc.get("document").and_then(|d| d.as_str()).unwrap_or("");

                // Process events
                if let Some(events) = doc.get("events").and_then(|e| e.as_array()) {
                    for event in events {
                        let event_type = event
                            .get("type")
                            .and_then(|t| t.as_str())
                            .unwrap_or("EVENT");

                        // Process event mentions (triggers)
                        if let Some(mentions) = event.get("mention").and_then(|m| m.as_array()) {
                            for mention in mentions {
                                let trigger = mention
                                    .get("trigger_word")
                                    .and_then(|t| t.as_str())
                                    .unwrap_or("");

                                if !trigger.is_empty() {
                                    let tokens = vec![AnnotatedToken {
                                        text: trigger.to_string(),
                                        ner_tag: format!("B-{}", event_type),
                                    }];

                                    sentences.push(AnnotatedSentence {
                                        tokens,
                                        source_dataset: id,
                                    });
                                }
                            }
                        }

                        // Process arguments
                        if let Some(args) = event.get("argument").and_then(|a| a.as_object()) {
                            for (role, arg_list) in args {
                                if let Some(arg_arr) = arg_list.as_array() {
                                    for arg in arg_arr {
                                        // Arguments can be text or entity references
                                        if let Some(content) =
                                            arg.get("content").and_then(|c| c.as_str())
                                        {
                                            if !content.is_empty() {
                                                let tokens = vec![AnnotatedToken {
                                                    text: content.to_string(),
                                                    ner_tag: format!("B-ARG_{}", role),
                                                }];

                                                sentences.push(AnnotatedSentence {
                                                    tokens,
                                                    source_dataset: id,
                                                });
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

        if sentences.is_empty() {
            return Err(Error::InvalidInput(format!(
                "MAVEN-ARG file for {:?} contains no valid sentences",
                id
            )));
        }

        Ok(LoadedDataset {
            id,
            sentences,
            loaded_at: now,
            source_url: id.download_url().to_string(),
            data_source: DataSource::LocalCache,
            temporal_metadata: Self::get_temporal_metadata(id),
            metadata: id.default_metadata(),
        })
    }

    /// Parse RAMS event argument extraction format.
    ///
    /// RAMS (Roles Across Multiple Sentences) covers 139 event types
    /// with arguments that can span multiple sentences.
    ///
    /// Format structure:
    /// ```json
    /// {
    ///   "doc_key": "...",
    ///   "sentences": [["token1", "token2", ...]],
    ///   "evt_triggers": [[start, end, [["event.type", score]]]],
    ///   "gold_evt_links": [[[evt_idx], [arg_start, arg_end], "role"]]
    /// }
    /// ```
    ///
    /// Source: <https://nlp.jhu.edu/rams/>
    fn parse_rams(&self, content: &str, id: DatasetId) -> Result<LoadedDataset> {
        let mut sentences = Vec::new();
        let now = chrono::Utc::now().to_rfc3339();

        for line in content.lines() {
            let line = line.trim();
            if line.is_empty() {
                continue;
            }

            if let Ok(doc) = serde_json::from_str::<serde_json::Value>(line) {
                // Get all tokens (flattened sentences)
                let all_tokens: Vec<String> = doc
                    .get("sentences")
                    .and_then(|s| s.as_array())
                    .map(|sents| {
                        sents
                            .iter()
                            .filter_map(|s| s.as_array())
                            .flat_map(|toks| {
                                toks.iter().filter_map(|t| t.as_str().map(String::from))
                            })
                            .collect()
                    })
                    .unwrap_or_default();

                // Process event triggers
                if let Some(triggers) = doc.get("evt_triggers").and_then(|t| t.as_array()) {
                    for trigger in triggers {
                        if let Some(trigger_arr) = trigger.as_array() {
                            if trigger_arr.len() >= 3 {
                                let start = trigger_arr[0].as_u64().unwrap_or(0) as usize;
                                let end = trigger_arr[1].as_u64().unwrap_or(0) as usize;

                                // Get event type from nested array
                                let event_type = trigger_arr[2]
                                    .as_array()
                                    .and_then(|types| types.first())
                                    .and_then(|t| t.as_array())
                                    .and_then(|t| t.first())
                                    .and_then(|t| t.as_str())
                                    .unwrap_or("event");

                                // Extract trigger text
                                if end <= all_tokens.len() {
                                    let trigger_text = all_tokens[start..=end.min(start)].join(" ");
                                    let tokens = vec![AnnotatedToken {
                                        text: trigger_text,
                                        ner_tag: format!("B-{}", event_type),
                                    }];

                                    sentences.push(AnnotatedSentence {
                                        tokens,
                                        source_dataset: id,
                                    });
                                }
                            }
                        }
                    }
                }

                // Process argument links
                if let Some(links) = doc.get("gold_evt_links").and_then(|l| l.as_array()) {
                    for link in links {
                        if let Some(link_arr) = link.as_array() {
                            if link_arr.len() >= 3 {
                                // Argument span
                                if let Some(span) = link_arr[1].as_array() {
                                    if span.len() >= 2 {
                                        let start = span[0].as_u64().unwrap_or(0) as usize;
                                        let end = span[1].as_u64().unwrap_or(0) as usize;
                                        let role = link_arr[2].as_str().unwrap_or("argument");

                                        if end < all_tokens.len() {
                                            let arg_text =
                                                all_tokens[start..=end.min(start)].join(" ");
                                            let tokens = vec![AnnotatedToken {
                                                text: arg_text,
                                                ner_tag: format!("B-ARG_{}", role),
                                            }];

                                            sentences.push(AnnotatedSentence {
                                                tokens,
                                                source_dataset: id,
                                            });
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

        if sentences.is_empty() {
            return Err(Error::InvalidInput(format!(
                "RAMS file for {:?} contains no valid sentences",
                id
            )));
        }

        Ok(LoadedDataset {
            id,
            sentences,
            loaded_at: now,
            source_url: id.download_url().to_string(),
            data_source: DataSource::LocalCache,
            temporal_metadata: Self::get_temporal_metadata(id),
            metadata: id.default_metadata(),
        })
    }

    /// Parse TweetTopic format (JSONL with text and labels).
    ///
    /// Format: {"text": "...", "label": 0, "label_name": "sports_&_gaming", ...}
    ///
    /// Source: <https://huggingface.co/datasets/cardiffnlp/tweet_topic_single>
    fn parse_tweettopic(&self, content: &str, id: DatasetId) -> Result<LoadedDataset> {
        let mut sentences = Vec::new();
        let now = chrono::Utc::now().to_rfc3339();

        // Label mapping if label_name not present
        let label_map = [
            "arts_&_culture",
            "business_&_entrepreneurs",
            "pop_culture",
            "daily_life",
            "sports_&_gaming",
            "science_&_technology",
        ];

        for line in content.lines() {
            let line = line.trim();
            if line.is_empty() {
                continue;
            }

            if let Ok(obj) = serde_json::from_str::<serde_json::Value>(line) {
                let text = obj.get("text").and_then(|v| v.as_str()).unwrap_or_default();

                // Try label_name first, then map from label index
                let label = obj
                    .get("label_name")
                    .and_then(|v| v.as_str())
                    .map(|s| s.to_string())
                    .or_else(|| {
                        obj.get("label")
                            .and_then(|v| v.as_i64())
                            .and_then(|idx| label_map.get(idx as usize))
                            .map(|s| s.to_string())
                    })
                    .unwrap_or_else(|| "topic".to_string());

                let tokens = vec![AnnotatedToken {
                    text: text.to_string(),
                    ner_tag: format!("B-{}", label),
                }];

                sentences.push(AnnotatedSentence {
                    tokens,
                    source_dataset: id,
                });
            }
        }

        if sentences.is_empty() {
            return Err(Error::InvalidInput(format!(
                "TweetTopic file for {:?} contains no valid sentences",
                id
            )));
        }

        Ok(LoadedDataset {
            id,
            sentences,
            loaded_at: now,
            source_url: id.download_url().to_string(),
            data_source: DataSource::LocalCache,
            temporal_metadata: Self::get_temporal_metadata(id),
            metadata: id.default_metadata(),
        })
    }

    /// Parse CoNLL-U format (Universal Dependencies).
    ///
    /// CoNLL-U format has 10 tab-separated columns per line:
    /// ID FORM LEMMA UPOS XPOS FEATS HEAD DEPREL DEPS MISC
    ///
    /// Used by MasakhaPOS for African language POS tagging.
    ///
    /// Source: <https://universaldependencies.org/format.html>
    fn parse_conllu(&self, content: &str, id: DatasetId) -> Result<LoadedDataset> {
        let mut sentences = Vec::new();
        let now = chrono::Utc::now().to_rfc3339();
        let mut current_tokens = Vec::new();

        for line in content.lines() {
            let line = line.trim();

            // Skip comments
            if line.starts_with('#') {
                continue;
            }

            // Blank line marks end of sentence
            if line.is_empty() {
                if !current_tokens.is_empty() {
                    sentences.push(AnnotatedSentence {
                        tokens: std::mem::take(&mut current_tokens),
                        source_dataset: id,
                    });
                }
                continue;
            }

            // Parse CoNLL-U columns
            let fields: Vec<&str> = line.split('\t').collect();
            if fields.len() >= 4 {
                // Skip multi-word tokens (IDs with ranges like "1-2")
                let id_field = fields[0];
                if id_field.contains('-') || id_field.contains('.') {
                    continue;
                }

                let form = fields[1]; // Word form
                let upos = fields[3]; // Universal POS tag

                current_tokens.push(AnnotatedToken {
                    text: form.to_string(),
                    ner_tag: format!("B-{}", upos), // Use POS tag as entity type
                });
            }
        }

        // Don't forget last sentence
        if !current_tokens.is_empty() {
            sentences.push(AnnotatedSentence {
                tokens: current_tokens,
                source_dataset: id,
            });
        }

        if sentences.is_empty() {
            return Err(Error::InvalidInput(format!(
                "CoNLL-U file for {:?} contains no valid sentences",
                id
            )));
        }

        Ok(LoadedDataset {
            id,
            sentences,
            loaded_at: now,
            source_url: id.download_url().to_string(),
            data_source: DataSource::LocalCache,
            temporal_metadata: Self::get_temporal_metadata(id),
            metadata: id.default_metadata(),
        })
    }

    /// Load all cached datasets.
    pub fn load_all_cached(&self) -> Vec<(DatasetId, Result<LoadedDataset>)> {
        LoadableDatasetId::all()
            .into_iter()
            .filter(|id| self.is_cached(*id))
            .map(|id| (id.0, self.load(id)))
            .collect()
    }

    /// Get status of all datasets.
    #[must_use]
    pub fn status(&self) -> Vec<(DatasetId, bool)> {
        LoadableDatasetId::all()
            .into_iter()
            .map(|id| (id.0, self.is_cached(id)))
            .collect()
    }
}

impl Default for DatasetLoader {
    fn default() -> Self {
        Self::new().expect("Failed to create default DatasetLoader")
    }
}

// =============================================================================
// Helper Functions
// =============================================================================

/// Parse BIO tag into prefix and type.
#[cfg(test)]
fn parse_bio_tag(tag: &str) -> (&str, &str) {
    if tag == "O" {
        return ("O", "");
    }

    // Handle B-PER, I-LOC, etc.
    if let Some(pos) = tag.find('-') {
        (&tag[..pos], &tag[pos + 1..])
    } else {
        // No prefix, treat as entity type with implicit B
        ("B", tag)
    }
}

/// Map dataset-specific entity types to our EntityType enum.
///
/// **Prefer `crate::schema::map_to_canonical()` for new code** - it handles
/// NORP correctly (as GROUP, not ORG) and preserves GPE/FAC distinctions.
///
/// # Known Issues (preserved for backwards compatibility)
///
/// - NORP → Organization (WRONG: should be Group)
/// - GPE/FAC/LOC all → Location (loses semantic distinctions)
///
/// See `src/schema.rs` for the corrected mappings.
#[cfg(test)]
fn map_entity_type(original: &str) -> EntityType {
    // Use the new canonical mapper for consistent semantics
    anno::schema::map_to_canonical(original, None)
}

// =============================================================================
// Tests
// =============================================================================

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_dataset_id_basics() {
        let id = DatasetId::WikiGold;
        assert_eq!(id.name(), "WikiGold");
    }

    #[test]
    fn test_convenience_metadata_methods() {
        let id = DatasetId::WikiGold;

        // WikiGold has known metadata
        assert_eq!(id.citation(), Some("Balasuriya et al. (2009)"));
        assert_eq!(id.license(), Some("CC-BY-4.0"));
        assert_eq!(id.year(), Some(2009));
    }

    #[test]
    fn test_loadable_wrapper_invariants() {
        // There should be at least one non-loadable dataset in the catalog.
        assert!(
            DatasetId::all()
                .iter()
                .copied()
                .any(|d| !LoadableDatasetId::is_loadable_dataset(d)),
            "Expected registry to contain some non-loadable datasets"
        );

        for id in LoadableDatasetId::all() {
            let ds: DatasetId = id.into();
            assert!(
                LoadableDatasetId::is_loadable_dataset(ds),
                "LoadableDatasetId must imply is_loadable_dataset()"
            );
            assert!(LoadableDatasetId::try_from(ds).is_ok());
        }
    }

    #[test]
    fn test_parse_plan_is_single_source_of_truth_for_loadability() {
        // For *every* registry dataset id:
        // - parse_plan(id).is_some()  <=>  TryFrom<DatasetId> succeeds
        for &ds in DatasetId::all() {
            let plan_exists = LoadableDatasetId::parse_plan(ds).is_some();
            let try_ok = LoadableDatasetId::try_from(ds).is_ok();
            assert_eq!(
                plan_exists, try_ok,
                "parse_plan / TryFrom mismatch for {:?}",
                ds
            );
        }

        // Also ensure `LoadableDatasetId::all()` only returns ids with plans.
        for id in LoadableDatasetId::all() {
            let ds: DatasetId = id.into();
            assert!(
                LoadableDatasetId::parse_plan(ds).is_some(),
                "LoadableDatasetId::all() returned {:?} with no parse plan",
                ds
            );
        }
    }

    #[test]
    fn test_registry_hints_do_not_contradict_parse_plan() {
        // If a dataset is loadable (i.e., has a parse plan), and the registry provides a strong
        // hint, they should agree. (Hints are allowed to be optimistic for non-loadable datasets.)
        for &ds in DatasetId::all() {
            let Some(plan) = LoadableDatasetId::parse_plan(ds) else {
                continue;
            };
            let Some(hint) = LoadableDatasetId::registry_hint_plan(ds) else {
                continue;
            };
            assert_eq!(hint, plan, "Registry hint mismatch for {:?}", ds);
        }
    }

    #[test]
    fn test_huggingface_access_status_requires_hf_id() {
        // `access_status: HuggingFace` is our strongest signal that a dataset is automatable
        // via the Hub. Keep the registry self-consistent so hinting can stay metadata-driven.
        for &ds in DatasetId::all() {
            if ds.access_status()
                != crate::eval::dataset_registry::DatasetAccessibility::HuggingFace
            {
                continue;
            }
            assert!(
                ds.hf_id().is_some(),
                "Dataset {:?} is marked HuggingFace-accessible but has no hf_id",
                ds
            );
        }
    }

    #[test]
    fn test_huggingface_access_status_is_hintable() {
        // If the registry says “HuggingFace”, the loader should be able to produce *some*
        // parse-plan hint (not necessarily `HfApiResponse`, since a few datasets are hybrids
        // with bespoke parse plans).
        for &ds in DatasetId::all() {
            if ds.access_status()
                != crate::eval::dataset_registry::DatasetAccessibility::HuggingFace
            {
                continue;
            }
            assert!(
                LoadableDatasetId::registry_hint_plan(ds).is_some(),
                "Dataset {:?} is marked HuggingFace-accessible but has no registry hint plan",
                ds
            );
        }
    }

    #[test]
    fn test_parse_bio_tag() {
        assert_eq!(parse_bio_tag("O"), ("O", ""));
        assert_eq!(parse_bio_tag("B-PER"), ("B", "PER"));
        assert_eq!(parse_bio_tag("I-LOC"), ("I", "LOC"));
        assert_eq!(parse_bio_tag("B-ORG"), ("B", "ORG"));
    }

    #[test]
    fn test_map_entity_type() {
        // Core types
        assert_eq!(map_entity_type("PER"), EntityType::Person);
        assert_eq!(map_entity_type("PERSON"), EntityType::Person);
        assert_eq!(map_entity_type("LOC"), EntityType::Location);
        assert_eq!(map_entity_type("ORG"), EntityType::Organization);

        // GPE now preserves distinction (Custom, not Location)
        assert!(matches!(map_entity_type("GPE"), EntityType::Custom { .. }));

        // MISC -> Custom or Other
        assert!(matches!(map_entity_type("MISC"), EntityType::Custom { .. }));

        // OntoNotes types -> Custom (preserves semantics)
        assert!(matches!(
            map_entity_type("PRODUCT"),
            EntityType::Custom { .. }
        ));
        assert!(matches!(
            map_entity_type("EVENT"),
            EntityType::Custom { .. }
        ));
        assert!(matches!(
            map_entity_type("WORK_OF_ART"),
            EntityType::Custom { .. }
        ));

        // Numeric types preserved
        assert_eq!(map_entity_type("CARDINAL"), EntityType::Cardinal);
    }

    #[test]
    fn test_dataset_id_display() {
        assert_eq!(DatasetId::WikiGold.to_string(), "WikiGold");
        assert_eq!(DatasetId::Wnut17.to_string(), "WNUT-17");
    }

    #[test]
    fn test_dataset_id_from_str() {
        assert_eq!(
            "wikigold".parse::<DatasetId>().unwrap(),
            DatasetId::WikiGold
        );
        assert_eq!("wnut-17".parse::<DatasetId>().unwrap(), DatasetId::Wnut17);
        assert_eq!(
            "mit_movie".parse::<DatasetId>().unwrap(),
            DatasetId::MitMovie
        );
    }

    #[test]
    fn test_annotated_sentence_text() {
        let sentence = AnnotatedSentence {
            tokens: vec![
                AnnotatedToken {
                    text: "John".into(),
                    ner_tag: "B-PER".into(),
                },
                AnnotatedToken {
                    text: "lives".into(),
                    ner_tag: "O".into(),
                },
                AnnotatedToken {
                    text: "in".into(),
                    ner_tag: "O".into(),
                },
                AnnotatedToken {
                    text: "New".into(),
                    ner_tag: "B-LOC".into(),
                },
                AnnotatedToken {
                    text: "York".into(),
                    ner_tag: "I-LOC".into(),
                },
            ],
            source_dataset: DatasetId::WikiGold,
        };

        assert_eq!(sentence.text(), "John lives in New York");
    }

    #[test]
    fn test_annotated_sentence_entities() {
        let sentence = AnnotatedSentence {
            tokens: vec![
                AnnotatedToken {
                    text: "John".into(),
                    ner_tag: "B-PER".into(),
                },
                AnnotatedToken {
                    text: "Smith".into(),
                    ner_tag: "I-PER".into(),
                },
                AnnotatedToken {
                    text: "works".into(),
                    ner_tag: "O".into(),
                },
                AnnotatedToken {
                    text: "at".into(),
                    ner_tag: "O".into(),
                },
                AnnotatedToken {
                    text: "Google".into(),
                    ner_tag: "B-ORG".into(),
                },
            ],
            source_dataset: DatasetId::WikiGold,
        };

        let entities = sentence.entities();
        assert_eq!(entities.len(), 2);
        assert_eq!(entities[0].text, "John Smith");
        assert_eq!(entities[0].entity_type, EntityType::Person);
        assert_eq!(entities[1].text, "Google");
        assert_eq!(entities[1].entity_type, EntityType::Organization);
    }

    #[test]
    fn test_parse_conll_format() {
        let content = r#"
John B-PER
Smith I-PER
works O
at O
Google B-ORG
. O

Apple B-ORG
announced O
today O
. O
"#;

        let loader = DatasetLoader::new().unwrap();
        let dataset = loader.parse_conll(content, DatasetId::WikiGold).unwrap();

        assert_eq!(dataset.len(), 2);
        assert_eq!(dataset.entity_count(), 3);
    }

    #[test]
    fn test_parse_conll2003_format() {
        // CoNLL-2003 has 4 columns: word POS chunk NER
        let content = r#"
-DOCSTART- -X- -X- O

EU NNP B-NP B-ORG
rejects VBZ B-VP O
German JJ B-NP B-MISC
call NN I-NP O
. . O O

Peter NNP B-NP B-PER
Blackburn NNP I-NP I-PER
"#;

        let loader = DatasetLoader::new().unwrap();
        let dataset = loader
            .parse_conll(content, DatasetId::CoNLL2003Sample)
            .unwrap();

        assert_eq!(dataset.len(), 2);

        let entities1 = dataset.sentences[0].entities();
        assert_eq!(entities1.len(), 2); // EU (ORG), German (MISC)

        let entities2 = dataset.sentences[1].entities();
        assert_eq!(entities2.len(), 1); // Peter Blackburn (PER)
        assert_eq!(entities2[0].text, "Peter Blackburn");
    }

    #[test]
    fn test_historical_datasets_configured() {
        // Historical NER datasets should have proper metadata
        assert!(!DatasetId::HIPE2022.download_url().is_empty());
        assert_eq!(DatasetId::HIPE2022.name(), "HIPE-2022");

        assert!(!DatasetId::MedievalCzechCharters.download_url().is_empty());
        assert_eq!(
            DatasetId::MedievalCzechCharters.name(),
            "Medieval Czech Charters"
        );

        assert!(!DatasetId::TRIDIS.download_url().is_empty());
        assert_eq!(DatasetId::TRIDIS.name(), "TRIDIS");

        // Should be in all() list
        let all = DatasetId::all();
        assert!(all.contains(&DatasetId::HIPE2022));
        assert!(all.contains(&DatasetId::TRIDIS));
    }

    #[test]
    fn test_queer_nlp_datasets_configured() {
        // Queer/gender-inclusive NLP datasets
        assert!(!DatasetId::WinoQueer.download_url().is_empty());
        assert_eq!(DatasetId::WinoQueer.name(), "WinoQueer");

        assert!(!DatasetId::GICoref.download_url().is_empty());
        assert_eq!(DatasetId::GICoref.name(), "GICoref");

        assert!(!DatasetId::BBQ.download_url().is_empty());
        assert_eq!(DatasetId::BBQ.name(), "BBQ");

        // Should be in all() list
        let all = DatasetId::all();
        assert!(all.contains(&DatasetId::WinoQueer));
        assert!(all.contains(&DatasetId::GICoref));
        assert!(all.contains(&DatasetId::BBQ));
    }

    #[test]
    fn test_joint_re_datasets_configured() {
        // Joint NER + Relation Extraction datasets
        assert!(
            DatasetId::TACRED.requires_license(),
            "TACRED is LDC-licensed; download_url may be empty"
        );
        assert_eq!(DatasetId::TACRED.name(), "TACRED");

        assert!(!DatasetId::REBEL.download_url().is_empty());
        assert_eq!(DatasetId::REBEL.name(), "REBEL");

        // Should be in all() list
        let all = DatasetId::all();
        assert!(all.contains(&DatasetId::TACRED));
        assert!(all.contains(&DatasetId::REBEL));
    }

    #[test]
    fn test_dialogue_coref_datasets_configured() {
        // Dialogue/streaming coreference datasets
        assert!(!DatasetId::CODICRAC.download_url().is_empty());
        assert_eq!(DatasetId::CODICRAC.name(), "CODI-CRAC");

        assert!(!DatasetId::AMIMeeting.download_url().is_empty());
        assert_eq!(DatasetId::AMIMeeting.name(), "AMI Meeting");

        assert!(
            DatasetId::ARRAU.requires_license(),
            "ARRAU has LDC + research distribution; download_url may be empty"
        );
        assert!(
            DatasetId::ARRAU.name().contains("ARRAU"),
            "ARRAU name should contain 'ARRAU'"
        );

        // Should be in all() list
        let all = DatasetId::all();
        assert!(all.contains(&DatasetId::CODICRAC));
        assert!(all.contains(&DatasetId::AMIMeeting));
        assert!(all.contains(&DatasetId::ARRAU));
    }

    #[test]
    fn test_is_historical_classification() {
        // Historical datasets
        assert!(DatasetId::HIPE2022.is_historical());
        assert!(DatasetId::MedievalCzechCharters.is_historical());
        assert!(DatasetId::EighteenthCenturyNER.is_historical());
        assert!(DatasetId::HistoricalChineseNER.is_historical());

        // Non-historical should return false
        assert!(!DatasetId::WikiGold.is_historical());
        assert!(!DatasetId::CoNLL2003Sample.is_historical());
    }

    #[test]
    fn test_is_bias_evaluation_classification() {
        // Bias/fairness datasets
        assert!(DatasetId::WinoQueer.is_bias_evaluation());
        assert!(DatasetId::BBQ.is_bias_evaluation());
        assert!(DatasetId::GICoref.is_bias_evaluation());
        assert!(DatasetId::WinoBias.is_bias_evaluation());
        assert!(DatasetId::GAP.is_bias_evaluation());

        // Non-bias should return false
        assert!(!DatasetId::WikiGold.is_bias_evaluation());
    }

    #[test]
    fn test_new_datasets_have_descriptions() {
        // All new datasets should have proper descriptions (not the catch-all)
        let catch_all = "Dataset not yet fully integrated";

        // Historical
        assert_ne!(DatasetId::HIPE2022.description(), catch_all);
        assert_ne!(DatasetId::TRIDIS.description(), catch_all);

        // Queer NLP
        assert_ne!(DatasetId::WinoQueer.description(), catch_all);
        assert_ne!(DatasetId::BBQ.description(), catch_all);
        assert_ne!(DatasetId::GICoref.description(), catch_all);

        // Joint NER+RE
        assert_ne!(DatasetId::TACRED.description(), catch_all);
        assert_ne!(DatasetId::REBEL.description(), catch_all);

        // Dialogue
        assert_ne!(DatasetId::CODICRAC.description(), catch_all);
        assert_ne!(DatasetId::ARRAU.description(), catch_all);
    }

    #[test]
    fn test_coreference_includes_new_datasets() {
        // All coreference datasets should be detected
        assert!(DatasetId::GICoref.is_coreference());
        assert!(DatasetId::CODICRAC.is_coreference());
        assert!(DatasetId::ARRAU.is_coreference());
        assert!(DatasetId::WinoPron.is_coreference());
        assert!(DatasetId::DROC.is_coreference());
        assert!(DatasetId::KoCoNovel.is_coreference());
    }

    #[test]
    fn test_chisiec_is_historical_and_relation_extraction() {
        // CHisIEC should be both historical and relation extraction
        assert!(DatasetId::CHisIEC.is_historical());
        assert!(DatasetId::CHisIEC.is_relation_extraction());

        // Verify entity types
        let types = DatasetId::CHisIEC.entity_types();
        assert!(types.contains(&"PER"));
        assert!(types.contains(&"LOC"));
        assert!(types.contains(&"OFI"));
        assert!(types.contains(&"BOOK"));
    }

    #[test]
    fn test_chisiec_from_str() {
        // Test various string representations
        assert_eq!("chisiec".parse::<DatasetId>().unwrap(), DatasetId::CHisIEC);
        assert_eq!(
            "ch-is-iec".parse::<DatasetId>().unwrap(),
            DatasetId::CHisIEC
        );
        assert_eq!(
            "chinese-historical-ie".parse::<DatasetId>().unwrap(),
            DatasetId::CHisIEC
        );
        assert_eq!(
            "ancient-chinese-ner".parse::<DatasetId>().unwrap(),
            DatasetId::CHisIEC
        );
    }

    #[test]
    fn test_chisiec_parse_ner() {
        // Test CHisIEC NER parsing with sample data
        let sample_json = r#"[
            {
                "tokens": "衞鞅奔魏",
                "entities": [
                    {"type": "PER", "start": 0, "end": 2, "span": "衞鞅"},
                    {"type": "LOC", "start": 3, "end": 4, "span": "魏"}
                ],
                "relations": []
            }
        ]"#;

        let loader = DatasetLoader::new().unwrap();
        let result = loader.parse_chisiec(sample_json, DatasetId::CHisIEC);
        assert!(result.is_ok());

        let dataset = result.unwrap();
        assert_eq!(dataset.sentences.len(), 1);

        let sentence = &dataset.sentences[0];
        // 4 characters: 衞 鞅 奔 魏
        assert_eq!(sentence.tokens.len(), 4);

        // Check BIO tags
        assert_eq!(sentence.tokens[0].ner_tag, "B-PER"); //        assert_eq!(sentence.tokens[1].ner_tag, "I-PER"); //        assert_eq!(sentence.tokens[2].ner_tag, "O"); //        assert_eq!(sentence.tokens[3].ner_tag, "B-LOC"); //    }

    #[test]
    fn test_chisiec_parse_relations() {
        // Test CHisIEC relation extraction parsing
        let sample_json = r#"[
            {
                "tokens": "嚴公遣玉汝使",
                "entities": [
                    {"type": "PER", "start": 0, "end": 2, "span": "嚴公"},
                    {"type": "PER", "start": 2, "end": 4, "span": "玉汝"}
                ],
                "relations": [
                    {"type": "上下級", "head": 0, "tail": 1, "head_span": "嚴公", "tail_span": "玉汝"}
                ]
            }
        ]"#;

        let loader = DatasetLoader::new().unwrap();
        let result = loader.parse_chisiec_relations(sample_json);
        assert!(result.is_ok());

        let docs = result.unwrap();
        assert_eq!(docs.len(), 1);

        let doc = &docs[0];
        assert_eq!(doc.relations.len(), 1);

        let rel = &doc.relations[0];
        assert_eq!(rel.relation_type, "上下級");
        assert_eq!(rel.head_text, "嚴公");
        assert_eq!(rel.tail_text, "玉汝");
        assert_eq!(rel.head_type, "PER");
        assert_eq!(rel.tail_type, "PER");
        // Character offsets (嚴公 at 0-2, 玉汝 at 2-4)
        assert_eq!(rel.head_span, (0, 2));
        assert_eq!(rel.tail_span, (2, 4));
    }

    #[test]
    fn test_chisiec_all_entity_types() {
        // Test all 4 CHisIEC entity types: PER, LOC, OFI (官职), BOOK (书籍)
        // This is important because OFI (Official position) is domain-specific
        let sample_json = r#"[
            {
                "tokens": "司馬遷為太史令著史記於長安",
                "entities": [
                    {"type": "PER", "start": 0, "end": 3, "span": "司馬遷"},
                    {"type": "OFI", "start": 4, "end": 7, "span": "太史令"},
                    {"type": "BOOK", "start": 8, "end": 10, "span": "史記"},
                    {"type": "LOC", "start": 11, "end": 13, "span": "長安"}
                ],
                "relations": []
            }
        ]"#;

        let loader = DatasetLoader::new().unwrap();
        let dataset = loader
            .parse_chisiec(sample_json, DatasetId::CHisIEC)
            .unwrap();

        assert_eq!(dataset.sentences.len(), 1);
        let sentence = &dataset.sentences[0];

        // Verify each entity type is correctly tagged
        // 司馬遷 (Sima Qian - historian)
        assert_eq!(sentence.tokens[0].ner_tag, "B-PER");
        assert_eq!(sentence.tokens[1].ner_tag, "I-PER");
        assert_eq!(sentence.tokens[2].ner_tag, "I-PER");

        // 為 (was)
        assert_eq!(sentence.tokens[3].ner_tag, "O");

        // 太史令 (Grand Historian - official position)
        assert_eq!(sentence.tokens[4].ner_tag, "B-OFI");
        assert_eq!(sentence.tokens[5].ner_tag, "I-OFI");
        assert_eq!(sentence.tokens[6].ner_tag, "I-OFI");

        // 著 (wrote)
        assert_eq!(sentence.tokens[7].ner_tag, "O");

        // 史記 (Records of the Grand Historian - book)
        assert_eq!(sentence.tokens[8].ner_tag, "B-BOOK");
        assert_eq!(sentence.tokens[9].ner_tag, "I-BOOK");

        // 於 (at)
        assert_eq!(sentence.tokens[10].ner_tag, "O");

        // 長安 (Chang'an - capital city)
        assert_eq!(sentence.tokens[11].ner_tag, "B-LOC");
        assert_eq!(sentence.tokens[12].ner_tag, "I-LOC");
    }

    #[test]
    fn test_chisiec_unicode_character_offsets() {
        // Critical: CHisIEC uses CHARACTER offsets, not byte offsets
        // This test ensures we handle multi-byte Chinese characters correctly
        let sample_json = r#"[
            {
                "tokens": "曹操",
                "entities": [
                    {"type": "PER", "start": 0, "end": 2, "span": "曹操"}
                ],
                "relations": []
            }
        ]"#;

        let loader = DatasetLoader::new().unwrap();
        let dataset = loader
            .parse_chisiec(sample_json, DatasetId::CHisIEC)
            .unwrap();

        // 曹操 is 2 characters (but 6 bytes in UTF-8)
        let sentence = &dataset.sentences[0];
        assert_eq!(sentence.tokens.len(), 2);
        assert_eq!(sentence.tokens[0].text, "");
        assert_eq!(sentence.tokens[1].text, "");
    }

    #[test]
    fn test_chisiec_multiple_relations_same_document() {
        // Test parsing multiple relations in a single document
        // Relations: 任職 (holds office), 管理 (manages)
        let sample_json = r#"[
            {
                "tokens": "曹操為丞相管冀州",
                "entities": [
                    {"type": "PER", "start": 0, "end": 2, "span": "曹操"},
                    {"type": "OFI", "start": 3, "end": 5, "span": "丞相"},
                    {"type": "LOC", "start": 6, "end": 8, "span": "冀州"}
                ],
                "relations": [
                    {"type": "任職", "head": 0, "tail": 1, "head_span": "曹操", "tail_span": "丞相"},
                    {"type": "管理", "head": 0, "tail": 2, "head_span": "曹操", "tail_span": "冀州"}
                ]
            }
        ]"#;

        let loader = DatasetLoader::new().unwrap();
        let docs = loader.parse_chisiec_relations(sample_json).unwrap();

        assert_eq!(docs.len(), 1);
        assert_eq!(docs[0].relations.len(), 2);

        // First relation: 任職 (office holding)
        assert_eq!(docs[0].relations[0].relation_type, "任職");
        assert_eq!(docs[0].relations[0].head_type, "PER");
        assert_eq!(docs[0].relations[0].tail_type, "OFI");

        // Second relation: 管理 (manages)
        assert_eq!(docs[0].relations[1].relation_type, "管理");
        assert_eq!(docs[0].relations[1].head_type, "PER");
        assert_eq!(docs[0].relations[1].tail_type, "LOC");
    }

    #[test]
    fn test_chisiec_distinct_from_historical_chinese_ner() {
        // CHisIEC and HistoricalChineseNER are DIFFERENT datasets
        // This test documents their distinction

        // Both should be classified as historical
        assert!(DatasetId::CHisIEC.is_historical());
        assert!(DatasetId::HistoricalChineseNER.is_historical());

        // But they are different datasets
        assert_ne!(DatasetId::CHisIEC, DatasetId::HistoricalChineseNER);

        // CHisIEC supports relation extraction; HistoricalChineseNER may not
        assert!(DatasetId::CHisIEC.is_relation_extraction());

        // Different entity types:
        // CHisIEC: PER, LOC, OFI, BOOK (ancient Chinese)
        // HistoricalChineseNER: PER, LOC, ORG, DATE, etc. (modern Chinese 1872-1949)
        let chisiec_types = DatasetId::CHisIEC.entity_types();
        assert!(chisiec_types.contains(&"OFI")); // Official position - unique to CHisIEC
        assert!(chisiec_types.contains(&"BOOK")); // Classical texts

        // Different names
        assert_eq!(DatasetId::CHisIEC.name(), "CHisIEC");
        assert_eq!(
            DatasetId::HistoricalChineseNER.name(),
            "Historical Chinese NER"
        );
    }

    #[test]
    fn test_chisiec_empty_entities_handled() {
        // Test graceful handling of documents with no entities
        let sample_json = r#"[
            {
                "tokens": "天下太平",
                "entities": [],
                "relations": []
            }
        ]"#;

        let loader = DatasetLoader::new().unwrap();
        let dataset = loader
            .parse_chisiec(sample_json, DatasetId::CHisIEC)
            .unwrap();

        assert_eq!(dataset.sentences.len(), 1);
        let sentence = &dataset.sentences[0];

        // All tokens should be tagged as O
        for token in &sentence.tokens {
            assert_eq!(token.ner_tag, "O");
        }
    }

    #[test]
    fn test_chisiec_entity_types_in_schema() {
        // Verify CHisIEC entity types are properly mapped in the schema
        use anno::schema::map_to_canonical;

        // PER -> Person
        let per_type = map_to_canonical("PER", None);
        assert_eq!(per_type, EntityType::Person);

        // LOC -> Location
        let loc_type = map_to_canonical("LOC", None);
        assert_eq!(loc_type, EntityType::Location);

        // OFI -> Custom OFFICIAL type (domain-specific for ancient Chinese)
        let ofi_type = map_to_canonical("OFI", None);
        assert!(matches!(ofi_type, EntityType::Custom { .. }));

        // BOOK -> WORK_OF_ART (creative works)
        let book_type = map_to_canonical("BOOK", None);
        assert!(matches!(book_type, EntityType::Custom { .. }));
    }

    #[test]
    fn test_chisiec_language_and_domain() {
        // CHisIEC is Classical Chinese (文言文) - ISO 639-3: lzh
        assert_eq!(DatasetId::CHisIEC.language(), "lzh");

        // CHisIEC is a historical dataset (24 dynastic histories)
        assert_eq!(DatasetId::CHisIEC.domain(), "historical");

        // Compare with HistoricalChineseNER which is modern Chinese (1872-1949)
        assert_eq!(DatasetId::HistoricalChineseNER.language(), "zh");
        assert_eq!(DatasetId::HistoricalChineseNER.domain(), "historical");
    }

    // =========================================================================
    // African Language Dataset Tests
    // =========================================================================

    #[test]
    fn test_african_datasets_configured() {
        // MasakhaNER datasets should have download URLs
        assert!(!DatasetId::MasakhaNER.download_url().is_empty());
        assert!(!DatasetId::MasakhaNER2.download_url().is_empty());
        assert!(!DatasetId::AfriSenti.download_url().is_empty());
        assert!(!DatasetId::AfriQA.download_url().is_empty());
        assert!(!DatasetId::MasakhaNEWS.download_url().is_empty());
        assert!(!DatasetId::MasakhaPOS.download_url().is_empty());

        // Names should be set
        assert_eq!(DatasetId::MasakhaNER.name(), "MasakhaNER");
        assert_eq!(DatasetId::MasakhaNER2.name(), "MasakhaNER 2.0");
        assert_eq!(DatasetId::AfriSenti.name(), "AfriSenti");
        assert_eq!(DatasetId::AfriQA.name(), "AfriQA");
        assert_eq!(DatasetId::MasakhaNEWS.name(), "MasakhaNEWS");
        assert_eq!(DatasetId::MasakhaPOS.name(), "MasakhaPOS");
    }

    #[test]
    fn test_african_datasets_entity_types() {
        // MasakhaNER uses PER, ORG, LOC, DATE
        let ner_types = DatasetId::MasakhaNER.entity_types();
        assert!(ner_types.contains(&"PER"));
        assert!(ner_types.contains(&"LOC"));
        assert!(ner_types.contains(&"ORG"));
        assert!(ner_types.contains(&"DATE"));

        // AfriSenti uses sentiment labels
        let senti_types = DatasetId::AfriSenti.entity_types();
        assert!(senti_types.contains(&"positive"));
        assert!(senti_types.contains(&"neutral"));
        assert!(senti_types.contains(&"negative"));

        // MasakhaNEWS uses topic labels
        let news_types = DatasetId::MasakhaNEWS.entity_types();
        assert!(news_types.contains(&"politics"));
        assert!(news_types.contains(&"sports"));
        assert!(news_types.contains(&"business"));

        // MasakhaPOS uses Universal Dependencies POS tags
        let pos_types = DatasetId::MasakhaPOS.entity_types();
        assert!(pos_types.contains(&"NOUN"));
        assert!(pos_types.contains(&"VERB"));
        assert!(pos_types.contains(&"ADJ"));
    }

    #[test]
    fn test_parse_afrisenti() {
        // Test AfriSenti TSV parsing
        let sample_tsv = "This movie is great!\tpositive\n\
                          Awful experience\tnegative\n\
                          It was okay\tneutral";

        let loader = DatasetLoader::new().unwrap();
        let result = loader.parse_afrisenti(sample_tsv, DatasetId::AfriSenti);
        assert!(result.is_ok());

        let dataset = result.unwrap();
        assert_eq!(dataset.sentences.len(), 3);

        // Check first sentence has positive label
        assert_eq!(dataset.sentences[0].tokens[0].ner_tag, "B-positive");
        // Check second sentence has negative label
        assert_eq!(dataset.sentences[1].tokens[0].ner_tag, "B-negative");
        // Check third sentence has neutral label
        assert_eq!(dataset.sentences[2].tokens[0].ner_tag, "B-neutral");
    }

    #[test]
    fn test_parse_masakhanews() {
        // Test MasakhaNEWS TSV parsing
        let sample_tsv = "headline\tbody\tcategory\n\
                          Breaking: Election Results\tThe results are in...\tpolitics\n\
                          Team Wins Championship\tIn an exciting match...\tsports";

        let loader = DatasetLoader::new().unwrap();
        let result = loader.parse_masakhanews(sample_tsv, DatasetId::MasakhaNEWS);
        assert!(result.is_ok());

        let dataset = result.unwrap();
        // Header line should be skipped
        assert_eq!(dataset.sentences.len(), 2);

        // Check categories
        assert_eq!(dataset.sentences[0].tokens[0].ner_tag, "B-politics");
        assert_eq!(dataset.sentences[1].tokens[0].ner_tag, "B-sports");
    }

    #[test]
    fn test_parse_conllu() {
        // Test CoNLL-U parsing (MasakhaPOS format)
        let sample_conllu = "# sent_id = 1\n\
                             # text = John loves Mary\n\
                             1\tJohn\tJohn\tPROPN\tNNP\t_\t2\tnsubj\t_\t_\n\
                             2\tloves\tlove\tVERB\tVBZ\t_\t0\troot\t_\t_\n\
                             3\tMary\tMary\tPROPN\tNNP\t_\t2\tobj\t_\t_\n\
                             \n\
                             # sent_id = 2\n\
                             1\tHe\the\tPRON\tPRP\t_\t2\tnsubj\t_\t_\n\
                             2\truns\trun\tVERB\tVBZ\t_\t0\troot\t_\t_\n";

        let loader = DatasetLoader::new().unwrap();
        let result = loader.parse_conllu(sample_conllu, DatasetId::MasakhaPOS);
        assert!(result.is_ok());

        let dataset = result.unwrap();
        assert_eq!(dataset.sentences.len(), 2);

        // First sentence: John loves Mary
        assert_eq!(dataset.sentences[0].tokens.len(), 3);
        assert_eq!(dataset.sentences[0].tokens[0].text, "John");
        assert_eq!(dataset.sentences[0].tokens[0].ner_tag, "B-PROPN");
        assert_eq!(dataset.sentences[0].tokens[1].text, "loves");
        assert_eq!(dataset.sentences[0].tokens[1].ner_tag, "B-VERB");

        // Second sentence: He runs
        assert_eq!(dataset.sentences[1].tokens.len(), 2);
        assert_eq!(dataset.sentences[1].tokens[0].text, "He");
        assert_eq!(dataset.sentences[1].tokens[0].ner_tag, "B-PRON");
    }

    #[test]
    fn test_ancient_language_ud_datasets_are_loadable() {
        // Ancient language UD treebanks should be loadable via registry hints
        // These have format: "CoNLLU" and categories: [ner, ancient]
        let ancient_datasets = [
            DatasetId::AncientGreekUD,
            DatasetId::LatinUD,
            DatasetId::SanskritUD,
            DatasetId::OldEnglishUD,
            DatasetId::OldNorseUD,
        ];

        for ds in ancient_datasets {
            assert!(
                LoadableDatasetId::is_loadable_dataset(ds),
                "{:?} should be loadable via registry hint (format={:?})",
                ds,
                ds.format()
            );
        }
    }

    #[test]
    fn test_conllu_with_ner_tags_from_ancient_greek() {
        // Test CoNLLU parsing with MISC column NER tags (Ancient Greek Perseus format)
        // Real format from UD Ancient Greek Perseus
        let sample_conllu = "\
# sent_id = tlg0012.tlg001.perseus-grc1:1.1
# text = μῆνιν ἄειδε θεὰ Πηληϊάδεω Ἀχιλῆος
1\tμῆνιν\tμῆνις\tNOUN\tn-s---fa-\tCase=Acc|Gender=Fem|Number=Sing\t2\tobj\t_\tO
2\tἄειδε\tᾄδω\tVERB\tv2sama---\tMood=Imp|Number=Sing|Person=2|Tense=Pres|VerbForm=Fin|Voice=Act\t0\troot\t_\tO
3\tθεὰ\tθεά\tNOUN\tn-s---fv-\tCase=Voc|Gender=Fem|Number=Sing\t2\tvocative\t_\tO
4\tΠηληϊάδεω\tΠηληϊάδης\tNOUN\tn-s---mg-\tCase=Gen|Gender=Masc|Number=Sing\t5\tnmod\t_\tB-PER
5\tἈχιλῆος\tἈχιλλεύς\tPROPN\tn-s---mg-\tCase=Gen|Gender=Masc|Number=Sing\t1\tnmod\t_\tI-PER

";

        let loader = DatasetLoader::new().unwrap();
        let result = loader.parse_conllu(sample_conllu, DatasetId::AncientGreekUD);
        assert!(
            result.is_ok(),
            "Failed to parse Ancient Greek CoNLLU: {:?}",
            result.err()
        );

        let dataset = result.unwrap();
        assert_eq!(dataset.sentences.len(), 1);
        assert_eq!(dataset.sentences[0].tokens.len(), 5);

        // Check Achilles (Ἀχιλῆος) entity
        assert_eq!(dataset.sentences[0].tokens[4].text, "Ἀχιλῆος");
        // Note: CoNLLU parser may use POS tags if MISC doesn't have NER
        // This depends on how the parser handles the MISC column
    }

    #[test]
    fn test_registry_hints_cover_all_conllu_ner_datasets() {
        // Datasets with format CoNLLU/CoNLL-U and task NER should get a registry hint
        for &ds in DatasetId::all() {
            let format = ds.format().unwrap_or("");
            let is_conllu = format == "CoNLLU" || format == "CoNLL-U";
            let is_ner = ds.is_ner();

            if is_conllu && is_ner {
                let hint = LoadableDatasetId::registry_hint_plan(ds);
                assert!(
                    hint.is_some(),
                    "{:?} has format={} and is NER but no registry hint",
                    ds,
                    format
                );
                if let Some(plan) = hint {
                    assert_eq!(
                        plan,
                        DatasetParsePlan::Conllu,
                        "{:?} should use Conllu parse plan",
                        ds
                    );
                }
            }
        }
    }

    #[test]
    fn test_datasets_with_public_url_and_format_are_hintable() {
        // Datasets with a public URL and a parseable format should get hints
        let hintable_formats = ["CoNLL", "CoNLLU", "CoNLL-U", "BIO", "IOB2", "JSONL"];

        let mut missing_hints = Vec::new();

        for &ds in DatasetId::all() {
            let url = ds.download_url();
            let format = ds.format().unwrap_or("");
            let is_ner = ds.is_ner();

            // Skip datasets without URLs or non-NER datasets
            if url.is_empty() || !is_ner {
                continue;
            }

            // Skip formats we don't auto-detect
            if !hintable_formats.contains(&format) {
                continue;
            }

            let hint = LoadableDatasetId::registry_hint_plan(ds);
            if hint.is_none() {
                missing_hints.push((ds, format));
            }
        }

        // Allow some datasets to not have hints (complex formats, etc.)
        // but document them
        if !missing_hints.is_empty() {
            // These are known to be missing hints (need special parsers)
            let known_missing: &[DatasetId] = &[
                // Add any datasets that intentionally don't have hints here
            ];
            for (ds, format) in &missing_hints {
                if !known_missing.contains(ds) {
                    eprintln!(
                        "Warning: {:?} (format={}) has public URL but no registry hint",
                        ds, format
                    );
                }
            }
        }
    }

    #[test]
    fn test_loadable_count_is_reasonable() {
        // Ensure we have a reasonable number of loadable datasets
        let loadable_count = LoadableDatasetId::all().len();
        let total_count = DatasetId::all().len();

        // We should have at least 50% of datasets loadable via either parse_plan or hints
        let min_expected = total_count / 2;
        assert!(
            loadable_count >= min_expected,
            "Only {} of {} datasets are loadable (expected at least {})",
            loadable_count,
            total_count,
            min_expected
        );
    }

    #[test]
    fn test_datasets_with_urls_have_formats() {
        // Datasets with public URLs should ideally have format info for auto-loading
        let mut missing_format = Vec::new();

        for &ds in DatasetId::all() {
            let url = ds.download_url();
            let format = ds.format();
            let access = ds.access_status();

            // Skip datasets that require registration or aren't publicly available
            if url.is_empty() {
                continue;
            }

            // Check if format is missing for public datasets
            if format.is_none()
                && access == crate::eval::dataset_registry::DatasetAccessibility::Public
            {
                missing_format.push(ds);
            }
        }

        // Log datasets that could benefit from format info
        if !missing_format.is_empty() {
            eprintln!(
                "Datasets with public URLs but no format field ({}):",
                missing_format.len()
            );
            for ds in &missing_format[..missing_format.len().min(10)] {
                eprintln!("  - {:?}", ds);
            }
        }

        // We expect most public datasets to have format info
        // Allow up to 20% to be missing format (some have unusual formats)
        let max_missing = DatasetId::all().len() / 5;
        assert!(
            missing_format.len() <= max_missing,
            "Too many public datasets missing format: {} (max {})",
            missing_format.len(),
            max_missing
        );
    }

    #[test]
    fn test_conll_format_ner_only_datasets_are_parseable() {
        // All NER-only datasets with CoNLL/CoNLLU format should have a parse plan
        // (Datasets with joint RE/coref tasks may use different column formats)
        let mut not_loadable = Vec::new();

        for &ds in DatasetId::all() {
            let format = ds.format().unwrap_or("");
            let is_conll = format == "CoNLL" || format == "CoNLLU" || format == "CoNLL-U";
            let is_ner = ds.is_ner();
            let is_re = ds.is_relation_extraction();
            let is_coref = ds.is_coreference();
            let is_event = ds.is_event_coref();

            if !is_conll || !is_ner {
                continue;
            }

            // Skip explicitly blocked datasets (they may be present in the registry for metadata,
            // but intentionally cannot be downloaded/loaded automatically).
            if ds.tasks_or_inferred().contains(&"blocked") {
                continue;
            }

            // Skip joint task datasets (they use CoNLL but with different structure)
            if is_re || is_coref || is_event {
                continue;
            }

            // Pure NER CoNLL datasets should be loadable
            let is_loadable = LoadableDatasetId::is_loadable_dataset(ds);
            if !is_loadable {
                not_loadable.push((ds, format));
            }
        }

        if !not_loadable.is_empty() {
            eprintln!("Pure NER CoNLL datasets not loadable:");
            for (ds, format) in &not_loadable {
                eprintln!("  - {:?} (format={})", ds, format);
            }
        }

        // All pure NER CoNLL datasets should be loadable
        assert!(
            not_loadable.is_empty(),
            "{} pure NER CoNLL datasets are not loadable",
            not_loadable.len()
        );
    }

    #[test]
    fn test_jsonl_ner_datasets_are_parseable() {
        // JSONL datasets with NER task should ideally be loadable
        let mut jsonl_ner_not_loadable = Vec::new();

        for &ds in DatasetId::all() {
            let format = ds.format().unwrap_or("");
            let is_jsonl = format == "JSONL" || format == "JSON-Lines" || format == "jsonl";
            let is_ner = ds.is_ner();

            if !is_jsonl || !is_ner {
                continue;
            }

            if !LoadableDatasetId::is_loadable_dataset(ds) {
                jsonl_ner_not_loadable.push(ds);
            }
        }

        // Log for debugging
        if !jsonl_ner_not_loadable.is_empty() {
            eprintln!(
                "JSONL NER datasets not loadable ({}):",
                jsonl_ner_not_loadable.len()
            );
            for ds in &jsonl_ner_not_loadable {
                eprintln!("  - {:?}", ds);
            }
        }
    }

    #[test]
    fn test_parse_afriqa() {
        // Test AfriQA JSON parsing
        let sample_json = r#"[
            {
                "context": "Lagos is a major city in Nigeria.",
                "question": "What is Lagos?",
                "answers": {
                    "text": ["major city"],
                    "answer_start": [11]
                }
            }
        ]"#;

        let loader = DatasetLoader::new().unwrap();
        let result = loader.parse_afriqa(sample_json, DatasetId::AfriQA);
        assert!(result.is_ok());

        let dataset = result.unwrap();
        assert_eq!(dataset.sentences.len(), 1);

        // Check that answer is marked
        let tokens = &dataset.sentences[0].tokens;
        // The answer "major city" should have B-ANSWER and I-ANSWER tags
        let answer_tokens: Vec<_> = tokens
            .iter()
            .filter(|t| t.ner_tag.contains("ANSWER"))
            .collect();
        assert!(!answer_tokens.is_empty(), "Should have answer tokens");
    }

    #[test]
    fn test_african_datasets_in_all_list() {
        let all = DatasetId::all();
        assert!(all.contains(&DatasetId::MasakhaNER));
        assert!(all.contains(&DatasetId::MasakhaNER2));
        assert!(all.contains(&DatasetId::AfriSenti));
        assert!(all.contains(&DatasetId::AfriQA));
        assert!(all.contains(&DatasetId::MasakhaNEWS));
        assert!(all.contains(&DatasetId::MasakhaPOS));
    }

    // =========================================================================
    // Event Extraction Parser Tests
    // =========================================================================

    #[test]
    fn test_parse_maven_jsonl() {
        // Test MAVEN full JSONL format with events array
        let sample_jsonl = r#"{"id": "doc1", "content": [{"sentence": "The earthquake struck Tokyo.", "tokens": ["The", "earthquake", "struck", "Tokyo", "."]}], "events": [{"type": "Disaster", "mention": [{"trigger_word": "earthquake", "sent_id": 0, "offset": [1, 2]}]}]}"#;

        let loader = DatasetLoader::new().unwrap();
        let result = loader.parse_maven(sample_jsonl, DatasetId::MAVEN);
        assert!(result.is_ok(), "parse_maven should succeed");

        let dataset = result.unwrap();
        assert!(!dataset.sentences.is_empty(), "Should have sentences");

        // Check event type tag
        let has_disaster = dataset
            .sentences
            .iter()
            .any(|s| s.tokens.iter().any(|t| t.ner_tag.contains("Disaster")));
        assert!(has_disaster, "Should have Disaster event tag");
    }

    #[test]
    fn test_parse_maven_docid2topic_fallback() {
        // Test fallback format (docid2topic.json)
        let sample_json = r#"{"doc1": "Natural_Disaster", "doc2": "Political_Event"}"#;

        let loader = DatasetLoader::new().unwrap();
        let result = loader.parse_maven(sample_json, DatasetId::MAVEN);
        assert!(result.is_ok(), "parse_maven fallback should succeed");

        let dataset = result.unwrap();
        assert_eq!(dataset.sentences.len(), 2, "Should have 2 entries");
    }

    #[test]
    fn test_parse_casie() {
        // Test CASIE cybersecurity event format
        let sample_jsonl = r#"{"content": "A vulnerability was discovered in Apache.", "cyberevent": {"hopper": [{"events": [{"subtype": "Vulnerability", "nugget": {"text": "vulnerability"}, "argument": [{"text": "Apache", "role": {"type": "Affected_System"}}]}]}]}}"#;

        let loader = DatasetLoader::new().unwrap();
        let result = loader.parse_casie(sample_jsonl, DatasetId::CASIE);
        assert!(result.is_ok(), "parse_casie should succeed");

        let dataset = result.unwrap();
        assert!(!dataset.sentences.is_empty(), "Should have sentences");

        // Check for vulnerability trigger
        let has_vuln = dataset
            .sentences
            .iter()
            .any(|s| s.tokens.iter().any(|t| t.ner_tag.contains("Vulnerability")));
        assert!(has_vuln, "Should have Vulnerability tag");

        // Check for argument
        let has_arg = dataset
            .sentences
            .iter()
            .any(|s| s.tokens.iter().any(|t| t.ner_tag.contains("ARG_")));
        assert!(has_arg, "Should have argument tag");
    }

    #[test]
    fn test_parse_maven_arg() {
        // Test MAVEN-ARG format with arguments
        let sample_jsonl = r#"{"id": "doc1", "document": "The company announced layoffs.", "events": [{"type": "Employment", "mention": [{"trigger_word": "layoffs", "offset": [4, 5]}], "argument": {"Employer": [{"content": "company", "offset": [1, 2]}]}}]}"#;

        let loader = DatasetLoader::new().unwrap();
        let result = loader.parse_maven_arg(sample_jsonl, DatasetId::MAVENArg);
        assert!(result.is_ok(), "parse_maven_arg should succeed");

        let dataset = result.unwrap();
        assert!(!dataset.sentences.is_empty(), "Should have sentences");

        // Check for trigger
        let has_trigger = dataset
            .sentences
            .iter()
            .any(|s| s.tokens.iter().any(|t| t.ner_tag.contains("Employment")));
        assert!(has_trigger, "Should have Employment event tag");

        // Check for argument role
        let has_employer = dataset
            .sentences
            .iter()
            .any(|s| s.tokens.iter().any(|t| t.ner_tag.contains("ARG_Employer")));
        assert!(has_employer, "Should have Employer argument tag");
    }

    #[test]
    fn test_parse_rams() {
        // Test RAMS tokenized format
        let sample_jsonl = r#"{"doc_key": "doc1", "sentences": [["The", "soldier", "fired", "his", "weapon", "."]], "evt_triggers": [[2, 2, [["conflict.attack", 1.0]]]], "gold_evt_links": [[[0], [1, 1], "attacker"]]}"#;

        let loader = DatasetLoader::new().unwrap();
        let result = loader.parse_rams(sample_jsonl, DatasetId::RAMS);
        assert!(result.is_ok(), "parse_rams should succeed");

        let dataset = result.unwrap();
        assert!(!dataset.sentences.is_empty(), "Should have sentences");

        // Check for event trigger
        let has_event = dataset
            .sentences
            .iter()
            .any(|s| s.tokens.iter().any(|t| t.ner_tag.starts_with("B-")));
        assert!(has_event, "Should have event tags");
    }

    #[test]
    fn test_parse_trec() {
        // Test TREC question classification format
        let sample =
            "NUM:dist How far is it from Denver to Aspen ?\nLOC:city What county is Modesto in ?\n";

        let loader = DatasetLoader::new().unwrap();
        let result = loader.parse_trec(sample, DatasetId::TREC);
        assert!(result.is_ok(), "parse_trec should succeed");

        let dataset = result.unwrap();
        assert_eq!(dataset.sentences.len(), 2, "Should have 2 questions");

        // Check coarse labels
        assert!(dataset.sentences[0].tokens[0].ner_tag.contains("NUM"));
        assert!(dataset.sentences[1].tokens[0].ner_tag.contains("LOC"));
    }

    #[test]
    fn test_parse_litbank_ner_improved() {
        // Test improved LitBank NER parser with word-level tokenization
        let sample_ann = "T1\tPER 0 5\tAlice\nT2\tPER 10 14\tBob\nT3\tORG 20 28\tMicrosoft";

        let loader = DatasetLoader::new().unwrap();
        let result = loader.parse_litbank(sample_ann, DatasetId::LitBank);
        assert!(result.is_ok(), "parse_litbank should succeed");

        let dataset = result.unwrap();
        assert!(!dataset.sentences.is_empty(), "Should have sentences");

        // Check that entities are tokenized into words (not just single tokens)
        let sentence = &dataset.sentences[0];
        let entity_tokens: Vec<_> = sentence
            .tokens
            .iter()
            .filter(|t| t.ner_tag.starts_with("B-") || t.ner_tag.starts_with("I-"))
            .collect();

        // Should have at least the entity tokens (Alice, Bob, Microsoft)
        assert!(
            entity_tokens.len() >= 3,
            "Should have at least 3 entity tokens, got {}",
            entity_tokens.len()
        );

        // Check BIO tagging is correct (B- for first word, I- for subsequent words)
        let mut found_b_tag = false;
        for token in &sentence.tokens {
            if token.ner_tag.starts_with("B-") {
                found_b_tag = true;
                // First word of entity should be B-
                assert!(
                    token.ner_tag.starts_with("B-"),
                    "First word of entity should have B- tag"
                );
            }
        }
        assert!(found_b_tag, "Should have at least one B- tag");
    }

    #[test]
    fn test_parse_tweettopic() {
        // Test TweetTopic JSONL format
        let sample_jsonl = r#"{"text": "Amazing game last night!", "label": 4, "label_name": "sports_&_gaming"}
{"text": "New AI breakthrough announced", "label": 5, "label_name": "science_&_technology"}"#;

        let loader = DatasetLoader::new().unwrap();
        let result = loader.parse_tweettopic(sample_jsonl, DatasetId::TweetTopic);
        assert!(result.is_ok(), "parse_tweettopic should succeed");

        let dataset = result.unwrap();
        assert_eq!(dataset.sentences.len(), 2, "Should have 2 tweets");

        // Check label names are used
        assert!(dataset.sentences[0].tokens[0]
            .ner_tag
            .contains("sports_&_gaming"));
        assert!(dataset.sentences[1].tokens[0]
            .ner_tag
            .contains("science_&_technology"));
    }

    #[test]
    fn test_african_dataset_from_str() {
        // Test string parsing for African datasets
        assert_eq!(
            "masakhaner".parse::<DatasetId>().unwrap(),
            DatasetId::MasakhaNER
        );
        assert_eq!(
            "masakhaner2".parse::<DatasetId>().unwrap(),
            DatasetId::MasakhaNER2
        );
        assert_eq!(
            "afrisenti".parse::<DatasetId>().unwrap(),
            DatasetId::AfriSenti
        );
        assert_eq!("afriqa".parse::<DatasetId>().unwrap(), DatasetId::AfriQA);
        assert_eq!(
            "masakhanews".parse::<DatasetId>().unwrap(),
            DatasetId::MasakhaNEWS
        );
        assert_eq!(
            "masakhapos".parse::<DatasetId>().unwrap(),
            DatasetId::MasakhaPOS
        );

        // Alternative spellings
        assert_eq!(
            "masakhane-ner".parse::<DatasetId>().unwrap(),
            DatasetId::MasakhaNER
        );
        assert_eq!(
            "afri-senti".parse::<DatasetId>().unwrap(),
            DatasetId::AfriSenti
        );
        assert_eq!(
            "masakhane-news".parse::<DatasetId>().unwrap(),
            DatasetId::MasakhaNEWS
        );
    }

    #[test]
    fn test_afrisenti_parse_with_tonal_diacritics() {
        // Test Yoruba text with tonal diacritics (common in AfriSenti)
        let yoruba_tsv = "Ó dára púpọ̀!\tpositive\n\
                          Kò dára rárá\tnegative\n\
                          Ẹ ṣé, mo dupẹ́\tpositive";

        let loader = DatasetLoader::new().unwrap();
        let result = loader.parse_afrisenti(yoruba_tsv, DatasetId::AfriSenti);
        assert!(result.is_ok());

        let dataset = result.unwrap();
        assert_eq!(dataset.sentences.len(), 3);

        // Verify the text is preserved correctly with diacritics
        assert!(dataset.sentences[0].tokens[0].text.contains("dára"));
        assert!(dataset.sentences[1].tokens[0].text.contains("rárá"));
        assert!(dataset.sentences[2].tokens[0].text.contains("dupẹ́"));
    }

    #[test]
    fn test_masakhaner_parse_with_ethiopic_script() {
        // Test Amharic text in MasakhaNER CoNLL format (Ethiopic script)
        let amharic_conll = "ዶክተር B-PER\n\
                             አቢይ I-PER\n\
                             አህመድ I-PER\n\
                             ኢትዮጵያ B-LOC\n\
                             ውስጥ O\n\
                             ተወለዱ O\n";

        let loader = DatasetLoader::new().unwrap();
        let result = loader.parse_conll(amharic_conll, DatasetId::MasakhaNER);
        assert!(result.is_ok());

        let dataset = result.unwrap();
        assert_eq!(dataset.sentences.len(), 1);

        let tokens = &dataset.sentences[0].tokens;
        assert_eq!(tokens.len(), 6);

        // Verify Ethiopic script is preserved
        assert_eq!(tokens[0].text, "ዶክተር");
        assert_eq!(tokens[0].ner_tag, "B-PER");
        assert_eq!(tokens[3].text, "ኢትዮጵያ");
        assert_eq!(tokens[3].ner_tag, "B-LOC");
    }

    #[test]
    fn test_conllu_parse_with_nguni_clicks() {
        // Test isiXhosa/isiZulu text with click consonants (MasakhaPOS)
        let xhosa_conllu = "# sent_id = xho_test_1\n\
                           # text = UMongameli uCyril Ramaphosa\n\
                           1\tUMongameli\tumongameli\tNOUN\tN\t_\t0\troot\t_\t_\n\
                           2\tuCyril\tuCyril\tPROPN\tNNP\t_\t1\tappos\t_\t_\n\
                           3\tRamaphosa\tRamaphosa\tPROPN\tNNP\t_\t2\tflat:name\t_\t_\n\
                           \n\
                           # sent_id = xho_test_2\n\
                           # text = Ndiqala ukuthetha isiXhosa\n\
                           1\tNdiqala\tqala\tVERB\tV\t_\t0\troot\t_\t_\n\
                           2\tukuthetha\tthetha\tVERB\tV\t_\t1\txcomp\t_\t_\n\
                           3\tisiXhosa\tisiXhosa\tNOUN\tN\t_\t2\tobj\t_\t_\n";

        let loader = DatasetLoader::new().unwrap();
        let result = loader.parse_conllu(xhosa_conllu, DatasetId::MasakhaPOS);
        assert!(result.is_ok());

        let dataset = result.unwrap();
        assert_eq!(dataset.sentences.len(), 2);

        // Verify text with click-like consonants is preserved
        assert_eq!(dataset.sentences[0].tokens[0].text, "UMongameli");
        assert_eq!(dataset.sentences[1].tokens[2].text, "isiXhosa");
    }

    #[test]
    fn test_masakhanews_parse_with_arabic_variants() {
        // MasakhaNEWS includes Algerian/Moroccan Arabic variants
        let news_tsv = "headline\tbody\tcategory\n\
                        الأخبار العاجلة\tتفاصيل الخبر...\tpolitics\n\
                        رياضة محلية\tمباراة اليوم...\tsports";

        let loader = DatasetLoader::new().unwrap();
        let result = loader.parse_masakhanews(news_tsv, DatasetId::MasakhaNEWS);
        assert!(result.is_ok());

        let dataset = result.unwrap();
        // Header skipped, 2 data rows
        assert_eq!(dataset.sentences.len(), 2);

        // Check Arabic text is preserved
        assert!(dataset.sentences[0].tokens[0].text.contains("الأخبار"));
        assert_eq!(dataset.sentences[0].tokens[0].ner_tag, "B-politics");
        assert_eq!(dataset.sentences[1].tokens[0].ner_tag, "B-sports");
    }

    #[test]
    fn test_afriqa_multilingual_qa() {
        // AfriQA has questions in target language, context may be in English
        let qa_json = r#"[
            {
                "context": "Yorùbá is a tonal language spoken in Nigeria.",
                "question": "Kí ni Yorùbá?",
                "answers": {
                    "text": ["tonal language"],
                    "answer_start": [13]
                },
                "language": "yo"
            }
        ]"#;

        let loader = DatasetLoader::new().unwrap();
        let result = loader.parse_afriqa(qa_json, DatasetId::AfriQA);
        assert!(result.is_ok());

        let dataset = result.unwrap();
        assert_eq!(dataset.sentences.len(), 1);
    }

    // NOTE: We intentionally do not embed language-code→URL expansion helpers here.
    // If we add them, they should live in the registry (metadata) or a dedicated dataset
    // URL builder module, not in the loader tests.

    // =========================================================================
    // Comprehensive Parser Smoke Tests (one per DatasetParsePlan)
    // =========================================================================

    #[test]
    fn test_parse_content_rejects_empty_for_all_loadable_datasets() {
        // Global invariant: no dataset should parse from empty content.
        let loader = DatasetLoader::new().unwrap();
        for loadable in LoadableDatasetId::all() {
            let id: DatasetId = loadable.into();
            let err = loader
                .parse_content_impl("   \n\t", id)
                .expect_err("empty content must error");
            let msg = format!("{err}");
            assert!(
                msg.to_lowercase().contains("empty"),
                "Expected an 'empty' error message for {:?}, got: {}",
                id,
                msg
            );
        }
    }

    #[test]
    fn test_parse_docred_smoke() {
        let sample = r#"{"doc_key":"d1","sentence":["John","met","Mary","in","Paris","."],"ner":[[0,0,"PER"],[2,2,"PER"],[4,4,"LOC"]],"relations":[]}"#;
        let loader = DatasetLoader::new().unwrap();
        let ds = loader.parse_docred(sample, DatasetId::DocRED).unwrap();
        assert!(!ds.sentences.is_empty());
        assert!(ds.sentences[0]
            .tokens
            .iter()
            .any(|t| t.ner_tag.starts_with("B-")));
    }

    #[test]
    fn test_parse_cadec_jsonl_smoke() {
        // Character offsets in `entities` are interpreted over reconstructed text from tokens.
        // "I took aspirin" → aspirin starts at char 7, ends at 14 (exclusive).
        let sample = r#"{"tokens":["I","took","aspirin"],"entities":[{"text":"aspirin","label":"DRUG","start":7,"end":14}]}"#;
        let loader = DatasetLoader::new().unwrap();
        let ds = loader.parse_cadec_jsonl(sample, DatasetId::CADEC).unwrap();
        assert!(!ds.sentences.is_empty());
        assert!(ds.sentences[0]
            .tokens
            .iter()
            .any(|t| t.ner_tag == "B-DRUG" || t.ner_tag == "I-DRUG"));
    }

    #[test]
    fn test_parse_cadec_hf_api_smoke() {
        let sample = r#"{"rows":[{"row":{"text":"I took aspirin","ade":"aspirin"}}]}"#;
        let loader = DatasetLoader::new().unwrap();
        let ds = loader.parse_cadec_hf_api(sample, DatasetId::CADEC).unwrap();
        assert!(!ds.sentences.is_empty());
        assert!(ds.sentences[0]
            .tokens
            .iter()
            .any(|t| t.ner_tag.contains("adverse_drug_event")));
    }

    #[test]
    fn test_parse_bc5cdr_smoke() {
        let sample = "Aspirin\tNN\tO\tB-CHEMICAL\nhelps\tVBZ\tO\tO\n\n";
        let loader = DatasetLoader::new().unwrap();
        let ds = loader.parse_bc5cdr(sample, DatasetId::BC5CDR).unwrap();
        assert_eq!(ds.sentences.len(), 1);
        assert_eq!(ds.sentences[0].tokens[0].ner_tag, "B-CHEMICAL");
    }

    #[test]
    fn test_parse_ncbi_disease_smoke() {
        let sample = "Cancer\tNN\tO\tB-Disease\nprogresses\tVBZ\tO\tO\n\n";
        let loader = DatasetLoader::new().unwrap();
        let ds = loader
            .parse_ncbi_disease(sample, DatasetId::NCBIDisease)
            .unwrap();
        assert_eq!(ds.sentences.len(), 1);
        assert!(ds.sentences[0].tokens[0].ner_tag.starts_with("B-"));
    }

    #[test]
    fn test_parse_gap_smoke() {
        let sample =
            "ID\tText\tPronoun\tPronoun-offset\tA\tA-offset\tA-coref\tB\tB-offset\tB-coref\tURL\n\
g1\tJohn met Mary. He waved.\tHe\t14\tJohn\t0\tTRUE\tMary\t9\tFALSE\thttp://example\n";
        let loader = DatasetLoader::new().unwrap();
        let ds = loader.parse_gap(sample, DatasetId::GAP).unwrap();
        assert_eq!(ds.sentences.len(), 1);
        assert!(!ds.sentences[0].tokens.is_empty());
    }

    #[test]
    fn test_parse_preco_jsonl_smoke() {
        let sample = r#"{"sentences":[["John","went","home","."],["He","slept","."]]}"#;
        let loader = DatasetLoader::new().unwrap();
        let ds = loader.parse_preco_jsonl(sample, DatasetId::PreCo).unwrap();
        assert_eq!(ds.sentences.len(), 2);
        assert_eq!(ds.sentences[0].tokens[0].text, "John");
    }

    #[test]
    fn test_parse_wikiann_json_array_smoke() {
        let sample =
            r#"[{"tokens":["John","went","to","Paris"],"ner_tags":["B-PER","O","O","B-LOC"]}]"#;
        let loader = DatasetLoader::new().unwrap();
        let ds = loader.parse_wikiann_json(sample, DatasetId::UNER).unwrap();
        assert_eq!(ds.sentences.len(), 1);
        assert_eq!(ds.sentences[0].tokens[0].ner_tag, "B-PER");
    }

    #[test]
    fn test_parse_hf_api_response_smoke() {
        let sample = r#"{
  "features":[{"name":"tokens"},{"name":"ner_tags","type":{"feature":{"names":["O","B-PER","I-PER"]}}}],
  "rows":[{"row_idx":0,"row":{"tokens":["John"],"ner_tags":[1]}}]
}"#;
        let loader = DatasetLoader::new().unwrap();
        let ds = loader
            .parse_hf_api_response(sample, DatasetId::UniversalNER)
            .unwrap();
        assert_eq!(ds.sentences.len(), 1);
        assert_eq!(ds.sentences[0].tokens[0].ner_tag, "B-PER");
    }

    #[test]
    fn test_parse_hf_api_response_temporal_standoff_smoke() {
        let sample = r#"{
  "features":[{"name":"text"},{"name":"time_expressions"}],
  "rows":[{"row_idx":0,"row":{
    "text":"A 10/30/89 .",
    "time_expressions":[{"text":"10/30/89","start_char":2,"end_char":10,"tid":"t1","type":"DATE","value":"1989-10-30"}],
    "event_expressions":[],
    "signal_expressions":[]
  }}]
}"#;
        let loader = DatasetLoader::new().unwrap();
        let ds = loader
            .parse_hf_api_response(sample, DatasetId::TimexRecognitionSentenceOriginal)
            .unwrap();
        assert_eq!(ds.sentences.len(), 1);
        assert_eq!(ds.sentences[0].tokens.len(), 3);
        assert_eq!(ds.sentences[0].tokens[0].text, "A");
        assert_eq!(ds.sentences[0].tokens[0].ner_tag, "O");
        assert_eq!(ds.sentences[0].tokens[1].text, "10/30/89");
        assert_eq!(ds.sentences[0].tokens[1].ner_tag, "B-TIMEX");
        assert_eq!(ds.sentences[0].tokens[2].text, ".");
        assert_eq!(ds.sentences[0].tokens[2].ner_tag, "O");
    }

    #[test]
    fn test_parse_hf_api_response_pairwise_discourse_smoke() {
        let sample = r#"{
  "features":[{"name":"unit1_txt"},{"name":"unit2_txt"},{"name":"label"}],
  "rows":[{"row_idx":0,"row":{
    "unit1_txt":"Because it rained",
    "unit2_txt":"the game was canceled",
    "label":"Cause"
  }}]
}"#;
        let loader = DatasetLoader::new().unwrap();
        let ds = loader
            .parse_hf_api_response(sample, DatasetId::DisrptEngDepScidtbRels)
            .unwrap();
        assert_eq!(ds.sentences.len(), 1);
        assert_eq!(ds.sentences[0].tokens.len(), 1);
        assert_eq!(
            ds.sentences[0].tokens[0].text,
            "Because it rained [SEP] the game was canceled"
        );
        assert_eq!(ds.sentences[0].tokens[0].ner_tag, "B-Cause");
    }

    #[test]
    fn test_parse_hf_api_response_disrpt_conllu_seg_smoke() {
        let sample = r#"{
  "features":[{"name":"form"},{"name":"misc"}],
  "rows":[{"row_idx":0,"row":{
    "form":["We","propose","a","method","."],
    "misc":["Seg=B-seg","Seg=O","Seg=O","Seg=B-seg","Seg=O"]
  }}]
}"#;
        let loader = DatasetLoader::new().unwrap();
        let ds = loader
            .parse_hf_api_response(sample, DatasetId::DisrptEngDepScidtbConlluSeg)
            .unwrap();
        assert_eq!(ds.sentences.len(), 1);
        let tags: Vec<&str> = ds.sentences[0]
            .tokens
            .iter()
            .map(|t| t.ner_tag.as_str())
            .collect();
        assert_eq!(tags, vec!["B-SEG", "I-SEG", "I-SEG", "B-SEG", "I-SEG"]);
    }

    #[test]
    fn test_parse_agnews_smoke() {
        let sample = r#"{"text":"Stocks rally on earnings","label":2}"#;
        let loader = DatasetLoader::new().unwrap();
        let ds = loader.parse_agnews(sample, DatasetId::AGNews).unwrap();
        assert_eq!(ds.sentences.len(), 1);
        assert!(ds.sentences[0].tokens[0].ner_tag.starts_with("B-"));
    }

    #[test]
    fn test_parse_dbpedia14_smoke() {
        let sample = r#"{"content":"The Beatles released Abbey Road","label":5}"#;
        let loader = DatasetLoader::new().unwrap();
        let ds = loader
            .parse_dbpedia14(sample, DatasetId::DBPedia14)
            .unwrap();
        assert_eq!(ds.sentences.len(), 1);
        assert!(ds.sentences[0].tokens[0].ner_tag.starts_with("B-"));
    }

    #[test]
    fn test_parse_yahoo_answers_smoke() {
        let sample = r#"{"question_title":"Why is the sky blue?","topic":1}"#;
        let loader = DatasetLoader::new().unwrap();
        let ds = loader
            .parse_yahoo_answers(sample, DatasetId::YahooAnswers)
            .unwrap();
        assert_eq!(ds.sentences.len(), 1);
        assert!(ds.sentences[0].tokens[0].ner_tag.starts_with("B-"));
    }

    // =========================================================================
    // Dataset Registry Integration Tests
    // =========================================================================

    #[test]
    fn test_sec_filings_has_raw_url() {
        // Verify SEC-filings dataset has a raw GitHub URL for direct download
        let url = DatasetId::SECFilingsNER.download_url();
        assert!(
            url.contains("raw.githubusercontent.com"),
            "SEC-filings should have raw GitHub URL, got: {}",
            url
        );
        assert!(
            url.ends_with(".txt"),
            "SEC-filings should point to a .txt file, got: {}",
            url
        );
    }

    #[test]
    fn test_twiconv_has_format() {
        // Verify TwiConv dataset has format field
        let format = DatasetId::TwiConv.format();
        assert!(format.is_some(), "TwiConv should have format field");
        // TwiConv uses CoNLL format for coreference data
        assert_eq!(format.unwrap(), "CoNLL", "TwiConv should be CoNLL format");
    }

    #[test]
    fn test_mudoco_has_format() {
        // Verify MuDoCo dataset has format field
        let format = DatasetId::MuDoCo.format();
        assert!(format.is_some(), "MuDoCo should have format field");
        assert_eq!(format.unwrap(), "JSON", "MuDoCo should be JSON format");
    }

    #[test]
    fn test_all_public_ud_datasets_have_conllu_format() {
        // All Universal Dependencies datasets should have CoNLLU format
        let ud_datasets = vec![
            DatasetId::AncientGreekUD,
            DatasetId::LatinUD,
            DatasetId::SanskritUD,
            DatasetId::OldEnglishUD,
            DatasetId::UDEsperantoCairo,
        ];

        for ds in ud_datasets {
            let format = ds.format();
            assert!(format.is_some(), "{:?} should have format field", ds);
            assert_eq!(
                format.unwrap(),
                "CoNLLU",
                "{:?} should be CoNLLU format",
                ds
            );
        }
    }

    #[test]
    fn test_datasets_with_public_urls_are_accessible() {
        // Verify that key public datasets have valid URLs (format check only)
        let test_cases = vec![
            (DatasetId::AncientGreekUD, "universaldependencies"),
            (DatasetId::LatinUD, "universaldependencies"),
            (DatasetId::SECFilingsNER, "entity-recognition-datasets"),
            (DatasetId::TwiConv, "twiconv"), // lowercase for case-insensitive match
        ];

        for (ds, expected_substring) in test_cases {
            let url = ds.download_url();
            assert!(!url.is_empty(), "{:?} should have a download URL", ds);
            assert!(
                url.to_lowercase()
                    .contains(&expected_substring.to_lowercase()),
                "{:?} URL should contain '{}', got: {}",
                ds,
                expected_substring,
                url
            );
        }
    }

    #[test]
    fn test_loadable_datasets_count_is_stable() {
        // Track the number of loadable datasets to detect regressions
        // This should only increase as we add more loaders
        let loadable = LoadableDatasetId::all();
        let count = loadable.len();

        // As of 2025-12-15, after recent fixes we have 295 loadable datasets
        assert!(
            count >= 295,
            "Expected at least 295 loadable datasets, got {}. \
             This may indicate a regression in the loading system.",
            count
        );
    }

    #[test]
    fn test_conll_format_variants_all_detected() {
        // Ensure CoNLL format variants for pure NER datasets are properly detected
        // We exclude RE/coref datasets as they have special parsing needs
        for &ds in DatasetId::all() {
            let format = ds.format();
            if let Some(fmt) = format {
                let is_conll_variant =
                    fmt == "CoNLL" || fmt == "CoNLLU" || fmt == "CoNLL-U" || fmt == "CoNLL03";

                // Only check pure NER datasets (no coref, no RE)
                let is_pure_ner = ds.supports_ner() && !ds.supports_coref() && !ds.supports_re();

                if is_conll_variant && is_pure_ner {
                    // Should be loadable via hint system
                    let hint = LoadableDatasetId::registry_hint_plan(ds);
                    assert!(
                        hint.is_some() || LoadableDatasetId::is_loadable_dataset(ds),
                        "{:?} with format {} and pure NER task should be loadable",
                        ds,
                        fmt
                    );
                }
            }
        }
    }

    #[test]
    fn test_parse_csv_ner_smoke() {
        // Test CSV NER format (E-NER/EDGAR-NER style: Token,Tag)
        let sample = "\
-DOCSTART-,O
,O
Check,O
the,O
appropriate,O
box,O
,O
Nuveen,I-BUSINESS
New,I-BUSINESS
York,I-BUSINESS
Fund,I-BUSINESS
,O

The,O
SEC,I-GOVERNMENT
filed,O
charges,O
.

John,I-PERSON
Smith,I-PERSON
is,O
the,O
CEO,O
.
";
        let loader = DatasetLoader::new().unwrap();
        let ds = loader.parse_csv_ner(sample, DatasetId::ENer).unwrap();

        // Should have 3 sentences (separated by empty lines and -DOCSTART-)
        assert_eq!(
            ds.sentences.len(),
            3,
            "Expected 3 sentences, got {:?}",
            ds.sentences.len()
        );

        // First sentence should have BUSINESS entities
        let first_sentence = &ds.sentences[0];
        assert!(
            first_sentence
                .tokens
                .iter()
                .any(|t| t.ner_tag == "I-BUSINESS"),
            "First sentence should contain I-BUSINESS tags"
        );

        // Second sentence should have GOVERNMENT entity
        let second_sentence = &ds.sentences[1];
        assert!(
            second_sentence
                .tokens
                .iter()
                .any(|t| t.ner_tag == "I-GOVERNMENT"),
            "Second sentence should contain I-GOVERNMENT tag"
        );

        // Third sentence should have PERSON entities
        let third_sentence = &ds.sentences[2];
        assert!(
            third_sentence
                .tokens
                .iter()
                .any(|t| t.ner_tag == "I-PERSON"),
            "Third sentence should contain I-PERSON tags"
        );

        // Check specific token/tag pairs
        let nuveen_token = first_sentence.tokens.iter().find(|t| t.text == "Nuveen");
        assert!(nuveen_token.is_some(), "Should have Nuveen token");
        assert_eq!(nuveen_token.unwrap().ner_tag, "I-BUSINESS");

        let john_token = third_sentence.tokens.iter().find(|t| t.text == "John");
        assert!(john_token.is_some(), "Should have John token");
        assert_eq!(john_token.unwrap().ner_tag, "I-PERSON");
    }

    #[test]
    fn test_csv_ner_format_is_detected() {
        // Ensure CSV format datasets with NER tasks are properly detected as loadable
        let ener_hint = LoadableDatasetId::registry_hint_plan(DatasetId::ENer);
        assert_eq!(
            ener_hint,
            Some(DatasetParsePlan::CsvNer),
            "ENer should use CsvNer parse plan"
        );

        // Verify ENer is loadable
        assert!(
            LoadableDatasetId::is_loadable_dataset(DatasetId::ENer),
            "ENer should be loadable"
        );
    }

    // =========================================================================
    // Tests for newly added dataset loaders (2025-12)
    // =========================================================================

    #[test]
    fn test_newly_added_conll_datasets_are_loadable() {
        let new_conll = [
            DatasetId::QxoRef,
            DatasetId::GICoref,
            DatasetId::WNUT16,
            DatasetId::NoiseBench,
            DatasetId::CrossWeigh,
            DatasetId::ZELDA,
            DatasetId::GENIANested,
        ];

        for id in new_conll {
            assert!(
                LoadableDatasetId::is_loadable_dataset(id),
                "{:?} should be loadable with Conll parse plan",
                id
            );
            assert_eq!(
                LoadableDatasetId::parse_plan(id),
                Some(DatasetParsePlan::Conll),
                "{:?} should use Conll plan",
                id
            );
        }
    }

    #[test]
    fn test_newly_added_jsonl_datasets_are_loadable() {
        let new_jsonl = [
            DatasetId::REBEL,
            DatasetId::BBQ,
            DatasetId::RealToxicityPrompts,
            DatasetId::BookCoref,
            DatasetId::BookCorefSplit,
            DatasetId::WIESP2022NER,
            DatasetId::FewRel,
            DatasetId::PIIMasking200k,
            DatasetId::B2NERD,
            DatasetId::OpenNER,
            DatasetId::FictionNER750M,
        ];

        for id in new_jsonl {
            assert!(
                LoadableDatasetId::is_loadable_dataset(id),
                "{:?} should be loadable with JsonlNer parse plan",
                id
            );
            assert_eq!(
                LoadableDatasetId::parse_plan(id),
                Some(DatasetParsePlan::JsonlNer),
                "{:?} should use JsonlNer plan",
                id
            );
        }
    }

    #[test]
    fn test_genia_nested_conll_parse() {
        // GENIA nested NER uses multi-layered BIO tags
        let nested_conll = "IL-2\tB-protein\n\
                            gene\tI-protein\n\
                            expression\tO\n\
                            \n\
                            T\tB-cell_type\n\
                            cells\tI-cell_type\n";

        let loader = DatasetLoader::new().unwrap();
        let result = loader.parse_conll(nested_conll, DatasetId::GENIANested);
        assert!(result.is_ok());

        let dataset = result.unwrap();
        assert_eq!(dataset.sentences.len(), 2);
        assert_eq!(dataset.sentences[0].tokens[0].ner_tag, "B-protein");
        assert_eq!(dataset.sentences[1].tokens[0].ner_tag, "B-cell_type");
    }

    #[test]
    fn test_gicoref_gender_inclusive_parse() {
        // GICoref uses neopronouns and singular they
        let gicoref_conll = "Alex\tB-PER\n\
                             uses\tO\n\
                             they\tB-PER\n\
                             pronouns\tO\n\
                             \n\
                             Jordan\tB-PER\n\
                             introduced\tO\n\
                             themself\tB-PER\n";

        let loader = DatasetLoader::new().unwrap();
        let result = loader.parse_conll(gicoref_conll, DatasetId::GICoref);
        assert!(result.is_ok());

        let dataset = result.unwrap();
        assert_eq!(dataset.sentences.len(), 2);

        // Verify pronoun tokens are correctly tagged
        assert_eq!(dataset.sentences[0].tokens[2].text, "they");
        assert_eq!(dataset.sentences[0].tokens[2].ner_tag, "B-PER");
        assert_eq!(dataset.sentences[1].tokens[2].text, "themself");
        assert_eq!(dataset.sentences[1].tokens[2].ner_tag, "B-PER");
    }

    #[test]
    fn test_fewrel_jsonl_parse() {
        // FewRel has relation extraction in JSONL format
        // The parser expects integer tags mapping to MultiNERD labels:
        // 0=O, 1=B-PER, 3=B-ORG
        let fewrel_sample =
            r#"{"tokens":["John","works","at","Google","."],"ner_tags":[1,0,0,3,0]}"#;

        let loader = DatasetLoader::new().unwrap();
        let result = loader.parse_jsonl_ner(fewrel_sample, DatasetId::FewRel);
        assert!(result.is_ok());

        let dataset = result.unwrap();
        assert_eq!(dataset.sentences.len(), 1);
        assert_eq!(dataset.sentences[0].tokens.len(), 5);
        assert_eq!(dataset.sentences[0].tokens[0].ner_tag, "B-PER");
        assert_eq!(dataset.sentences[0].tokens[3].ner_tag, "B-ORG");
    }

    #[test]
    fn test_b2nerd_business_entities_parse() {
        // B2NERD focuses on business/financial entity types
        // Using standard MultiNERD indices: 3=B-ORG (closest to COMPANY), 0=O
        let b2nerd_sample =
            r#"{"tokens":["Apple","Inc",".","reports","Q4","earnings"],"ner_tags":[3,4,4,0,0,0]}"#;

        let loader = DatasetLoader::new().unwrap();
        let result = loader.parse_jsonl_ner(b2nerd_sample, DatasetId::B2NERD);
        assert!(result.is_ok());

        let dataset = result.unwrap();
        assert_eq!(dataset.sentences.len(), 1);
        assert_eq!(dataset.sentences[0].tokens[0].ner_tag, "B-ORG");
        assert_eq!(dataset.sentences[0].tokens[1].ner_tag, "I-ORG");
    }

    #[test]
    fn test_ud_classical_languages_are_loadable() {
        // Universal Dependencies treebanks for classical/ancient languages
        let ud_datasets = [
            DatasetId::AncientGreekUD,
            DatasetId::LatinUD,
            DatasetId::SanskritUD,
            DatasetId::OldEnglishUD,
            DatasetId::OldNorseUD,
            DatasetId::UDEsperantoCairo,
        ];

        for id in ud_datasets {
            assert!(
                LoadableDatasetId::is_loadable_dataset(id),
                "{:?} should be loadable with Conllu parse plan",
                id
            );
            assert_eq!(
                LoadableDatasetId::parse_plan(id),
                Some(DatasetParsePlan::Conllu),
                "{:?} should use Conllu plan",
                id
            );
        }
    }

    #[test]
    fn test_hipe2022_tsv_is_loadable() {
        assert!(
            LoadableDatasetId::is_loadable_dataset(DatasetId::HIPE2022),
            "HIPE2022 should be loadable"
        );
        assert_eq!(
            LoadableDatasetId::parse_plan(DatasetId::HIPE2022),
            Some(DatasetParsePlan::TsvNer),
            "HIPE2022 should use TsvNer plan"
        );
    }

    #[test]
    fn test_ener_csv_is_loadable() {
        assert!(
            LoadableDatasetId::is_loadable_dataset(DatasetId::ENer),
            "ENer should be loadable"
        );
        assert_eq!(
            LoadableDatasetId::parse_plan(DatasetId::ENer),
            Some(DatasetParsePlan::CsvNer),
            "ENer should use CsvNer plan"
        );
    }

    #[test]
    fn test_loadable_count_increased() {
        // Regression test: ensure we have at least 295 loadable datasets
        // (Updated 2025-12 after adding CoNLL/JSONL/CoNLLU batches)
        let loadable_count = LoadableDatasetId::all().len();
        assert!(
            loadable_count >= 295,
            "Expected at least 295 loadable datasets, got {}",
            loadable_count
        );
    }

    // =========================================================================
    // Domain-Specific Parser Tests
    // =========================================================================

    #[test]
    fn test_biomedical_conll_with_chemical_entities() {
        // CHEMDNER-style biomedical NER with chemical entity types
        let chemdner_conll = "Aspirin\tB-CHEMICAL\n\
                              inhibits\tO\n\
                              COX-2\tB-GENE\n\
                              expression\tO\n\
                              \n\
                              Metformin\tB-CHEMICAL\n\
                              treats\tO\n\
                              diabetes\tB-DISEASE\n";

        let loader = DatasetLoader::new().unwrap();
        let result = loader.parse_conll(chemdner_conll, DatasetId::CHEMDNER);
        assert!(result.is_ok());

        let dataset = result.unwrap();
        assert_eq!(dataset.sentences.len(), 2);
        assert_eq!(dataset.sentences[0].tokens[0].ner_tag, "B-CHEMICAL");
        assert_eq!(dataset.sentences[0].tokens[2].ner_tag, "B-GENE");
        assert_eq!(dataset.sentences[1].tokens[2].ner_tag, "B-DISEASE");
    }

    #[test]
    fn test_historical_ner_with_archaic_spelling() {
        // Historical NER should handle archaic spellings and diacritics
        let historical_conll = "Præsident\tB-PER\n\
                                 Washington\tI-PER\n\
                                 addresseth\tO\n\
                                 ye\tO\n\
                                 Congreſs\tB-ORG\n";

        let loader = DatasetLoader::new().unwrap();
        let result = loader.parse_conll(historical_conll, DatasetId::EighteenthCenturyNER);
        assert!(result.is_ok());

        let dataset = result.unwrap();
        assert_eq!(dataset.sentences.len(), 1);
        // Verify archaic characters are preserved
        assert!(dataset.sentences[0].tokens[0].text.contains('æ'));
        assert!(dataset.sentences[0].tokens[4].text.contains('ſ'));
    }

    #[test]
    fn test_multilingual_code_switching_ner() {
        // LinCE/CALCS datasets have code-switched text (e.g., Spanish-English)
        let codeswitched_conll = "My\tO\n\
                                   abuela\tB-PER\n\
                                   lives\tO\n\
                                   in\tO\n\
                                   Ciudad\tB-LOC\n\
                                   de\tI-LOC\n\
                                   México\tI-LOC\n";

        let loader = DatasetLoader::new().unwrap();
        let result = loader.parse_conll(codeswitched_conll, DatasetId::LinCE);
        assert!(result.is_ok());

        let dataset = result.unwrap();
        assert_eq!(dataset.sentences.len(), 1);
        assert_eq!(dataset.sentences[0].tokens[1].text, "abuela");
        assert_eq!(dataset.sentences[0].tokens[1].ner_tag, "B-PER");
        // Multi-word location
        assert_eq!(dataset.sentences[0].tokens[4].ner_tag, "B-LOC");
        assert_eq!(dataset.sentences[0].tokens[6].ner_tag, "I-LOC");
    }

    #[test]
    fn test_indigenous_language_ner() {
        // Test parsing of indigenous language NER (Guarani/Shipibo-Konibo)
        let guarani_conll = "Paraguái\tB-LOC\n\
                              ha\tO\n\
                              yvypora\tO\n\
                              oiko\tO\n\
                              Asunción\tB-LOC\n\
                              pe\tO\n";

        let loader = DatasetLoader::new().unwrap();
        let result = loader.parse_conll(guarani_conll, DatasetId::GuaraniNER);
        assert!(result.is_ok());

        let dataset = result.unwrap();
        assert_eq!(dataset.sentences.len(), 1);
        assert_eq!(dataset.sentences[0].tokens[0].text, "Paraguái");
        assert_eq!(dataset.sentences[0].tokens[0].ner_tag, "B-LOC");
    }

    #[test]
    fn test_ancient_greek_conllu_with_polytonic() {
        // Ancient Greek with polytonic diacritics
        let greek_conllu = "# sent_id = grc_test_1\n\
                            # text = ἐπιστήμη καὶ δικαιοσύνη\n\
                            1\tἐπιστήμη\tἐπιστήμη\tNOUN\tN\tCase=Nom|Gender=Fem|Number=Sing\t0\troot\t_\tSpaceAfter=Yes\n\
                            2\tκαὶ\tκαί\tCCONJ\tC\t_\t3\tcc\t_\tSpaceAfter=Yes\n\
                            3\tδικαιοσύνη\tδικαιοσύνη\tNOUN\tN\tCase=Nom|Gender=Fem|Number=Sing\t1\tconj\t_\tSpaceAfter=No\n";

        let loader = DatasetLoader::new().unwrap();
        let result = loader.parse_conllu(greek_conllu, DatasetId::AncientGreekUD);
        assert!(result.is_ok());

        let dataset = result.unwrap();
        assert_eq!(dataset.sentences.len(), 1);
        // Verify polytonic Greek is preserved
        assert_eq!(dataset.sentences[0].tokens[0].text, "ἐπιστήμη");
        assert_eq!(dataset.sentences[0].tokens[2].text, "δικαιοσύνη");
    }

    #[test]
    fn test_latin_conllu_with_macrons() {
        // Latin with optional macrons for vowel length
        let latin_conllu = "# sent_id = lat_test_1\n\
                            # text = Rōma āterna est\n\
                            1\tRōma\tRoma\tPROPN\tNNP\tCase=Nom|Gender=Fem|Number=Sing\t3\tnsubj\t_\tSpaceAfter=Yes\n\
                            2\tāterna\taeternus\tADJ\tA\tCase=Nom|Gender=Fem|Number=Sing\t1\tamod\t_\tSpaceAfter=Yes\n\
                            3\test\tsum\tAUX\tV\tMood=Ind|Number=Sing|Person=3|Tense=Pres|VerbForm=Fin|Voice=Act\t0\troot\t_\tSpaceAfter=No\n";

        let loader = DatasetLoader::new().unwrap();
        let result = loader.parse_conllu(latin_conllu, DatasetId::LatinUD);
        assert!(result.is_ok());

        let dataset = result.unwrap();
        assert_eq!(dataset.sentences.len(), 1);
        // Verify macrons are preserved
        assert!(dataset.sentences[0].tokens[0].text.contains('ō'));
        assert!(dataset.sentences[0].tokens[1].text.contains('ā'));
    }

    #[test]
    fn test_sanskrit_conllu_with_devanagari() {
        // Sanskrit in Devanagari script
        let sanskrit_conllu = "# sent_id = sa_test_1\n\
                               # text = रामः सीतां पश्यति\n\
                               1\tरामः\tराम\tNOUN\tN\tCase=Nom|Gender=Masc|Number=Sing\t3\tnsubj\t_\tSpaceAfter=Yes\n\
                               2\tसीतां\tसीता\tPROPN\tNNP\tCase=Acc|Gender=Fem|Number=Sing\t3\tobj\t_\tSpaceAfter=Yes\n\
                               3\tपश्यति\tदृश्\tVERB\tV\tMood=Ind|Number=Sing|Person=3|Tense=Pres|VerbForm=Fin|Voice=Act\t0\troot\t_\tSpaceAfter=No\n";

        let loader = DatasetLoader::new().unwrap();
        let result = loader.parse_conllu(sanskrit_conllu, DatasetId::SanskritUD);
        assert!(result.is_ok());

        let dataset = result.unwrap();
        assert_eq!(dataset.sentences.len(), 1);
        assert_eq!(dataset.sentences[0].tokens.len(), 3);
        // Verify Devanagari is preserved
        assert_eq!(dataset.sentences[0].tokens[0].text, "रामः");
        assert_eq!(dataset.sentences[0].tokens[1].text, "सीतां");
    }

    #[test]
    fn test_klingon_conllu_is_loadable() {
        // Klingon (tlh) is a constructed language in TaggedPBCKlingon
        assert!(
            LoadableDatasetId::is_loadable_dataset(DatasetId::TaggedPBCKlingon),
            "Klingon dataset should be loadable"
        );
        assert_eq!(
            LoadableDatasetId::parse_plan(DatasetId::TaggedPBCKlingon),
            Some(DatasetParsePlan::Conllu),
            "Klingon should use Conllu plan"
        );
    }

    #[test]
    fn test_financial_ner_entities() {
        // FinanceNER with financial entity types
        let finance_conll = "Tesla\tB-COMPANY\n\
                             stock\tO\n\
                             rose\tO\n\
                             5%\tB-PERCENTAGE\n\
                             after\tO\n\
                             Q4\tB-PERIOD\n\
                             earnings\tO\n";

        let loader = DatasetLoader::new().unwrap();
        let result = loader.parse_conll(finance_conll, DatasetId::FinanceNER);
        assert!(result.is_ok());

        let dataset = result.unwrap();
        assert_eq!(dataset.sentences.len(), 1);
        assert_eq!(dataset.sentences[0].tokens[0].ner_tag, "B-COMPANY");
        assert_eq!(dataset.sentences[0].tokens[3].ner_tag, "B-PERCENTAGE");
    }

    #[test]
    fn test_recipe_ner_food_entities() {
        // RecipeNER with culinary entity types
        let recipe_conll = "Add\tO\n\
                            2\tB-QUANTITY\n\
                            cups\tI-QUANTITY\n\
                            of\tO\n\
                            flour\tB-INGREDIENT\n\
                            and\tO\n\
                            1\tB-QUANTITY\n\
                            tsp\tI-QUANTITY\n\
                            salt\tB-INGREDIENT\n";

        let loader = DatasetLoader::new().unwrap();
        let result = loader.parse_conll(recipe_conll, DatasetId::RecipeNER);
        assert!(result.is_ok());

        let dataset = result.unwrap();
        assert_eq!(dataset.sentences.len(), 1);
        assert_eq!(dataset.sentences[0].tokens[4].ner_tag, "B-INGREDIENT");
        assert_eq!(dataset.sentences[0].tokens[8].ner_tag, "B-INGREDIENT");
    }

    #[test]
    fn test_astronomy_ner_entities() {
        // AstroNER with astronomical entity types
        let astro_conll = "The\tO\n\
                           Andromeda\tB-GALAXY\n\
                           Galaxy\tI-GALAXY\n\
                           is\tO\n\
                           2.5\tB-DISTANCE\n\
                           million\tI-DISTANCE\n\
                           light-years\tI-DISTANCE\n\
                           away\tO\n";

        let loader = DatasetLoader::new().unwrap();
        let result = loader.parse_conll(astro_conll, DatasetId::AstroNER);
        assert!(result.is_ok());

        let dataset = result.unwrap();
        assert_eq!(dataset.sentences.len(), 1);
        assert_eq!(dataset.sentences[0].tokens[1].ner_tag, "B-GALAXY");
        assert_eq!(dataset.sentences[0].tokens[4].ner_tag, "B-DISTANCE");
    }

    #[test]
    fn test_nested_ner_datasets_are_loadable() {
        // Nested NER datasets (entities within entities)
        let nested_datasets = [DatasetId::GENIANested, DatasetId::ChineseNestedNER];

        for id in nested_datasets {
            assert!(
                LoadableDatasetId::is_loadable_dataset(id),
                "{:?} should be loadable",
                id
            );
        }
    }

    #[test]
    fn test_discontinuous_ner_datasets_are_loadable() {
        // Discontinuous NER datasets (non-contiguous entity spans)
        let discontinuous_datasets = [
            DatasetId::GermEvalDiscontinuous,
            DatasetId::PubMedDiscontinuous,
        ];

        for id in discontinuous_datasets {
            assert!(
                LoadableDatasetId::is_loadable_dataset(id),
                "{:?} should be loadable",
                id
            );
        }
    }

    #[test]
    fn test_social_media_ner_datasets_are_loadable() {
        // Social media NER datasets (noisy text, hashtags, mentions)
        let social_datasets = [
            DatasetId::WNUT16,
            DatasetId::TwiConv,
            DatasetId::NERsocialFood,
        ];

        for id in social_datasets {
            assert!(
                LoadableDatasetId::is_loadable_dataset(id),
                "{:?} should be loadable",
                id
            );
            assert_eq!(
                LoadableDatasetId::parse_plan(id),
                Some(DatasetParsePlan::Conll),
                "{:?} should use Conll plan",
                id
            );
        }
    }

    #[test]
    fn test_literary_ner_datasets_are_loadable() {
        // Literary NER datasets (fiction, novels)
        let literary_datasets = [
            DatasetId::CharacterCodex,
            DatasetId::FictionNER750M,
            DatasetId::BookCoref,
        ];

        for id in literary_datasets {
            assert!(
                LoadableDatasetId::is_loadable_dataset(id),
                "{:?} should be loadable",
                id
            );
        }
    }

    #[test]
    fn test_jsonl_ner_with_empty_tokens_handled() {
        // Edge case: JSONL with some empty tokens
        let jsonl_with_empty = r#"{"tokens":["Hello","","world"],"ner_tags":[0,0,0]}"#;

        let loader = DatasetLoader::new().unwrap();
        let result = loader.parse_jsonl_ner(jsonl_with_empty, DatasetId::MultiWOZNER);
        assert!(result.is_ok());

        let dataset = result.unwrap();
        assert_eq!(dataset.sentences.len(), 1);
        // Empty token should still be preserved in parsing
        assert_eq!(dataset.sentences[0].tokens.len(), 3);
    }

    #[test]
    fn test_conll_with_long_entity_spans() {
        // Test parsing of very long entity spans (e.g., legal document titles)
        let long_span_conll = "The\tB-DOCUMENT\n\
                               United\tI-DOCUMENT\n\
                               States\tI-DOCUMENT\n\
                               Constitution\tI-DOCUMENT\n\
                               Article\tI-DOCUMENT\n\
                               I\tI-DOCUMENT\n\
                               Section\tI-DOCUMENT\n\
                               8\tI-DOCUMENT\n\
                               Clause\tI-DOCUMENT\n\
                               3\tI-DOCUMENT\n";

        let loader = DatasetLoader::new().unwrap();
        let result = loader.parse_conll(long_span_conll, DatasetId::LegNER);
        assert!(result.is_ok());

        let dataset = result.unwrap();
        assert_eq!(dataset.sentences.len(), 1);
        assert_eq!(dataset.sentences[0].tokens.len(), 10);
        // All tokens should be part of the same entity
        assert_eq!(dataset.sentences[0].tokens[0].ner_tag, "B-DOCUMENT");
        assert_eq!(dataset.sentences[0].tokens[9].ner_tag, "I-DOCUMENT");
    }

    #[test]
    fn test_all_added_conll_datasets_are_loadable() {
        // Comprehensive check for all newly added CoNLL datasets
        let added_conll = [
            DatasetId::HistNERo,
            DatasetId::DutchArchaeology,
            DatasetId::FINER,
            DatasetId::CALCS2018,
            DatasetId::MedievalCharterNER,
            DatasetId::RockNER,
            DatasetId::AIDACoNLL,
            DatasetId::NNE,
            DatasetId::IndicNER,
            DatasetId::NorNE,
            DatasetId::TASTEset,
            DatasetId::TechNER,
            DatasetId::FinTechPatent,
            DatasetId::WaterAgriNER,
            DatasetId::RussianCulturalNER,
            DatasetId::BASHI,
            DatasetId::ENER,
        ];

        for id in added_conll {
            assert!(
                LoadableDatasetId::is_loadable_dataset(id),
                "{:?} should be loadable",
                id
            );
        }
    }

    #[test]
    fn test_all_added_jsonl_datasets_are_loadable() {
        // Comprehensive check for all newly added JSONL datasets
        let added_jsonl = [
            DatasetId::MultiWOZNER,
            DatasetId::HinglishNER,
            DatasetId::AgCNER,
            DatasetId::LongDocNER,
            DatasetId::MultiBioNERLong,
            DatasetId::ReasoningNER,
            DatasetId::BioNERLLaMA,
            DatasetId::LexGLUENER,
            DatasetId::FinBenNER,
            DatasetId::FiNER139,
            DatasetId::SciNER,
            DatasetId::AIONER,
            DatasetId::WIESPAstro,
            DatasetId::CEREC,
            DatasetId::DELICATE,
            DatasetId::CSN,
        ];

        for id in added_jsonl {
            assert!(
                LoadableDatasetId::is_loadable_dataset(id),
                "{:?} should be loadable",
                id
            );
        }
    }

    #[test]
    fn test_all_added_ud_datasets_are_loadable() {
        // Comprehensive check for all newly added UD datasets
        let added_ud = [
            DatasetId::CopticScriptorium,
            DatasetId::TaggedPBCEsperanto,
            DatasetId::TaggedPBCKlingon,
            DatasetId::AkkadianUD,
            DatasetId::AncientHebrewUD,
            DatasetId::ClassicalChineseUD,
            DatasetId::CopticUD,
            DatasetId::GothicUD,
            DatasetId::HittiteUD,
            DatasetId::OldChurchSlavonicUD,
            DatasetId::LatinITTB,
            DatasetId::LatinPROIEL,
            DatasetId::EsperantoUD,
            DatasetId::NavajoMorph,
        ];

        for id in added_ud {
            assert!(
                LoadableDatasetId::is_loadable_dataset(id),
                "{:?} should be loadable",
                id
            );
            assert_eq!(
                LoadableDatasetId::parse_plan(id),
                Some(DatasetParsePlan::Conllu),
                "{:?} should use Conllu plan",
                id
            );
        }
    }

    // =========================================================================
    // Edge Case and Robustness Tests
    // =========================================================================

    #[test]
    fn test_conll_handles_windows_line_endings() {
        // Test CRLF line endings (Windows format)
        let windows_conll = "John\tB-PER\r\nSmith\tI-PER\r\n\r\nLondon\tB-LOC\r\n";

        let loader = DatasetLoader::new().unwrap();
        let result = loader.parse_conll(windows_conll, DatasetId::WikiGold);
        assert!(result.is_ok());

        let dataset = result.unwrap();
        assert_eq!(dataset.sentences.len(), 2);
    }

    #[test]
    fn test_conll_handles_extra_whitespace() {
        // Test lines with trailing/leading whitespace
        let whitespace_conll = "  John  \t  B-PER  \n  meets  \t  O  \n\n";

        let loader = DatasetLoader::new().unwrap();
        let result = loader.parse_conll(whitespace_conll, DatasetId::WikiGold);
        // Should handle gracefully (may skip malformed lines)
        assert!(result.is_ok());
    }

    #[test]
    fn test_conllu_handles_multiword_tokens() {
        // CoNLL-U multi-word tokens (e.g., "don't" -> "do" + "n't")
        let mwt_conllu = "# sent_id = test\n\
                          # text = I can't go\n\
                          1\tI\tI\tPRON\tPRP\t_\t3\tnsubj\t_\tSpaceAfter=Yes\n\
                          2-3\tcan't\t_\t_\t_\t_\t_\t_\t_\tSpaceAfter=Yes\n\
                          2\tca\tcan\tAUX\tMD\t_\t3\taux\t_\t_\n\
                          3\tn't\tnot\tPART\tRB\t_\t0\troot\t_\t_\n\
                          4\tgo\tgo\tVERB\tVB\t_\t3\txcomp\t_\tSpaceAfter=No\n";

        let loader = DatasetLoader::new().unwrap();
        let result = loader.parse_conllu(mwt_conllu, DatasetId::LatinUD);
        assert!(result.is_ok());

        let dataset = result.unwrap();
        assert_eq!(dataset.sentences.len(), 1);
        // MWT range tokens (2-3) should be skipped, only atomic tokens kept
        assert!(dataset.sentences[0].tokens.len() >= 3);
    }

    #[test]
    fn test_conllu_handles_empty_nodes() {
        // CoNLL-U empty nodes (e.g., 2.1 for elided elements)
        let empty_node_conllu = "# sent_id = test\n\
                                 # text = I saw and heard\n\
                                 1\tI\tI\tPRON\tPRP\t_\t2\tnsubj\t_\tSpaceAfter=Yes\n\
                                 2\tsaw\tsee\tVERB\tVBD\t_\t0\troot\t_\tSpaceAfter=Yes\n\
                                 2.1\tI\tI\tPRON\tPRP\t_\t4\tnsubj\t_\t_\n\
                                 3\tand\tand\tCCONJ\tCC\t_\t4\tcc\t_\tSpaceAfter=Yes\n\
                                 4\theard\thear\tVERB\tVBD\t_\t2\tconj\t_\tSpaceAfter=No\n";

        let loader = DatasetLoader::new().unwrap();
        let result = loader.parse_conllu(empty_node_conllu, DatasetId::LatinUD);
        assert!(result.is_ok());

        let dataset = result.unwrap();
        assert_eq!(dataset.sentences.len(), 1);
        // Empty nodes (2.1) should be skipped
        assert_eq!(dataset.sentences[0].tokens.len(), 4);
    }

    #[test]
    fn test_conll_with_bio_tag_normalization() {
        // Some corpora use I- at start of entity (should be B-)
        let malformed_bio = "Paris\tI-LOC\n\
                             is\tO\n\
                             beautiful\tO\n";

        let loader = DatasetLoader::new().unwrap();
        let result = loader.parse_conll(malformed_bio, DatasetId::WikiGold);
        assert!(result.is_ok());

        let dataset = result.unwrap();
        assert_eq!(dataset.sentences.len(), 1);
        // Parser should normalize or preserve the tag
        let first_tag = &dataset.sentences[0].tokens[0].ner_tag;
        assert!(first_tag == "I-LOC" || first_tag == "B-LOC");
    }

    #[test]
    fn test_conll_with_unicode_normalization() {
        // Test that precomposed (U+00E9) and decomposed (U+0065 U+0301) forms
        // both parse and produce equivalent tokens.
        let composed = "Caf\u{00e9}\tB-LOC\n";
        let decomposed = "Cafe\u{0301}\tB-LOC\n";
        assert_ne!(
            composed, decomposed,
            "test inputs must actually differ in bytes"
        );

        let loader = DatasetLoader::new().unwrap();
        let d1 = loader.parse_conll(composed, DatasetId::WikiGold).unwrap();
        let d2 = loader.parse_conll(decomposed, DatasetId::WikiGold).unwrap();

        assert_eq!(d1.sentences.len(), d2.sentences.len());
        assert_eq!(d1.sentences[0].tokens.len(), d2.sentences[0].tokens.len());
        assert_eq!(
            d1.sentences[0].tokens[0].ner_tag,
            d2.sentences[0].tokens[0].ner_tag
        );
    }

    #[test]
    fn test_cadec_hf_api_unicode_prefix_case_insensitive_span_search_is_safe() {
        // Regression: span finding must not rely on Unicode lowercasing index alignment.
        let loader = DatasetLoader::new().unwrap();
        // Include `features` and compact JSON so `is_hf_api_response()` recognizes it.
        let content = r#"{"features":[{"name":"text"},{"name":"ade"},{"name":"term_PT"}],"rows":[{"row":{"text":"Müller reported HEADACHE after taking aspirin.","ade":"headache","term_PT":"Headache"}}]}"#;

        let ds = loader
            .parse_content_str(content, DatasetId::CADEC)
            .expect("parse CADEC HF API");
        assert_eq!(ds.id, DatasetId::CADEC);
        assert!(!ds.sentences.is_empty());

        let sent = &ds.sentences[0];
        // We should tag the ADE as B-/I- adverse_drug_event in BIO space.
        assert!(
            sent.tokens
                .iter()
                .any(|t| t.ner_tag == "B-adverse_drug_event" || t.ner_tag == "I-adverse_drug_event"),
            "Expected ADE tags in tokens: {:?}",
            sent.tokens
        );
    }

    #[test]
    fn test_jsonl_with_unicode_tokens() {
        // JSONL with various Unicode characters
        let unicode_jsonl = r#"{"tokens":["北京","🎉","Москва","القاهرة"],"ner_tags":[5,0,5,5]}"#;

        let loader = DatasetLoader::new().unwrap();
        let result = loader.parse_jsonl_ner(unicode_jsonl, DatasetId::MultiWOZNER);
        assert!(result.is_ok());

        let dataset = result.unwrap();
        assert_eq!(dataset.sentences.len(), 1);
        assert_eq!(dataset.sentences[0].tokens.len(), 4);
        assert_eq!(dataset.sentences[0].tokens[0].text, "北京");
        assert_eq!(dataset.sentences[0].tokens[1].text, "🎉");
        assert_eq!(dataset.sentences[0].tokens[2].text, "Москва");
    }

    #[test]
    fn test_parse_plan_consistency_with_is_loadable() {
        // Invariant: parse_plan returns Some iff is_loadable_dataset returns true
        for &id in DatasetId::all() {
            let has_plan = LoadableDatasetId::parse_plan(id).is_some();
            let is_loadable = LoadableDatasetId::is_loadable_dataset(id);
            assert_eq!(
                has_plan, is_loadable,
                "Mismatch for {:?}: parse_plan={}, is_loadable={}",
                id, has_plan, is_loadable
            );
        }
    }

    #[test]
    fn test_dataset_coverage_by_plan() {
        // Verify we have good coverage across different parse plans
        let mut conll_count = 0;
        let mut jsonl_count = 0;
        let mut conllu_count = 0;
        let mut _other_count = 0;

        for id in LoadableDatasetId::all() {
            let ds: DatasetId = id.into();
            match LoadableDatasetId::parse_plan(ds) {
                Some(DatasetParsePlan::Conll) => conll_count += 1,
                Some(DatasetParsePlan::JsonlNer) => jsonl_count += 1,
                Some(DatasetParsePlan::Conllu) => conllu_count += 1,
                Some(_) => _other_count += 1,
                None => {}
            }
        }

        // Should have substantial coverage for each format
        assert!(
            conll_count >= 50,
            "Expected at least 50 CoNLL datasets loadable, got {}",
            conll_count
        );
        assert!(
            jsonl_count >= 20,
            "Expected at least 20 JSONL datasets loadable, got {}",
            jsonl_count
        );
        assert!(
            conllu_count >= 10,
            "Expected at least 10 CoNLLU datasets loadable, got {}",
            conllu_count
        );
    }

    #[test]
    fn test_loadable_datasets_have_valid_metadata() {
        // All loadable datasets should have basic metadata
        for id in LoadableDatasetId::all() {
            let ds: DatasetId = id.into();

            // Name should not be empty
            assert!(!ds.name().is_empty(), "{:?} has empty name", ds);

            // Description should exist for most
            // (not asserting as some may legitimately have no description)
        }
    }
}