File size: 10,848 Bytes
9b1aef8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/**
 * Text preprocessor — converts numbers, currencies, ordinals, etc. to words.
 * Port of KittenTTS preprocess.py.
 * https://github.com/KittenML/KittenTTS
 */

// ── Number → Words ──

const ONES = [
  "", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine",
  "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen",
  "seventeen", "eighteen", "nineteen",
];
const TENS = ["", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"];
const SCALE = ["", "thousand", "million", "billion", "trillion"];

const ORDINAL_EXCEPTIONS: Record<string, string> = {
  one: "first", two: "second", three: "third", four: "fourth",
  five: "fifth", six: "sixth", seven: "seventh", eight: "eighth",
  nine: "ninth", twelve: "twelfth",
};

const CURRENCY_SYMBOLS: Record<string, string> = {
  "$": "dollar", "€": "euro", "£": "pound", "¥": "yen",
  "₹": "rupee", "₩": "won", "₿": "bitcoin",
};

function threeDigitsToWords(n: number): string {
  if (n === 0) return "";
  const parts: string[] = [];
  const hundreds = Math.floor(n / 100);
  const remainder = n % 100;
  if (hundreds) parts.push(`${ONES[hundreds]} hundred`);
  if (remainder < 20) {
    if (remainder) parts.push(ONES[remainder]);
  } else {
    const tensWord = TENS[Math.floor(remainder / 10)];
    const onesWord = ONES[remainder % 10];
    parts.push(onesWord ? `${tensWord}-${onesWord}` : tensWord);
  }
  return parts.join(" ");
}

export function numberToWords(n: number): string {
  if (!Number.isInteger(n)) n = Math.floor(n);
  if (n === 0) return "zero";
  if (n < 0) return `negative ${numberToWords(-n)}`;
  if (n >= 100 && n <= 9999 && n % 100 === 0 && n % 1000 !== 0) {
    const hundreds = Math.floor(n / 100);
    if (hundreds < 20) return `${ONES[hundreds]} hundred`;
  }
  const parts: string[] = [];
  let remaining = n;
  for (let i = 0; i < SCALE.length; i++) {
    const chunk = remaining % 1000;
    if (chunk) {
      const w = threeDigitsToWords(chunk);
      parts.push(SCALE[i] ? `${w} ${SCALE[i]}` : w);
    }
    remaining = Math.floor(remaining / 1000);
    if (remaining === 0) break;
  }
  return parts.reverse().join(" ");
}

export function floatToWords(value: string | number, sep = "point"): string {
  const text = typeof value === "string" ? value : `${value}`;
  const negative = text.startsWith("-");
  const clean = negative ? text.slice(1) : text;
  let result: string;
  if (clean.includes(".")) {
    const [intPart, decPart] = clean.split(".");
    const intWords = intPart ? numberToWords(parseInt(intPart, 10)) : "zero";
    const digitMap = ["zero", ...ONES.slice(1)];
    const decWords = [...decPart].map((d) => digitMap[parseInt(d, 10)]).join(" ");
    result = `${intWords} ${sep} ${decWords}`;
  } else {
    result = numberToWords(parseInt(clean, 10));
  }
  return negative ? `negative ${result}` : result;
}

function ordinalSuffix(n: number): string {
  const word = numberToWords(n);
  let prefix: string, last: string, joiner: string;
  if (word.includes("-")) {
    const idx = word.lastIndexOf("-");
    prefix = word.slice(0, idx);
    last = word.slice(idx + 1);
    joiner = "-";
  } else {
    const parts = word.split(" ");
    if (parts.length >= 2) {
      last = parts.pop()!;
      prefix = parts.join(" ");
      joiner = " ";
    } else {
      last = word;
      prefix = "";
      joiner = "";
    }
  }
  let lastOrd: string;
  if (ORDINAL_EXCEPTIONS[last]) {
    lastOrd = ORDINAL_EXCEPTIONS[last];
  } else if (last.endsWith("t")) {
    lastOrd = last + "h";
  } else if (last.endsWith("e")) {
    lastOrd = last.slice(0, -1) + "th";
  } else {
    lastOrd = last + "th";
  }
  return prefix ? `${prefix}${joiner}${lastOrd}` : lastOrd;
}

// ── Regex patterns ──

const RE_NUMBER = /(?<![a-zA-Z])-?[\d,]+(?:\.\d+)?/g;
const RE_ORDINAL = /\b(\d+)(st|nd|rd|th)\b/gi;
const RE_PERCENT = /(-?[\d,]+(?:\.\d+)?)\s*%/g;
const RE_CURRENCY = /([$€£¥₹₩₿])\s*([\d,]+(?:\.\d+)?)\s*([KMBT])?(?![a-zA-Z\d])/g;
const RE_TIME = /\b(\d{1,2}):(\d{2})(?::(\d{2}))?\s*(am|pm)?\b/gi;
const RE_RANGE = /(?<!\w)(\d+)-(\d+)(?!\w)/g;
const RE_MODEL_VER = /\b([a-zA-Z][a-zA-Z0-9]*)-(\d[\d.]*)(?=[^\d.]|$)/g;
const RE_UNIT = /(\d+(?:\.\d+)?)\s*(km|kg|mg|ml|gb|mb|kb|tb|hz|khz|mhz|ghz|mph|kph|°[cCfF]|[cCfF]°|ms|ns|µs)\b/gi;
const RE_SCALE = /(?<![a-zA-Z])(\d+(?:\.\d+)?)\s*([KMBT])(?![a-zA-Z\d])/g;
const RE_SCI = /(?<![a-zA-Z\d])(-?\d+(?:\.\d+)?)[eE]([+-]?\d+)(?![a-zA-Z\d])/g;
const RE_FRACTION = /\b(\d+)\s*\/\s*(\d+)\b/g;
const RE_DECADE = /\b(\d{1,3})0s\b/g;

const UNIT_MAP: Record<string, string> = {
  km: "kilometers", kg: "kilograms", mg: "milligrams", ml: "milliliters",
  gb: "gigabytes", mb: "megabytes", kb: "kilobytes", tb: "terabytes",
  hz: "hertz", khz: "kilohertz", mhz: "megahertz", ghz: "gigahertz",
  mph: "miles per hour", kph: "kilometers per hour",
  ms: "milliseconds", ns: "nanoseconds", "µs": "microseconds",
  "°c": "degrees Celsius", "c°": "degrees Celsius",
  "°f": "degrees Fahrenheit", "f°": "degrees Fahrenheit",
};

const SCALE_MAP: Record<string, string> = {
  K: "thousand", M: "million", B: "billion", T: "trillion",
};

const DECADE_MAP: Record<number, string> = {
  0: "hundreds", 1: "tens", 2: "twenties", 3: "thirties", 4: "forties",
  5: "fifties", 6: "sixties", 7: "seventies", 8: "eighties", 9: "nineties",
};

// ── Expansion functions ──

function expandOrdinals(text: string): string {
  return text.replace(RE_ORDINAL, (_, n) => ordinalSuffix(parseInt(n, 10)));
}

function expandPercentages(text: string): string {
  return text.replace(RE_PERCENT, (_, raw) => {
    const clean = raw.replace(/,/g, "");
    const w = clean.includes(".") ? floatToWords(parseFloat(clean)) : numberToWords(parseInt(clean, 10));
    return `${w} percent`;
  });
}

function expandCurrency(text: string): string {
  return text.replace(RE_CURRENCY, (_, symbol, raw, scaleSuffix) => {
    const clean = raw.replace(/,/g, "");
    const unit = CURRENCY_SYMBOLS[symbol] || "";
    if (scaleSuffix) {
      const scaleWord = SCALE_MAP[scaleSuffix];
      const num = clean.includes(".") ? floatToWords(clean) : numberToWords(parseInt(clean, 10));
      return `${num} ${scaleWord} ${unit}s`.trim();
    }
    if (clean.includes(".")) {
      const [intPart, decPart] = clean.split(".");
      const decVal = parseInt(decPart.slice(0, 2).padEnd(2, "0"), 10);
      let result = `${numberToWords(parseInt(intPart, 10))} ${unit}s`;
      if (decVal) result += ` and ${numberToWords(decVal)} cent${decVal !== 1 ? "s" : ""}`;
      return result;
    }
    const val = parseInt(clean, 10);
    return `${numberToWords(val)} ${unit}${val !== 1 && unit ? "s" : ""}`;
  });
}

function expandTime(text: string): string {
  return text.replace(RE_TIME, (_, h, m, _s, suffix) => {
    const hour = parseInt(h, 10);
    const mins = parseInt(m, 10);
    const sfx = suffix ? ` ${suffix.toLowerCase()}` : "";
    const hWords = numberToWords(hour);
    if (mins === 0) return suffix ? `${hWords}${sfx}` : `${hWords} hundred${sfx}`;
    if (mins < 10) return `${hWords} oh ${numberToWords(mins)}${sfx}`;
    return `${hWords} ${numberToWords(mins)}${sfx}`;
  });
}

function expandRanges(text: string): string {
  return text.replace(RE_RANGE, (_, lo, hi) =>
    `${numberToWords(parseInt(lo, 10))} to ${numberToWords(parseInt(hi, 10))}`
  );
}

function expandModelNames(text: string): string {
  return text.replace(RE_MODEL_VER, (_, name, ver) => `${name} ${ver}`);
}

function expandUnits(text: string): string {
  return text.replace(RE_UNIT, (_, raw, unit) => {
    const expanded = UNIT_MAP[unit.toLowerCase()] || unit;
    const num = raw.includes(".") ? floatToWords(parseFloat(raw)) : numberToWords(parseInt(raw, 10));
    return `${num} ${expanded}`;
  });
}

function expandScaleSuffixes(text: string): string {
  return text.replace(RE_SCALE, (_, raw, suffix) => {
    const scaleWord = SCALE_MAP[suffix] || suffix;
    const num = raw.includes(".") ? floatToWords(raw) : numberToWords(parseInt(raw, 10));
    return `${num} ${scaleWord}`;
  });
}

function expandScientific(text: string): string {
  return text.replace(RE_SCI, (_, coeff, exp) => {
    const coeffW = coeff.includes(".") ? floatToWords(coeff) : numberToWords(parseInt(coeff, 10));
    const expVal = parseInt(exp, 10);
    const sign = expVal < 0 ? "negative " : "";
    return `${coeffW} times ten to the ${sign}${numberToWords(Math.abs(expVal))}`;
  });
}

function expandFractions(text: string): string {
  return text.replace(RE_FRACTION, (m, num, den) => {
    const n = parseInt(num, 10);
    const d = parseInt(den, 10);
    if (d === 0) return m;
    const nWords = numberToWords(n);
    let dWord: string;
    if (d === 2) dWord = n === 1 ? "half" : "halves";
    else if (d === 4) dWord = n === 1 ? "quarter" : "quarters";
    else {
      dWord = ordinalSuffix(d);
      if (n !== 1) dWord += "s";
    }
    return `${nWords} ${dWord}`;
  });
}

function expandDecades(text: string): string {
  return text.replace(RE_DECADE, (_, base) => {
    const b = parseInt(base, 10);
    const decadeDigit = b % 10;
    const decadeWord = DECADE_MAP[decadeDigit] || "";
    if (b < 10) return decadeWord;
    return `${numberToWords(Math.floor(b / 10))} ${decadeWord}`;
  });
}

function replaceNumbers(text: string): string {
  return text.replace(RE_NUMBER, (m) => {
    const clean = m.replace(/,/g, "");
    if (clean.includes(".")) return floatToWords(clean);
    return numberToWords(parseInt(clean, 10));
  });
}

function normalizeLeadingDecimals(text: string): string {
  text = text.replace(/(?<!\d)(-)\.([\d])/g, "$1" + "0.$2");
  text = text.replace(/(?<!\d)\.([\d])/g, "0.$1");
  return text;
}

const RE_URL = /https?:\/\/\S+|www\.\S+/g;
const RE_EMAIL = /\b[\w.+-]+@[\w-]+\.[a-z]{2,}\b/gi;
const RE_HTML = /<[^>]+>/g;
const RE_PUNCT = /[^\w\s.,?!;:\-\u2014\u2013\u2026]/g;
const RE_SPACES = /\s+/g;

export function preprocessText(text: string): string {
  // Remove URLs, emails, HTML
  text = text.replace(RE_URL, "");
  text = text.replace(RE_EMAIL, "");
  text = text.replace(RE_HTML, " ");

  // Normalize leading decimals
  text = normalizeLeadingDecimals(text);

  // Expand special forms before generic number replacement
  text = expandCurrency(text);
  text = expandPercentages(text);
  text = expandScientific(text);
  text = expandTime(text);
  text = expandOrdinals(text);
  text = expandUnits(text);
  text = expandScaleSuffixes(text);
  text = expandFractions(text);
  text = expandDecades(text);
  text = expandRanges(text);
  text = expandModelNames(text);
  text = replaceNumbers(text);

  // Remove non-prosodic punctuation
  text = text.replace(RE_PUNCT, " ");

  // Lowercase and collapse whitespace
  text = text.toLowerCase();
  text = text.replace(RE_SPACES, " ").trim();

  return text;
}