uchcharan 0.2.2 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -29,7 +29,13 @@ normalize('आपका ₹2,500 का EMI 05/12/2026 को due है।');
29
29
  // → 'आपका दो हज़ार पाँच सौ रुपये का ई एम आई पाँच दिसंबर दो हज़ार छब्बीस को due है।'
30
30
 
31
31
  normalize('₹1.5 lakh पासून, दुकान 7:00 PM पर्यंत', { lang: 'mr' });
32
- // → 'दीड लाख रुपये पासून, दुकान संध्याकाळी सात वाजता पर्यंत'
32
+ // → 'दीड लाख रुपये पासून, दुकान संध्याकाळी सात वाजेपर्यंत'
33
+
34
+ normalize('Your order of Rs. 1,25,000 arrives on 25/11/2026 at 5:30 pm.', { lang: 'en' });
35
+ // → 'Your order of one lakh twenty five thousand rupees arrives on twenty fifth November twenty twenty six at five thirty P M.'
36
+
37
+ normalize('पैसे rahul@okicici पर भेजें, गाड़ी नंबर MH15AB1234', { lang: 'hi' });
38
+ // → 'पैसे rahul ऐट ओके आई सी आई सी आई पर भेजें, गाड़ी नंबर एम एच पंद्रह ए बी एक दो तीन चार'
33
39
 
34
40
  // See exactly what changed (useful for debugging voice agents)
35
41
  normalize('₹500 at 10:30', { explain: true });
@@ -47,12 +53,13 @@ echo "PNR 4521879630" | npx uchcharan --explain
47
53
 
48
54
  | option | default | meaning |
49
55
  |---|---|---|
50
- | `lang` | `'hi'` | `'hi'` Hindi or `'mr'` Marathi |
56
+ | `lang` | `'hi'` | `'hi'` Hindi, `'mr'` Marathi, `'en'` Indian English, or `'auto'` (mostly-Latin text → `en`; Marathi words such as आहे / आणि → `mr`; else `hi`) |
51
57
  | `spellAcronyms` | `true` | `GST` → `जी एस टी`. Known word-acronyms (`ISRO` → `इसरो`) are always spoken as words |
52
58
  | `digitRunMin` | `6` | digit strings this long (PNR, OTP, account, PIN) are read digit by digit |
59
+ | `vehicleDigits` | `'digits'` | last part of a number plate: `'digits'` (एक दो तीन चार) or `'pairs'` (बारह चौंतीस) |
53
60
  | `explain` | `false` | return `{ text, changes }` instead of a string |
54
61
 
55
- ## What it handles (v0.1)
62
+ ## What it handles
56
63
 
57
64
  | Input | Hindi | Marathi |
58
65
  |---|---|---|
@@ -69,10 +76,31 @@ echo "PNR 4521879630" | npx uchcharan --explain
69
76
  | `Dr.`, `Smt.`, `No. 5`, `etc.` | डॉक्टर, श्रीमती, नंबर पाँच, वगैरह | डॉक्टर, श्रीमती, नंबर पाच, वगैरे |
70
77
  | `GST`, `EMI`, `ISRO`, `AIIMS` | जी एस टी, ई एम आई, इसरो, एम्स | जी एस टी, ई एम आय, इस्रो, एम्स |
71
78
  | Devanagari digits `२५` | पच्चीस | पंचवीस |
79
+ | `संध्याकाळी 7:00 PM पर्यंत`, `शाम 7 PM` | शाम सात बजे (time-of-day word never repeated) | संध्याकाळी सात वाजेपर्यंत |
80
+ | `OTP 4821`, `Your verification code is 7392`, `4821 is your OTP` | digit by digit | digit by digit |
81
+ | UPI `rahul@okicici`, `rk@ptyes`, `9876543210@ybl` | rahul ऐट ओके आई सी आई सी आई | rahul ॲट ओके आय सी आय सी आय |
82
+ | `ट्रेन नंबर 12951`, `गाड़ी संख्या 12951`, `Train No. 12951` | एक दो नौ पाँच एक | एक दोन नऊ पाच एक |
83
+ | `MH15AB1234`, `MH-04-K-0042`, `22 BH 4567 AA` | एम एच पंद्रह ए बी एक दो तीन चार | एम एच पंधरा ए बी एक दोन तीन चार |
84
+ | `error code E4`, `seat A12` | ई चार, ए बारह | ई चार |
85
+
86
+ ### English mode (`lang: 'en'`)
87
+
88
+ Reads Indian numbers the way Indians say them in English, with no Hindi words mixed in:
89
+ `₹1,25,000` → one lakh twenty five thousand rupees, `₹2.5 lakh` → two and a half lakh rupees, `₹2,500.50` → two thousand five hundred rupees and fifty paise,
90
+ `25/11/2026` → twenty fifth November twenty twenty six, `5:30 pm` → five thirty P M, `10:05` → ten oh five, `in 1998` → in nineteen ninety eight, `GST` → G S T.
91
+
92
+ ### How the new cases are spoken (decisions)
93
+
94
+ - **Train numbers**: digit by digit, the way Indian Railways announcements say them ("गाड़ी संख्या एक दो नौ पाँच एक"). Only a 5-digit number right after ट्रेन / गाड़ी / train / express (optionally with नंबर, संख्या, क्रमांक, No.) is treated as a train number.
95
+ - **Vehicle numbers**: state letters spelled, district code as a number (पंद्रह; a leading zero is read as digits: शून्य चार), series letters spelled, last digits one by one. Digit by digit is unambiguous for IVR and support calls; casual speech often uses pairs (बारह चौंतीस), so `vehicleDigits: 'pairs'` is available. Only real state / UT codes (MH, DL, KA, ...) and the BH series are matched.
96
+ - **UPI IDs**: the handle is read as a word when it looks like a name (`rahul`, `priya`), spelled when it is short or has no vowels (`rk`), digits one by one, `.` as डॉट / dot. `@` is ऐट / ॲट / at. Bank suffixes are split into known parts: `okicici` → ओके आई सी आई सी आई, `okhdfcbank` → ओके एच डी एफ सी बैंक, `ybl` → वाई बी एल, `paytm` → पेटीएम, `ptyes` → पी टी यस. Emails (`x@gmail.com`) and unknown suffixes are left alone unless the text mentions UPI.
97
+ - **OTP / codes**: any number after OTP, PIN, MPIN, TPIN, CVV, PNR, code, verification code, सत्यापन कोड, कोड, or before "is your OTP" / "हा तुमचा OTP", is read digit by digit in all three languages.
72
98
 
73
99
  ## Known limits
74
100
 
75
- - It does not inflect for grammar. `7:00 PM पर्यंत` becomes `सात वाजता पर्यंत`, but a native speaker would say `सात वाजेपर्यंत`.
101
+ - It does not inflect for grammar in general (`पर्यंत` after a clock time is handled; other postpositions are not).
102
+ - Latin words inside Hindi / Marathi text (a UPI handle like `rahul`, English words like `error`) are kept in Latin for the TTS to read; they are not transliterated.
103
+ - English mode reads the same rules in English; it does not expand US-style "million / billion" wording.
76
104
  - A bare 10-digit number starting with 6–9 is treated as a phone number.
77
105
  - A bare 6+ digit number with no commas (`100000`) is read digit by digit. Write `1,00,000` for amounts.
78
106
  - Hinglish and Romanized Hindi ("aapka order") are not transliterated yet.
@@ -89,7 +117,7 @@ Contributions are welcome, especially from native speakers checking number words
89
117
 
90
118
  ## Hosted API (no setup)
91
119
 
92
- Don't want to run it yourself? Use the hosted API at **https://uchcharan.dev**. Try it in the browser at https://huggingface.co/spaces/Densa850/uchcharan.
120
+ Don't want to run it yourself? Use the hosted API at **https://uchcharan.dev**. Try it in the browser at https://huggingface.co/spaces/Densa850/uchcharan. The hosted API and billing live in a separate private service.
93
121
 
94
122
  ```bash
95
123
  curl -X POST https://uchcharan.dev/v1/normalize \
package/bin/uchcharan.js CHANGED
@@ -9,8 +9,9 @@ for (let i = 0; i < args.length; i++) {
9
9
  if (a === '--lang' || a === '-l') opts.lang = args[++i];
10
10
  else if (a === '--explain') opts.explain = true;
11
11
  else if (a === '--no-spell') opts.spellAcronyms = false;
12
+ else if (a === '--pairs') opts.vehicleDigits = 'pairs';
12
13
  else if (a === '--help' || a === '-h') {
13
- console.log('Usage: uchcharan [--lang hi|mr] [--explain] [--no-spell] "text"\n echo "text" | uchcharan --lang mr');
14
+ console.log('Usage: uchcharan [--lang hi|mr|en|auto] [--pairs] [--explain] [--no-spell] "text"\n echo "text" | uchcharan --lang mr');
14
15
  process.exit(0);
15
16
  } else text.push(a);
16
17
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "uchcharan",
3
- "version": "0.2.2",
4
- "description": "Make any TTS speak Indian text correctly. Normalizes ₹ amounts, lakh/crore, dates, times, phone numbers, PNR/OTP, %, units and acronyms into spoken Hindi and Marathi.",
3
+ "version": "0.3.0",
4
+ "description": "Make any TTS speak Indian text correctly. Normalizes ₹ amounts, lakh/crore, dates, times, phone numbers, PNR/OTP, %, units, UPI IDs, train and vehicle numbers and acronyms into spoken Hindi, Marathi and Indian English.",
5
5
  "type": "module",
6
6
  "main": "src/index.js",
7
7
  "exports": "./src/index.js",
package/src/index.js CHANGED
@@ -1,11 +1,23 @@
1
1
  import hi from './lang/hi.js';
2
2
  import mr from './lang/mr.js';
3
+ import en from './lang/en.js';
3
4
  import { buildRules } from './rules.js';
4
5
  import { intToWords, numberStringToWords, yearToWords } from './numbers.js';
5
6
 
6
- export const LANGUAGES = { hi, mr };
7
+ export const LANGUAGES = { hi, mr, en };
7
8
 
8
- const DEFAULTS = { lang: 'hi', spellAcronyms: true, digitRunMin: 6, explain: false };
9
+ const DEFAULTS = { lang: 'hi', spellAcronyms: true, digitRunMin: 6, vehicleDigits: 'digits', explain: false };
10
+
11
+ const MARATHI_HINTS = /(?:^|[\s,.])(?:आहे|आहेत|आणि|मध्ये|तुमचा|तुमचे|तुमची|तुमच्या|नाही|करा|भरा|पर्यंत|आम्ही|होते|झाले|वाजता)(?=$|[\s,.?!।])/u;
12
+
13
+ /** Pick 'en' for mostly-Latin text, 'mr' when Marathi words appear, else 'hi'. */
14
+ export function detectLang(text) {
15
+ const s = String(text ?? '');
16
+ const latin = (s.match(/[A-Za-z]/g) || []).length;
17
+ const dev = (s.match(/[\u0900-\u097F]/g) || []).length;
18
+ if (latin > dev) return 'en';
19
+ return MARATHI_HINTS.test(s.normalize('NFC')) ? 'mr' : 'hi';
20
+ }
9
21
  const ruleCache = new Map();
10
22
 
11
23
  function getLang(code) {
@@ -15,15 +27,16 @@ function getLang(code) {
15
27
  }
16
28
 
17
29
  /**
18
- * Normalize text so any TTS engine speaks it correctly in Hindi or Marathi.
30
+ * Normalize text so any TTS engine speaks it correctly in Hindi, Marathi or Indian English.
19
31
  * @param {string} text
20
- * @param {{lang?: 'hi'|'mr', spellAcronyms?: boolean, digitRunMin?: number, explain?: boolean}} [options]
32
+ * @param {{lang?: 'hi'|'mr'|'en'|'auto', vehicleDigits?: 'digits'|'pairs', spellAcronyms?: boolean, digitRunMin?: number, explain?: boolean}} [options]
21
33
  * @returns {string | {text: string, changes: {rule: string, from: string, to: string}[]}}
22
34
  */
23
35
  export function normalize(text, options = {}) {
24
36
  const opts = { ...DEFAULTS, ...options };
37
+ if (opts.lang === 'auto') opts.lang = detectLang(text);
25
38
  const L = getLang(opts.lang);
26
- const key = `${opts.lang}|${opts.spellAcronyms}|${opts.digitRunMin}`;
39
+ const key = `${opts.lang}|${opts.spellAcronyms}|${opts.digitRunMin}|${opts.vehicleDigits}`;
27
40
  if (!ruleCache.has(key)) ruleCache.set(key, buildRules(L, opts));
28
41
 
29
42
  // NFC + Devanagari digits (०-९) → ASCII so every rule sees one form.
@@ -38,7 +51,7 @@ export function normalize(text, options = {}) {
38
51
  const end = hasNamed ? -3 : -2;
39
52
  const offset = args[args.length + end];
40
53
  const input = args[args.length + end + 1];
41
- let res = rule.fn(match, args.slice(1, end), L);
54
+ let res = rule.fn(match, args.slice(1, end), L, input);
42
55
  if (res === match) return match;
43
56
  // Keep words apart when a number was glued to Latin text ("COVID19").
44
57
  if (/[A-Za-z]/.test(input[offset - 1] ?? '')) res = ` ${res}`;
package/src/lang/en.js ADDED
@@ -0,0 +1,89 @@
1
+ // Indian English (en) language data for uchcharan.
2
+ // Numbers use the Indian system (thousand, lakh, crore), as Indians say them in English.
3
+ const units = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine',
4
+ 'ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen'];
5
+ const tens = ['', '', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety'];
6
+ // Words are kept apart with plain spaces (no hyphens), which every TTS reads cleanly.
7
+ const ones = Array.from({ length: 100 }, (_, n) =>
8
+ n < 20 ? units[n] : n % 10 ? `${tens[Math.floor(n / 10)]} ${units[n % 10]}` : tens[n / 10]);
9
+
10
+ const ORD = { one: 'first', two: 'second', three: 'third', five: 'fifth', eight: 'eighth', nine: 'ninth', twelve: 'twelfth' };
11
+ function ordinalWord(w) {
12
+ if (ORD[w]) return ORD[w];
13
+ if (w.endsWith('ty')) return `${w.slice(0, -1)}ieth`;
14
+ return `${w}th`;
15
+ }
16
+
17
+ function ordinal(n, words) {
18
+ const w = words(n).split(' ');
19
+ w.push(ordinalWord(w.pop()));
20
+ return w.join(' ');
21
+ }
22
+
23
+ export default {
24
+ code: 'en',
25
+ name: 'English (India)',
26
+ ones,
27
+ zero: 'zero',
28
+ point: 'point',
29
+ minus: 'minus',
30
+ plus: 'plus',
31
+ times: 'times',
32
+ equals: 'equals',
33
+ to: 'to',
34
+ and: 'and',
35
+ number: 'number',
36
+ percent: 'percent',
37
+ at: 'at',
38
+ dot: 'dot',
39
+ underscore: 'underscore',
40
+ dash: 'dash',
41
+ scales: { thousand: 'thousand', lakh: 'lakh', crore: 'crore' },
42
+ hundred: (h, rest) => (rest ? `${ones[h]} hundred and` : `${ones[h]} hundred`),
43
+ century: (c) => `${ones[c]} hundred`,
44
+ // 2.5 lakh → "two and a half lakh"
45
+ half: (n, words) => (n === 1 ? 'one and a half' : `${words(n)} and a half`),
46
+ rupee: (n) => (n === 1 ? 'rupee' : 'rupees'),
47
+ paise: (n) => (n === 1 ? 'paisa' : 'paise'),
48
+ paiseJoin: 'and',
49
+ months: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
50
+ // 25/11/2026 → "twenty fifth November twenty twenty six"
51
+ day: (d, words) => ordinal(d, words),
52
+ year(y, words) {
53
+ if (y >= 2000 && y <= 2009) return words(y);
54
+ if (y >= 1100 && y <= 2099 && y % 100 !== 0) {
55
+ const r = y % 100;
56
+ return `${ones[Math.floor(y / 100)]} ${r < 10 ? `oh ${ones[r]}` : ones[r]}`;
57
+ }
58
+ if (y >= 1100 && y <= 1999) return `${ones[y / 100]} hundred`;
59
+ return words(y);
60
+ },
61
+ time(h24, m, showPeriod, words) {
62
+ const h12 = h24 % 12 || 12;
63
+ const ap = showPeriod ? ` ${h24 < 12 ? 'A M' : 'P M'}` : '';
64
+ if (!m) return showPeriod ? `${words(h12)}${ap}` : `${words(h12)} o'clock`;
65
+ return `${words(h12)} ${m < 10 ? `oh ${words(m)}` : words(m)}${ap}`;
66
+ },
67
+ ordinal,
68
+ letters: Object.fromEntries([...'ABCDEFGHIJKLMNOPQRSTUVWXYZ'].map((c) => [c, c])),
69
+ wordAcronyms: {
70
+ ISRO: 'Isro', NASA: 'Nasa', AIIMS: 'Aims', NEET: 'Neet', OK: 'okay', SEBI: 'Sebi', NABARD: 'Nabard',
71
+ BHIM: 'Bheem', COVID: 'Covid', PIN: 'pin', UNESCO: 'Unesco', NITI: 'Niti', WiFi: 'WiFi',
72
+ },
73
+ units: {
74
+ 'km/h': 'kilometres per hour', kmph: 'kilometres per hour', km: 'kilometres', kg: 'kilos', gm: 'grams', g: 'grams',
75
+ cm: 'centimetres', mm: 'millimetres', ml: 'millilitres', gb: 'GB', mb: 'MB', tb: 'TB',
76
+ '°c': 'degrees Celsius', '°f': 'degrees Fahrenheit',
77
+ },
78
+ abbreviations: [
79
+ ['Dr\\.', 'Doctor'], ['Mr\\.', 'Mister'], ['Mrs\\.', 'Missus'], ['Smt\\.', 'Shrimati'],
80
+ ['Prof\\.', 'Professor'], ['etc\\.', 'etcetera'], ['vs\\.?', 'versus'], ['approx\\.', 'approximately'],
81
+ ['Ltd\\.', 'Limited'], ['Pvt\\.', 'Private'],
82
+ ],
83
+ periodWords: [],
84
+ // UPI handle suffix pieces: spoken word, or null = spell the letters.
85
+ upiTokens: {
86
+ ok: 'okay', axis: 'axis', bank: 'bank', yes: 'yes', paytm: 'Paytm', kotak: 'Kotak', airtel: 'Airtel',
87
+ jio: 'Jio', jupiter: 'Jupiter', freecharge: 'Freecharge', slice: 'Slice', federal: 'Federal', indus: 'Indus',
88
+ },
89
+ };
package/src/lang/hi.js CHANGED
@@ -34,6 +34,16 @@ export default {
34
34
  to: 'से',
35
35
  and: 'और',
36
36
  number: 'नंबर',
37
+ at: 'ऐट',
38
+ dot: 'डॉट',
39
+ underscore: 'अंडरस्कोर',
40
+ dash: 'डैश',
41
+ // Time-of-day words. When the text already has one before a clock time, it is not added again.
42
+ periodWords: ['सुबह', 'सवेरे', 'दोपहर', 'शाम', 'रात', 'तड़के', 'भोर'],
43
+ upiTokens: {
44
+ ok: 'ओके', axis: 'ऐक्सिस', bank: 'बैंक', yes: 'यस', paytm: 'पेटीएम', kotak: 'कोटक', airtel: 'एयरटेल',
45
+ jio: 'जियो', jupiter: 'जूपिटर', freecharge: 'फ्रीचार्ज', slice: 'स्लाइस', federal: 'फ़ेडरल', indus: 'इंडस',
46
+ },
37
47
  percent: 'प्रतिशत',
38
48
  scales: { thousand: 'हज़ार', lakh: 'लाख', crore: 'करोड़' },
39
49
  hundred: (h) => `${ones[h]} सौ`,
@@ -63,7 +73,7 @@ export default {
63
73
  '°c': 'डिग्री सेल्सियस', '°f': 'डिग्री फ़ारेनहाइट',
64
74
  },
65
75
  abbreviations: [
66
- ['Dr\\.', 'डॉक्टर'], ['Mr\\.', 'मिस्टर'], ['Mrs\\.', 'मिसेज़'], ['Ms\\.', 'मिस'], ['Smt\\.', 'श्रीमती'],
76
+ ['दि\\.', 'दिनांक'], ['Dr\\.', 'डॉक्टर'], ['Mr\\.', 'मिस्टर'], ['Mrs\\.', 'मिसेज़'], ['Ms\\.', 'मिस'], ['Smt\\.', 'श्रीमती'],
67
77
  ['Prof\\.', 'प्रोफ़ेसर'], ['etc\\.', 'वगैरह'], ['vs\\.?', 'बनाम'], ['approx\\.', 'लगभग'],
68
78
  ['Ltd\\.', 'लिमिटेड'], ['Pvt\\.', 'प्राइवेट'], ['डॉ\\.', 'डॉक्टर'],
69
79
  ],
package/src/lang/mr.js CHANGED
@@ -34,6 +34,15 @@ export default {
34
34
  to: 'ते',
35
35
  and: 'आणि',
36
36
  number: 'नंबर',
37
+ at: 'ॲट',
38
+ dot: 'डॉट',
39
+ underscore: 'अंडरस्कोर',
40
+ dash: 'डॅश',
41
+ periodWords: ['सकाळी', 'दुपारी', 'संध्याकाळी', 'सायंकाळी', 'रात्री', 'पहाटे'],
42
+ upiTokens: {
43
+ ok: 'ओके', axis: 'ॲक्सिस', bank: 'बँक', yes: 'येस', paytm: 'पेटीएम', kotak: 'कोटक', airtel: 'एअरटेल',
44
+ jio: 'जिओ', jupiter: 'ज्युपिटर', freecharge: 'फ्रीचार्ज', slice: 'स्लाइस', federal: 'फेडरल', indus: 'इंडस',
45
+ },
37
46
  percent: 'टक्के',
38
47
  scales: { thousand: 'हजार', lakh: 'लाख', crore: 'कोटी' },
39
48
  // 100 alone is "शंभर"; otherwise एकशे, दोनशे, ... नऊशे.
@@ -65,7 +74,7 @@ export default {
65
74
  '°c': 'डिग्री सेल्सिअस', '°f': 'डिग्री फॅरनहाइट',
66
75
  },
67
76
  abbreviations: [
68
- ['Dr\\.', 'डॉक्टर'], ['Mr\\.', 'मिस्टर'], ['Mrs\\.', 'मिसेस'], ['Ms\\.', 'मिस'], ['Smt\\.', 'श्रीमती'],
77
+ ['दि\\.', 'दिनांक'], ['Dr\\.', 'डॉक्टर'], ['Mr\\.', 'मिस्टर'], ['Mrs\\.', 'मिसेस'], ['Ms\\.', 'मिस'], ['Smt\\.', 'श्रीमती'],
69
78
  ['Prof\\.', 'प्रोफेसर'], ['etc\\.', 'वगैरे'], ['vs\\.?', 'विरुद्ध'], ['approx\\.', 'अंदाजे'],
70
79
  ['Ltd\\.', 'लिमिटेड'], ['Pvt\\.', 'प्रायव्हेट'], ['डॉ\\.', 'डॉक्टर'],
71
80
  ],
package/src/numbers.js CHANGED
@@ -50,6 +50,7 @@ export function numberStringToWords(str, L) {
50
50
  /** 1998 → "उन्नीस सौ अट्ठानवे" / "एकोणीसशे अठ्ठ्याण्णव"; 2026 → normal reading. */
51
51
  export function yearToWords(y, L) {
52
52
  y = Number(y);
53
+ if (L.year) return L.year(y, (n) => intToWords(n, L));
53
54
  if (y >= 1100 && y <= 1999) {
54
55
  const c = Math.floor(y / 100);
55
56
  const r = y % 100;
@@ -64,6 +65,7 @@ export function amountWords(str, L) {
64
65
  const m = /^(\d+)\.50*$/.exec(clean);
65
66
  if (m) {
66
67
  const n = Number(m[1]);
68
+ if (typeof L.half === 'function' && n >= 1) return L.half(n, (x) => intToWords(x, L));
67
69
  if (n === 1) return L.half[1];
68
70
  if (n === 2) return L.half[2];
69
71
  if (n > 2) return `${L.half.prefix} ${intToWords(n, L)}`;
package/src/rules.js CHANGED
@@ -19,17 +19,79 @@ const PLAIN_MULT_RE = String.raw`lakhs?|lacs?|crores?|cr\.?|लाख|करो
19
19
  const re = (src, flags = 'gu') => new RegExp(src.normalize('NFC'), flags);
20
20
  const multKey = (s) => MULTIPLIER[s.replace(/\.$/, '').toLowerCase()] || MULTIPLIER[s];
21
21
 
22
+ // Indian state / UT codes used on vehicle number plates.
23
+ const STATES = 'AN|AP|AR|AS|BR|CG|CH|DD|DL|DN|GA|GJ|HP|HR|JH|JK|KA|KL|LA|LD|MH|ML|MN|MP|MZ|NL|OD|OR|PB|PY|RJ|SK|TN|TR|TS|UA|UK|UP|WB';
24
+ // UPI handle suffix pieces that are spelled letter by letter (bank / app short names).
25
+ const UPI_SPELL = ['icici', 'sbi', 'hdfc', 'idfc', 'ybl', 'ibl', 'axl', 'apl', 'yapl', 'upi', 'kbl', 'kmbl', 'pt', 'pnb', 'boi', 'bob', 'rbl', 'fbl', 'hsbc', 'au', 'idbi', 'uco', 'cnrb', 'barodampay', 'waaxis', 'wahdfc', 'wasbi', 'waicici'];
26
+ // Spoken whole: names that are not abbreviations.
27
+ const UPI_WORDS = ['ok', 'axis', 'bank', 'yes', 'paytm', 'kotak', 'airtel', 'jio', 'jupiter', 'freecharge', 'slice', 'federal', 'indus'];
28
+ const UPI_SPLIT = { barodampay: ['baroda', 'mpay'], waaxis: ['wa', 'axis'], wahdfc: ['wa', 'hdfc'], wasbi: ['wa', 'sbi'], waicici: ['wa', 'icici'] };
29
+
22
30
  export function buildRules(L, opts) {
23
31
  const words = (n) => intToWords(n, L);
32
+ const spell = (str) => [...str.toUpperCase()].map((c) => L.letters[c] ?? c).join(' ');
33
+ const dayWord = (d) => (L.day ? L.day(d, words) : words(d));
34
+
35
+ // "okicici" → ["ok", "icici"]; null when the suffix is not made of known pieces.
36
+ const pieces = [...UPI_SPELL.filter((p) => !UPI_SPLIT[p]), ...UPI_WORDS].sort((a, b) => b.length - a.length);
37
+ const splitSuffix = (suf) => {
38
+ if (suf === '') return [];
39
+ for (const p of pieces) {
40
+ if (!suf.startsWith(p)) continue;
41
+ const rest = splitSuffix(suf.slice(p.length));
42
+ if (rest) return [p, ...rest];
43
+ }
44
+ return null;
45
+ };
46
+ const upiPiece = (p) => (UPI_WORDS.includes(p) ? L.upiTokens[p] : spell(p));
47
+ const handlePart = (part) =>
48
+ part.split(/([._-])/).filter(Boolean).map((seg) => {
49
+ if (seg === '.') return L.dot;
50
+ if (seg === '_') return L.underscore;
51
+ if (seg === '-') return L.dash;
52
+ return seg.split(/(\d+)/).filter(Boolean).map((run) => {
53
+ if (/^\d+$/.test(run)) return digitsToWords(run, L);
54
+ // Short or vowel-less runs ("rk", "xyz") are spelled; names ("rahul") stay a word.
55
+ if (run.length <= 3 || !/[aeiouy]/i.test(run)) return spell(run);
56
+ return run.toLowerCase();
57
+ }).join(' ');
58
+ }).join(' ');
59
+
60
+ const PERIOD = L.periodWords.length
61
+ ? String.raw`(?:(?<![${DEV}])(${L.periodWords.join('|')})(\s+(?:के\s+|को\s+)?))?`
62
+ : '()()';
63
+ const timeFn = (pw, psp, h, min, ap, suffix, until) => {
64
+ let h24 = Number(h);
65
+ if (ap) {
66
+ if (ap.toLowerCase() === 'p' && h24 < 12) h24 += 12;
67
+ if (ap.toLowerCase() === 'a' && h24 === 12) h24 = 0;
68
+ }
69
+ // If the text already says शाम / संध्याकाळी, do not add the period word a second time.
70
+ const showPeriod = !pw && (Boolean(ap) || h24 > 12 || h24 === 0);
71
+ let out = L.time(h24, Number(min || 0), showPeriod, words);
72
+ // Marathi "10:30 वाजता" means "at 10:30": दहा वाजून तीस मिनिटांनी.
73
+ if (suffix === 'वाजता') out = out.replace(/मिनिटे?$/u, 'मिनिटांनी');
74
+ if (until) {
75
+ // "7:00 PM पर्यंत" → सात वाजेपर्यंत
76
+ if (L.code === 'mr') out = out.replace(/वाजता$/u, 'वाजेपर्यंत').replace(/(मिनिटे?|मिनिटांनी)$/u, 'मिनिटांपर्यंत');
77
+ else out += until;
78
+ }
79
+ return pw ? `${pw}${psp}${out}` : out;
80
+ };
24
81
  const num = (s) => numberStringToWords(s, L);
25
82
  const date = (d, m, y) => {
26
83
  d = Number(d); m = Number(m);
27
84
  if (m < 1 || m > 12 || d < 1 || d > 31) return null;
28
- const parts = [words(d), L.months[m - 1]];
85
+ const parts = [dayWord(d), L.months[m - 1]];
29
86
  if (y) parts.push(yearToWords(String(y).length === 2 ? 2000 + Number(y) : y, L));
30
87
  return parts.join(' ');
31
88
  };
32
89
 
90
+ const plateDigits = (num) => {
91
+ if (opts.vehicleDigits !== 'pairs' || num.length !== 4) return digitsToWords(num, L);
92
+ return [num.slice(0, 2), num.slice(2)].map((p) => (p.startsWith('0') ? digitsToWords(p, L) : words(Number(p)))).join(' ');
93
+ };
94
+
33
95
  return [
34
96
  {
35
97
  name: 'currency',
@@ -40,7 +102,7 @@ export function buildRules(L, opts) {
40
102
  const rupees = Number(int);
41
103
  let out = `${words(rupees)} ${L.rupee(rupees)}`;
42
104
  const paise = frac ? Number(frac.slice(0, 2).padEnd(2, '0')) : 0;
43
- if (paise) out += ` ${words(paise)} ${L.paise(paise)}`;
105
+ if (paise) out += `${L.paiseJoin ? ` ${L.paiseJoin}` : ''} ${words(paise)} ${L.paise(paise)}`;
44
106
  return out;
45
107
  },
46
108
  },
@@ -49,6 +111,62 @@ export function buildRules(L, opts) {
49
111
  re: re(String.raw`(?<![A-Za-z${DEV}])${pat}(?![A-Za-z])`),
50
112
  fn: () => rep,
51
113
  })),
114
+ {
115
+ // UPI IDs: rahul@okicici → "rahul ऐट ओके आई सी आई सी आई". Emails (x@gmail.com) are left alone.
116
+ name: 'upi',
117
+ re: re(String.raw`(?<![A-Za-z0-9._\-@])([A-Za-z0-9][A-Za-z0-9._\-]{1,48})@([A-Za-z]{2,24})(?![A-Za-z0-9@]|\.[A-Za-z])`),
118
+ fn: (m, [handle, suffix], _L, input) => {
119
+ const known = splitSuffix(suffix.toLowerCase());
120
+ const upiContext = /UPI|यूपीआई|यूपीआय/i.test(input);
121
+ if (!known && !upiContext) return m;
122
+ const suf = known
123
+ ? known.flatMap((p) => UPI_SPLIT[p] ?? [p]).map((p) => (p === 'baroda' ? handlePart(p) : p === 'mpay' ? `${spell('m')} pay` : upiPiece(p))).join(' ')
124
+ : handlePart(suffix);
125
+ return `${handlePart(handle)} ${L.at} ${suf}`;
126
+ },
127
+ },
128
+ {
129
+ // Vehicle registration: MH15AB1234 / MH 15 AB 1234 / MH-15-AB-1234.
130
+ // Letters spelled, district as a number (leading zero read as digits), last digits one by one
131
+ // (or in pairs with { vehicleDigits: 'pairs' }).
132
+ name: 'vehicle',
133
+ re: re(String.raw`(?<![A-Za-z0-9])(${STATES})[\s-]?(\d{1,2})[\s-]?([A-Z]{1,3})[\s-]?(\d{1,4})(?![A-Za-z0-9])`, 'g'),
134
+ fn: (m, [st, dist, series, num]) => {
135
+ const d = dist.startsWith('0') ? digitsToWords(dist, L) : words(Number(dist));
136
+ return `${spell(st)} ${d} ${spell(series)} ${plateDigits(num)}`;
137
+ },
138
+ },
139
+ {
140
+ // Bharat series: 22 BH 1234 AA
141
+ name: 'vehicle',
142
+ re: re(String.raw`(?<![A-Za-z0-9])(\d{2})[\s-]?BH[\s-]?(\d{4})[\s-]?([A-Z]{1,2})(?![A-Za-z0-9])`, 'g'),
143
+ fn: (m, [yy, num, series]) => `${words(Number(yy))} ${spell('BH')} ${plateDigits(num)} ${spell(series)}`,
144
+ },
145
+ {
146
+ // One capital letter + a short number, as in error codes and seat numbers: E4 → ई चार, A12 → ए बारह.
147
+ name: 'letter-code',
148
+ re: re(String.raw`(?<![A-Za-z0-9])([A-Z])-?(\d{1,3})(?![A-Za-z0-9]|[.,]\d)`),
149
+ fn: (m, [c, d]) => `${spell(c)} ${d.length <= 2 && !d.startsWith('0') ? words(Number(d)) : digitsToWords(d, L)}`,
150
+ },
151
+ {
152
+ // Indian Railways announces train numbers digit by digit: "गाड़ी संख्या एक दो नौ पाँच एक".
153
+ name: 'train',
154
+ re: re(String.raw`(?<![A-Za-z${DEV}])(trains?|Trains?|TRAIN|ट्रेन|गाड़ी|गाडी|रेलगाड़ी|रेलगाडी|एक्सप्रेस|[Ee]xpress)(\s*(?:no\.?|No\.?|number|Number|नंबर|नं\.|क्रमांक|क्र\.|संख्या|#)?\s*:?\s*)(\d{5})(?![\d.,]\d|\d)`),
155
+ fn: (m, [k, sep, d]) => `${k}${sep.replace(/(?:No|no)\.?|#/, L.number)}${digitsToWords(d, L)}`,
156
+ },
157
+ {
158
+ // A number right after OTP / PIN / PNR / CVV / code is always read digit by digit,
159
+ // even when it is short (a 4-digit OTP must not become "four thousand ...").
160
+ name: 'code',
161
+ re: re(String.raw`(?<![A-Za-z${DEV}])(OTP|[Oo]tp|MPIN|TPIN|UPI PIN|PIN|PNR|CVV|ओटीपी|पिन|सत्यापन कोड|कोड|[Pp]asscode|[Vv]erification code|[Ss]ecurity code|[Cc]ode|[Oo]ne[- ]time password)((?:\s+(?:for|to|of)\s+[A-Za-z]+)?\s*(?:[:\-]|is|no\.?|number|नंबर|क्रमांक)?\s*:?\s*)(\d{3,})(?![\d.,]\d|\d)`),
162
+ fn: (m, [k, sep, d]) => `${k}${sep}${digitsToWords(d, L)}`,
163
+ },
164
+ {
165
+ // "4821 is your OTP" / "482913 हा तुमचा OTP आहे"
166
+ name: 'code',
167
+ re: re(String.raw`(?<![\d.,])(\d{4,8})(?=\s+(?:is your|is the|आपका|आपकी|यह आपका|ये आपका|हा तुमचा|तुमचा|हा)\s+(?:OTP|otp|one[- ]time password|verification code|code|ओटीपी|कोड)(?![A-Za-z]))`),
168
+ fn: (m, [d]) => digitsToWords(d, L),
169
+ },
52
170
  { name: 'number-sign', re: re(String.raw`(?<![A-Za-z])(?:No\.|#)\s*(?=\d)`), fn: () => `${L.number} ` },
53
171
  {
54
172
  name: 'date',
@@ -72,15 +190,15 @@ export function buildRules(L, opts) {
72
190
  },
73
191
  {
74
192
  name: 'time',
75
- re: re(String.raw`(?<![\d:])([01]?\d|2[0-3]):([0-5]\d)(?:\s*([AaPp])\.?\s?[Mm]\.?(?![A-Za-z]))?(?![\d:])`),
76
- fn: (m, [h, min, ap]) => {
77
- let h24 = Number(h);
78
- if (ap) {
79
- if (ap.toLowerCase() === 'p' && h24 < 12) h24 += 12;
80
- if (ap.toLowerCase() === 'a' && h24 === 12) h24 = 0;
81
- }
82
- return L.time(h24, Number(min), Boolean(ap) || h24 > 12 || h24 === 0, words);
83
- },
193
+ re: re(String.raw`${PERIOD}(?<![\d:])([01]?\d|2[0-3]):([0-5]\d)(?:\s*([AaPp])(?:\.\s?[Mm]\.?|\s?[Mm])(?![A-Za-z]))?(?![\d:])(?:\s*(बजे|वाजता|वाजले)(?![${DEV}]))?(\s*पर्यंत)?`),
194
+ // A trailing बजे / वाजता is dropped: the spoken time already ends in its own word.
195
+ fn: (m, [pw, psp, h, min, ap, suffix, until]) => timeFn(pw, psp, h, min, ap, suffix, until),
196
+ },
197
+ {
198
+ // "7 PM", "शाम 7 PM"
199
+ name: 'time',
200
+ re: re(String.raw`${PERIOD}(?<![\d:.,])(1[0-2]|0?[1-9])\s*([AaPp])(?:\.\s?[Mm]\.?|\s?[Mm])(?![A-Za-z])(?:\s*(बजे|वाजता)(?![${DEV}]))?(\s*पर्यंत)?`),
201
+ fn: (m, [pw, psp, h, ap, suffix, until]) => timeFn(pw, psp, h, 0, ap, suffix, until),
84
202
  },
85
203
  {
86
204
  name: 'phone',