|
| 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 | +import pynini |
| 16 | +from pynini import closure, cross, string_file |
| 17 | +from pynini.lib.pynutil import add_weight, delete, insert |
| 18 | + |
| 19 | +from itn.english.rules.cardinal import Cardinal |
| 20 | +from itn.english.rules.ordinal import Ordinal |
| 21 | +from tn.processor import Processor |
| 22 | +from tn.utils import get_abs_path |
| 23 | + |
| 24 | + |
| 25 | +class Date(Processor): |
| 26 | + |
| 27 | + def __init__(self, cardinal=None, ordinal=None): |
| 28 | + super().__init__(name="date", ordertype="itn") |
| 29 | + self.cardinal = cardinal or Cardinal() |
| 30 | + self.ordinal = ordinal or Ordinal(cardinal=self.cardinal) |
| 31 | + self.build_tagger() |
| 32 | + self.build_verbalizer() |
| 33 | + |
| 34 | + def build_tagger(self): |
| 35 | + ds = delete(" ") |
| 36 | + month_names = string_file(get_abs_path("../itn/english/data/months.tsv")) |
| 37 | + month = insert('month: "') + month_names + insert('"') |
| 38 | + |
| 39 | + # Day: accept ordinal words ("fifth", "twenty first") or cardinal |
| 40 | + # words ("thirty") -- both resolve to a number via the cardinal graph. |
| 41 | + # Restrict to 1-31 range via composition with DIGIT{1,2}. |
| 42 | + day_graph = self.ordinal.graph | self.cardinal.graph |
| 43 | + day_graph = pynini.compose(day_graph, self.DIGIT + closure(self.DIGIT, 0, 1)) |
| 44 | + day = insert('day: "') + day_graph + insert('"') |
| 45 | + |
| 46 | + # Year graph: handles common spoken year forms |
| 47 | + digit = string_file(get_abs_path("../itn/english/data/numbers/digit.tsv")) |
| 48 | + teen = string_file(get_abs_path("../itn/english/data/numbers/teen.tsv")) |
| 49 | + ties = string_file(get_abs_path("../itn/english/data/numbers/ties.tsv")) |
| 50 | + |
| 51 | + # Two-digit: 10-99 |
| 52 | + two_digit = teen | (ties + (ds + digit | insert("0"))) |
| 53 | + |
| 54 | + # "oh five" / "o five" => 05 |
| 55 | + oh_digit = (cross("oh", "0") | cross("o", "0")) + ds + digit |
| 56 | + |
| 57 | + # Year as two groups of two digits: "twenty twelve" => 2012 |
| 58 | + year_two_parts = (teen | two_digit) + ds + (two_digit | oh_digit | teen) |
| 59 | + |
| 60 | + # Year as "X thousand Y": "two thousand twelve" => 2012 |
| 61 | + # Need zero-padded variants so "two thousand three" => 2003 |
| 62 | + hundreds = digit + ds + delete("hundred") + (ds + two_digit | ds + insert("0") + digit | insert("00")) |
| 63 | + up_to_999_padded = hundreds | insert("0") + two_digit | insert("00") + digit |
| 64 | + year_thousands = ( |
| 65 | + digit |
| 66 | + + ds |
| 67 | + + delete("thousand") |
| 68 | + + (ds + up_to_999_padded | insert("000")) |
| 69 | + ) |
| 70 | + |
| 71 | + # Year as hundreds: "nineteen oh five" => 1905 |
| 72 | + year_hundreds = (teen | two_digit) + ds + oh_digit |
| 73 | + |
| 74 | + year_graph = year_two_parts | year_thousands | year_hundreds |
| 75 | + |
| 76 | + # Delete optional "and" within year |
| 77 | + delete_and = self.build_rule(delete("and "), " ", self.ALPHA) |
| 78 | + year_graph = (delete_and @ year_graph).optimize() |
| 79 | + |
| 80 | + year = insert('year: "') + year_graph + insert('"') |
| 81 | + |
| 82 | + # Marker to preserve field order through TokenParser |
| 83 | + po = insert(' preserve_order: "true"') |
| 84 | + |
| 85 | + # Format: month day year => "july twenty fifth two thousand twelve" |
| 86 | + graph_mdy = month + ds + insert(" ") + day + ds + insert(" ") + year + po |
| 87 | + # Format: month day (no year) => "january first" |
| 88 | + graph_md = month + ds + insert(" ") + day + po |
| 89 | + # Format: month year (no day) => "july two thousand twelve" |
| 90 | + graph_my = month + ds + insert(" ") + add_weight(year, -0.1) + po |
| 91 | + # Format: "the day of month year" => "the twenty fifth of july twenty twelve" |
| 92 | + graph_dmy = ( |
| 93 | + delete("the") |
| 94 | + + ds |
| 95 | + + day |
| 96 | + + ds |
| 97 | + + delete("of") |
| 98 | + + ds |
| 99 | + + insert(" ") |
| 100 | + + month |
| 101 | + + ds |
| 102 | + + insert(" ") |
| 103 | + + year |
| 104 | + + po |
| 105 | + ) |
| 106 | + # Format: "the day of month" (no year) => "the fifteenth of january" |
| 107 | + graph_dm = ( |
| 108 | + delete("the") |
| 109 | + + ds |
| 110 | + + day |
| 111 | + + ds |
| 112 | + + delete("of") |
| 113 | + + ds |
| 114 | + + insert(" ") |
| 115 | + + month |
| 116 | + + po |
| 117 | + ) |
| 118 | + # Year only => "twenty twelve", "two thousand three" |
| 119 | + graph_y = add_weight(year, 0.01) + po |
| 120 | + |
| 121 | + final_graph = graph_mdy | graph_md | graph_my | graph_dmy | graph_dm | graph_y |
| 122 | + self.tagger = self.add_tokens(final_graph) |
| 123 | + |
| 124 | + def build_verbalizer(self): |
| 125 | + month = ( |
| 126 | + delete("month:") |
| 127 | + + self.DELETE_SPACE |
| 128 | + + delete('"') |
| 129 | + + self.NOT_QUOTE.plus |
| 130 | + + delete('"') |
| 131 | + ) |
| 132 | + day = ( |
| 133 | + delete("day:") |
| 134 | + + self.DELETE_SPACE |
| 135 | + + delete('"') |
| 136 | + + self.NOT_QUOTE.plus |
| 137 | + + delete('"') |
| 138 | + ) |
| 139 | + year = ( |
| 140 | + delete("year:") |
| 141 | + + self.DELETE_SPACE |
| 142 | + + delete('"') |
| 143 | + + self.NOT_QUOTE.plus |
| 144 | + + delete('"') |
| 145 | + ) |
| 146 | + delete_po = ( |
| 147 | + delete("preserve_order:") |
| 148 | + + self.DELETE_SPACE |
| 149 | + + delete('"') |
| 150 | + + delete("true") |
| 151 | + + delete('"') |
| 152 | + ) |
| 153 | + |
| 154 | + optional_day = closure(self.DELETE_SPACE + insert(" ") + day, 0, 1) |
| 155 | + optional_year = closure(self.DELETE_SPACE + insert(" ") + year, 0, 1) |
| 156 | + |
| 157 | + # month (day) (year) |
| 158 | + graph_mdy = month + optional_day + optional_year |
| 159 | + # day month (year) |
| 160 | + graph_dmy = day + self.DELETE_SPACE + insert(" ") + month + optional_year |
| 161 | + # year only |
| 162 | + graph_y = year |
| 163 | + |
| 164 | + graph = (graph_mdy | graph_dmy | graph_y) + self.DELETE_SPACE + delete_po |
| 165 | + self.verbalizer = self.delete_tokens(graph) |
0 commit comments