|
| 1 | +# Copyright (c) 2026 Zhendong Peng (pzd17@tsinghua.org.cn) |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +from pynini import closure, cross, string_file, union |
| 16 | +from pynini.lib.pynutil import delete, insert |
| 17 | + |
| 18 | +from tn.processor import Processor |
| 19 | +from tn.utils import get_abs_path |
| 20 | + |
| 21 | + |
| 22 | +class Cardinal(Processor): |
| 23 | + |
| 24 | + def __init__(self): |
| 25 | + super().__init__(name="cardinal", ordertype="itn") |
| 26 | + self.build_tagger() |
| 27 | + self.build_verbalizer() |
| 28 | + |
| 29 | + def build_tagger(self): |
| 30 | + zero = string_file(get_abs_path("../itn/english/data/numbers/zero.tsv")) |
| 31 | + digit = string_file(get_abs_path("../itn/english/data/numbers/digit.tsv")) |
| 32 | + teen = string_file(get_abs_path("../itn/english/data/numbers/teen.tsv")) |
| 33 | + ties = string_file(get_abs_path("../itn/english/data/numbers/ties.tsv")) |
| 34 | + ds = delete(" ") |
| 35 | + |
| 36 | + # 1~9, 10~19, 20~99 |
| 37 | + one_digit = digit |
| 38 | + two_digit = teen | (ties + (ds + digit | insert("0"))) |
| 39 | + up_to_99 = one_digit | two_digit |
| 40 | + |
| 41 | + # one hundred, one hundred twenty three, one hundred one |
| 42 | + hundreds = digit + ds + delete("hundred") + (ds + two_digit | ds + insert("0") + one_digit | insert("00")) |
| 43 | + # eleven hundred => 1100, twenty one hundred => 2100 |
| 44 | + hundreds_as_thousand = (teen | ties + ds + digit) + ds + delete("hundred") + ( |
| 45 | + ds + two_digit | ds + insert("0") + one_digit | insert("00") |
| 46 | + ) |
| 47 | + |
| 48 | + # 1~999 |
| 49 | + up_to_999 = up_to_99 | hundreds |
| 50 | + # 1~999 with zero-padding to 3 digits |
| 51 | + up_to_999_padded = hundreds | insert("0") + two_digit | insert("00") + one_digit |
| 52 | + |
| 53 | + def _with_mag(name): |
| 54 | + return up_to_999 + ds + delete(name) |
| 55 | + |
| 56 | + def _with_mag_padded(name): |
| 57 | + return up_to_999_padded + ds + delete(name) |
| 58 | + |
| 59 | + graph = zero | up_to_999 | hundreds_as_thousand |
| 60 | + graph |= _with_mag("thousand") + (ds + up_to_999_padded | insert("000")) |
| 61 | + graph |= ( |
| 62 | + _with_mag("million") |
| 63 | + + (ds + _with_mag_padded("thousand") | insert("000")) |
| 64 | + + (ds + up_to_999_padded | insert("000")) |
| 65 | + ) |
| 66 | + graph |= ( |
| 67 | + _with_mag("billion") |
| 68 | + + (ds + _with_mag_padded("million") | insert("000")) |
| 69 | + + (ds + _with_mag_padded("thousand") | insert("000")) |
| 70 | + + (ds + up_to_999_padded | insert("000")) |
| 71 | + ) |
| 72 | + graph |= ( |
| 73 | + _with_mag("trillion") |
| 74 | + + (ds + _with_mag_padded("billion") | insert("000")) |
| 75 | + + (ds + _with_mag_padded("million") | insert("000")) |
| 76 | + + (ds + _with_mag_padded("thousand") | insert("000")) |
| 77 | + + (ds + up_to_999_padded | insert("000")) |
| 78 | + ) |
| 79 | + graph |= ( |
| 80 | + _with_mag("quadrillion") |
| 81 | + + (ds + _with_mag_padded("trillion") | insert("000")) |
| 82 | + + (ds + _with_mag_padded("billion") | insert("000")) |
| 83 | + + (ds + _with_mag_padded("million") | insert("000")) |
| 84 | + + (ds + _with_mag_padded("thousand") | insert("000")) |
| 85 | + + (ds + up_to_999_padded | insert("000")) |
| 86 | + ) |
| 87 | + graph |= ( |
| 88 | + _with_mag("quintillion") |
| 89 | + + (ds + _with_mag_padded("quadrillion") | insert("000")) |
| 90 | + + (ds + _with_mag_padded("trillion") | insert("000")) |
| 91 | + + (ds + _with_mag_padded("billion") | insert("000")) |
| 92 | + + (ds + _with_mag_padded("million") | insert("000")) |
| 93 | + + (ds + _with_mag_padded("thousand") | insert("000")) |
| 94 | + + (ds + up_to_999_padded | insert("000")) |
| 95 | + ) |
| 96 | + graph |= ( |
| 97 | + _with_mag("sextillion") |
| 98 | + + (ds + _with_mag_padded("quintillion") | insert("000")) |
| 99 | + + (ds + _with_mag_padded("quadrillion") | insert("000")) |
| 100 | + + (ds + _with_mag_padded("trillion") | insert("000")) |
| 101 | + + (ds + _with_mag_padded("billion") | insert("000")) |
| 102 | + + (ds + _with_mag_padded("million") | insert("000")) |
| 103 | + + (ds + _with_mag_padded("thousand") | insert("000")) |
| 104 | + + (ds + up_to_999_padded | insert("000")) |
| 105 | + ) |
| 106 | + |
| 107 | + # strip leading zeros |
| 108 | + graph = graph @ union(delete(closure("0")) + (self.DIGIT - "0") + closure(self.DIGIT), "0") |
| 109 | + # delete optional "and" |
| 110 | + delete_and = self.build_rule(delete("and "), " ", self.ALPHA) |
| 111 | + graph = (delete_and @ graph).optimize() |
| 112 | + |
| 113 | + self.graph = graph |
| 114 | + |
| 115 | + minus = delete("minus") | delete("negative") |
| 116 | + optional_minus = closure(insert('negative: "-" ') + minus + ds, 0, 1) |
| 117 | + final_graph = optional_minus + insert('integer: "') + graph + insert('"') |
| 118 | + self.tagger = self.add_tokens(final_graph) |
| 119 | + |
| 120 | + def build_verbalizer(self): |
| 121 | + optional_sign = closure( |
| 122 | + delete('negative:') + self.DELETE_SPACE + delete('"') + self.NOT_QUOTE + delete('"') + self.DELETE_SPACE, |
| 123 | + 0, 1, |
| 124 | + ) |
| 125 | + integer = delete("integer:") + self.DELETE_SPACE + delete('"') + self.NOT_QUOTE.plus + delete('"') |
| 126 | + self.numbers = integer |
| 127 | + self.verbalizer = self.delete_tokens(optional_sign + integer) |
0 commit comments