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
//! Port of `lang_TH.py` (Thai). Registry key `"th"` → `Num2Word_TH`
//! (verified in `num2words2/__init__.py`).
//!
//! Shape: **self-contained**. `Num2Word_TH` subclasses `Num2Word_Base` and
//! *does* define `high_numwords`/`mid_numwords`/`low_numwords`, so Python's
//! `__init__` builds `self.cards` and sets `MAXVAL` — but every one of those
//! is dead code, because TH overrides `to_cardinal` outright and drives its
//! own `left_num_to_text` → `split_six` → `splitnum` → `add_text_million`
//! pipeline. `cards`/`maxval`/`merge` therefore stay at their trait defaults
//! here, and there is **no overflow check** at any size (see bug 1 below).
//!
//! Inherited from `Num2Word_Base` (unchanged by TH, so the trait defaults do
//! the right thing):
//! * `to_ordinal_num(value) -> value` → default `Ok(value.to_string())`.
//! Corpus confirms `to_ordinal_num(-1)` == "-1" (no sign handling, no
//! ordinal suffix — Python literally returns the input unchanged and the
//! dispatcher stringifies it).
//! * `to_year(value) -> self.to_cardinal(value)` → default delegates through
//! `&self`, picking up the `to_cardinal` override below. There is no BC/AD
//! handling whatsoever: `to_year(-500)` == "ติดลบห้าร้อย" ("minus five
//! hundred"), verified against the interpreter.
//!
//! # Thai numeral system, as this module models it
//!
//! Thai groups by **six** digits (ล้าน = 10^6), not three, and stacks the
//! ล้าน marker for higher powers: 10^12 is "million million"
//! (หนึ่งล้านล้าน), 10^18 is "million million million". `split_six` chops the
//! digit string into 6-digit groups, and `add_text_million` rejoins them with
//! a literal "ล้าน" between each. This scales indefinitely, which is why the
//! absent overflow check never actually bites.
//!
//! Two morphological rules live in `splitnum`:
//! * **เอ็ด** — a trailing 1 in a multi-digit group is "et", not "one"
//! (11 → สิบเอ็ด, 21 → ยี่สิบเอ็ด, 101 → หนึ่งร้อยเอ็ด).
//! * **ยี่** — 2 in the tens place is "yi", not "song" (20 → ยี่สิบ).
//! * A bare 1 in the tens place gets no unit word at all (10 → สิบ, not
//! "หนึ่งสิบ"), via the `index != 1 or num != 1` guard.
//!
//! # Faithfully reproduced Python quirks
//!
//! This is a port, not a rewrite. All of the following are verified against
//! the interpreter and preserved verbatim:
//!
//! 1. **`cards` is garbage and `MAXVAL` is 9000.** TH's `low_numwords` is
//! listed *ascending* (ศูนย์=0 … เก้า=9), but `Num2Word_Base.set_low_numwords`
//! assumes *descending* and zips against `range(len-1, -1, -1)`. The
//! resulting table maps `cards[9] = "ศูนย์"` ("zero"), `cards[8] = "หนึ่ง"`
//! ("one"), … `cards[0] = "เก้า"` ("nine") — every entry inverted. `MAXVAL`
//! is then `1000 * 9 == 9000`. None of this is ever read, because
//! `to_cardinal` is overridden and never calls `splitnum`(base)/`clean`/
//! `merge`. Reproduced here only by *omission*: we leave `cards`/`maxval`
//! at their trait defaults rather than modelling a table nothing consults.
//! The consequence that *is* observable: TH never raises `OverflowError`,
//! at any magnitude (corpus goes to 10^21 and returns fine).
//!
//! 2. **Three hardcoded string special-cases in `left_num_to_text`**, compared
//! against the *decimal string*, not the value:
//! * `"100"` → "ร้อย" (the pipeline would emit "หนึ่งร้อย")
//! * `"100000000"` → "ร้อยล้าน" (pipeline: "หนึ่งร้อยล้าน")
//! * `"1000000000"` → "พันล้าน" (pipeline: "หนึ่งพันล้าน")
//! These drop the leading "หนึ่ง" ("one") for exactly three magnitudes and
//! nothing else, which is inconsistent: 1000 stays "หนึ่งพัน", 10^6 stays
//! "หนึ่งล้าน", 10^12 stays "หนึ่งล้านล้าน". Colloquial Thai does drop the
//! "one" here, so it is arguably a partial fix rather than a bug — but it
//! is applied to a string, so it fires *after* the minus sign is stripped:
//! `to_cardinal(-100)` == "ติดลบร้อย" (corpus-confirmed), and it fires only
//! on the whole number, never on an internal group (1000100 is unaffected).
//! Modelled by [`left_num_to_text`].
//!
//! 3. **`length` is a `bool` compared against `0`.** `splitnum` does
//! `length = len(six_num) > 1` and later tests `length == 0`, which in
//! Python is `False == 0` → `True`. It means "this group is a single
//! digit". Ported as `!length`.
//!
//! 4. **`word_num += "เอ็ด"` appends where every sibling branch prepends.**
//! Harmless in practice: the branch is guarded by `index == 0`, and at
//! index 0 the `if index:` prefix never ran, so `word_num` is always empty
//! there. Ported literally regardless.
//!
//! 5. **Zero-suppression asymmetry.** A group of all zeros yields `""`, and
//! `add_text_million` still welds "ล้าน" onto it — which is exactly how
//! 10^6 becomes "หนึ่งล้าน" (`"หนึ่ง" + "ล้าน" + ""`). Only a *single-digit*
//! group of "0" produces the word ศูนย์, so the string "0" is the sole way
//! to ever see it in an integer result.
//!
//! # The currency surface
//!
//! `Num2Word_TH` overrides `to_currency` **wholesale** and shares almost
//! nothing with `Num2Word_Base`'s version — no `pluralize`, no `has_decimal`
//! guard, no `CURRENCY_PRECISION`, no adjective support, and its own
//! `NotImplementedError` wording. See [`LangTh::to_currency`] for the
//! divergences, each of which is corpus- or interpreter-confirmed.
//!
//! `to_cheque` is **not** overridden: TH inherits `Num2Word_Base.to_cheque`
//! verbatim, so the trait default (`currency::default_to_cheque`) is left in
//! place and only `lang_name` + `currency_forms` are supplied for it to read.
//! That inheritance is why the two error messages differ — see
//! [`LangTh::to_currency`].
//!
//! `round_2_decimal` is **dead library code**: `lang_TH.py` defines it, but no
//! conversion path calls it (`to_currency` inlines its own rounding via
//! `parse_currency_parts`). Only `tests/lang/test_th.py` pokes it directly, and
//! it is unreachable from every `to_*` entry point the dispatcher exposes, so
//! it is deliberately not ported.
//!
//! # The float / Decimal cardinal path
//!
//! `Num2Word_TH.to_cardinal` is a single method covering *both* integer and
//! non-integer input. The integer half (above) leaves `post == "0"` and skips
//! the "จุด" branch — provably dead for `int` input, since `float2tuple` on an
//! `int` returns `post == 0` with `self.precision = 0`. Float and `Decimal`
//! input open that branch instead.
//!
//! The trait splits the one Python method in two: [`to_cardinal`] takes the
//! `BigInt` path, and the trait hook [`LangTh::to_cardinal_float`] takes the
//! `FloatValue` path. Both run the same `left_num_to_text` pipeline and the
//! same "จุด" + per-digit `low_numwords` rendering, shared through
//! [`cardinal_float_from_value`]. TH never delegates to
//! `Num2Word_Base.to_cardinal_float`, which is why the byte shape differs from
//! the base's — no spaces, no titled pointword ("หนึ่งจุดหนึ่ง", not
//! "หนึ่ง จุด หนึ่ง") — and why the trait's `cardinal_from_decimal` default is
//! left unused.
//!
//! The *currency* fractional-cents branch reaches the same rendering by a
//! separate door — Python's `self.to_cardinal(float(right))` — ported as the
//! f64-only [`to_cardinal_float`] free helper that [`LangTh::to_currency`]
//! calls. It is kept distinct from the `FloatValue` core only because the
//! subunits it sees are always in `[0, 100)`, so it can safely recompute
//! precision from `repr` (see [`cardinal_float_from_value`] for why the general
//! path cannot).
//!
//! # Cross-call mutable state
//!
//! `float2tuple` writes `self.precision` (to 0 for integer input) and
//! `to_cardinal` reads it back on the next line. It is a genuine
//! write-then-read handshake on instance state, but it is confined to a single
//! call and, for integer input, the value it computes is never used — `post`
//! is `"0"` and the padding branch `len(post) < precision` is `1 < 0`, i.e.
//! false. No state survives a call, so the stateless Rust path is safe here.
use crate;
use crate;
use crate;
use BigDecimal;
use BigInt;
use ;
use HashMap;
/// `self.negword`, set in `setup`. Used as a bare prefix with **no space** —
/// TH concatenates it directly (`"ติดลบ" + result`) rather than going through
/// the base's `"%s " % negword.strip()` spacing.
///
/// `to_currency` is the one exception: it builds `self.negword + " "`, so the
/// currency path *does* get a separating space. See [`LangTh::to_currency`].
const NEGWORD: &str = "ติดลบ";
/// `self.pointword`, set in `setup`. Concatenated with no surrounding spaces,
/// unlike the base's `" ".join` float path.
const POINTWORD: &str = "จุด";
/// `self.mid_numwords`. Indexed by digit position within a 6-digit group, so
/// only 0..=5 are ever reachable from `splitnum` (a group is at most 6 chars).
/// Index 6 ("ล้าน") is dead here — `add_text_million` hardcodes that literal
/// instead of reading this table.
const MID_NUMWORDS: = ;
/// `self.low_numwords`, ascending 0..=9 (note: the base class expects this
/// *descending* — see bug 1 in the module docs).
const LOW_NUMWORDS: = ;
/// "et" — replaces "one" in the units slot of a multi-digit group.
const ET: &str = "เอ็ด";
/// "yi" — replaces "song" ("two") in the tens slot.
const YI: &str = "ยี่";
/// "million" (10^6), the group separator stacked by [`add_text_million`].
const MILLION: &str = "ล้าน";
/// `self.CURRENCY_FORMS`, assigned in `setup()`.
///
/// TH **rebinds** the attribute (`self.CURRENCY_FORMS = {...}`) rather than
/// mutating it, and subclasses `Num2Word_Base` directly rather than
/// `Num2Word_EUR`. Both facts matter: the shared-class-dict mutation that
/// `Num2Word_EN.__init__` performs on `Num2Word_EUR.CURRENCY_FORMS` — the trap
/// that hands Swedish and Hungarian English plurals — cannot reach TH. Three
/// codes is genuinely all there is; verified against the live interpreter.
///
/// Both forms of every pair are identical because Thai does not inflect for
/// number. The arity is kept at 2 anyway to match the Python tuples: TH's
/// `to_currency` only ever reads index `[0]`, but `Num2Word_Base.to_cheque` —
/// which TH inherits — reads `cr1[-1]`, so a 1-tuple would not be equivalent.
/// Port of `utils.splitbyx(n, x, format_int=False)`.
///
/// Yields left-to-right chunks of `x` chars, with any short remainder first.
/// `format_int=False` means chunks stay strings (leading zeros preserved),
/// which the rest of the pipeline depends on — `splitnum` reads digit
/// *positions* out of them.
///
/// Python's `range(start, length, x)` with `start = length % x` guarantees
/// every subsequent chunk is exactly `x` long, so no bounds check is needed.
/// Port of `Num2Word_TH.split_six`.
///
/// Splits into 6-digit groups, reverses the *group order* (least-significant
/// group first), then reverses the *digits within* each group (least-
/// significant digit first). The double reversal is what lets `splitnum`
/// use the loop index directly as a power-of-ten position.
/// Port of `Num2Word_TH.splitnum`.
///
/// `six_num` is a digit group **already reversed** by [`split_six`], so
/// `index` is the power of ten and `MID_NUMWORDS[index]` is its unit word.
///
/// The branch order is load-bearing and reproduced exactly:
/// 1. `length && num == 1 && index == 0` → เอ็ด (trailing "one" → "et")
/// 2. `index == 1 && num == 2` → ยี่ prefix ("twenty", not "two-ten")
/// 3. `index != 1 || num != 1` → the ordinary digit word
///
/// Falling through all three (i.e. `index == 1 && num == 1`) deliberately
/// emits *no* digit word, leaving a bare "สิบ" — that is how 10 → "สิบ".
/// Port of `Num2Word_TH.add_text_million`.
///
/// Welds the per-group words back together with "ล้าน" between each, walking
/// the (already least-significant-first) list in reverse so the output reads
/// most-significant-first. An empty group contributes an empty string but
/// still gets its separator — that is how 10^6 → "หนึ่ง" + "ล้าน" + "".
/// Port of `Num2Word_TH.left_num_to_text`.
///
/// `number` is the *decimal string* of the (already sign-stripped) integer.
/// The three special cases compare against that string — see bug 2.
/// Port of `Num2Word_TH.to_cardinal`, integer path only.
///
/// Python does:
/// ```text
/// negative = number < 0
/// pre, post = self.float2tuple(number) # int input -> (number, 0), precision := 0
/// pre = "{}".format(pre) # e.g. "-100"
/// post = "{}".format(post) # always "0" for ints
/// if negative: pre = pre.lstrip("-") # -> "100"
/// result = self.left_num_to_text(pre)
/// if not post == "0": ... # dead for ints
/// if negative: result = "ติดลบ" + result
/// ```
/// So for integers this reduces to: stringify, strip the sign, run the
/// pipeline, re-attach the sign word with **no separating space**.
/// `abs(Decimal(str(f)).as_tuple().exponent)` — the `self.precision` that
/// `float2tuple` assigns as a side effect and `to_cardinal` reads back.
///
/// Mirrors `floatpath::float_repr_precision`, which is private to that module.
/// Rust's `{}` for f64 is shortest-round-trip, the same contract as Python's
/// `repr`, so counting digits after the point agrees. The two disagree only in
/// exponent form (`str(1e21)` is "1e+21" → precision 21, while Rust prints all
/// 22 digits → precision 0), which is unreachable here: this is only ever fed
/// subunits in `[0, 100)`.
/// Port of `Num2Word_TH.to_cardinal`, **float path** — the other half of the
/// method [`to_cardinal`] ports.
///
/// Reached only from the fractional-cents branch of [`LangTh::to_currency`],
/// as Python's `self.to_cardinal(float(right))`. That dispatches to *TH's* own
/// `to_cardinal`, which handles floats itself and never delegates to
/// `Num2Word_Base.to_cardinal_float`. The trait's `cardinal_from_decimal` hook
/// is therefore unusable here: its default routes through the base's float
/// path, which joins words with spaces and titles the pointword —
/// "หนึ่ง จุด หนึ่ง" where TH says "หนึ่งจุดหนึ่ง". So this is a local helper
/// and `cardinal_from_decimal` is left at its (for TH, dead) default.
///
/// Python:
/// ```text
/// negative = number < 0
/// pre, post = self.float2tuple(number) # sets self.precision
/// precision = self.precision
/// pre = "{}".format(pre); post = "{}".format(post)
/// if negative: pre = pre.lstrip("-")
/// if len(post) < precision: post = "0" * (precision - len(post)) + post
/// result = self.left_num_to_text(pre)
/// if not post == "0":
/// result += "จุด" + "".join(self.low_numwords[i] for i in map(int, post))
/// if negative: result = "ติดลบ" + result
/// ```
/// Note this is *not* the base's loop: TH iterates every char of `post` rather
/// than `range(self.precision)`, and pads on the left instead of the right.
///
/// The `negative` arm is dead on the only live call path —
/// `parse_currency_parts` returns `right` already absolute — but is ported
/// anyway so the helper is a faithful whole.
/// Port of `Num2Word_TH.to_cardinal`, **float/Decimal path**, driving the trait
/// hook [`LangTh::to_cardinal_float`].
///
/// This is the same Python method [`to_cardinal`] ports, taken down its
/// non-integer branch — the branch the f64-only [`to_cardinal_float`] above
/// ports for the currency caller. The two differ only in where `precision`
/// comes from:
/// * the currency helper recomputes it from `repr(f64)`, safe because it is
/// only ever handed subunits in `[0, 100)`;
/// * here it is read from the [`FloatValue`] — `abs(exponent)` for the
/// Decimal arm, the Python-`repr`-derived digit count for the Float arm.
/// That source is load-bearing: a plain f64 like `1e21` prints with no `.`
/// (Rust `repr` precision 0), yet Python's `str(1e21)` is `"1e+21"` →
/// precision 21, so `num2words(1e21, lang="th")` renders the integer then
/// "จุด" and twenty-one ศูนย์. Only the carried precision reproduces that.
///
/// Python (`number` is a `float` or a `Decimal`):
/// ```text
/// negative = number < 0
/// pre, post = self.float2tuple(number) # sets self.precision from the value
/// precision = self.precision
/// pre = "{}".format(pre); post = "{}".format(post)
/// if negative: pre = pre.lstrip("-")
/// if len(post) < precision: post = "0" * (precision - len(post)) + post
/// result = self.left_num_to_text(pre)
/// if not post == "0":
/// result += "จุด" + "".join(self.low_numwords[i] for i in map(int, post))
/// if negative: result = "ติดลบ" + result
/// ```
/// Unlike the base's float path this iterates *every* char of `post` (not
/// `range(precision)`) and left-pads instead of right-truncating; here the two
/// coincide because `post` is exactly `precision` digits after padding.