sinhal commited on
Commit
2f41aa3
·
verified ·
1 Parent(s): 6bbec6a

gujnorm demo (static, runs in the browser)

Browse files
Files changed (6) hide show
  1. README.md +25 -6
  2. gujnorm/__init__.py +14 -0
  3. gujnorm/numbers.py +113 -0
  4. gujnorm/rules.py +343 -0
  5. gujnorm/sarvam.py +36 -0
  6. index.html +127 -17
README.md CHANGED
@@ -1,10 +1,29 @@
1
  ---
2
- title: Gujnorm
3
- emoji: 🏢
4
- colorFrom: red
5
- colorTo: purple
6
  sdk: static
7
- pinned: false
 
 
 
 
 
 
 
8
  ---
9
 
10
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
+ title: gujnorm
3
+ emoji: 🗣️
4
+ colorFrom: indigo
5
+ colorTo: green
6
  sdk: static
7
+ app_file: index.html
8
+ pinned: true
9
+ license: apache-2.0
10
+ short_description: Gujarati text normalizer so TTS reads numbers right
11
+ tags:
12
+ - gujarati
13
+ - text-normalization
14
+ - tts
15
  ---
16
 
17
+ # gujnorm
18
+
19
+ Written Gujarati → spoken Gujarati words, before text-to-speech.
20
+
21
+ | Written | TTS says without it | With gujnorm |
22
+ |---|---|---|
23
+ | સવારે 9:30 વાગ્યે | નવ વાગ્યે ત્રીસ મિનિટ વાગ્યે | સાડા નવ વાગ્યે |
24
+ | લોન ₹1.5 લાખની | એક દશાંશ પાંચ લાખ રૂપીઝની | દોઢ લાખ રૂપિયાની |
25
+ | 8.5% | આઠ દશાંશ પાંચ પર્સેન્ટ | સાડા આઠ ટકા |
26
+
27
+ - Code and tests: https://github.com/Zephyr4772/gujnorm
28
+ - Measured results (Sarvam Bulbul, 3 voices, blind listening test):
29
+ https://huggingface.co/datasets/sinhal/gujarati-tts-number-reading
gujnorm/__init__.py ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- coding: utf-8 -*-
2
+ """gujnorm — Gujarati text normalizer for TTS (times, money, fractions, dates, ranges, ordinals, IDs)."""
3
+ from .rules import PASSES
4
+ from .numbers import num_words, year_words, time_words, fraction_words
5
+
6
+ __all__ = ["normalize", "num_words", "year_words", "time_words", "fraction_words"]
7
+ __version__ = "0.1.1"
8
+
9
+
10
+ def normalize(text: str) -> str:
11
+ """Rewrite written Gujarati into fully spoken Gujarati words, ready for TTS."""
12
+ for p in PASSES:
13
+ text = p(text)
14
+ return text
gujnorm/numbers.py ADDED
@@ -0,0 +1,113 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- coding: utf-8 -*-
2
+ """Gujarati number words (Indian numbering system)."""
3
+
4
+ # 0–99 are irregular in Gujarati and must be listed.
5
+ UNITS = (
6
+ "શૂન્ય એક બે ત્રણ ચાર પાંચ છ સાત આઠ નવ "
7
+ "દસ અગિયાર બાર તેર ચૌદ પંદર સોળ સત્તર અઢાર ઓગણીસ "
8
+ "વીસ એકવીસ બાવીસ ત્રેવીસ ચોવીસ પચીસ છવ્વીસ સત્તાવીસ અઠ્ઠાવીસ ઓગણત્રીસ "
9
+ "ત્રીસ એકત્રીસ બત્રીસ તેત્રીસ ચોત્રીસ પાંત્રીસ છત્રીસ સાડત્રીસ આડત્રીસ ઓગણચાલીસ "
10
+ "ચાલીસ એકતાલીસ બેતાલીસ તેતાલીસ ચુંમાલીસ પિસ્તાલીસ છેતાલીસ સુડતાલીસ અડતાલીસ ઓગણપચાસ "
11
+ "પચાસ એકાવન બાવન ત્રેપન ચોપન પંચાવન છપ્પન સત્તાવન અઠ્ઠાવન ઓગણસાઠ "
12
+ "સાઠ એકસઠ બાસઠ ત્રેસઠ ચોસઠ પાંસઠ છાસઠ સડસઠ અડસઠ અગણોસિત્તેર "
13
+ "સિત્તેર એકોતેર બોતેર તોતેર ચુંમોતેર પંચોતેર છોતેર સિત્યોતેર ઇઠ્યોતેર ઓગણએંસી "
14
+ "એંસી એક્યાસી બ્યાસી ત્યાસી ચોર્યાસી પંચ્યાસી છ્યાસી સિત્યાસી ઈઠ્યાસી નેવ્યાસી "
15
+ "નેવું એકાણું બાણું ત્રાણું ચોરાણું પંચાણું છન્નું સત્તાણું અઠ્ઠાણું નવ્વાણું"
16
+ ).split()
17
+ assert len(UNITS) == 100, len(UNITS)
18
+
19
+ HUNDREDS = {1: "એકસો", 2: "બસો", 3: "ત્રણસો", 4: "ચારસો", 5: "પાંચસો",
20
+ 6: "છસો", 7: "સાતસો", 8: "આઠસો", 9: "નવસો"}
21
+
22
+ DIGITS = UNITS[:10]
23
+
24
+ MONTHS = ["જાન્યુઆરી", "ફેબ્રુઆરી", "માર્ચ", "એપ્રિલ", "મે", "જૂન",
25
+ "જુલાઈ", "ઓગસ્ટ", "સપ્ટેમ્બર", "ઓક્ટોબર", "નવેમ્બર", "ડિસેમ્બર"]
26
+
27
+ # ordinal stems: 1 પહેલ-, 2 બીજ-, 3 ત્રીજ-, 4 ચોથ-, 6 છઠ્ઠ-, else <number>મ-
28
+ ORD_STEM = {1: "પહેલ", 2: "બીજ", 3: "ત્રીજ", 4: "ચોથ", 6: "છઠ્ઠ"}
29
+
30
+
31
+ def num_words(n: int) -> str:
32
+ """Cardinal in Indian grouping: 1,25,000 -> એક લાખ પચીસ હજાર."""
33
+ if n < 0:
34
+ return "માઇનસ " + num_words(-n)
35
+ if n < 100:
36
+ return UNITS[n]
37
+ parts = []
38
+ crore, n = divmod(n, 10**7)
39
+ if crore:
40
+ parts.append(num_words(crore) + " કરોડ")
41
+ lakh, n = divmod(n, 10**5)
42
+ if lakh:
43
+ parts.append(UNITS[lakh] + " લાખ")
44
+ thousand, n = divmod(n, 1000)
45
+ if thousand:
46
+ parts.append(UNITS[thousand] + " હજાર")
47
+ hundred, n = divmod(n, 100)
48
+ if hundred:
49
+ parts.append(HUNDREDS[hundred])
50
+ if n:
51
+ parts.append(UNITS[n])
52
+ return " ".join(parts)
53
+
54
+
55
+ def year_words(y: int) -> str:
56
+ """1947 -> ઓગણીસો સુડતાલીસ, 1857 -> અઢારસો સત્તાવન, 2026 -> બે હજાર છવ્વીસ."""
57
+ if 1100 <= y <= 1999:
58
+ hi, lo = divmod(y, 100)
59
+ head = "ઓગણીસો" if hi == 19 else UNITS[hi] + "સો"
60
+ return head if lo == 0 else f"{head} {UNITS[lo]}"
61
+ return num_words(y)
62
+
63
+
64
+ def digit_words(s: str) -> str:
65
+ """'0045' -> શૂન્ય શૂન્ય ચાર પાંચ (ignores spaces/dashes)."""
66
+ return " ".join(DIGITS[int(c)] for c in s if c.isdigit())
67
+
68
+
69
+ def fraction_words(i: int, frac: str) -> str:
70
+ """Gujarati fraction words for x.5 / x.25 / x.75; otherwise '<n> દશાંશ <digits>'."""
71
+ f = frac.rstrip("0")
72
+ if i <= 99:
73
+ if f == "5":
74
+ return {0: "અડધો", 1: "દોઢ", 2: "અઢી"}.get(i, "સાડા " + UNITS[i])
75
+ if f == "25":
76
+ return {0: "પા", 1: "સવા"}.get(i, "સવા " + UNITS[i])
77
+ if f == "75":
78
+ return "પોણો" if i == 0 else "પોણા " + num_words(i + 1)
79
+ if f == "":
80
+ return num_words(i)
81
+ return f"{num_words(i)} દશાંશ {digit_words(frac)}"
82
+
83
+
84
+ def ordinal_words(n: int, ending: str) -> str:
85
+ """n + inflection vowel (ો ા ી ું ે) -> પહેલો / બીજી / ચોથું / એકવીસમી."""
86
+ stem = ORD_STEM.get(n, num_words(n) + "મ")
87
+ return stem + ending
88
+
89
+
90
+ def time_words(h: int, m: int) -> str:
91
+ """Clock time the way Gujarati speakers say it (12-hour)."""
92
+ h12 = h % 12 or 12
93
+ nxt = h12 % 12 + 1
94
+ if m == 0:
95
+ return UNITS[h12]
96
+ if m == 15:
97
+ return "સવા " + UNITS[h12]
98
+ if m == 30:
99
+ return {1: "દોઢ", 2: "અઢી"}.get(h12, "સાડા " + UNITS[h12])
100
+ if m == 45:
101
+ return "પોણા " + UNITS[nxt]
102
+ return f"{UNITS[h12]} ને {UNITS[m]}"
103
+
104
+
105
+ def day_period(h: int) -> str:
106
+ """24h hour -> સવારે / બપોરે / સાંજે / રાત્રે."""
107
+ if 4 <= h < 12:
108
+ return "સવારે"
109
+ if 12 <= h < 16:
110
+ return "બપોરે"
111
+ if 16 <= h < 20:
112
+ return "સાંજે"
113
+ return "રાત્રે"
gujnorm/rules.py ADDED
@@ -0,0 +1,343 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- coding: utf-8 -*-
2
+ """Ordered normalization passes. Every pass emits words (no digits), so later passes never re-match."""
3
+ import re
4
+ import threading
5
+ from .numbers import (num_words, year_words, digit_words, fraction_words, ordinal_words,
6
+ time_words, day_period, MONTHS)
7
+
8
+ GU = "઀-૿"
9
+ NS = r"(?<![\d.,/:])" # number start
10
+ NE = r"(?![\d])" # number end
11
+ END = r"(?=[\s.,!?;:)\]\"'”’…]|$)" # word end
12
+
13
+
14
+ # ---------------- 1. digits ----------------
15
+ _GU_DIGITS = str.maketrans("૦૧૨૩૪૫૬૭૮૯", "0123456789")
16
+
17
+
18
+ def gujarati_digits(t):
19
+ t = t.translate(_GU_DIGITS)
20
+ # "2026 માં" -> "2026માં": once spelled out, Gujarati writes the postposition joined to the number word
21
+ # ("છવ્વીસમાં", not "છવ્વીસ માં"); the gap made Bulbul pause. થી / એ / ને are left alone on purpose
22
+ # ("4 થી" vs ordinal "4થી"; "1947 એ" -> "…સુડતાલીસે" needs vowel sandhi).
23
+ num = r"(?<![A-Za-z\d])(\d[\d,.:/\-]*)" # a plain number, not the tail of an ID like V4
24
+ t = re.sub(rf"{num}\s+(માં|ના|નાં|નો|ની|નું|નુ){END}", r"\1\2", t)
25
+ return re.sub(rf"{num}\s+થી(?=\s*\d)", r"\1થી", t) # "10 થી 15" -> "10થી 15" (range, not ordinal)
26
+
27
+
28
+ # ---------------- 2. abbreviations ----------------
29
+ _ABBR = [
30
+ (r"તા\.\s?ક\.", "તાજા કલમ"),
31
+ (r"[ઇઈ]\.\s?સ\.\s?પૂ\.", "ઈસવીસન પૂર્વે"),
32
+ (r"[ઇઈ]\.\s?સ\.", "ઈસવીસન"),
33
+ (r"કિ\.\s?મી\.", "કિલોમીટર"),
34
+ (r"કિ\.\s?ગ્રા\.", "કિલોગ્રામ"),
35
+ (r"સૌ\.\s?યુ\.", "સૌરાષ્ટ્ર યુનિવર્સિટી"),
36
+ (r"ગુ\.\s?યુ\.", "ગુજરાત યુનિવર્સિટી"),
37
+ (rf"(?<![{GU}])તા\.", "તારીખ"),
38
+ (rf"(?<![{GU}])નં\.", "નંબર"),
39
+ (rf"(?<![{GU}])ડૉ\.", "ડૉક્ટર"),
40
+ (rf"(?<![{GU}])ડો\.", "ડૉક્ટર"),
41
+ (rf"(?<![{GU}])રૂ\.(?=\s*\d)", "₹"),
42
+ (rf"(?<![{GU}])રૂ\.", "રૂપિયા"),
43
+ ]
44
+
45
+
46
+ def abbreviations(t):
47
+ for pat, rep in _ABBR:
48
+ t = re.sub(pat, rep, t)
49
+ return t
50
+
51
+
52
+ # ---------------- 3. dates ----------------
53
+ _MONTH_ALT = "|".join(MONTHS + ["ઑગસ્ટ", "ઓગષ્ટ", "જુન", "જુલાઇ", "ઑક્ટોબર", "સપ્ટેંબર", "નવેંબર", "ડિસેંબર"])
54
+
55
+
56
+ def _date(d, m, y):
57
+ d, m, y = int(d), int(m), int(y)
58
+ if not (1 <= d <= 31 and 1 <= m <= 12):
59
+ return None
60
+ return f"{num_words(d)} {MONTHS[m - 1]} {year_words(y)}"
61
+
62
+
63
+ def dates(t):
64
+ t = re.sub(rf"{NS}(\d{{4}})-(\d{{1,2}})-(\d{{1,2}}){NE}",
65
+ lambda m: _date(m[3], m[2], m[1]) or m[0], t)
66
+ t = re.sub(rf"{NS}(\d{{1,2}})[/.\-](\d{{1,2}})[/.\-](\d{{4}}){NE}",
67
+ lambda m: _date(m[1], m[2], m[3]) or m[0], t)
68
+ t = re.sub(rf"{NS}(\d{{1,2}})\s+({_MONTH_ALT})\s+(\d{{4}}){NE}",
69
+ lambda m: f"{num_words(int(m[1]))} {m[2]} {year_words(int(m[3]))}", t)
70
+ # English month names, either order: "March 12, 1930" / "12 March 1930" / "April 21"
71
+ t = re.sub(rf"\b({_EN_MONTH_ALT})\.?\s+(\d{{1,2}})(?:st|nd|rd|th)?(?:,?\s+(\d{{4}}))?{NE}",
72
+ lambda m: _en_date(m[2], m[1], m[3]), t)
73
+ t = re.sub(rf"{NS}(\d{{1,2}})(?:st|nd|rd|th)?\s+({_EN_MONTH_ALT})\.?(?:,?\s+(\d{{4}}))?{NE}",
74
+ lambda m: _en_date(m[1], m[2], m[3]), t)
75
+ return t
76
+
77
+
78
+ _EN_MONTHS = ["january", "february", "march", "april", "may", "june", "july", "august",
79
+ "september", "october", "november", "december"]
80
+ _EN_MONTH_ALT = "|".join([m.capitalize() for m in _EN_MONTHS] + [m[:3].capitalize() for m in _EN_MONTHS if m != "may"])
81
+
82
+
83
+ def _en_date(d, mon, y):
84
+ d = int(d)
85
+ key = mon.lower()[:3]
86
+ idx = [m[:3] for m in _EN_MONTHS].index(key)
87
+ if not 1 <= d <= 31:
88
+ return f"{mon} {d}"
89
+ s = f"{num_words(d)} {MONTHS[idx]}"
90
+ return s + (f" {year_words(int(y))}" if y else "")
91
+
92
+
93
+ def simple_fractions(t):
94
+ fr = {"1/2": "અડધો", "1/4": "પા", "3/4": "પોણો"}
95
+ return re.sub(r"(?<![\d/])([13])/([24])(?![\d/])", lambda m: fr.get(m[0], m[0]), t)
96
+
97
+
98
+ # ---------------- 4. times ----------------
99
+ _AMPM = r"(?:[AaPp]\.?\s?[Mm]\.?)(?![A-Za-z])"
100
+ _HAS_PERIOD = re.compile(r"(સવારે|બપોરે|સાંજે|રાત્રે|સવારના|બપોરના|સાંજના|રાતના|રાત્રિના)\s*$")
101
+ _VAGYE = re.compile(r"\s*(વાગ્યે|વાગે|વાગ્યા|વાગ્યાથી|વાગ્યાનો|વાગ્યાની|વાગ્યાના)")
102
+ _GLUE_POST = re.compile(r"(થી|નો|ની|નું|ના)" + END)
103
+ _SPACE_POST = re.compile(r"\s+(થી|સુધીમાં|સુધી|વચ્ચે|પહેલાં|પહેલા|પછી|બાદ|આસપાસ|નો|ની|નું|ના)" + END)
104
+ _AT = re.compile(r"\s+(એ|પર|પરે)" + END)
105
+ _TIME = re.compile(rf"{NS}(\d{{1,2}})(?::(\d{{2}})(?:\s*({_AMPM}))?|\s*({_AMPM})){NE}")
106
+
107
+
108
+ def times(t):
109
+ out, pos = [], 0
110
+ for m in _TIME.finditer(t):
111
+ h = int(m[1])
112
+ mins = int(m[2]) if m[2] else 0
113
+ ampm = (m[3] or m[4] or "").lower().replace(".", "").replace(" ", "")
114
+ if h > 23 or mins > 59 or (ampm and not 1 <= h <= 12):
115
+ continue
116
+ if m.start() < pos:
117
+ continue
118
+ # 24h hour for the period word
119
+ if ampm == "pm":
120
+ h24 = h % 12 + 12
121
+ elif ampm == "am":
122
+ h24 = h % 12
123
+ else:
124
+ h24 = h
125
+ before = t[pos:m.start()]
126
+ period = ""
127
+ if (ampm or h >= 13 or h == 0) and not _HAS_PERIOD.search(t[:m.start()]):
128
+ period = day_period(h24) + " "
129
+ words = period + time_words(h24, mins)
130
+ rest_start = m.end()
131
+ rest = t[rest_start:]
132
+ if (v := _VAGYE.match(rest)):
133
+ words += " " + v[1]
134
+ rest_start += v.end()
135
+ elif (g := _GLUE_POST.match(rest)):
136
+ words += " વાગ્યા" + g[1]
137
+ rest_start += g.end()
138
+ elif (s := _SPACE_POST.match(rest)):
139
+ glue = s[1] in ("થી", "નો", "ની", "નું", "ના")
140
+ words += " વાગ્યા" + ("" if glue else " ") + s[1]
141
+ rest_start += s.end()
142
+ elif (a := _AT.match(rest)):
143
+ words += " વાગ્યે"
144
+ rest_start += a.end()
145
+ else:
146
+ words += " વાગ્યે"
147
+ out.append(before + words)
148
+ pos = rest_start
149
+ out.append(t[pos:])
150
+ return "".join(out)
151
+
152
+
153
+ # ---------------- 5. phone / OTP / IDs ----------------
154
+ _PHONE = re.compile(r"(\+\s?91[\s\-]?)?(?<!\d)([6-9]\d{4})[\s\-]?(\d{5})(?!\d)")
155
+ _KEYWORD_ID = re.compile(
156
+ r"(OTP|ઓટીપી|PIN|પિન(?:\s*કોડ)?|પીન(?:\s*કોડ)?|કોડ|AWB|આઈડી|આઇડી|ID|અંક|એકાઉન્ટ\s*નંબર|ખાતા\s*નંબર)"
157
+ r"([^\d\n]{0,12}?)(\d[\d \-]{2,}\d)(?!\d)")
158
+ _LONG_NUMBER_AFTER_NO = re.compile(r"(નંબર|નં)(\s*)(\d{5,})(?!\d)")
159
+ _SPACED_GROUPS = re.compile(r"(?<![\d,.])\d{3,5}(?:[ \-]\d{3,5})+(?![\d,.])")
160
+
161
+
162
+ def ids(t):
163
+ t = _PHONE.sub(lambda m: ("પ્લસ નવ એક " if m[1] else "") + digit_words(m[2]) + ", " + digit_words(m[3]), t)
164
+ t = _KEYWORD_ID.sub(lambda m: m[1] + m[2] + digit_words(m[3]), t)
165
+ t = _LONG_NUMBER_AFTER_NO.sub(lambda m: m[1] + m[2] + digit_words(m[3]), t)
166
+ t = _SPACED_GROUPS.sub(lambda m: ", ".join(digit_words(g) for g in re.split(r"[ \-]", m[0])), t)
167
+ return t
168
+
169
+
170
+ # ---------------- 6. math ----------------
171
+ def math_symbols(t):
172
+ t = re.sub(r"(?<=\d)\s*\+\s*(?=\d)", " વત્તા ", t)
173
+ t = re.sub(rf"(?<=[\d{GU}])\s*=\s*(?=[\d₹$])", " બરાબર ", t)
174
+ return t
175
+
176
+
177
+ # ---------------- 7. money ----------------
178
+ _POST_GLUE = r"(ની|નું|નો|ના|નાં|માં|થી|નુ)"
179
+ _SCALE = r"લાખ|કરોડ|હજાર|Crore|crore|Cr\.?|cr\.?|Lakh|lakh|Lac|lac|L(?![A-Za-z])"
180
+ _SCALE_MAP = {"cr": "કરોડ", "crore": "કરોડ", "lakh": "લાખ", "lac": "લાખ", "l": "લાખ"}
181
+ _MONEY = re.compile(
182
+ rf"(?:₹|Rs\.?|INR|Rs)\s*(\d[\d,]*)(?:\.(\d+))?(?:\s*({_SCALE}))?"
183
+ rf"(?:{_POST_GLUE}{END}|\s+(ની|નું|નો|ના|નાં){END})?")
184
+
185
+
186
+ def _amount(intpart, frac, scale):
187
+ n = int(intpart.replace(",", ""))
188
+ if scale:
189
+ scale = _SCALE_MAP.get(scale.lower().rstrip("."), scale)
190
+ w = fraction_words(n, frac) if frac else num_words(n)
191
+ return f"{w} {scale} રૂપિયા"
192
+ s = f"{num_words(n)} રૂપિયા"
193
+ if frac:
194
+ paise = int((frac + "0")[:2])
195
+ if paise:
196
+ s += f" {num_words(paise)} પૈસા"
197
+ return s
198
+
199
+
200
+ def money(t):
201
+ t = _MONEY.sub(lambda m: _amount(m[1], m[2], m[3]) + (m[4] or m[5] or ""), t)
202
+ t = re.sub(rf"{NS}(\d[\d,]*)\s*/-", lambda m: num_words(int(m[1].replace(",", ""))) + " રૂપિયા", t)
203
+ def dollars(m):
204
+ d = int(m[1].replace(",", ""))
205
+ s = f"{num_words(d)} ડોલર"
206
+ if m[2]:
207
+ cents = int((m[2] + "0")[:2])
208
+ if cents:
209
+ s += f" {num_words(cents)} સેન્ટ"
210
+ return s
211
+ t = re.sub(r"\$\s*(\d[\d,]*)(?:\.(\d+))?", dollars, t)
212
+ return t
213
+
214
+
215
+ # ---------------- 8. percent ----------------
216
+ def percent(t):
217
+ return re.sub(rf"{NS}(\d+)(?:\.(\d+))?\s*%",
218
+ lambda m: (fraction_words(int(m[1]), m[2]) if m[2] else num_words(int(m[1]))) + " ટકા", t)
219
+
220
+
221
+ # ---------------- 9. units ----------------
222
+ _UNITS = [
223
+ ("km/h", "કિલોમીટર પ્રતિ કલાક"), ("kmph", "કિલોમીટર પ્રતિ કલાક"), ("km/l", "કિલોમીટર પ્રતિ લીટર"),
224
+ ("m/s", "મીટર પ્રતિ ��ેકન્ડ"), ("km", "કિલોમીટર"), ("kWh", "કિલોવોટ કલાક"), ("kW", "કિલોવોટ"),
225
+ ("kg", "કિલો"), ("mg", "મિલિગ્રામ"), ("ml", "મિલીલીટર"), ("cm", "સેન્ટિમીટર"),
226
+ ("mm", "મિલીમીટર"), ("g", "ગ્રામ"), ("°C", "ડિગ્રી સેલ્સિયસ"), ("°F", "ડિગ્રી ફેરનહીટ"), ("°", "ડિગ્રી"),
227
+ ]
228
+ # single-letter / SI units: only after "<number><space>" and before a word end, to avoid eating ordinary letters
229
+ _SI_UNITS = [("Pa", "પાસ્કલ"), ("Hz", "હર્ટ્ઝ"), ("A", "એમ્પિયર"), ("W", "વોટ"), ("V", "વોલ્ટ"),
230
+ ("N", "ન્યૂટન"), ("s", "સેકન્ડ"), ("m", "મીટર")]
231
+
232
+
233
+ def units(t):
234
+ for u, w in _UNITS:
235
+ t = re.sub(rf"(?<=\d)\s*{re.escape(u)}(?![A-Za-z])", " " + w, t)
236
+ for u, w in _SI_UNITS:
237
+ t = re.sub(rf"(?<=\d) ?{u}(?=[\s.,;:!?)]|$)", " " + w, t)
238
+ return t
239
+
240
+
241
+ def sci_notation(t):
242
+ """3 x 10^8 -> ત્રણ ગુણ્યા દસની ઘાત આઠ"""
243
+ return re.sub(r"(?<![\d.])(\d+(?:\.\d+)?)\s*[x×]\s*10\^(-?\d+)",
244
+ lambda m: f"{m[1]} ગુણ્યા દસની ઘાત {'માઇનસ ' if m[2].startswith('-') else ''}{m[2].lstrip('-')}", t)
245
+
246
+
247
+ def slashes(t):
248
+ """7/12 (Gujarat land record) -> સાત બાર; other N/M that is not a date -> N બટા M"""
249
+ t = re.sub(r"(?<![\d/])7/12(?![\d/])", "સાત બાર", t)
250
+ return re.sub(r"(?<![\d/])(\d+)/(\d+)(?![\d/])", lambda m: f"{m[1]} બટા {m[2]}", t)
251
+
252
+
253
+ # ---------------- 10. ranges ----------------
254
+ _DAYS = "સોમ|મંગળ|બુધ|ગુરુ|શુક્ર|શનિ|રવિ"
255
+
256
+
257
+ def ranges(t):
258
+ def rng(m):
259
+ a, b = int(m[1]), int(m[2])
260
+ if a >= b:
261
+ return m[0]
262
+ return f"{num_words(a)}થી {num_words(b)}"
263
+ t = re.sub(rf"(?<![A-Za-z\-\d.,/:])(\d+)\s*[-–]\s*(\d+)(?![\d.,]|\s*[-–/]\s*\d)", rng, t)
264
+ t = re.sub(rf"({_DAYS})(વાર)?\s*[-–]\s*({_DAYS})(વાર)?", lambda m: f"{m[1]}{m[2] or ''}થી {m[3]}{m[4] or ''}", t)
265
+ t = re.sub(r"(?<![\w\d])-(?=\d)", "માઇનસ ", t)
266
+ return t
267
+
268
+
269
+ # ---------------- 11. ordinals ----------------
270
+ _ORD = re.compile(rf"{NS}(\d+)(લ|જ|થ|ઠ્ઠ|મ)(ું|ો|ા|ી|ે|ુ)(?!ં)")
271
+
272
+
273
+ def ordinals(t):
274
+ def ordf(m):
275
+ n, ending = int(m[1]), m[3]
276
+ if m[2] == "થ" and ending == "ી" and re.match(r"\s*\d", t[m.end():]):
277
+ return f"{num_words(n)}થી" # ablative "from n" (e.g. 10થી 15)
278
+ if m[2] == "મ" and n in (1, 2, 3, 4, 6): # "1મી" style is unusual; still say ordinal
279
+ pass
280
+ return ordinal_words(n, ending)
281
+ return _ORD.sub(ordf, t)
282
+
283
+
284
+ # ---------------- 12. decimals ----------------
285
+ def decimals(t):
286
+ return re.sub(rf"{NS}(\d+)\.(\d+){NE}", lambda m: fraction_words(int(m[1]), m[2]), t)
287
+
288
+
289
+ # ---------------- 13. integers ----------------
290
+ def integers(t):
291
+ def intf(m):
292
+ s = m[0]
293
+ if "," in s:
294
+ return num_words(int(s.replace(",", "")))
295
+ after = t[m.end():m.end() + 12]
296
+ if len(s) > 1 and s[0] == "0":
297
+ return digit_words(s)
298
+ if len(s) >= 8 and not re.match(r"\s*(રૂપિયા|ડોલર|લોકો)", after):
299
+ return digit_words(s) # long bare numbers are IDs; 6–7 digits stay cardinal (101325 Pa)
300
+ n = int(s)
301
+ if 1100 <= n <= 1999:
302
+ return year_words(n)
303
+ return num_words(n)
304
+ return re.sub(rf"{NS}(\d{{1,3}}(?:,\d{{2,3}})+|\d+){NE}", intf, t)
305
+
306
+
307
+ # ---------------- protect alphanumeric IDs (GJ01AB1234, SBIN0001234, SR-2026-00451, #A7B9) ----------------
308
+ # Bulbul already reads these letter-by-letter; we only make sure no number pass touches them.
309
+ # also chemistry names / formulas: 1,3-butadiene, Si(CH3)4, C6H12O6, B12
310
+ _ALNUM = re.compile(r"(?<![A-Za-z0-9])#?(?=[A-Za-z0-9\-,()]*[A-Za-z])(?=[A-Za-z0-9\-,()]*\d)"
311
+ r"[A-Za-z0-9()]+(?:[\-,/][A-Za-z0-9()]+)*")
312
+ _PH_BASE = 0xE100
313
+ _local = threading.local() # voice agents call this from many threads
314
+
315
+
316
+ def glue_ampm(t):
317
+ return re.sub(r"(?<=\d)(?=(?:AM|PM|am|pm)(?![A-Za-z]))", " ", t)
318
+
319
+
320
+ def protect(t):
321
+ _local.store = store = []
322
+
323
+ def keep(m):
324
+ store.append(m[0])
325
+ return chr(_PH_BASE + len(store) - 1)
326
+ return _ALNUM.sub(keep, t)
327
+
328
+
329
+ def restore(t):
330
+ for i, s in enumerate(getattr(_local, "store", [])):
331
+ t = t.replace(chr(_PH_BASE + i), s)
332
+ return t
333
+
334
+
335
+ def cleanup(t):
336
+ t = re.sub(r"[ \t]{2,}", " ", t)
337
+ t = re.sub(r"\s+([.,!?])", r"\1", t)
338
+ return t.strip()
339
+
340
+
341
+ PASSES = [gujarati_digits, units, glue_ampm, protect, abbreviations, dates, simple_fractions, times, ids,
342
+ sci_notation, math_symbols, money, percent, slashes, ranges, ordinals, decimals, integers,
343
+ restore, cleanup]
gujnorm/sarvam.py ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- coding: utf-8 -*-
2
+ """Drop-in helper: normalize Gujarati text, then call Sarvam Bulbul.
3
+
4
+ from sarvamai import SarvamAI
5
+ from gujnorm.sarvam import speak
6
+ audio_b64_chunks = speak(SarvamAI(api_subscription_key=...), "તમારો હપ્તો ₹4,350 તા. 05/12/2026 ના રોજ છે.")
7
+ """
8
+ from . import normalize
9
+
10
+
11
+ def speak(client, text, language_code="gu-IN", **tts_kwargs):
12
+ """Like client.text_to_speech.convert, but runs gujnorm first for gu-IN text."""
13
+ if language_code == "gu-IN":
14
+ text = normalize(text)
15
+ tts_kwargs.setdefault("model", "bulbul:v3")
16
+ return client.text_to_speech.convert(text=text, language_code=language_code, **tts_kwargs).audios
17
+
18
+
19
+ def wrap_tts_text(fn):
20
+ """Decorator for voice-agent pipelines (LiveKit / Pipecat): normalize the text an LLM emits before TTS.
21
+
22
+ @wrap_tts_text
23
+ async def tts_node(text): ...
24
+ """
25
+ import functools, inspect
26
+
27
+ if inspect.iscoroutinefunction(fn):
28
+ @functools.wraps(fn)
29
+ async def aw(text, *a, **k):
30
+ return await fn(normalize(text), *a, **k)
31
+ return aw
32
+
33
+ @functools.wraps(fn)
34
+ def w(text, *a, **k):
35
+ return fn(normalize(text), *a, **k)
36
+ return w
index.html CHANGED
@@ -1,19 +1,129 @@
1
  <!doctype html>
2
- <html>
3
- <head>
4
- <meta charset="utf-8" />
5
- <meta name="viewport" content="width=device-width" />
6
- <title>My static Space</title>
7
- <link rel="stylesheet" href="style.css" />
8
- </head>
9
- <body>
10
- <div class="card">
11
- <h1>Welcome to your static Space!</h1>
12
- <p>You can modify this app directly by editing <i>index.html</i> in the Files and versions tab.</p>
13
- <p>
14
- Also don't forget to check the
15
- <a href="https://huggingface.co/docs/hub/spaces" target="_blank">Spaces documentation</a>.
16
- </p>
17
- </div>
18
- </body>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
  </html>
 
1
  <!doctype html>
2
+ <html lang="gu">
3
+ <head>
4
+ <meta charset="utf-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1">
6
+ <title>gujnorm</title>
7
+ <style>
8
+ :root { --bg:#fafaf9; --card:#ffffff; --ink:#1c1917; --muted:#57534e; --line:#e7e5e4; --accent:#4338ca; --accent-ink:#ffffff; --chip:#f5f5f4; }
9
+ @media (prefers-color-scheme: dark) { :root { --bg:#141414; --card:#1e1e1e; --ink:#f5f5f4; --muted:#a8a29e; --line:#333; --accent:#818cf8; --accent-ink:#111; --chip:#262626; } }
10
+ * { box-sizing:border-box; }
11
+ body { margin:0; background:var(--bg); color:var(--ink); font:16px/1.55 system-ui, "Noto Sans Gujarati", sans-serif; }
12
+ main { max-width:880px; margin:0 auto; padding:32px 16px 48px; }
13
+ h1 { font-size:1.6rem; margin:0 0 4px; }
14
+ .sub { color:var(--muted); margin:0 0 20px; }
15
+ .card { background:var(--card); border:1px solid var(--line); border-radius:12px; padding:16px; }
16
+ label { display:block; font-weight:600; font-size:.9rem; margin:0 0 6px; }
17
+ textarea { width:100%; min-height:96px; padding:10px 12px; border:1px solid var(--line); border-radius:8px; background:var(--bg); color:var(--ink); font:1.05rem/1.6 "Noto Sans Gujarati", system-ui, sans-serif; resize:vertical; }
18
+ .out { min-height:96px; padding:10px 12px; border:1px solid var(--line); border-radius:8px; background:var(--chip); font-size:1.05rem; white-space:pre-wrap; }
19
+ .grid { display:grid; grid-template-columns:1fr 1fr; gap:16px; }
20
+ @media (max-width:640px) { .grid { grid-template-columns:1fr; } }
21
+ .status { color:var(--muted); font-size:.85rem; margin-top:8px; min-height:1.2em; }
22
+ h2 { font-size:1rem; margin:24px 0 8px; }
23
+ .chips { display:flex; flex-wrap:wrap; gap:8px; }
24
+ .chips button { border:1px solid var(--line); background:var(--chip); color:var(--ink); border-radius:999px; padding:6px 12px; font:inherit; font-size:.9rem; cursor:pointer; }
25
+ .chips button:hover { border-color:var(--accent); }
26
+ table { width:100%; border-collapse:collapse; font-size:.92rem; }
27
+ th, td { text-align:left; padding:8px; border-bottom:1px solid var(--line); vertical-align:top; }
28
+ th { color:var(--muted); font-weight:600; }
29
+ .wrap { overflow-x:auto; }
30
+ a { color:var(--accent); }
31
+ footer { color:var(--muted); font-size:.85rem; margin-top:28px; }
32
+ code { background:var(--chip); padding:1px 5px; border-radius:4px; }
33
+ </style>
34
+ </head>
35
+ <body>
36
+ <main>
37
+ <h1>gujnorm</h1>
38
+ <p class="sub">Written Gujarati → the words a Gujarati speaker would say, so any TTS voice reads times, ₹ amounts,
39
+ fractions, dates, ranges and ordinals correctly. On Sarvam Bulbul it cut misread sentences from 59/55/56 to 0/2/1
40
+ (of 108) on three voices.</p>
41
+
42
+ <div class="card">
43
+ <div class="grid">
44
+ <div>
45
+ <label for="inp">Written Gujarati</label>
46
+ <textarea id="inp">તમારો આગલો હપ્તો ₹4,350 તા. 05/12/2026 ના રોજ ભરવાનો છે.</textarea>
47
+ </div>
48
+ <div>
49
+ <label>Spoken form (send this to TTS)</label>
50
+ <div class="out" id="out" aria-live="polite"></div>
51
+ </div>
52
+ </div>
53
+ <div class="status" id="status">Loading Python in your browser…</div>
54
+ </div>
55
+
56
+ <h2>Try an example</h2>
57
+ <div class="chips" id="chips"></div>
58
+
59
+ <h2>What TTS does without it</h2>
60
+ <div class="wrap"><table>
61
+ <tr><th>Written</th><th>Sarvam Bulbul said</th><th>With gujnorm</th></tr>
62
+ <tr><td>સવારે 9:30 વાગ્યે</td><td>નવ વાગ્યે ત્રીસ મિનિટ વાગ્યે</td><td>સાડા નવ વાગ્યે</td></tr>
63
+ <tr><td>લોન ₹1.5 લાખની</td><td>એક દશાંશ પાંચ લાખ રૂપીઝની</td><td>દોઢ લાખ રૂપિયાની</td></tr>
64
+ <tr><td>10-15 દિવસ</td><td>એક શૂન્ય એક પાંચ દિવસ</td><td>દસથી પંદર દિવસ</td></tr>
65
+ <tr><td>1લા નંબરે</td><td>એકલા ("alone") નંબરે</td><td>પહેલા નંબરે</td></tr>
66
+ <tr><td>8.5%</td><td>આઠ દશાંશ પાંચ પર્સેન્ટ</td><td>સાડા આઠ ટકા</td></tr>
67
+ </table></div>
68
+
69
+ <footer>
70
+ Runs entirely in your browser (Pyodide). Nothing you type is sent anywhere.<br>
71
+ <a href="https://github.com/Zephyr4772/gujnorm">Code and tests</a> ·
72
+ <a href="https://huggingface.co/datasets/sinhal/gujarati-tts-number-reading">Dataset and results</a> ·
73
+ <code>pip install git+https://github.com/Zephyr4772/gujnorm</code>
74
+ </footer>
75
+ </main>
76
+
77
+ <script src="https://cdn.jsdelivr.net/pyodide/v0.26.4/full/pyodide.js"></script>
78
+ <script>
79
+ const EXAMPLES = [
80
+ "સવારે 9:30 વાગ્યે આવજો.",
81
+ "લોન ₹1.5 લાખની છે.",
82
+ "તમારું બેલેન્સ ₹99.50 છે.",
83
+ "10-15 દિવસ લાગશે.",
84
+ "તે 1લા નંબરે આવ્યો.",
85
+ "વ્યાજ દર 8.5% છે.",
86
+ "બાકી રકમ ₹2,37,450 છે અને વ્યાજ દર 10.75% છે.",
87
+ "અમારી શાખા સવારે 9:30 થી સાંજે 4:30 સુધી ખુલ્લી રહે છે.",
88
+ "તમારો OTP 482913 છે. મોબાઇલ નંબર 98765 43210.",
89
+ "આઝાદી 15 ઓગસ્ટ 1947 એ મળી.",
90
+ "દવા 2.5 ml દિવસમાં 3 વાર લેવી.",
91
+ ];
92
+ const inp = document.getElementById("inp"), out = document.getElementById("out"), status = document.getElementById("status");
93
+ const chips = document.getElementById("chips");
94
+ let normalize = null;
95
+
96
+ for (const ex of EXAMPLES) {
97
+ const b = document.createElement("button");
98
+ b.textContent = ex;
99
+ b.onclick = () => { inp.value = ex; run(); };
100
+ chips.appendChild(b);
101
+ }
102
+
103
+ function run() {
104
+ if (!normalize) return;
105
+ try { out.textContent = normalize(inp.value); }
106
+ catch (e) { out.textContent = ""; status.textContent = "Error: " + e.message; }
107
+ }
108
+ inp.addEventListener("input", run);
109
+
110
+ (async () => {
111
+ try {
112
+ const py = await loadPyodide();
113
+ py.FS.mkdir("gujnorm");
114
+ for (const f of ["__init__.py", "numbers.py", "rules.py"]) {
115
+ const r = await fetch("gujnorm/" + f);
116
+ if (!r.ok) throw new Error("could not load gujnorm/" + f);
117
+ py.FS.writeFile("gujnorm/" + f, await r.text());
118
+ }
119
+ const mod = py.pyimport("gujnorm");
120
+ normalize = (t) => mod.normalize(t);
121
+ status.textContent = "Ready · gujnorm " + mod.__version__ + " · updates as you type";
122
+ run();
123
+ } catch (e) {
124
+ status.textContent = "Could not load: " + e.message;
125
+ }
126
+ })();
127
+ </script>
128
+ </body>
129
  </html>