lindera 6.2.0

A morphological analysis library.
Documentation
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
use std::borrow::Cow;
use std::str::FromStr;
use std::sync::Arc;

use lindera_dictionary::mode::Mode;
use log::warn;

use lindera_dictionary::dictionary::character_definition::CategoryId;
use lindera_dictionary::dictionary::{Dictionary, UserDictionary};
use lindera_dictionary::space_penalty::{SpacePenaltyConfig, SpacePenaltyTable};
use lindera_dictionary::viterbi::{Lattice, LatticeOptions, WordId};
use serde_json::Value;

use crate::LinderaResult;
use crate::dictionary::{load_dictionary_with_options, load_user_dictionary};
use crate::error::LinderaErrorKind;
use crate::token::Token;

pub type SegmenterConfig = Value;

/// Upper bound, in bytes, on a single sentence passed to the Viterbi lattice
/// when no real sentence delimiter (`\n`, `\t`, `。`, `、`) is found.
///
/// Without this bound, delimiter-free input (e.g. minified or line-joined
/// machine-generated text) builds one ever-growing lattice for the whole
/// input. Past a content-dependent point, the accumulated path cost
/// saturates `i32::MAX` and the lattice silently stops accepting edges,
/// collapsing the remainder of the input into a single giant token with no
/// error or warning (see <https://github.com/lindera/lindera/issues/871>).
/// 32 KiB stays well under both that saturation point and downstream
/// consumers' own token-length limits (e.g. tantivy's `MAX_TOKEN_LEN`).
pub(crate) const MAX_SENTENCE_BYTES: usize = 32 * 1024;

/// Finds the end of the next sentence starting at `sentence_start`, scanning
/// for a real sentence delimiter (`\n`, `\t`, `。`, `、`) or, failing that,
/// forcing a cut at [`MAX_SENTENCE_BYTES`] to bound lattice size.
///
/// # 引数
///
/// * `text` - The full input text.
/// * `sentence_start` - The byte offset to start scanning from.
///
/// # 戻り値
///
/// A tuple of the byte offset (exclusive) where the sentence ends, and
/// whether the cut was forced (`true`) rather than a real delimiter
/// (`false`).
fn find_sentence_end(text: &str, sentence_start: usize) -> (usize, bool) {
    let text_bytes = text.as_bytes();
    let text_len = text_bytes.len();
    let mut sentence_end = sentence_start;
    while sentence_end < text_len {
        let ch = text_bytes[sentence_end];
        sentence_end += 1;
        // Check for sentence delimiters
        if ch == b'\n' || ch == b'\t' {
            return (sentence_end, false);
        }
        // Check for Japanese punctuation (multi-byte). "。" and "、"
        // share the same 2-byte UTF-8 lead (E3 80) and differ only
        // in their last byte (0x82 / 0x81, which is exactly `ch`
        // here), so this cheap pre-check skips the 3-byte slice
        // comparison for ~254/256 possible byte values.
        if (ch == 0x81 || ch == 0x82) && sentence_end >= 3 {
            let last_3 = &text_bytes[sentence_end - 3..sentence_end];
            if last_3 == "。".as_bytes() || last_3 == "、".as_bytes() {
                return (sentence_end, false);
            }
        }
        // No real delimiter within MAX_SENTENCE_BYTES: force a cut at the
        // next valid char boundary to bound lattice size.
        if sentence_end - sentence_start >= MAX_SENTENCE_BYTES
            && text.is_char_boundary(sentence_end)
        {
            return (sentence_end, true);
        }
    }
    (sentence_end, false)
}

/// Segmenter
#[derive(Clone)]
pub struct Segmenter {
    /// The segmentation mode to be used by the segmenter.
    /// This determines how the text will be split into segments.
    pub mode: Mode,

    /// The dictionary used for segmenting text. This dictionary contains the necessary
    /// data structures and algorithms to perform morphological analysis and tokenization.
    ///
    /// Assigning to this field after construction is not supported: [`Segmenter::new`]
    /// derives per-dictionary state from it (the `SPACE` category lookup behind
    /// `keep_whitespace`, and the [`Segmenter::space_penalty`] table when one is set),
    /// and that state is *not* recomputed here. A replacement dictionary therefore
    /// leaves those caches addressing the previous dictionary's ids, which yields
    /// wrong results silently rather than failing. Build a new `Segmenter` instead.
    pub dictionary: Dictionary,

    /// An optional user-defined dictionary that can be used to customize the segmentation process.
    /// If provided, this dictionary will be used in addition to the default dictionary to improve
    /// the accuracy of segmentation for specific words or phrases.
    ///
    /// Assigning to this field after construction is not supported, for the same reason
    /// as [`Segmenter::dictionary`] plus one of its own: [`Segmenter::new`] is where a
    /// user dictionary's context IDs are remapped into the system dictionary's ID space
    /// (see [`UserDictionary::remap_context_ids`]), so a dictionary put here directly
    /// keeps its original IDs and addresses the wrong connection-matrix cells. Build a
    /// new `Segmenter` instead.
    pub user_dictionary: Option<UserDictionary>,

    /// Keep whitespace tokens in output.
    ///
    /// When false (default), whitespace is ignored for MeCab compatibility.
    /// When true, whitespace tokens are included in the output.
    pub keep_whitespace: bool,

    /// Cap on unknown-word grouping, counted in characters beyond the
    /// first (MeCab's `max-grouping-size`; MeCab defaults to 24). The cap
    /// is applied at each lattice position: when the same-category run
    /// starting there is longer than the cap, the grouped candidate is not
    /// emitted at that position -- the single-character candidate (plus
    /// the `unknown_word_ladder` candidates and any dictionary words)
    /// remains, and the remaining tail is grouped again once it fits. No
    /// unknown token is therefore longer than cap + 1 characters, but an
    /// over-long run does not degrade to single characters throughout.
    /// `None` (default) leaves grouping unbounded, matching previous
    /// behavior. `Some(0)` never groups a run of two or more characters;
    /// note that the `max_grouping_len` config key and the CLI flag treat
    /// `0` as unbounded instead.
    pub max_grouping_len: Option<usize>,

    /// Whether to additionally emit MeCab/Vibrato-inspired shorter
    /// unknown-word candidates up to each category's `char.def` `LENGTH`
    /// field (#945). Lindera parsed but never read this field before this
    /// option existed. Defaults to `true` starting with this release --
    /// set to `false` to match pre-v6 output exactly.
    pub unknown_word_ladder: bool,

    /// Left-space penalty rules (mecab-ko's `left-space-penalty-factor`,
    /// nori's `computeSpacePenalty`): a candidate that starts right after
    /// whitespace and whose first part-of-speech tag matches a rule gets the
    /// rule's cost added, so e.g. a particle or ending is not chosen across
    /// a space. [`Segmenter::new`] initializes it from the rules the
    /// dictionary ships in its metadata (`Metadata::space_penalty`; ko-dic
    /// does, the other bundled dictionaries do not), so for ko-dic the
    /// penalty is on by default; `None` adds no penalty and reproduces the
    /// v6.0 output. Set through [`Segmenter::space_penalty`] /
    /// [`Segmenter::set_space_penalty`], which also build the per-word-id
    /// lookup the lattice uses; the field is read-only for that reason.
    space_penalty: Option<SpacePenaltyConfig>,

    /// Per-word-id lookup derived from `space_penalty` for the current
    /// dictionary pair; `Arc` so `Clone` stays cheap.
    space_penalty_table: Option<Arc<SpacePenaltyTable>>,

    /// The category ID for space characters, used when keep_whitespace is false.
    space_category_id: Option<CategoryId>,

    /// Precomputed, per-codepoint (0..256) SPACE-category membership for the
    /// ASCII/Latin-1 range, used as a fast path for the keep_whitespace=false
    /// skip check. Built once from the loaded dictionary's actual character
    /// definitions (not a hardcoded byte pattern), so it stays correct for
    /// any dictionary regardless of which characters its char.def classifies
    /// as SPACE. Codepoints >= 256 always fall back to
    /// `character_definition.lookup_categories`.
    space_ascii_table: Option<[bool; 256]>,
}

impl Segmenter {
    /// Creates a new instance with the specified mode, dictionary, and optional user dictionary.
    ///
    /// # Arguments
    ///
    /// * `mode` - The `Mode` in which the instance will operate. This typically defines how aggressively the text is segmented or processed.
    /// * `dictionary` - A `Dictionary` object that provides the core data and rules for processing text.
    /// * `user_dictionary` - An optional `UserDictionary` that allows for additional, user-defined tokens or rules to be used in conjunction with the main dictionary.
    ///
    /// # Returns
    ///
    /// Returns a new instance of the struct with the provided mode, dictionary, and user dictionary (if any).
    ///
    /// # Details
    ///
    /// - `mode`: This defines the behavior of the instance, such as whether to process text in normal or aggressive mode.
    /// - `dictionary`: The main dictionary containing tokenization or processing rules.
    /// - `user_dictionary`: This is optional. If provided, it allows the user to extend or override the rules of the main dictionary with custom tokens.
    /// - Left-space penalty: when the dictionary ships rules in its metadata
    ///   (`Metadata::space_penalty`, as ko-dic does), they are applied here, so
    ///   the penalty is on by default for such dictionaries. Rules that cannot
    ///   be applied (a schema without a part-of-speech field) log a warning
    ///   and leave the penalty off. Opt out with [`Segmenter::space_penalty`]
    ///   and `None`.
    pub fn new(
        mode: Mode,
        dictionary: Dictionary,
        user_dictionary: Option<UserDictionary>,
    ) -> Self {
        // Get SPACE category ID for MeCab compatibility (ignore whitespace by default)
        let space_category_id = dictionary.character_definition.category_id_by_name("SPACE");

        // Precompute ASCII/Latin-1 SPACE-category membership once, from the
        // dictionary's actual character definitions.
        let space_ascii_table = space_category_id.map(|space_id| {
            let mut table = [false; 256];
            for (codepoint, is_space) in table.iter_mut().enumerate() {
                if let Some(c) = char::from_u32(codepoint as u32) {
                    *is_space = dictionary
                        .character_definition
                        .lookup_categories(c)
                        .contains(&space_id);
                }
            }
            table
        });

        // A user dictionary is always compiled in the original context-ID space. If the
        // system dictionary was built with `connection_id_mapping`, relabel the user
        // entries into the same space; otherwise their connection costs would address
        // the wrong matrix cells. This is the single point where both dictionaries are
        // available, and `user_dictionary` is taken by value so it cannot be remapped
        // twice.
        let user_dictionary = match (user_dictionary, dictionary.metadata.context_id_map.as_ref()) {
            (Some(mut user_dictionary), Some(map)) => {
                user_dictionary.remap_context_ids(map);
                Some(user_dictionary)
            }
            (user_dictionary, _) => user_dictionary,
        };

        let mut segmenter = Self {
            mode,
            dictionary,
            user_dictionary,
            keep_whitespace: false, // Default: ignore whitespace for MeCab compatibility
            max_grouping_len: None, // Default: unbounded grouping
            unknown_word_ladder: true, // Default (v6+): honor char.def's LENGTH field
            space_penalty: None,    // Set below from the dictionary's shipped rules, if any
            space_penalty_table: None,
            space_category_id,
            space_ascii_table,
        };

        // Default (6.1+): apply the left-space penalty rules the dictionary
        // ships in its metadata (ko-dic carries mecab-ko-dic's). `new` cannot
        // return an error, so a dictionary whose rules cannot be applied (a
        // schema without a part-of-speech field) logs a warning and runs
        // unpenalized, the state `Segmenter::space_penalty(None)` selects
        // explicitly.
        let shipped_rules = segmenter.dictionary.metadata.space_penalty.clone();
        if let Some(rules) = shipped_rules
            && let Err(err) = segmenter.set_space_penalty(Some(rules))
        {
            warn!(
                "space penalty rules shipped by dictionary '{}' were not applied: {err}",
                segmenter.dictionary.metadata.name
            );
        }

        segmenter
    }

    /// Builder method to set whether to keep whitespace tokens in output.
    ///
    /// When `keep_whitespace` is false (default), whitespace is ignored for MeCab compatibility.
    /// When true, whitespace tokens are included in the output.
    ///
    /// # Arguments
    ///
    /// * `keep_whitespace` - If true, whitespace tokens will be included in the output.
    ///
    /// # Example
    ///
    /// ```
    /// use lindera::mode::Mode;
    /// use lindera::dictionary::load_dictionary;
    /// use lindera::segmenter::Segmenter;
    ///
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// # #[cfg(feature = "embed-ipadic")]
    /// # {
    /// let dictionary = load_dictionary("embedded://ipadic")?;
    /// let segmenter = Segmenter::new(Mode::Normal, dictionary, None)
    ///     .keep_whitespace(true);
    /// # }
    /// # Ok(())
    /// # }
    /// ```
    pub fn keep_whitespace(mut self, keep_whitespace: bool) -> Self {
        self.keep_whitespace = keep_whitespace;
        self
    }

    /// Builder method to cap unknown-word grouping (MeCab's
    /// `max-grouping-size` semantics; see the `max_grouping_len` field).
    ///
    /// # 引数
    ///
    /// * `max_grouping_len` - Maximum grouped characters beyond the first,
    ///   or `None` for unbounded grouping (the default). `Some(0)` never
    ///   groups two or more characters, unlike the config key, where `0`
    ///   means unbounded.
    ///
    /// # 戻り値
    ///
    /// `self`, for chaining.
    pub fn max_grouping_len(mut self, max_grouping_len: Option<usize>) -> Self {
        self.max_grouping_len = max_grouping_len;
        self
    }

    /// Builder method to enable/disable the unknown-word length ladder
    /// (see the `unknown_word_ladder` field; defaults to `true`).
    ///
    /// # 引数
    ///
    /// * `unknown_word_ladder` - Whether to emit the length ladder.
    ///
    /// # 戻り値
    ///
    /// `self`, for chaining.
    pub fn unknown_word_ladder(mut self, unknown_word_ladder: bool) -> Self {
        self.unknown_word_ladder = unknown_word_ladder;
        self
    }

    /// Builder method to set the left-space penalty rules (see the
    /// `space_penalty` field). `None` disables the penalty, including the
    /// rules the dictionary ships and [`Segmenter::new`] applies by default.
    ///
    /// Building the per-word-id lookup reads every entry's part-of-speech
    /// tag once, so this costs a few tens of milliseconds on a large
    /// dictionary; call it once at construction, not per document.
    ///
    /// # 引数
    ///
    /// * `space_penalty` - The rules, or `None` to disable the penalty.
    ///
    /// # 戻り値
    ///
    /// `self`, for chaining, or an error when the dictionary schema has no
    /// part-of-speech field (`part_of_speech_tag` or `part_of_speech`).
    ///
    /// # Example
    ///
    /// ```
    /// use lindera::mode::Mode;
    /// use lindera::dictionary::load_dictionary;
    /// use lindera::segmenter::Segmenter;
    /// use lindera::space_penalty::{SpacePenaltyConfig, SpacePenaltyRule};
    ///
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// # #[cfg(feature = "embed-ko-dic")]
    /// # {
    /// // mecab-ko-dic's `left-space-penalty-factor`, expressed by first POS tag.
    /// let rules = SpacePenaltyConfig::new(vec![
    ///     SpacePenaltyRule::new(["EC", "EF", "EP", "ETM", "ETN", "VCP", "XSA", "XSN", "XSV"], 3000),
    ///     SpacePenaltyRule::new(["JC", "JKB", "JKC", "JKG", "JKO", "JKQ", "JKS", "JKV", "JX"], 6000),
    /// ]);
    /// let dictionary = load_dictionary("embedded://ko-dic")?;
    /// let segmenter = Segmenter::new(Mode::Normal, dictionary, None).space_penalty(Some(rules))?;
    /// # }
    /// # Ok(())
    /// # }
    /// ```
    pub fn space_penalty(
        mut self,
        space_penalty: Option<SpacePenaltyConfig>,
    ) -> LinderaResult<Self> {
        self.set_space_penalty(space_penalty)?;
        Ok(self)
    }

    /// In-place form of [`Segmenter::space_penalty`], for holders that own
    /// a `Segmenter` by `&mut` (e.g. a reusable worker).
    ///
    /// # 引数
    ///
    /// * `space_penalty` - The rules, or `None` to disable the penalty.
    ///
    /// # 戻り値
    ///
    /// `Ok(())`, or the schema error described on
    /// [`Segmenter::space_penalty`]; on error the previous setting is kept.
    pub fn set_space_penalty(
        &mut self,
        space_penalty: Option<SpacePenaltyConfig>,
    ) -> LinderaResult<()> {
        let table = match &space_penalty {
            Some(config) => Some(Arc::new(SpacePenaltyTable::build(
                config,
                &self.dictionary,
                self.user_dictionary.as_ref(),
            )?)),
            None => None,
        };
        self.space_penalty = space_penalty;
        self.space_penalty_table = table;
        Ok(())
    }

    /// Returns the configured left-space penalty rules, if any.
    ///
    /// # 戻り値
    ///
    /// The rules set through [`Segmenter::space_penalty`], or `None`.
    pub fn space_penalty_config(&self) -> Option<&SpacePenaltyConfig> {
        self.space_penalty.as_ref()
    }

    /// Builder method to enable the left-space penalty with the rules the
    /// dictionary ships in its `metadata.json` (`space_penalty`); ko-dic
    /// carries mecab-ko-dic's `left-space-penalty-factor` there.
    ///
    /// # 戻り値
    ///
    /// `self`, for chaining, or an error when the dictionary ships no rules
    /// (or its schema has no part-of-speech field).
    ///
    /// # Example
    ///
    /// ```
    /// use lindera::mode::Mode;
    /// use lindera::dictionary::load_dictionary;
    /// use lindera::segmenter::Segmenter;
    ///
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// # #[cfg(feature = "embed-ko-dic")]
    /// # {
    /// let dictionary = load_dictionary("embedded://ko-dic")?;
    /// let segmenter = Segmenter::new(Mode::Normal, dictionary, None).space_penalty_from_dictionary()?;
    /// # }
    /// # Ok(())
    /// # }
    /// ```
    pub fn space_penalty_from_dictionary(mut self) -> LinderaResult<Self> {
        self.set_space_penalty_from_dictionary()?;
        Ok(self)
    }

    /// In-place form of [`Segmenter::space_penalty_from_dictionary`].
    ///
    /// # 戻り値
    ///
    /// `Ok(())`, or an error when the dictionary ships no rules; the
    /// previous setting is kept on error.
    pub fn set_space_penalty_from_dictionary(&mut self) -> LinderaResult<()> {
        let rules = self
            .dictionary
            .metadata
            .space_penalty
            .clone()
            .ok_or_else(|| {
                LinderaErrorKind::Dictionary.with_error(anyhow::anyhow!(
                    "dictionary '{}' ships no space_penalty rules in its metadata; pass explicit rules instead",
                    self.dictionary.metadata.name
                ))
            })?;
        self.set_space_penalty(Some(rules))
    }

    /// Bundles the per-sentence lattice options from this segmenter's
    /// settings.
    ///
    /// # 戻り値
    ///
    /// The options for `set_text_with_options` /
    /// `set_text_nbest_with_options`.
    fn lattice_options(&self) -> LatticeOptions<'_> {
        let mut options = LatticeOptions::new(&self.mode);
        options.max_grouping_len = self.max_grouping_len;
        options.unknown_word_ladder = self.unknown_word_ladder;
        options.space_penalty = self.space_penalty_table.as_deref();
        options
    }

    /// A struct representing a segmenter for tokenizing text.
    ///
    /// The `Segmenter` struct provides methods for creating a segmenter from a configuration,
    /// creating a new segmenter, and segmenting text into tokens.
    ///
    /// # Methods
    ///
    /// - `from_config`: Creates a `Segmenter` from a given configuration.
    /// - `new`: Creates a new `Segmenter` with the specified mode, dictionary, and optional user dictionary.
    /// - `segment`: Segments the given text into tokens.
    ///
    /// # Errors
    ///
    /// Methods that return `LinderaResult` may produce errors related to dictionary loading,
    /// user dictionary loading, or tokenization process.
    pub fn from_config(config: &SegmenterConfig) -> LinderaResult<Self> {
        // Whether to route filesystem-loaded dictionaries through memory-mapped
        // reads. Ignored for `embedded://` dictionaries. Defaults to on when
        // the `mmap` feature is compiled in (#879); set `"use_mmap": false`
        // to force eager reads.
        let use_mmap = config
            .get("use_mmap")
            .and_then(Value::as_bool)
            .unwrap_or(cfg!(feature = "mmap"));

        // Load the dictionary from the config
        let dictionary = load_dictionary_with_options(
            config
                .get("dictionary")
                .and_then(Value::as_str)
                .ok_or_else(|| {
                    LinderaErrorKind::Parse
                        .with_error(anyhow::anyhow!("dictionary field is missing"))
                })?,
            use_mmap,
        )?;

        // Get metadata from the dictionary
        let metadata = &dictionary.metadata;

        // Load the user dictionary from the config
        let user_dictionary_uri = config
            .get("user_dictionary")
            .and_then(Value::as_str)
            .map(String::from);

        let user_dictionary = match user_dictionary_uri {
            Some(uri) => Some(load_user_dictionary(&uri, metadata)?),
            None => None,
        };

        // Load the mode from the config
        let mode: Mode = config.get("mode").map_or_else(
            || Ok(Mode::Normal),
            |v| {
                if let Some(s) = v.as_str() {
                    Mode::from_str(s).map_err(|e| {
                        LinderaErrorKind::Parse
                            .with_error(anyhow::anyhow!("mode field is invalid string: {e}"))
                    })
                } else {
                    serde_json::from_value::<Mode>(v.clone()).map_err(|e| {
                        LinderaErrorKind::Parse
                            .with_error(anyhow::anyhow!("mode field is invalid object: {e}"))
                    })
                }
            },
        )?;

        // Load the keep_whitespace option from the config
        // Default is false (MeCab compatible - ignore whitespace)
        // Set to true explicitly to include whitespace tokens
        let keep_whitespace = config
            .get("keep_whitespace")
            .and_then(Value::as_bool)
            .unwrap_or(false); // Default: false (ignore whitespace)

        // Ignoring whitespace requires a SPACE category to detect it by.
        if !keep_whitespace {
            dictionary
                .character_definition
                .category_id_by_name("SPACE")
                .ok_or_else(|| {
                    LinderaErrorKind::Parse.with_error(anyhow::anyhow!(
                        "SPACE category is not defined in the dictionary (char.def)"
                    ))
                })?;
        }

        // Go through `new` so the user-dictionary context-ID remap has a single
        // application point. `space_category_id` is only read when `keep_whitespace` is
        // false, so letting `new` always resolve it is equivalent to the previous
        // conditional.
        // Load the max_grouping_len option from the config.
        // Absent or 0 means unbounded grouping (the default).
        let max_grouping_len = config
            .get("max_grouping_len")
            .and_then(Value::as_u64)
            .filter(|&n| n > 0)
            .map(|n| n as usize);

        // Load the unknown_word_ladder option from the config.
        // Absent means the default (true).
        let unknown_word_ladder = config
            .get("unknown_word_ladder")
            .and_then(Value::as_bool)
            .unwrap_or(true);

        // Load the space_penalty option from the config. Absent or `null`
        // keeps the default `Segmenter::new` chose (the rules the dictionary
        // ships, if any); `false` turns the penalty off; `true` requires the
        // dictionary's rules; an object holds explicit rules.
        enum SpacePenaltySetting {
            Default,
            Off,
            FromDictionary,
            Rules(SpacePenaltyConfig),
        }
        let space_penalty = match config.get("space_penalty") {
            None | Some(Value::Null) => SpacePenaltySetting::Default,
            Some(Value::Bool(false)) => SpacePenaltySetting::Off,
            Some(Value::Bool(true)) => SpacePenaltySetting::FromDictionary,
            Some(value) => SpacePenaltySetting::Rules(
                serde_json::from_value::<SpacePenaltyConfig>(value.clone()).map_err(|e| {
                    LinderaErrorKind::Parse
                        .with_error(anyhow::anyhow!("space_penalty field is invalid: {e}"))
                })?,
            ),
        };

        let segmenter = Self::new(mode, dictionary, user_dictionary)
            .keep_whitespace(keep_whitespace)
            .max_grouping_len(max_grouping_len)
            .unknown_word_ladder(unknown_word_ladder);
        match space_penalty {
            SpacePenaltySetting::Default => Ok(segmenter),
            SpacePenaltySetting::Off => segmenter.space_penalty(None),
            SpacePenaltySetting::FromDictionary => segmenter.space_penalty_from_dictionary(),
            SpacePenaltySetting::Rules(rules) => segmenter.space_penalty(Some(rules)),
        }
    }

    /// Segments the input text into tokens based on the dictionary and user-defined rules.
    ///
    /// # Arguments
    ///
    /// * `text` - A `Cow<'a, str>` representing the input text. This can either be borrowed or owned, allowing for efficient text handling depending on the use case.
    ///
    /// # Returns
    ///
    /// Returns a `LinderaResult<Vec<Token<'a>>>` which contains a vector of tokens segmented from the input text. Each token represents a portion of the original text, along with metadata such as byte offsets and dictionary information.
    ///
    /// # Process
    ///
    /// 1. **Sentence Splitting**:
    ///    - The input text is split into sentences using Japanese punctuation (`。`, `、`, `\n`, `\t`). Each sentence is processed individually.
    ///
    /// 2. **Lattice Processing**:
    ///    - For each sentence, a lattice structure is set up using the main dictionary and, if available, the user dictionary. The lattice helps identify possible token boundaries within the sentence.
    ///    - The cost matrix is used to calculate the best path (i.e., the optimal sequence of tokens) through the lattice based on the mode.
    ///
    /// 3. **Token Generation**:
    ///    - For each segment (determined by the lattice), a token is generated using the byte offsets. The tokens contain the original text (in `Cow::Owned` form to ensure safe return), byte start/end positions, token positions, and dictionary references.
    ///
    /// # Notes
    ///
    /// - The function ensures that each token is safely returned by converting substrings into `Cow::Owned` strings.
    /// - Byte offsets are carefully calculated to ensure that token boundaries are correct even across multiple sentences.
    ///
    /// # Example Flow
    ///
    /// - Text is split into sentences based on punctuation.
    /// - A lattice is created and processed for each sentence.
    /// - Tokens are extracted from the lattice and returned in a vector.
    ///
    /// # Errors
    ///
    /// - If the lattice fails to be processed or if there is an issue with the segmentation process, the function returns an error.
    pub fn segment<'a>(&'a self, text: Cow<'a, str>) -> LinderaResult<Vec<Token<'a>>> {
        let mut lattice = Lattice::default();
        self.segment_with_lattice(text, &mut lattice)
    }

    /// Segments the input text into tokens based on the dictionary and user-defined rules.
    ///
    /// # Arguments
    ///
    /// * `text` - A `Cow<'a, str>` representing the input text. This can either be borrowed or owned, allowing for efficient text handling depending on the use case.
    /// * `lattice` - A mutable reference to a `Lattice` structure. This allows reusing the lattice across multiple calls to avoid memory allocation.
    ///
    /// # Returns
    ///
    /// Returns a `LinderaResult<Vec<Token<'a>>>` which contains a vector of tokens segmented from the input text. Each token represents a portion of the original text, along with metadata such as byte offsets and dictionary information.
    ///
    /// # Process
    ///
    /// 1. **Sentence Splitting**:
    ///    - The input text is split into sentences using Japanese punctuation (`。`, `、`, `\n`, `\t`). Each sentence is processed individually.
    ///
    /// 2. **Lattice Processing**:
    ///    - For each sentence, a lattice structure is set up using the main dictionary and, if available, the user dictionary. The lattice helps identify possible token boundaries within the sentence.
    ///    - The cost matrix is used to calculate the best path (i.e., the optimal sequence of tokens) through the lattice based on the mode.
    ///
    /// 3. **Token Generation**:
    ///    - For each segment (determined by the lattice), a token is generated using the byte offsets. The tokens contain the original text (in `Cow::Owned` form to ensure safe return), byte start/end positions, token positions, and dictionary references.
    ///
    /// # Notes
    ///
    /// - The function ensures that each token is safely returned by converting substrings into `Cow::Owned` strings.
    /// - Byte offsets are carefully calculated to ensure that token boundaries are correct even across multiple sentences.
    ///
    /// # Example Flow
    ///
    /// - Text is split into sentences based on punctuation.
    /// - A lattice is created and processed for each sentence.
    /// - Tokens are extracted from the lattice and returned in a vector.
    ///
    /// # Errors
    ///
    /// - If the lattice fails to be processed or if there is an issue with the segmentation process, the function returns an error.
    pub fn segment_with_lattice<'a>(
        &'a self,
        text: Cow<'a, str>,
        lattice: &mut Lattice,
    ) -> LinderaResult<Vec<Token<'a>>> {
        // The backtrace buffer is allocated once per call; SegmentWorker
        // routes through segment_with_buffers to reuse it across calls too.
        let mut offsets: Vec<(usize, WordId)> = Vec::new();
        self.segment_with_buffers(text, lattice, &mut offsets)
    }

    /// Segments the input text reusing both the caller's lattice and the
    /// caller's backtrace scratch buffer.
    ///
    /// This is the shared body behind [`Segmenter::segment_with_lattice`]
    /// (which passes a fresh buffer) and `SegmentWorker::segment` (which
    /// keeps one buffer alive across calls). Output is identical to
    /// `segment_with_lattice` for the same input.
    ///
    /// # 引数
    ///
    /// * `text` - The input text, borrowed or owned.
    /// * `lattice` - The Viterbi lattice to reuse across sentences/calls.
    /// * `offsets` - Backtrace scratch buffer; overwritten per sentence
    ///   (`tokens_offset_into` clears it), so no pre-clearing is required.
    ///
    /// # 戻り値
    ///
    /// The tokens segmented from `text`, in reading order.
    pub(crate) fn segment_with_buffers<'a>(
        &'a self,
        text: Cow<'a, str>,
        lattice: &mut Lattice,
        offsets: &mut Vec<(usize, WordId)>,
    ) -> LinderaResult<Vec<Token<'a>>> {
        let mut tokens: Vec<Token> = Vec::new();

        let mut position = 0_usize;
        let mut byte_position = 0_usize;

        // Whitespace-filter configuration, hoisted out of the per-token
        // loop: the per-char closure previously re-unwrapped the Option'd
        // ASCII table (a by-value array copy at source level) on every
        // character it examined (#942). `space_category_id` and
        // `space_ascii_table` are built together, so `zip` preserves the
        // old `keep_whitespace`/`Some` gating exactly.
        let space_filter = if self.keep_whitespace {
            None
        } else {
            self.space_category_id.zip(self.space_ascii_table.as_ref())
        };

        // Process whole text without splitting first for better performance with borrowed text
        let text_len = text.len();
        let mut sentence_start = 0;

        while sentence_start < text_len {
            // Find the end of the current sentence
            let (sentence_end, forced_cut) = find_sentence_end(&text, sentence_start);
            if forced_cut {
                warn!(
                    "no sentence delimiter (\\n, \\t, 。, 、) found within {MAX_SENTENCE_BYTES} bytes from offset {sentence_start}; forcing a sentence boundary to bound lattice size (see https://github.com/lindera/lindera/issues/871)"
                );
            }

            let sentence = &text[sentence_start..sentence_end];
            if sentence.is_empty() {
                sentence_start = sentence_end;
                continue;
            }

            // Process the sentence through lattice
            lattice.set_text_with_options(
                &self.dictionary.prefix_dictionary,
                &self.user_dictionary.as_ref().map(|d| &d.dict),
                &self.dictionary.character_definition,
                &self.dictionary.unknown_dictionary,
                &self.dictionary.connection_cost_matrix,
                sentence,
                &self.lattice_options(),
            );
            // Forward Viterbi implementation handles cost calculation within `set_text`.

            lattice.tokens_offset_into(offsets);
            tokens.reserve(offsets.len());

            for i in 0..offsets.len() {
                let (byte_start, word_id) = offsets[i];
                let byte_end = if i == offsets.len() - 1 {
                    sentence.len()
                } else {
                    let (next_start, _word_id) = offsets[i + 1];
                    next_start
                };

                // Calculate absolute position in the original text
                let absolute_start = sentence_start + byte_start;
                let absolute_end = sentence_start + byte_end;

                // Skip whitespace tokens if keep_whitespace is false (default MeCab behavior)
                if let Some((space_category_id, space_ascii_table)) = space_filter {
                    // Check if this token consists only of whitespace characters.
                    // ASCII/Latin-1 codepoints use the precomputed table (O(1));
                    // anything else falls back to the dictionary lookup.
                    let token_text = &sentence[byte_start..byte_end];
                    let is_space = token_text.chars().all(|c| {
                        if (c as u32) < 256 {
                            space_ascii_table[c as usize]
                        } else {
                            self.dictionary
                                .character_definition
                                .lookup_categories(c)
                                .contains(&space_category_id)
                        }
                    });

                    if is_space {
                        // Update byte_position to maintain correct offsets
                        byte_position += byte_end - byte_start;
                        continue;
                    }
                }

                // Create surface Cow efficiently - avoid unnecessary string allocation for owned strings
                let surface_cow = match &text {
                    Cow::Borrowed(s) => Cow::Borrowed(&s[absolute_start..absolute_end]),
                    Cow::Owned(s) => {
                        // Use slice from owned string instead of creating new string
                        Cow::Owned(s[absolute_start..absolute_end].to_owned())
                    }
                };

                // compute the token's absolute byte positions
                let token_start = byte_position;
                byte_position += byte_end - byte_start;
                let token_end = byte_position;

                tokens.push(Token::new(
                    surface_cow,
                    token_start,
                    token_end,
                    position,
                    word_id,
                    &self.dictionary,
                    self.user_dictionary.as_ref(),
                ));

                position += 1;
            }

            sentence_start = sentence_end;
        }

        Ok(tokens)
    }

    /// Segments the input text and returns the top-N segmentation results.
    ///
    /// Each result is a `Vec<Token>` representing one possible segmentation.
    /// Results are ordered by cost (best first).
    /// If `unique` is true, results with the same word boundaries but different
    /// POS tags are deduplicated (only the lowest-cost variant is kept).
    pub fn segment_nbest<'a>(
        &'a self,
        text: Cow<'a, str>,
        n: usize,
        unique: bool,
        cost_threshold: Option<i64>,
    ) -> LinderaResult<Vec<(Vec<Token<'a>>, i64)>> {
        let mut lattice = Lattice::default();
        self.segment_nbest_with_lattice(text, &mut lattice, n, unique, cost_threshold)
    }

    /// Segments the input text and returns the top-N segmentation results with costs.
    /// Each result is a (tokens, cost) pair.
    /// If `unique` is true, results with the same word boundaries but different
    /// POS tags are deduplicated (only the lowest-cost variant is kept).
    /// If `cost_threshold` is Some(t), paths whose cost exceeds best_cost + t
    /// are discarded.
    pub fn segment_nbest_with_lattice<'a>(
        &'a self,
        text: Cow<'a, str>,
        lattice: &mut Lattice,
        n: usize,
        unique: bool,
        cost_threshold: Option<i64>,
    ) -> LinderaResult<Vec<(Vec<Token<'a>>, i64)>> {
        let mut all_results: Vec<(Vec<Token>, i64)> = Vec::with_capacity(n);

        // Hoisted whitespace-filter configuration; see segment_with_buffers.
        let space_filter = if self.keep_whitespace {
            None
        } else {
            self.space_category_id.zip(self.space_ascii_table.as_ref())
        };

        let text_len = text.len();
        let mut sentence_start = 0;

        while sentence_start < text_len {
            // Find the end of the current sentence
            let (sentence_end, forced_cut) = find_sentence_end(&text, sentence_start);
            if forced_cut {
                warn!(
                    "no sentence delimiter (\\n, \\t, 。, 、) found within {MAX_SENTENCE_BYTES} bytes from offset {sentence_start}; forcing a sentence boundary to bound lattice size (see https://github.com/lindera/lindera/issues/871)"
                );
            }

            let sentence = &text[sentence_start..sentence_end];
            if sentence.is_empty() {
                sentence_start = sentence_end;
                continue;
            }

            // Process the sentence through N-Best lattice
            lattice.set_text_nbest_with_options(
                &self.dictionary.prefix_dictionary,
                &self.user_dictionary.as_ref().map(|d| &d.dict),
                &self.dictionary.character_definition,
                &self.dictionary.unknown_dictionary,
                &self.dictionary.connection_cost_matrix,
                sentence,
                &self.lattice_options(),
            );

            let nbest_offsets = lattice.nbest_tokens_offset(n, unique, cost_threshold);

            for (rank, (offsets, cost)) in nbest_offsets.into_iter().enumerate() {
                if rank >= all_results.len() {
                    all_results.resize_with(rank + 1, || (Vec::new(), 0));
                }

                // Accumulate cost across sentences
                all_results[rank].1 += cost;

                let mut position = all_results[rank].0.len();
                let mut byte_position: usize = if all_results[rank].0.is_empty() {
                    0
                } else {
                    all_results[rank].0.last().map_or(0, |t| t.byte_end)
                };

                for i in 0..offsets.len() {
                    let (byte_start, word_id) = offsets[i];
                    let byte_end = if i == offsets.len() - 1 {
                        sentence.len()
                    } else {
                        offsets[i + 1].0
                    };

                    let absolute_start = sentence_start + byte_start;
                    let absolute_end = sentence_start + byte_end;

                    // Skip whitespace tokens if keep_whitespace is false
                    if let Some((space_category_id, space_ascii_table)) = space_filter {
                        // ASCII/Latin-1 codepoints use the precomputed table
                        // (O(1)); anything else falls back to the dictionary
                        // lookup.
                        let token_text = &sentence[byte_start..byte_end];
                        let is_space = token_text.chars().all(|c| {
                            if (c as u32) < 256 {
                                space_ascii_table[c as usize]
                            } else {
                                self.dictionary
                                    .character_definition
                                    .lookup_categories(c)
                                    .contains(&space_category_id)
                            }
                        });

                        if is_space {
                            byte_position += byte_end - byte_start;
                            continue;
                        }
                    }

                    let surface_cow = match &text {
                        Cow::Borrowed(s) => Cow::Borrowed(&s[absolute_start..absolute_end]),
                        Cow::Owned(s) => Cow::Owned(s[absolute_start..absolute_end].to_owned()),
                    };

                    let token_start = byte_position;
                    byte_position += byte_end - byte_start;
                    let token_end = byte_position;

                    all_results[rank].0.push(Token::new(
                        surface_cow,
                        token_start,
                        token_end,
                        position,
                        word_id,
                        &self.dictionary,
                        self.user_dictionary.as_ref(),
                    ));

                    position += 1;
                }
            }

            sentence_start = sentence_end;
        }

        Ok(all_results)
    }
}

#[cfg(test)]
mod tests {
    #[cfg(feature = "embed-ipadic")]
    use std::{
        fs::File,
        io::{BufReader, Read},
        path::PathBuf,
    };

    use crate::segmenter::{MAX_SENTENCE_BYTES, find_sentence_end};
    #[cfg(feature = "embed-ipadic")]
    use crate::segmenter::{Segmenter, SegmenterConfig};

    #[test]
    fn test_find_sentence_end_no_delimiter_short_text() {
        let text = "hello world";
        let (end, forced) = find_sentence_end(text, 0);
        assert_eq!(end, text.len());
        assert!(!forced);
    }

    #[test]
    fn test_find_sentence_end_newline_delimiter() {
        let text = "hello\nworld";
        let (end, forced) = find_sentence_end(text, 0);
        assert_eq!(end, 6); // includes the '\n'
        assert!(!forced);
    }

    #[test]
    fn test_find_sentence_end_japanese_period_delimiter() {
        let text = "これはテストです。続き";
        let expected_end = text.find('。').unwrap() + '。'.len_utf8();
        let (end, forced) = find_sentence_end(text, 0);
        assert_eq!(end, expected_end);
        assert!(!forced);
    }

    #[test]
    fn test_find_sentence_end_touten_delimiter() {
        let text = "これは、テストです";
        let expected_end = text.find('、').unwrap() + '、'.len_utf8();
        let (end, forced) = find_sentence_end(text, 0);
        assert_eq!(end, expected_end);
        assert!(!forced);
    }

    #[test]
    fn test_find_sentence_end_forces_cut_when_no_delimiter() {
        // Every byte is a char boundary, so the forced cut lands exactly at
        // MAX_SENTENCE_BYTES.
        let text = "a".repeat(MAX_SENTENCE_BYTES * 2);
        let (end, forced) = find_sentence_end(&text, 0);
        assert_eq!(end, MAX_SENTENCE_BYTES);
        assert!(forced);
    }

    #[test]
    fn test_find_sentence_end_forced_cut_respects_char_boundary() {
        // Place a multi-byte character straddling MAX_SENTENCE_BYTES so a
        // naive byte-count cut would slice through it.
        let mut text = "a".repeat(MAX_SENTENCE_BYTES - 1);
        text.push('あ');
        text.push_str(&"a".repeat(100));

        let (end, forced) = find_sentence_end(&text, 0);
        assert!(forced);
        // Panics if `end` is not a valid char boundary.
        let _ = &text[..end];
        assert!(end >= MAX_SENTENCE_BYTES);
        assert!(end <= MAX_SENTENCE_BYTES - 1 + 'あ'.len_utf8());
    }

    #[test]
    #[cfg(feature = "embed-ipadic")]
    fn test_segmenter_config_ipadic_normal() {
        let config_str = r#"
        {
            "dictionary": "embedded://ipadic",
            "mode": "normal"
        }
        "#;

        let result: Result<SegmenterConfig, _> = serde_json::from_str(config_str);
        assert!(result.is_ok());
    }

    #[test]
    #[cfg(feature = "embed-ipadic")]
    fn test_segmenter_config_ipadic_decompose() {
        let config_str = r#"
        {
            "dictionary": "embedded://ipadic",
            "mode": {
                "decompose": {
                    "kanji_penalty_length_threshold": 2,
                    "kanji_penalty_length_penalty": 3000,
                    "other_penalty_length_threshold": 7,
                    "other_penalty_length_penalty": 1700
                }
            }
        }
        "#;

        let result: Result<SegmenterConfig, _> = serde_json::from_str(config_str);
        assert!(result.is_ok());
    }

    #[test]
    #[cfg(feature = "embed-ipadic")]
    fn test_segment_ipadic() {
        use std::borrow::Cow;

        let config_str = r#"
        {
            "dictionary": "embedded://ipadic",
            "mode": "normal"
        }
        "#;
        let config = serde_json::from_str::<SegmenterConfig>(config_str).unwrap();

        let segmenter = Segmenter::from_config(&config).unwrap();
        let mut tokens = segmenter
            .segment(Cow::Borrowed(
                "日本語の形態素解析を行うことができます。テスト。",
            ))
            .unwrap();
        let mut tokens_iter = tokens.iter_mut();
        {
            let token = tokens_iter.next().unwrap();
            assert_eq!(token.surface, "日本語");
            assert_eq!(token.byte_start, 0);
            assert_eq!(token.byte_end, 9);
            assert_eq!(token.position, 0);
            assert_eq!(token.position_length, 1);
            assert_eq!(
                token.details(),
                vec![
                    "名詞",
                    "一般",
                    "*",
                    "*",
                    "*",
                    "*",
                    "日本語",
                    "ニホンゴ",
                    "ニホンゴ"
                ]
            );
        }
        {
            let token = tokens_iter.next().unwrap();
            assert_eq!(token.surface, "の");
            assert_eq!(token.byte_start, 9);
            assert_eq!(token.byte_end, 12);
            assert_eq!(token.position, 1);
            assert_eq!(token.position_length, 1);
            assert_eq!(
                token.details(),
                vec!["助詞", "連体化", "*", "*", "*", "*", "の", "ノ", "ノ"]
            );
        }
        {
            let token = tokens_iter.next().unwrap();
            assert_eq!(token.surface, "形態素");
            assert_eq!(token.byte_start, 12);
            assert_eq!(token.byte_end, 21);
            assert_eq!(token.position, 2);
            assert_eq!(token.position_length, 1);
            assert_eq!(
                token.details(),
                vec![
                    "名詞",
                    "一般",
                    "*",
                    "*",
                    "*",
                    "*",
                    "形態素",
                    "ケイタイソ",
                    "ケイタイソ"
                ]
            );
        }
        {
            let token = tokens_iter.next().unwrap();
            assert_eq!(token.surface, "解析");
            assert_eq!(token.byte_start, 21);
            assert_eq!(token.byte_end, 27);
            assert_eq!(token.position, 3);
            assert_eq!(token.position_length, 1);
            assert_eq!(
                token.details(),
                vec![
                    "名詞",
                    "サ変接続",
                    "*",
                    "*",
                    "*",
                    "*",
                    "解析",
                    "カイセキ",
                    "カイセキ"
                ]
            );
        }
        {
            let token = tokens_iter.next().unwrap();
            assert_eq!(token.surface, "を");
            assert_eq!(token.byte_start, 27);
            assert_eq!(token.byte_end, 30);
            assert_eq!(token.position, 4);
            assert_eq!(token.position_length, 1);
            assert_eq!(
                token.details(),
                vec!["助詞", "格助詞", "一般", "*", "*", "*", "を", "ヲ", "ヲ"]
            );
        }
        {
            let token = tokens_iter.next().unwrap();
            assert_eq!(token.surface, "行う");
            assert_eq!(token.byte_start, 30);
            assert_eq!(token.byte_end, 36);
            assert_eq!(token.position, 5);
            assert_eq!(token.position_length, 1);
            assert_eq!(
                token.details(),
                vec![
                    "動詞",
                    "自立",
                    "*",
                    "*",
                    "五段・ワ行促音便",
                    "基本形",
                    "行う",
                    "オコナウ",
                    "オコナウ"
                ]
            );
        }
        {
            let token = tokens_iter.next().unwrap();
            assert_eq!(token.surface, "こと");
            assert_eq!(token.byte_start, 36);
            assert_eq!(token.byte_end, 42);
            assert_eq!(token.position, 6);
            assert_eq!(token.position_length, 1);
            assert_eq!(
                token.details(),
                vec![
                    "名詞",
                    "非自立",
                    "一般",
                    "*",
                    "*",
                    "*",
                    "こと",
                    "コト",
                    "コト"
                ]
            );
        }
        {
            let token = tokens_iter.next().unwrap();
            assert_eq!(token.surface, "が");
            assert_eq!(token.byte_start, 42);
            assert_eq!(token.byte_end, 45);
            assert_eq!(token.position, 7);
            assert_eq!(token.position_length, 1);
            assert_eq!(
                token.details(),
                vec!["助詞", "格助詞", "一般", "*", "*", "*", "が", "ガ", "ガ"]
            );
        }
        {
            let token = tokens_iter.next().unwrap();
            assert_eq!(token.surface, "でき");
            assert_eq!(token.byte_start, 45);
            assert_eq!(token.byte_end, 51);
            assert_eq!(token.position, 8);
            assert_eq!(token.position_length, 1);
            assert_eq!(
                token.details(),
                vec![
                    "動詞",
                    "自立",
                    "*",
                    "*",
                    "一段",
                    "連用形",
                    "できる",
                    "デキ",
                    "デキ"
                ]
            );
        }
        {
            let token = tokens_iter.next().unwrap();
            assert_eq!(token.surface, "ます");
            assert_eq!(token.byte_start, 51);
            assert_eq!(token.byte_end, 57);
            assert_eq!(token.position, 9);
            assert_eq!(token.position_length, 1);
            assert_eq!(
                token.details(),
                vec![
                    "助動詞",
                    "*",
                    "*",
                    "*",
                    "特殊・マス",
                    "基本形",
                    "ます",
                    "マス",
                    "マス"
                ]
            );
        }
        {
            let token = tokens_iter.next().unwrap();
            assert_eq!(token.surface, "。");
            assert_eq!(token.byte_start, 57);
            assert_eq!(token.byte_end, 60);
            assert_eq!(token.position, 10);
            assert_eq!(token.position_length, 1);
            assert_eq!(
                token.details(),
                vec!["記号", "句点", "*", "*", "*", "*", "。", "。", "。"]
            );
        }
        {
            let token = tokens_iter.next().unwrap();
            assert_eq!(token.surface, "テスト");
            assert_eq!(token.byte_start, 60);
            assert_eq!(token.byte_end, 69);
            assert_eq!(token.position, 11);
            assert_eq!(token.position_length, 1);
            assert_eq!(
                token.details(),
                vec![
                    "名詞",
                    "サ変接続",
                    "*",
                    "*",
                    "*",
                    "*",
                    "テスト",
                    "テスト",
                    "テスト"
                ]
            );
        }
        {
            let token = tokens_iter.next().unwrap();
            assert_eq!(token.surface, "。");
            assert_eq!(token.byte_start, 69);
            assert_eq!(token.byte_end, 72);
            assert_eq!(token.position, 12);
            assert_eq!(token.position_length, 1);
            assert_eq!(
                token.details(),
                vec!["記号", "句点", "*", "*", "*", "*", "。", "。", "。"]
            );
        }
    }

    #[test]
    #[cfg(feature = "embed-ipadic")]
    fn test_segment_with_simple_userdic_ipadic() {
        use std::borrow::Cow;

        let userdic_file = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
            .join("../resources")
            .join("user_dict")
            .join("ipadic_simple_userdic.csv");

        let config = serde_json::json!({
            "dictionary": "embedded://ipadic",
            "user_dictionary": userdic_file.to_str().unwrap(),
            "mode": "normal"
        });

        let segmenter = Segmenter::from_config(&config).unwrap();
        let mut tokens = segmenter
            .segment(Cow::Borrowed(
                "東京スカイツリーの最寄り駅はとうきょうスカイツリー駅です。",
            ))
            .unwrap();
        let mut tokens_iter = tokens.iter_mut();
        {
            let token = tokens_iter.next().unwrap();
            assert_eq!(token.surface, "東京スカイツリー");
            assert_eq!(token.byte_start, 0);
            assert_eq!(token.byte_end, 24);
            assert_eq!(token.position, 0);
            assert_eq!(token.position_length, 1);
            assert_eq!(
                token.details(),
                vec![
                    "カスタム名詞",
                    "*",
                    "*",
                    "*",
                    "*",
                    "*",
                    "*",
                    "トウキョウスカイツリー",
                    "*"
                ]
            );
        }
        {
            let token = tokens_iter.next().unwrap();
            assert_eq!(token.surface, "の");
            assert_eq!(token.byte_start, 24);
            assert_eq!(token.byte_end, 27);
            assert_eq!(token.position, 1);
            assert_eq!(token.position_length, 1);
            assert_eq!(
                token.details(),
                vec!["助詞", "連体化", "*", "*", "*", "*", "の", "ノ", "ノ"]
            );
        }
        {
            let token = tokens_iter.next().unwrap();
            assert_eq!(token.surface, "最寄り駅");
            assert_eq!(token.byte_start, 27);
            assert_eq!(token.byte_end, 39);
            assert_eq!(token.position, 2);
            assert_eq!(token.position_length, 1);
            assert_eq!(
                token.details(),
                vec![
                    "名詞",
                    "一般",
                    "*",
                    "*",
                    "*",
                    "*",
                    "最寄り駅",
                    "モヨリエキ",
                    "モヨリエキ"
                ]
            );
        }
        {
            let token = tokens_iter.next().unwrap();
            assert_eq!(token.surface, "は");
            assert_eq!(token.byte_start, 39);
            assert_eq!(token.byte_end, 42);
            assert_eq!(token.position, 3);
            assert_eq!(token.position_length, 1);
            assert_eq!(
                token.details(),
                vec!["助詞", "係助詞", "*", "*", "*", "*", "は", "ハ", "ワ"]
            );
        }
        {
            let token = tokens_iter.next().unwrap();
            assert_eq!(token.surface, "とうきょうスカイツリー駅");
            assert_eq!(token.byte_start, 42);
            assert_eq!(token.byte_end, 78);
            assert_eq!(token.position, 4);
            assert_eq!(token.position_length, 1);
            assert_eq!(
                token.details(),
                vec![
                    "カスタム名詞",
                    "*",
                    "*",
                    "*",
                    "*",
                    "*",
                    "*",
                    "トウキョウスカイツリーエキ",
                    "*"
                ]
            );
        }
        {
            let token = tokens_iter.next().unwrap();
            assert_eq!(token.surface, "です");
            assert_eq!(token.byte_start, 78);
            assert_eq!(token.byte_end, 84);
            assert_eq!(token.position, 5);
            assert_eq!(token.position_length, 1);
            assert_eq!(
                token.details(),
                vec![
                    "助動詞",
                    "*",
                    "*",
                    "*",
                    "特殊・デス",
                    "基本形",
                    "です",
                    "デス",
                    "デス"
                ]
            );
        }
        {
            let token = tokens_iter.next().unwrap();
            assert_eq!(token.surface, "。");
            assert_eq!(token.byte_start, 84);
            assert_eq!(token.byte_end, 87);
            assert_eq!(token.position, 6);
            assert_eq!(token.position_length, 1);
            assert_eq!(
                token.details(),
                vec!["記号", "句点", "*", "*", "*", "*", "。", "。", "。"]
            );
        }
    }

    #[test]
    #[cfg(feature = "embed-ipadic")]
    fn test_segment_with_simple_userdic_bin_ipadic() {
        use std::borrow::Cow;

        let userdic_file = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
            .join("../resources")
            .join("user_dict")
            .join("ipadic_simple_userdic.bin");

        let config = serde_json::json!({
            "dictionary": "embedded://ipadic",
            "user_dictionary": userdic_file.to_str().unwrap(),
            "mode": "normal"
        });

        let segmenter = Segmenter::from_config(&config).unwrap();
        let mut tokens = segmenter
            .segment(Cow::Borrowed(
                "東京スカイツリーの最寄り駅はとうきょうスカイツリー駅です。",
            ))
            .unwrap();
        let mut tokens_iter = tokens.iter_mut();
        {
            let token = tokens_iter.next().unwrap();
            assert_eq!(token.surface, "東京スカイツリー");
            assert_eq!(token.byte_start, 0);
            assert_eq!(token.byte_end, 24);
            assert_eq!(token.position, 0);
            assert_eq!(token.position_length, 1);
            assert_eq!(
                token.details(),
                vec![
                    "カスタム名詞",
                    "*",
                    "*",
                    "*",
                    "*",
                    "*",
                    "*",
                    "トウキョウスカイツリー",
                    "*"
                ]
            );
        }
        {
            let token = tokens_iter.next().unwrap();
            assert_eq!(token.surface, "の");
            assert_eq!(token.byte_start, 24);
            assert_eq!(token.byte_end, 27);
            assert_eq!(token.position, 1);
            assert_eq!(token.position_length, 1);
            assert_eq!(
                token.details(),
                vec!["助詞", "連体化", "*", "*", "*", "*", "の", "ノ", "ノ"]
            );
        }
        {
            let token = tokens_iter.next().unwrap();
            assert_eq!(token.surface, "最寄り駅");
            assert_eq!(token.byte_start, 27);
            assert_eq!(token.byte_end, 39);
            assert_eq!(token.position, 2);
            assert_eq!(token.position_length, 1);
            assert_eq!(
                token.details(),
                vec![
                    "名詞",
                    "一般",
                    "*",
                    "*",
                    "*",
                    "*",
                    "最寄り駅",
                    "モヨリエキ",
                    "モヨリエキ"
                ]
            );
        }
        {
            let token = tokens_iter.next().unwrap();
            assert_eq!(token.surface, "は");
            assert_eq!(token.byte_start, 39);
            assert_eq!(token.byte_end, 42);
            assert_eq!(token.position, 3);
            assert_eq!(token.position_length, 1);
            assert_eq!(
                token.details(),
                vec!["助詞", "係助詞", "*", "*", "*", "*", "は", "ハ", "ワ"]
            );
        }
        {
            let token = tokens_iter.next().unwrap();
            assert_eq!(token.surface, "とうきょうスカイツリー駅");
            assert_eq!(token.byte_start, 42);
            assert_eq!(token.byte_end, 78);
            assert_eq!(token.position, 4);
            assert_eq!(token.position_length, 1);
            assert_eq!(
                token.details(),
                vec![
                    "カスタム名詞",
                    "*",
                    "*",
                    "*",
                    "*",
                    "*",
                    "*",
                    "トウキョウスカイツリーエキ",
                    "*"
                ]
            );
        }
        {
            let token = tokens_iter.next().unwrap();
            assert_eq!(token.surface, "です");
            assert_eq!(token.byte_start, 78);
            assert_eq!(token.byte_end, 84);
            assert_eq!(token.position, 5);
            assert_eq!(token.position_length, 1);
            assert_eq!(
                token.details(),
                vec![
                    "助動詞",
                    "*",
                    "*",
                    "*",
                    "特殊・デス",
                    "基本形",
                    "です",
                    "デス",
                    "デス"
                ]
            );
        }
        {
            let token = tokens_iter.next().unwrap();
            assert_eq!(token.surface, "。");
            assert_eq!(token.byte_start, 84);
            assert_eq!(token.byte_end, 87);
            assert_eq!(token.position, 6);
            assert_eq!(token.position_length, 1);
            assert_eq!(
                token.details(),
                vec!["記号", "句点", "*", "*", "*", "*", "。", "。", "。"]
            );
        }
    }

    #[test]
    #[cfg(feature = "embed-ipadic")]
    fn test_segment_with_detailed_userdic_ipadic() {
        use std::borrow::Cow;

        let userdic_file = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
            .join("../resources")
            .join("user_dict")
            .join("ipadic_detailed_userdic.csv");

        let config = serde_json::json!({
            "dictionary": "embedded://ipadic",
            "user_dictionary": userdic_file.to_str().unwrap(),
            "mode": "normal"
        });

        let segmenter = Segmenter::from_config(&config).unwrap();
        let mut tokens = segmenter
            .segment(Cow::Borrowed(
                "東京スカイツリーの最寄り駅はとうきょうスカイツリー駅です。",
            ))
            .unwrap();
        let mut tokens_iter = tokens.iter_mut();
        {
            let token = tokens_iter.next().unwrap();
            assert_eq!(token.surface, "東京スカイツリー");
            assert_eq!(token.byte_start, 0);
            assert_eq!(token.byte_end, 24);
            assert_eq!(token.position, 0);
            assert_eq!(token.position_length, 1);
            assert_eq!(
                token.details(),
                vec![
                    "名詞",
                    "固有名詞",
                    "一般",
                    "カスタム名詞",
                    "*",
                    "*",
                    "東京スカイツリー",
                    "トウキョウスカイツリー",
                    "トウキョウスカイツリー"
                ]
            );
        }
        {
            let token = tokens_iter.next().unwrap();
            assert_eq!(token.surface, "の");
            assert_eq!(token.byte_start, 24);
            assert_eq!(token.byte_end, 27);
            assert_eq!(token.position, 1);
            assert_eq!(token.position_length, 1);
            assert_eq!(
                token.details(),
                vec!["助詞", "連体化", "*", "*", "*", "*", "の", "ノ", "ノ"]
            );
        }
        {
            let token = tokens_iter.next().unwrap();
            assert_eq!(token.surface, "最寄り駅");
            assert_eq!(token.byte_start, 27);
            assert_eq!(token.byte_end, 39);
            assert_eq!(token.position, 2);
            assert_eq!(token.position_length, 1);
            assert_eq!(
                token.details(),
                vec![
                    "名詞",
                    "一般",
                    "*",
                    "*",
                    "*",
                    "*",
                    "最寄り駅",
                    "モヨリエキ",
                    "モヨリエキ"
                ]
            );
        }
        {
            let token = tokens_iter.next().unwrap();
            assert_eq!(token.surface, "は");
            assert_eq!(token.byte_start, 39);
            assert_eq!(token.byte_end, 42);
            assert_eq!(token.position, 3);
            assert_eq!(token.position_length, 1);
            assert_eq!(
                token.details(),
                vec!["助詞", "係助詞", "*", "*", "*", "*", "は", "ハ", "ワ"]
            );
        }
        {
            let token = tokens_iter.next().unwrap();
            assert_eq!(token.surface, "とうきょうスカイツリー駅");
            assert_eq!(token.byte_start, 42);
            assert_eq!(token.byte_end, 78);
            assert_eq!(token.position, 4);
            assert_eq!(token.position_length, 1);
            assert_eq!(
                token.details(),
                vec![
                    "名詞",
                    "固有名詞",
                    "一般",
                    "カスタム名詞",
                    "*",
                    "*",
                    "とうきょうスカイツリー駅",
                    "トウキョウスカイツリーエキ",
                    "トウキョウスカイツリーエキ"
                ]
            );
        }
        {
            let token = tokens_iter.next().unwrap();
            assert_eq!(token.surface, "です");
            assert_eq!(token.byte_start, 78);
            assert_eq!(token.byte_end, 84);
            assert_eq!(token.position, 5);
            assert_eq!(token.position_length, 1);
            assert_eq!(
                token.details(),
                vec![
                    "助動詞",
                    "*",
                    "*",
                    "*",
                    "特殊・デス",
                    "基本形",
                    "です",
                    "デス",
                    "デス"
                ]
            );
        }
        {
            let token = tokens_iter.next().unwrap();
            assert_eq!(token.surface, "。");
            assert_eq!(token.byte_start, 84);
            assert_eq!(token.byte_end, 87);
            assert_eq!(token.position, 6);
            assert_eq!(token.position_length, 1);
            assert_eq!(
                token.details(),
                vec!["記号", "句点", "*", "*", "*", "*", "。", "。", "。"]
            );
        }
    }

    #[test]
    #[cfg(feature = "embed-ipadic")]
    #[should_panic(expected = "failed to parse word cost")]
    fn test_user_dict_invalid_word_cost() {
        let userdic_file = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
            .join("../resources")
            .join("user_dict")
            .join("ipadic_userdic_invalid_word_cost.csv");

        let config = serde_json::json!({
            "dictionary": "embedded://ipadic",
            "user_dictionary": userdic_file.to_str().unwrap(),
            "mode": "normal"
        });

        Segmenter::from_config(&config).unwrap();
    }

    #[test]
    #[cfg(feature = "embed-ipadic")]
    #[should_panic(expected = "user dictionary should be a CSV with 3 or 13+ fields")]
    fn test_user_dict_number_of_fields_is_11() {
        let userdic_file = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
            .join("../resources")
            .join("user_dict")
            .join("ipadic_userdic_insufficient_number_of_fields.csv");

        let config = serde_json::json!({
            "dictionary": "embedded://ipadic",
            "user_dictionary": userdic_file.to_str().unwrap(),
            "mode": "normal"
        });

        Segmenter::from_config(&config).unwrap();
    }

    #[test]
    #[cfg(feature = "embed-ipadic")]
    fn test_segment_with_nomal_mode() {
        use std::borrow::Cow;

        let config = serde_json::json!({
            "dictionary": "embedded://ipadic",
            "mode": "normal"
        });

        let segmenter = Segmenter::from_config(&config).unwrap();
        let mut tokens = segmenter
            .segment(Cow::Borrowed("羽田空港限定トートバッグ"))
            .unwrap();
        let mut tokens_iter = tokens.iter_mut();
        {
            let token = tokens_iter.next().unwrap();
            assert_eq!(token.surface, "羽田空港");
            assert_eq!(token.byte_start, 0);
            assert_eq!(token.byte_end, 12);
            assert_eq!(token.position, 0);
            assert_eq!(token.position_length, 1);
            assert_eq!(
                token.details(),
                vec![
                    "名詞",
                    "固有名詞",
                    "一般",
                    "*",
                    "*",
                    "*",
                    "羽田空港",
                    "ハネダクウコウ",
                    "ハネダクーコー"
                ]
            );
        }
        {
            let token = tokens_iter.next().unwrap();
            assert_eq!(token.surface, "限定");
            assert_eq!(token.byte_start, 12);
            assert_eq!(token.byte_end, 18);
            assert_eq!(token.position, 1);
            assert_eq!(token.position_length, 1);
            assert_eq!(
                token.details(),
                vec![
                    "名詞",
                    "サ変接続",
                    "*",
                    "*",
                    "*",
                    "*",
                    "限定",
                    "ゲンテイ",
                    "ゲンテイ"
                ]
            );
        }
        {
            let token = tokens_iter.next().unwrap();
            assert_eq!(token.surface, "トートバッグ");
            assert_eq!(token.byte_start, 18);
            assert_eq!(token.byte_end, 36);
            assert_eq!(token.position, 2);
            assert_eq!(token.position_length, 1);
            assert!(token.word_id.is_unknown());
            assert_eq!(
                token.details(),
                vec!["名詞", "一般", "*", "*", "*", "*", "*", "*", "*"]
            );
        }
    }

    #[test]
    #[cfg(feature = "embed-ipadic")]
    fn test_segment_with_decompose_mode() {
        use std::borrow::Cow;

        let config = serde_json::json!({
            "dictionary": "embedded://ipadic",
            "mode": {
                "decompose": {
                    "kanji_penalty_length_threshold": 2,
                    "kanji_penalty_length_penalty": 3000,
                    "other_penalty_length_threshold": 7,
                    "other_penalty_length_penalty": 1700
                }
            }
        });

        let segmenter = Segmenter::from_config(&config).unwrap();
        let mut tokens = segmenter
            .segment(Cow::Borrowed("羽田空港限定トートバッグ"))
            .unwrap();
        let mut tokens_iter = tokens.iter_mut();
        {
            let token = tokens_iter.next().unwrap();
            assert_eq!(token.surface, "羽田");
            assert_eq!(token.byte_start, 0);
            assert_eq!(token.byte_end, 6);
            assert_eq!(token.position, 0);
            assert_eq!(token.position_length, 1);
            assert_eq!(
                token.details(),
                vec![
                    "名詞",
                    "固有名詞",
                    "人名",
                    "姓",
                    "*",
                    "*",
                    "羽田",
                    "ハタ",
                    "ハタ"
                ]
            );
        }
        {
            let token = tokens_iter.next().unwrap();
            assert_eq!(token.surface, "空港");
            assert_eq!(token.byte_start, 6);
            assert_eq!(token.byte_end, 12);
            assert_eq!(token.position, 1);
            assert_eq!(token.position_length, 1);
            assert_eq!(
                token.details(),
                vec![
                    "名詞",
                    "一般",
                    "*",
                    "*",
                    "*",
                    "*",
                    "空港",
                    "クウコウ",
                    "クーコー"
                ]
            );
        }
        {
            let token = tokens_iter.next().unwrap();
            assert_eq!(token.surface, "限定");
            assert_eq!(token.byte_start, 12);
            assert_eq!(token.byte_end, 18);
            assert_eq!(token.position, 2);
            assert_eq!(token.position_length, 1);
            assert_eq!(
                token.details(),
                vec![
                    "名詞",
                    "サ変接続",
                    "*",
                    "*",
                    "*",
                    "*",
                    "限定",
                    "ゲンテイ",
                    "ゲンテイ"
                ]
            );
        }
        {
            let token = tokens_iter.next().unwrap();
            assert_eq!(token.surface, "トートバッグ");
            assert_eq!(token.byte_start, 18);
            assert_eq!(token.byte_end, 36);
            assert_eq!(token.position, 3);
            assert_eq!(token.position_length, 1);
            assert!(token.word_id.is_unknown());
            assert_eq!(
                token.details(),
                vec!["名詞", "一般", "*", "*", "*", "*", "*", "*", "*"]
            );
        }
    }

    #[test]
    #[cfg(feature = "embed-ipadic")]
    fn test_segment_with_decompose_mode_default_penalty() {
        use std::borrow::Cow;

        let config = serde_json::json!({
            "dictionary": "embedded://ipadic",
            "mode": "decompose"
        });

        let segmenter = Segmenter::from_config(&config).unwrap();

        let mut tokens = segmenter
            .segment(Cow::Borrowed("羽田空港限定トートバッグ"))
            .unwrap();
        let mut tokens_iter = tokens.iter_mut();
        {
            let token = tokens_iter.next().unwrap();
            assert_eq!(token.surface, "羽田");
            assert_eq!(token.byte_start, 0);
            assert_eq!(token.byte_end, 6);
            assert_eq!(token.position, 0);
            assert_eq!(token.position_length, 1);
            assert_eq!(
                token.details(),
                vec![
                    "名詞",
                    "固有名詞",
                    "人名",
                    "姓",
                    "*",
                    "*",
                    "羽田",
                    "ハタ",
                    "ハタ"
                ]
            );
        }
        {
            let token = tokens_iter.next().unwrap();
            assert_eq!(token.surface, "空港");
            assert_eq!(token.byte_start, 6);
            assert_eq!(token.byte_end, 12);
            assert_eq!(token.position, 1);
            assert_eq!(token.position_length, 1);
            assert_eq!(
                token.details(),
                vec![
                    "名詞",
                    "一般",
                    "*",
                    "*",
                    "*",
                    "*",
                    "空港",
                    "クウコウ",
                    "クーコー"
                ]
            );
        }
        {
            let token = tokens_iter.next().unwrap();
            assert_eq!(token.surface, "限定");
            assert_eq!(token.byte_start, 12);
            assert_eq!(token.byte_end, 18);
            assert_eq!(token.position, 2);
            assert_eq!(token.position_length, 1);
            assert_eq!(
                token.details(),
                vec![
                    "名詞",
                    "サ変接続",
                    "*",
                    "*",
                    "*",
                    "*",
                    "限定",
                    "ゲンテイ",
                    "ゲンテイ"
                ]
            );
        }
        {
            let token = tokens_iter.next().unwrap();
            assert_eq!(token.surface, "トートバッグ");
            assert_eq!(token.byte_start, 18);
            assert_eq!(token.byte_end, 36);
            assert_eq!(token.position, 3);
            assert_eq!(token.position_length, 1);
            assert!(token.word_id.is_unknown());
            assert_eq!(
                token.details(),
                vec!["名詞", "一般", "*", "*", "*", "*", "*", "*", "*"]
            );
        }
    }

    #[test]
    #[cfg(feature = "embed-ipadic")]
    fn test_segment_default_ignores_space() {
        use std::borrow::Cow;

        let config_str = r#"
        {
            "dictionary": "embedded://ipadic",
            "mode": "normal"
        }
        "#;
        let config = serde_json::from_str::<SegmenterConfig>(config_str).unwrap();

        let segmenter = Segmenter::from_config(&config).unwrap();
        let tokens = segmenter.segment(Cow::Borrowed("東京 都")).unwrap();

        // Default behavior: should have 2 tokens, space is ignored (MeCab compatible)
        assert_eq!(tokens.len(), 2);
        assert_eq!(tokens[0].surface, "東京");
        assert_eq!(tokens[1].surface, "都");
    }

    #[test]
    #[cfg(feature = "embed-ipadic")]
    fn test_segment_with_keep_whitespace() {
        use std::borrow::Cow;

        let config_str = r#"
        {
            "dictionary": "embedded://ipadic",
            "mode": "normal",
            "keep_whitespace": true
        }
        "#;
        let config = serde_json::from_str::<SegmenterConfig>(config_str).unwrap();

        let segmenter = Segmenter::from_config(&config).unwrap();
        let tokens = segmenter.segment(Cow::Borrowed("東京 都")).unwrap();

        // With keep_whitespace=true: should have 3 tokens including space
        assert_eq!(tokens.len(), 3);
        assert_eq!(tokens[0].surface, "東京");
        assert_eq!(tokens[1].surface, " ");
        assert_eq!(tokens[2].surface, "都");
    }

    #[test]
    #[cfg(feature = "embed-ipadic")]
    fn test_segment_with_builder_keep_whitespace() {
        use std::borrow::Cow;

        use crate::dictionary::load_dictionary;
        use crate::mode::Mode;

        let dictionary = load_dictionary("embedded://ipadic").unwrap();
        let segmenter = Segmenter::new(Mode::Normal, dictionary, None).keep_whitespace(true);
        let tokens = segmenter.segment(Cow::Borrowed("東京 都")).unwrap();

        // With keep_whitespace=true: should have 3 tokens including space
        assert_eq!(tokens.len(), 3);
        assert_eq!(tokens[0].surface, "東京");
        assert_eq!(tokens[1].surface, " ");
        assert_eq!(tokens[2].surface, "都");
    }

    #[test]
    #[cfg(feature = "embed-ipadic")]
    fn test_dictionary_clone_shares_heavy_fields_via_arc() {
        use std::sync::Arc;

        use crate::dictionary::load_dictionary;

        let dictionary = load_dictionary("embedded://ipadic").unwrap();
        assert_eq!(Arc::strong_count(&dictionary.prefix_dictionary), 1);
        assert_eq!(Arc::strong_count(&dictionary.connection_cost_matrix), 1);

        let cloned = dictionary.clone();

        // A cheap (Arc-based) clone bumps the refcount rather than
        // allocating a new copy of the trie/cost matrix.
        assert_eq!(Arc::strong_count(&dictionary.prefix_dictionary), 2);
        assert_eq!(Arc::strong_count(&dictionary.connection_cost_matrix), 2);
        assert!(Arc::ptr_eq(
            &dictionary.prefix_dictionary,
            &cloned.prefix_dictionary
        ));
        assert!(Arc::ptr_eq(
            &dictionary.connection_cost_matrix,
            &cloned.connection_cost_matrix
        ));

        drop(cloned);
        assert_eq!(Arc::strong_count(&dictionary.prefix_dictionary), 1);
        assert_eq!(Arc::strong_count(&dictionary.connection_cost_matrix), 1);
    }

    #[test]
    #[cfg(feature = "embed-ipadic")]
    fn test_from_config_use_mmap_is_ignored_for_embedded_dictionary() {
        // `use_mmap: true` has no effect on an `embedded://` dictionary
        // (the data is already a static, zero-copy byte slice) but must not
        // be treated as an error either.
        let config_str = r#"
        {
            "dictionary": "embedded://ipadic",
            "mode": "normal",
            "use_mmap": true
        }
        "#;

        let config: SegmenterConfig = serde_json::from_str(config_str).unwrap();
        let result = Segmenter::from_config(&config);
        assert!(result.is_ok());
    }

    #[test]
    #[cfg(feature = "embed-ipadic")]
    fn test_segment_default_multiple_spaces() {
        use std::borrow::Cow;

        let config_str = r#"
        {
            "dictionary": "embedded://ipadic",
            "mode": "normal"
        }
        "#;
        let config = serde_json::from_str::<SegmenterConfig>(config_str).unwrap();

        let segmenter = Segmenter::from_config(&config).unwrap();
        let tokens = segmenter.segment(Cow::Borrowed("東京   都")).unwrap();

        // Should have 2 tokens: "東京" and "都", multiple spaces are ignored
        assert_eq!(tokens.len(), 2);
        assert_eq!(tokens[0].surface, "東京");
        assert_eq!(tokens[1].surface, "都");
    }

    #[test]
    #[cfg(feature = "embed-ipadic")]
    fn test_segment_default_leading_trailing() {
        use std::borrow::Cow;

        let config_str = r#"
        {
            "dictionary": "embedded://ipadic",
            "mode": "normal"
        }
        "#;
        let config = serde_json::from_str::<SegmenterConfig>(config_str).unwrap();

        let segmenter = Segmenter::from_config(&config).unwrap();

        // Leading spaces - "   東京都" is segmented as "東京" and "都" (not "東京都")
        let tokens = segmenter.segment(Cow::Borrowed("   東京都")).unwrap();
        assert_eq!(tokens.len(), 2);
        assert_eq!(tokens[0].surface, "東京");
        assert_eq!(tokens[1].surface, "都");

        // Trailing spaces - "東京都   " is also segmented as "東京" and "都"
        let tokens = segmenter.segment(Cow::Borrowed("東京都   ")).unwrap();
        assert_eq!(tokens.len(), 2);
        assert_eq!(tokens[0].surface, "東京");
        assert_eq!(tokens[1].surface, "都");
    }

    #[test]
    #[cfg(feature = "embed-ipadic")]
    fn test_long_text() {
        use std::borrow::Cow;

        let mut large_file = BufReader::new(
            File::open(
                PathBuf::from(env!("CARGO_MANIFEST_DIR"))
                    .join("../resources")
                    .join("bocchan.txt"),
            )
            .unwrap(),
        );
        let mut large_text = String::new();
        let _size = large_file.read_to_string(&mut large_text).unwrap();

        let config = serde_json::json!({
            "dictionary": "embedded://ipadic",
            "mode": "normal"
        });

        let segmenter = Segmenter::from_config(&config).unwrap();

        let tokens = segmenter
            .segment(Cow::Borrowed(large_text.as_str()))
            .unwrap();
        assert!(!tokens.is_empty());
    }

    /// Regression test for https://github.com/lindera/lindera/issues/871:
    /// delimiter-free (no `\n`, `\t`, `。`, `、`) mixed-script text used to
    /// build one ever-growing Viterbi lattice whose accumulated path cost
    /// could saturate `i32::MAX`, silently collapsing the remainder of the
    /// input into a single giant token. This asserts that text well past
    /// `MAX_SENTENCE_BYTES` still segments into many small tokens instead of
    /// one oversized one.
    #[test]
    #[cfg(feature = "embed-ipadic")]
    fn test_segment_bounds_lattice_for_delimiter_free_text() {
        use std::borrow::Cow;

        use crate::dictionary::load_dictionary;
        use crate::mode::Mode;
        use crate::segmenter::MAX_SENTENCE_BYTES;

        // Space-joined, mixed Japanese/English words with no real sentence
        // delimiter, comfortably longer than MAX_SENTENCE_BYTES.
        let words = ["test", "検証", "buffer", "実装", "measure", "性能"];
        let mut text = String::new();
        while text.len() < MAX_SENTENCE_BYTES * 2 {
            for w in &words {
                text.push_str(w);
                text.push(' ');
            }
        }

        let dictionary = load_dictionary("embedded://ipadic").unwrap();
        let segmenter = Segmenter::new(Mode::Normal, dictionary, None);
        let tokens = segmenter.segment(Cow::Borrowed(text.as_str())).unwrap();

        assert!(!tokens.is_empty());
        let biggest = tokens
            .iter()
            .map(|t| t.byte_end - t.byte_start)
            .max()
            .unwrap();
        assert!(
            biggest < 100,
            "expected only small word tokens, got a {biggest}-byte token; \
             the lattice was not bounded for delimiter-free input"
        );
    }

    #[test]
    #[cfg(feature = "embed-ipadic")]
    fn test_segment_nbest_1best_matches_segment() {
        use std::borrow::Cow;

        let config = serde_json::json!({
            "dictionary": "embedded://ipadic",
            "mode": "normal"
        });
        let segmenter = Segmenter::from_config(&config).unwrap();

        let text = "すもももももももものうち";

        // 1-best result should match normal segment
        let normal_tokens = segmenter.segment(Cow::Borrowed(text)).unwrap();
        let nbest_results = segmenter
            .segment_nbest(Cow::Borrowed(text), 1, false, None)
            .unwrap();

        assert_eq!(nbest_results.len(), 1);
        let (nbest_tokens, _cost) = &nbest_results[0];
        assert_eq!(normal_tokens.len(), nbest_tokens.len());
        for (normal, nbest) in normal_tokens.iter().zip(nbest_tokens.iter()) {
            assert_eq!(normal.surface.as_ref(), nbest.surface.as_ref());
            assert_eq!(normal.byte_start, nbest.byte_start);
            assert_eq!(normal.byte_end, nbest.byte_end);
        }
    }

    #[test]
    #[cfg(feature = "embed-ipadic")]
    fn test_segment_nbest_multiple_results() {
        use std::borrow::Cow;

        let config = serde_json::json!({
            "dictionary": "embedded://ipadic",
            "mode": "normal"
        });
        let segmenter = Segmenter::from_config(&config).unwrap();

        let text = "すもももももももものうち";
        let results = segmenter
            .segment_nbest(Cow::Borrowed(text), 3, false, None)
            .unwrap();

        // Should return at least 2 different results for this ambiguous text
        assert!(results.len() >= 2);

        // All results should cover the full text
        for (tokens, _cost) in &results {
            assert!(!tokens.is_empty());
            // First token starts at 0
            assert_eq!(tokens[0].byte_start, 0);
            // Last token ends at the end of the text
            let last = tokens.last().unwrap();
            assert_eq!(last.byte_end, text.len());
        }
    }

    #[test]
    #[cfg(feature = "embed-ipadic")]
    fn test_segment_nbest_empty_input() {
        use std::borrow::Cow;

        let config = serde_json::json!({
            "dictionary": "embedded://ipadic",
            "mode": "normal"
        });
        let segmenter = Segmenter::from_config(&config).unwrap();

        let results = segmenter
            .segment_nbest(Cow::Borrowed(""), 3, false, None)
            .unwrap();
        assert!(results.is_empty() || results.iter().all(|(r, _)| r.is_empty()));
    }

    #[test]
    #[cfg(feature = "embed-ipadic")]
    fn test_segment_nbest_zero_n() {
        use std::borrow::Cow;

        let config = serde_json::json!({
            "dictionary": "embedded://ipadic",
            "mode": "normal"
        });
        let segmenter = Segmenter::from_config(&config).unwrap();

        let results = segmenter
            .segment_nbest(Cow::Borrowed("テスト"), 0, false, None)
            .unwrap();
        assert!(results.is_empty());
    }

    #[test]
    #[cfg(feature = "embed-ipadic")]
    fn test_segment_nbest_decompose_mode() {
        use std::borrow::Cow;

        let config = serde_json::json!({
            "dictionary": "embedded://ipadic",
            "mode": {
                "decompose": {
                    "kanji_penalty_length_threshold": 2,
                    "kanji_penalty_length_penalty": 3000,
                    "other_penalty_length_threshold": 7,
                    "other_penalty_length_penalty": 1700
                }
            }
        });
        let segmenter = Segmenter::from_config(&config).unwrap();

        let text = "関西国際空港限定トートバッグ";
        let results = segmenter
            .segment_nbest(Cow::Borrowed(text), 2, false, None)
            .unwrap();

        // Should get at least 1 result
        assert!(!results.is_empty());
        // All tokens should cover the full text
        for (tokens, _cost) in &results {
            assert!(!tokens.is_empty());
            assert_eq!(tokens[0].byte_start, 0);
            assert_eq!(tokens.last().unwrap().byte_end, text.len());
        }
    }

    /// Left-space penalty (mecab-ko `left-space-penalty-factor`) on ko-dic.
    #[cfg(feature = "embed-ko-dic")]
    mod space_penalty {
        use std::borrow::Cow;
        use std::io::Write;

        use lindera_dictionary::dictionary::schema::Schema;
        use lindera_dictionary::viterbi::{LexType, WordId};

        use crate::dictionary::{load_dictionary, load_user_dictionary};
        use crate::mode::{Mode, Penalty};
        use crate::segmenter::{Segmenter, SegmenterConfig};
        use crate::space_penalty::{SpacePenaltyConfig, SpacePenaltyRule};

        /// mecab-ko-dic's `left-space-penalty-factor`, expressed by first
        /// POS tag (pos-id.def: 100/200 = E*, 172/230 = VCP, 183-185/220-222
        /// = XS* -> 3000; 120/210 = J* -> 6000).
        fn ko_dic_rules() -> SpacePenaltyConfig {
            SpacePenaltyConfig::new(vec![
                SpacePenaltyRule::new(
                    ["EC", "EF", "EP", "ETM", "ETN", "VCP", "XSA", "XSN", "XSV"],
                    3000,
                ),
                SpacePenaltyRule::new(
                    ["JC", "JKB", "JKC", "JKG", "JKO", "JKQ", "JKS", "JKV", "JX"],
                    6000,
                ),
            ])
        }

        fn segmenter(mode: Mode, penalty: bool) -> Segmenter {
            let dictionary = load_dictionary("embedded://ko-dic").unwrap();
            Segmenter::new(mode, dictionary, None)
                .space_penalty(penalty.then(ko_dic_rules))
                .unwrap()
        }

        /// Renders tokens as `surface/POS` (the first detail field).
        fn render(segmenter: &Segmenter, text: &str) -> Vec<String> {
            segmenter
                .segment(Cow::Borrowed(text))
                .unwrap()
                .iter_mut()
                .map(|t| {
                    let pos = t.details()[0].to_string();
                    format!("{}/{}", t.surface, pos)
                })
                .collect()
        }

        /// N-best results rendered like [`render`], paired with their costs.
        fn render_nbest(segmenter: &Segmenter, text: &str, n: usize) -> Vec<(Vec<String>, i64)> {
            segmenter
                .segment_nbest(Cow::Borrowed(text), n, false, None)
                .unwrap()
                .into_iter()
                .map(|(mut tokens, cost)| {
                    let rendered = tokens
                        .iter_mut()
                        .map(|t| {
                            let pos = t.details()[0].to_string();
                            format!("{}/{}", t.surface, pos)
                        })
                        .collect();
                    (rendered, cost)
                })
                .collect()
        }

        fn words(s: &str) -> Vec<String> {
            s.split_whitespace().map(str::to_string).collect()
        }

        /// The penalty flips the particle/ending reading of a token that
        /// follows a space (`시/EP` -> `시/NNG`, `이/VCP` -> a non-VCP tag),
        /// which is what mecab-ko does for these inputs; with the option off
        /// the v6.0.0 output is unchanged.
        #[test]
        fn test_space_penalty_flips_spaced_particles_and_endings() {
            let off = segmenter(Mode::Normal, false);
            let on = segmenter(Mode::Normal, true);

            assert_eq!(
                render(&off, "서울 시 에서 출발"),
                words("서울/NNP 시/EP 에서/JKB 출발/NNG")
            );
            let penalized = render(&on, "서울 시 에서 출발");
            assert_eq!(penalized[1], "시/NNG", "{penalized:?}");

            assert_eq!(
                render(&off, "검색 이 잘 된다"),
                words("검색/NNG 이/VCP 잘/MAG 된다/VV+EC")
            );
            let penalized = render(&on, "검색 이 잘 된다");
            assert_ne!(penalized[1], "이/VCP", "{penalized:?}");
            assert_eq!(&penalized[2..], &words("잘/MAG 된다/VV+EC")[..]);
        }

        /// Sentences without a space before a particle/ending must not
        /// change, whether or not they contain other spaces.
        #[test]
        fn test_space_penalty_leaves_unspaced_sentences_unchanged() {
            let off = segmenter(Mode::Normal, false);
            let on = segmenter(Mode::Normal, true);

            let cases = [
                (
                    "무궁화꽃이 피었습니다.",
                    "무궁화/NNG 꽃/NNG 이/JKS 피/VV 었/EP 습니다/EF ./SF",
                ),
                (
                    "아버지가방에들어가신다",
                    "아버지/NNG 가/JKS 방/NNG 에/JKB 들어가/VV 신다/EP+EC",
                ),
                (
                    "기계학습을활용한이미지인식",
                    "기계/NNG 학습/NNG 을/JKO 활용/NNG 한/XSV+ETM 이미지/NNG 인식/NNG",
                ),
                (
                    "삼천2백2십삼원",
                    "삼/NR 천/NR 2/SN 백/NR 2/SN 십/NR 삼/NR 원/NNBC",
                ),
                ("서울시에서 출발", "서울시/NNP 에서/JKB 출발/NNG"),
                ("검색이 잘 된다", "검색/NNG 이/JKS 잘/MAG 된다/VV+EC"),
                // A particle at the sentence start has no preceding space.
                ("에서", "에서/JKB"),
            ];
            for (text, expected) in cases {
                assert_eq!(render(&off, text), words(expected), "off: {text}");
                assert_eq!(render(&on, text), words(expected), "on: {text}");
            }
        }

        /// The penalty is applied in Decompose mode too.
        #[test]
        fn test_space_penalty_applies_in_decompose_mode() {
            let mode = Mode::Decompose(Penalty::default());
            let off = render(&segmenter(mode.clone(), false), "서울 시 에서 출발");
            let on = render(&segmenter(mode, true), "서울 시 에서 출발");
            assert_eq!(off[1], "시/EP");
            assert_eq!(on[1], "시/NNG");
        }

        /// N-best costs account for the penalty exactly: a segmentation's
        /// cost rises by the sum of the rule costs of its tokens that follow
        /// a space, and the 1-best path agrees with `segment`.
        #[test]
        fn test_space_penalty_is_reflected_in_nbest_costs() {
            let text = "서울 시 에서 출발";
            let off = segmenter(Mode::Normal, false);
            let on = segmenter(Mode::Normal, true);

            let off_results = render_nbest(&off, text, 20);
            let on_results = render_nbest(&on, text, 20);

            // 1-best agrees with segment().
            assert_eq!(on_results[0].0, render(&on, text));
            assert_eq!(off_results[0].0, render(&off, text));

            // 시/EP (3000, after a space) and 에서/JKB (6000, after a space):
            // +9000 relative to the unpenalized lattice.
            let ep_path = words("서울/NNP 시/EP 에서/JKB 출발/NNG");
            let cost = |results: &[(Vec<String>, i64)], path: &[String]| {
                results
                    .iter()
                    .find(|(p, _)| p == path)
                    .map(|(_, c)| *c)
                    .unwrap_or_else(|| panic!("path {path:?} not in {results:?}"))
            };
            assert_eq!(
                cost(&on_results, &ep_path),
                cost(&off_results, &ep_path) + 9000
            );

            // 시/NNG carries no penalty; 에서/JKB carries 6000.
            let nng_path = words("서울/NNP 시/NNG 에서/JKB 출발/NNG");
            assert_eq!(
                cost(&on_results, &nng_path),
                cost(&off_results, &nng_path) + 6000
            );

            // The N-best list stays sorted by cost.
            assert!(on_results.windows(2).all(|w| w[0].1 <= w[1].1));
        }

        /// The precomputed table covers the system, user and unknown
        /// lexicons and resolves rules by first POS tag.
        #[test]
        fn test_space_penalty_table_covers_all_lexicons() {
            let dictionary = load_dictionary("embedded://ko-dic").unwrap();

            let mut csv = tempfile::Builder::new().suffix(".csv").tempfile().unwrap();
            writeln!(csv, "테스트조사,JKB,테스트조사").unwrap();
            writeln!(csv, "테스트명사,NNG,테스트명사").unwrap();
            writeln!(csv, "테스트어미,EP,테스트어미").unwrap();
            csv.flush().unwrap();
            let user_dictionary =
                load_user_dictionary(csv.path().to_str().unwrap(), &dictionary.metadata).unwrap();

            let segmenter = Segmenter::new(Mode::Normal, dictionary, Some(user_dictionary))
                .space_penalty(Some(ko_dic_rules()))
                .unwrap();
            let table = segmenter.space_penalty_table.as_deref().unwrap();

            // Resolve word ids through segmentation (user rows are re-numbered
            // in sorted order by the builder, so ids are not the CSV order).
            let by_surface = |tokens: &mut Vec<crate::token::Token>, surface: &str| {
                tokens
                    .iter_mut()
                    .find(|t| t.surface == surface)
                    .map(|t| (t.word_id, t.details()[0].to_string()))
                    .unwrap()
            };

            // User entries (the simple-format default cost makes them win).
            for (surface, pos, expected) in [
                ("테스트조사", "JKB", 6000),
                ("테스트명사", "NNG", 0),
                ("테스트어미", "EP", 3000),
            ] {
                let mut tokens = segmenter.segment(Cow::Borrowed(surface)).unwrap();
                let (id, found_pos) = by_surface(&mut tokens, surface);
                assert_eq!(id.lex_type(), LexType::User, "{surface}");
                assert_eq!(found_pos, pos, "{surface}");
                assert_eq!(table.cost(id), expected, "{surface}");
            }

            // System entries.
            let mut tokens = segmenter.segment(Cow::Borrowed("서울시에서")).unwrap();
            let (id, pos) = by_surface(&mut tokens, "에서");
            assert_eq!(id.lex_type(), LexType::System);
            assert_eq!(pos, "JKB");
            assert_eq!(table.cost(id), 6000);
            let (id, pos) = by_surface(&mut tokens, "서울시");
            assert_eq!(pos, "NNP");
            assert_eq!(table.cost(id), 0);

            // Unknown words (ko-dic unk.def has no penalized tag).
            let tokens = segmenter.segment(Cow::Borrowed("쀓쀓")).unwrap();
            assert!(tokens[0].word_id.is_unknown());
            assert_eq!(table.cost(tokens[0].word_id), 0);

            // Out-of-range ids never panic.
            assert_eq!(table.cost(WordId::new(LexType::User, 999)), 0);
            assert_eq!(table.cost(WordId::default()), 0);
        }

        /// `from_config` accepts an object of explicit rules, `true` for the
        /// rules the dictionary ships in its metadata, `false` for off,
        /// leaves the `Segmenter::new` default (the shipped rules) for
        /// `null`/absent, and rejects malformed objects.
        #[test]
        fn test_from_config_space_penalty() {
            let base = serde_json::json!({
                "dictionary": "embedded://ko-dic",
                "mode": "normal",
            });

            let mut config: SegmenterConfig = base.clone();
            config["space_penalty"] = serde_json::to_value(ko_dic_rules()).unwrap();
            let segmenter = Segmenter::from_config(&config).unwrap();
            assert_eq!(segmenter.space_penalty_config(), Some(&ko_dic_rules()));
            assert_eq!(render(&segmenter, "서울 시 에서 출발")[1], "시/NNG");

            // `false` turns the penalty off and reproduces the v6.0 output.
            let mut config = base.clone();
            config["space_penalty"] = serde_json::json!(false);
            let segmenter = Segmenter::from_config(&config).unwrap();
            assert!(segmenter.space_penalty_config().is_none());
            assert_eq!(render(&segmenter, "서울 시 에서 출발")[1], "시/EP");

            // Absent or `null` keeps the default `Segmenter::new` chose: the
            // rules ko-dic ships in its metadata.json.
            let mut null_config = base.clone();
            null_config["space_penalty"] = serde_json::Value::Null;
            for config in [&base, &null_config] {
                let segmenter = Segmenter::from_config(config).unwrap();
                assert_eq!(segmenter.space_penalty_config(), Some(&ko_dic_rules()));
                assert_eq!(render(&segmenter, "서울 시 에서 출발")[1], "시/NNG");
            }

            // `true` takes the rules ko-dic ships in its metadata.json, which
            // are mecab-ko-dic's `left-space-penalty-factor`.
            let mut config = base.clone();
            config["space_penalty"] = serde_json::json!(true);
            let segmenter = Segmenter::from_config(&config).unwrap();
            assert_eq!(segmenter.space_penalty_config(), Some(&ko_dic_rules()));
            assert_eq!(render(&segmenter, "서울 시 에서 출발")[1], "시/NNG");

            let mut config = base.clone();
            config["space_penalty"] = serde_json::json!({"rules": [{"pos": "JKB", "cost": 1}]});
            assert!(Segmenter::from_config(&config).is_err());
        }

        /// `SpacePenaltyTable::is_space` classifies by the dictionary's
        /// `SPACE` category, not by Unicode `White_Space`. The two differ on
        /// ko-dic: U+3000 IDEOGRAPHIC SPACE is `SYMBOL` in its `char.def`, so
        /// it must not trigger the penalty even though `char::is_whitespace`
        /// accepts it. The ASCII fast path must agree with the category
        /// lookup it replaces.
        #[test]
        fn test_space_penalty_whitespace_is_the_space_category() {
            let on = segmenter(Mode::Normal, true);
            let table = on.space_penalty_table.as_deref().unwrap();
            let char_definitions = &on.dictionary.character_definition;

            // ko-dic char.def SPACE: 0x20, 0x09, 0x0A, 0x0B, 0x0D.
            for c in [' ', '\t', '\n', '\u{0B}', '\r'] {
                assert!(table.is_space(c, char_definitions), "{c:?}");
            }
            for c in ['가', 'a', '0', '.', '\u{3000}'] {
                assert!(!table.is_space(c, char_definitions), "{c:?}");
            }
            // U+3000 is Unicode whitespace but not a ko-dic SPACE character.
            assert!('\u{3000}'.is_whitespace());

            // The ASCII fast path must return what the category lookup would.
            let space_id = char_definitions.category_id_by_name("SPACE").unwrap();
            for codepoint in 0..256u32 {
                let c = char::from_u32(codepoint).unwrap();
                assert_eq!(
                    table.is_space(c, char_definitions),
                    char_definitions.lookup_categories(c).contains(&space_id),
                    "U+{codepoint:04X}"
                );
            }

            // And the classification is what actually gates the penalty: an
            // input whose only "whitespace" is U+3000 is analyzed identically
            // with the penalty on and off. (U+3000 surfaces as its own `SY`
            // token rather than being dropped, which is the other half of not
            // being a `SPACE` character.)
            let off = segmenter(Mode::Normal, false);
            assert_eq!(
                render(&on, "서울\u{3000}시"),
                render(&off, "서울\u{3000}시")
            );
        }

        /// `Segmenter::new` applies the rules the dictionary ships (ko-dic),
        /// leaves a dictionary without rules unpenalized, and
        /// `space_penalty(None)` opts out and reproduces the v6.0 output.
        #[test]
        fn test_new_applies_shipped_rules_by_default() {
            let dictionary = load_dictionary("embedded://ko-dic").unwrap();
            let segmenter = Segmenter::new(Mode::Normal, dictionary, None);
            assert_eq!(segmenter.space_penalty_config(), Some(&ko_dic_rules()));
            assert_eq!(render(&segmenter, "서울 시 에서 출발")[1], "시/NNG");

            let off = segmenter.space_penalty(None).unwrap();
            assert!(off.space_penalty_config().is_none());
            assert_eq!(render(&off, "서울 시 에서 출발")[1], "시/EP");

            // A dictionary that ships no rules stays unpenalized.
            let mut dictionary = load_dictionary("embedded://ko-dic").unwrap();
            let mut metadata = (*dictionary.metadata).clone();
            metadata.space_penalty = None;
            dictionary.metadata = std::sync::Arc::new(metadata);
            let segmenter = Segmenter::new(Mode::Normal, dictionary, None);
            assert!(segmenter.space_penalty_config().is_none());
            assert_eq!(render(&segmenter, "서울 시 에서 출발")[1], "시/EP");
        }

        /// Shipped rules that cannot be applied (a schema without a
        /// part-of-speech field) must not make `new` fail: it warns and runs
        /// unpenalized.
        #[test]
        fn test_new_tolerates_shipped_rules_it_cannot_apply() {
            let mut dictionary = load_dictionary("embedded://ko-dic").unwrap();
            let mut metadata = (*dictionary.metadata).clone();
            // Rename the part-of-speech column so the lookup table cannot be
            // built; the rules themselves stay in place.
            let fields = metadata
                .dictionary_schema
                .fields
                .iter()
                .map(|field| {
                    if field == "part_of_speech_tag" {
                        "pos".to_string()
                    } else {
                        field.clone()
                    }
                })
                .collect();
            metadata.dictionary_schema = Schema::new(fields);
            dictionary.metadata = std::sync::Arc::new(metadata);

            let segmenter = Segmenter::new(Mode::Normal, dictionary, None);
            assert!(segmenter.space_penalty_config().is_none());
            assert_eq!(render(&segmenter, "서울 시 에서 출발")[1], "시/EP");
        }

        /// The dictionary-shipped rules are also reachable from the builder,
        /// and a dictionary without rules reports an error rather than
        /// silently running unpenalized.
        #[test]
        fn test_space_penalty_from_dictionary() {
            let dictionary = load_dictionary("embedded://ko-dic").unwrap();
            assert_eq!(
                dictionary.metadata.space_penalty.as_ref(),
                Some(&ko_dic_rules())
            );
            let segmenter = Segmenter::new(Mode::Normal, dictionary, None)
                .space_penalty_from_dictionary()
                .unwrap();
            assert_eq!(render(&segmenter, "서울 시 에서 출발")[1], "시/NNG");

            let mut dictionary = load_dictionary("embedded://ko-dic").unwrap();
            let mut metadata = (*dictionary.metadata).clone();
            metadata.space_penalty = None;
            dictionary.metadata = std::sync::Arc::new(metadata);
            let mut segmenter = Segmenter::new(Mode::Normal, dictionary, None);
            assert!(segmenter.set_space_penalty_from_dictionary().is_err());
            assert!(segmenter.space_penalty_config().is_none());
        }
    }
}