Spaces:
Running
Running
G2P: generic foreign-word reader and Đ look-alikes
Browse filesFiles changed (2)
- g2p.js+142-5
- index.html+1-1
122345112 unmodified lines11811912012112112212312412512612312712812913013139 unmodified lines1711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003014 unmodified lines306307308179309310311312181313314315316317318319320321322323324325326// Deterministic rule-based Northern (Hanoi) Vietnamese text normalizer, G2P, and chunker.// Bit-for-bit mirror of src/tiny_voca_vi/g2p.py (~10 KB gzipped, zero binary dictionary).// Bit-for-bit mirror of src/tiny_voca_vi/g2p.py (~12 KB gzipped, zero binary dictionary).// Works in classic Web Workers (self.TinyVocaG2P), browser window, and Node.
(function (root) {112 unmodified lines const UNITS = {"km/h": "ki lô mét trên giờ", "kmh": "ki lô mét trên giờ", "km²": "ki lô mét vuông", "km2": "ki lô mét vuông", "m²": "mét vuông", "m2": "mét vuông", "cm²": "xen ti mét vuông", "cm2": "xen ti mét vuông", "m³": "mét khối", "m3": "mét khối", "cm³": "xen ti mét khối", "cm3": "xen ti mét khối", "km": "ki lô mét", "m": "mét", "dm": "đề xi mét", "cm": "xen ti mét", "mm": "mi li mét", "nm": "na nô mét", "kg": "ki lô gam", "g": "gam", "mg": "mi li gam", "l": "lít", "ml": "mi li lít", "mL": "mi li lít", "ha": "héc ta", "s": "giây", "h": "giờ", "°C": "độ xê", "°F": "độ ép", "ºC": "độ xê", "°": "độ", "kW": "ki lô oát", "MW": "mê ga oát", "GW": "gi ga oát", "kWh": "ki lô oát giờ", "MWh": "mê ga oát giờ", "W": "oát", "V": "vôn", "Hz": "héc", "kHz": "ki lô héc", "MHz": "mê ga héc", "GHz": "gi ga héc", "KB": "ki lô bai", "MB": "mê ga bai", "GB": "gi ga bai", "TB": "tê ra bai", "Mbps": "mê ga bít trên giây", "Gbps": "gi ga bít trên giây", "kcal": "ki lô ca lo", "cal": "ca lo", "oz": "ao xơ", "lb": "pao", "lbs": "pao", "ft": "phít", "mph": "dặm trên giờ"}; const PER_WORDS = ["lượng", "chỉ", "thùng", "giờ", "phút", "giây", "ngày", "tuần", "tháng", "quý", "năm", "người", "lít", "tấn", "tạ", "kg", "cổ phiếu", "suất", "căn", "lần", "học sinh", "đêm", "vé", "chiếc"]; const ABBREV = {"TP": "thành phố", "Tp": "thành phố", "TPHCM": "thành phố Hồ Chí Minh", "HCM": "Hồ Chí Minh", "HN": "Hà Nội", "VN": "Việt Nam", "TW": "trung ương", "TƯ": "trung ương", "UBND": "ủy ban nhân dân", "HĐND": "hội đồng nhân dân", "MTTQ": "mặt trận tổ quốc", "UBTVQH": "ủy ban thường vụ quốc hội", "CSGT": "cảnh sát giao thông", "PCCC": "phòng cháy chữa cháy", "CHXHCN": "cộng hòa xã hội chủ nghĩa", "GS": "giáo sư", "PGS": "phó giáo sư", "TS": "tiến sĩ", "ThS": "thạc sĩ", "BS": "bác sĩ", "KTS": "kiến trúc sư", "LS": "luật sư", "NSND": "nghệ sĩ nhân dân", "NSƯT": "nghệ sĩ ưu tú", "ĐBQH": "đại biểu quốc hội", "BHXH": "bảo hiểm xã hội", "BHYT": "bảo hiểm y tế", "BHTN": "bảo hiểm thất nghiệp", "GTGT": "giá trị gia tăng", "TNCN": "thu nhập cá nhân", "TNHH": "trách nhiệm hữu hạn", "CTCP": "công ty cổ phần", "DN": "doanh nghiệp", "HTX": "hợp tác xã", "BĐS": "bất động sản", "NHNN": "ngân hàng nhà nước", "TTCK": "thị trường chứng khoán", "KCN": "khu công nghiệp", "THPT": "trung học phổ thông", "THCS": "trung học cơ sở", "ĐH": "đại học", "CĐ": "cao đẳng", "CLB": "câu lạc bộ", "HLV": "huấn luyện viên", "HCV": "huy chương vàng", "HCB": "huy chương bạc", "HCĐ": "huy chương đồng", "SVĐ": "sân vận động", "ĐT": "điện thoại", "SĐT": "số điện thoại", "CMND": "chứng minh nhân dân", "CCCD": "căn cước công dân", "GPLX": "giấy phép lái xe", "KTX": "ký túc xá", "GD": "giáo dục", "BV": "bệnh viện", "ATTP": "an toàn thực phẩm", "ATGT": "an toàn giao thông", "QL": "quốc lộ", "TNGT": "tai nạn giao thông", "NXB": "nhà xuất bản", "BTV": "biên tập viên", "PV": "phóng viên", "MC": "em xi", "NLĐ": "người lao động", "AI": "ây ai", "IT": "ai ti", "CEO": "xi i âu", "CFO": "xi ép âu", "GDP": "gi đi pi", "GNP": "gi en pi", "CPI": "xi pi ai", "FDI": "ép đi ai", "ODA": "âu đi ây", "IPO": "ai pi âu", "ETF": "i ti ép", "FED": "phét", "Fed": "phét", "IMF": "ai em ép", "WB": "ngân hàng thế giới", "WTO": "đáp liu ti âu", "WHO": "đáp liu ếch âu", "UN": "liên hợp quốc", "EU": "i iu", "US": "iu ét", "USA": "iu ét ây", "UK": "iu cây", "UAE": "iu ây i", "UAV": "iu ây vi", "NATO": "na tô", "ASEAN": "a xê an", "UNESCO": "u nét cô", "UNICEF": "iu ni xép", "FIFA": "phi pha", "UEFA": "u ê pha", "AFF": "ây ép ép", "AFC": "ây ép xi", "VAR": "va", "NASA": "na xa", "OPEC": "ô pếch", "APEC": "a péc", "CIA": "xi ai ây", "FBI": "ép bi ai", "PR": "pi a", "VIP": "víp", "SMS": "ét em ét", "PIN": "pin", "SIM": "xim", "ATM": "ây ti em", "OTP": "âu ti pi", "QR": "quy rờ", "GPS": "gi pi ét", "USB": "iu ét bi", "PC": "pi xi", "TV": "ti vi", "LED": "lét", "CPU": "xi pi iu", "GPU": "gi pi iu", "RAM": "ram", "URL": "iu a eo", "PDF": "pi đi ép", "CD": "xi đi", "DVD": "đi vi đi", "ID": "ai đi", "TOEIC": "tô ích", "VAT": "vát", "ISO": "i sô", "DNA": "đi en ây", "ADN": "a đê en", "HIV": "hát i vê", "AIDS": "ết", "COVID": "cô vít", "Covid": "cô vít", "VTV": "vê tê vê", "VOV": "vê ô vê", "KOL": "cây âu eo", "ESG": "i ét gi", "SUV": "ét iu vi", "SJC": "ét gi xi", "PNJ": "pi en gi", "DOJI": "đô gi", "VIB": "vê i bê", "FPT": "ép pê tê", "HSBC": "hát ét bê xê", "km": "ki lô mét", "kg": "ki lô gam", "cm": "xen ti mét", "mm": "mi li mét", "ml": "mi li lít", "kWh": "ki lô oát giờ"}; const ENGLISH = {"ounce": "ao xơ", "ounces": "ao xơ", "pound": "pao", "pounds": "pao", "inch": "in", "inches": "in", "feet": "phít", "foot": "phít", "mile": "mai", "miles": "mai", "gallon": "ga lông", "barrel": "ba reo", "yard": "da", "euro": "ơ rô", "euros": "ơ rô", "dollar": "đô la", "dollars": "đô la", "cent": "xen", "cents": "xen", "yen": "yên", "won": "uôn", "bitcoin": "bít coi", "bitcoins": "bít coi", "ethereum": "ê thơ ri âm", "crypto": "cờ ríp tô", "blockchain": "bờ lóc chen", "token": "tô cần", "online": "on lai", "offline": "óp lai", "website": "oép sai", "web": "oép", "wifi": "oai phai", "gmail": "gi meo", "clip": "cờ líp", "livestream": "lai sờ trim", "stream": "sờ trim", "podcast": "pót cát", "blog": "bờ lóc", "smartphone": "sờ mát phôn", "laptop": "láp tóp", "computer": "com piu tơ", "tablet": "táp lét", "chip": "chíp", "robot": "rô bốt", "app": "áp", "software": "sóp oe", "server": "sơ vơ", "cloud": "cờ lao", "data": "đa ta", "startup": "sờ tát ấp", "marketing": "ma két tinh", "sale": "seo", "sales": "seo", "shop": "sóp", "shopping": "sóp ping", "show": "sâu", "showroom": "sâu rum", "studio": "sờ tu đi ô", "casino": "ca xi nô", "resort": "ri sót", "homestay": "hôm sờ tây", "hotel": "hô teo", "tour": "tua", "visa": "vi da", "taxi": "tắc xi", "container": "công ten nơ", "logistics": "lô gít tích", "golf": "gôn", "tennis": "ten nít", "marathon": "ma ra tông", "game": "gêm", "games": "gêm", "gamer": "gêm mơ", "fan": "phan", "fans": "phan", "hot": "hót", "trend": "tren", "style": "sờ tai", "stress": "sờ trét", "vaccine": "vắc xin", "virus": "vi rút", "vitamin": "vi ta min", "protein": "pờ rô tê in", "calo": "ca lo", "cafe": "cà phê", "pizza": "pi da", "hamburger": "hăm bơ gơ", "cocktail": "cốc teo", "album": "an bum", "radio": "ra đi ô", "list": "lít", "top": "tóp", "file": "phai", "link": "linh", "click": "cờ lích", "chat": "chát", "check": "chéc", "test": "tét", "team": "tim", "leader": "li đơ", "manager": "me ni gờ", "deadline": "đét lai", "meeting": "mít tinh", "okay": "âu kê", "ok": "âu kê", "hello": "hê lô", "bye": "bai", "photo": "phô tô", "photocopy": "phô tô cóp pi", "facebook": "phây búc", "google": "gu gồ", "youtube": "diu túp", "tiktok": "tích tóc", "zalo": "da lô", "twitter": "tuýt tơ", "instagram": "in sờ ta gam", "microsoft": "mai cờ rô sóp", "apple": "áp pồ", "iphone": "ai phôn", "ipad": "ai pát", "android": "an đờ roi", "samsung": "sam sung", "windows": "uyn đâu", "tesla": "tét la", "amazon": "a ma dôn", "netflix": "nét phờ lích", "chatgpt": "chát gi pi ti", "messenger": "mét sen gơ", "telegram": "te lê gam", "shopee": "sóp pi", "grab": "gờ ráp", "honda": "hon đa", "toyota": "tô ô ta", "vinfast": "vin phát", "vingroup": "vin gờ rúp", "vietcombank": "việt com banh", "techcombank": "tếch com banh", "singapore": "sinh ga po", "malaysia": "ma lai xi a", "indonesia": "in đô nê xi a", "campuchia": "cam pu chia", "philippines": "phi líp pin", "australia": "ô xờ trây li a", "canada": "ca na đa", "brazil": "bờ ra xin", "argentina": "ác hen ti na", "ukraine": "u cờ rai na", "israel": "ít xa ra en", "iran": "i ran", "washington": "oa sinh tơn", "london": "luân đôn", "paris": "pa ri", "tokyo": "tô ki ô", "new": "niu", "york": "óc", "trump": "trăm", "donald": "đô nan", "biden": "bai đừn", "putin": "pu tin"}; const ENGLISH = {"ounce": "ao xơ", "ounces": "ao xơ", "pound": "pao", "pounds": "pao", "inch": "in", "inches": "in", "feet": "phít", "foot": "phít", "mile": "mai", "miles": "mai", "gallon": "ga lông", "barrel": "ba reo", "yard": "da", "euro": "ơ rô", "euros": "ơ rô", "dollar": "đô la", "dollars": "đô la", "cent": "xen", "cents": "xen", "yen": "yên", "won": "uôn", "bitcoin": "bít coi", "bitcoins": "bít coi", "ethereum": "ê thơ ri âm", "crypto": "cờ ríp tô", "blockchain": "bờ lóc chen", "token": "tô cần", "online": "on lai", "offline": "óp lai", "website": "oép sai", "web": "oép", "wifi": "oai phai", "gmail": "gi meo", "clip": "cờ líp", "livestream": "lai sờ trim", "stream": "sờ trim", "podcast": "pót cát", "blog": "bờ lóc", "smartphone": "sờ mát phôn", "laptop": "láp tóp", "computer": "com piu tơ", "tablet": "táp lét", "chip": "chíp", "robot": "rô bốt", "app": "áp", "software": "sóp oe", "server": "sơ vơ", "cloud": "cờ lao", "data": "đa ta", "startup": "sờ tát ấp", "marketing": "ma két tinh", "sale": "seo", "sales": "seo", "shop": "sóp", "shopping": "sóp ping", "show": "sâu", "showroom": "sâu rum", "studio": "sờ tu đi ô", "casino": "ca xi nô", "resort": "ri sót", "homestay": "hôm sờ tây", "hotel": "hô teo", "tour": "tua", "visa": "vi da", "taxi": "tắc xi", "container": "công ten nơ", "logistics": "lô gít tích", "golf": "gôn", "tennis": "ten nít", "marathon": "ma ra tông", "game": "gêm", "games": "gêm", "gamer": "gêm mơ", "fan": "phan", "fans": "phan", "hot": "hót", "trend": "tren", "style": "sờ tai", "stress": "sờ trét", "vaccine": "vắc xin", "virus": "vi rút", "vitamin": "vi ta min", "protein": "pờ rô tê in", "calo": "ca lo", "cafe": "cà phê", "pizza": "pi da", "hamburger": "hăm bơ gơ", "cocktail": "cốc teo", "album": "an bum", "radio": "ra đi ô", "list": "lít", "top": "tóp", "file": "phai", "link": "linh", "click": "cờ lích", "chat": "chát", "check": "chéc", "test": "tét", "team": "tim", "leader": "li đơ", "manager": "me ni gờ", "deadline": "đét lai", "meeting": "mít tinh", "okay": "âu kê", "ok": "âu kê", "mr": "mít tơ", "mrs": "mít xít", "dr": "đốc tơ", "mc": "mác", "st": "xanh", "hello": "hê lô", "bye": "bai", "photo": "phô tô", "photocopy": "phô tô cóp pi", "facebook": "phây búc", "google": "gu gồ", "youtube": "diu túp", "tiktok": "tích tóc", "zalo": "da lô", "twitter": "tuýt tơ", "instagram": "in sờ ta gam", "microsoft": "mai cờ rô sóp", "apple": "áp pồ", "iphone": "ai phôn", "ipad": "ai pát", "android": "an đờ roi", "samsung": "sam sung", "windows": "uyn đâu", "tesla": "tét la", "amazon": "a ma dôn", "netflix": "nét phờ lích", "chatgpt": "chát gi pi ti", "messenger": "mét sen gơ", "telegram": "te lê gam", "shopee": "sóp pi", "grab": "gờ ráp", "honda": "hon đa", "toyota": "tô ô ta", "vinfast": "vin phát", "vingroup": "vin gờ rúp", "vietcombank": "việt com banh", "techcombank": "tếch com banh", "singapore": "sinh ga po", "malaysia": "ma lai xi a", "indonesia": "in đô nê xi a", "campuchia": "cam pu chia", "philippines": "phi líp pin", "australia": "ô xờ trây li a", "canada": "ca na đa", "brazil": "bờ ra xin", "argentina": "ác hen ti na", "ukraine": "u cờ rai na", "israel": "ít xa ra en", "iran": "i ran", "washington": "oa sinh tơn", "london": "luân đôn", "paris": "pa ri", "tokyo": "tô ki ô", "new": "niu", "york": "óc", "trump": "trăm", "donald": "đô nan", "biden": "bai đừn", "putin": "pu tin"}; const LETTERS = {"a": "a", "b": "bê", "c": "xê", "d": "đê", "đ": "đê", "e": "e", "f": "ép", "g": "giê", "h": "hát", "i": "i", "j": "gi", "k": "ca", "l": "lờ", "m": "mờ", "n": "nờ", "o": "ô", "p": "pê", "q": "quy", "r": "rờ", "s": "ét", "t": "tê", "u": "u", "ư": "ư", "v": "vê", "w": "vê kép", "x": "ích", "y": "i", "z": "dét", "ă": "á", "â": "ớ", "ê": "ê", "ô": "ô", "ơ": "ơ"}; const F_VOWELS = {"a": "a", "ă": "ă", "â": "â", "e": "ê", "ê": "ê", "i": "i", "o": "ô", "ô": "ô", "ơ": "ơ", "u": "u", "ư": "ư", "y": "i", "ai": "ai", "ay": "ai", "au": "au", "aw": "o", "ao": "ao", "aa": "a", "ei": "ây", "ey": "ây", "ee": "i", "eu": "êu", "ew": "iu", "oi": "ôi", "oy": "ôi", "oo": "u", "ou": "u", "ow": "âu", "ui": "ui", "uu": "u", "ii": "i", "ie": "i", "ea": "i"}; const F_CLOSED = {"ê": "e"}; const F_ONSET = {"tch": "ch", "sch": "s", "tsch": "ch", "gi": "d", "ch": "ch", "sh": "s", "th": "th", "ph": "ph", "kh": "kh", "gh": "gh", "ng": "ng", "nh": "nh", "tr": "tr", "ck": "k", "qu": "qu", "wh": "w", "zh": "d", "dh": "đ", "bh": "b", "rh": "r", "ts": "x", "tz": "x", "cz": "x", "sz": "x", "b": "b", "c": "k", "ç": "x", "d": "đ", "đ": "đ", "f": "ph", "g": "g", "h": "h", "j": "d", "k": "k", "l": "l", "m": "m", "n": "n", "p": "p", "q": "k", "r": "r", "s": "s", "t": "t", "v": "v", "w": "w", "x": "x", "y": "y", "z": "d"}; const F_CODA = {"m": "m", "n": "n", "ng": "ng", "nh": "nh", "p": "p", "b": "p", "f": "p", "v": "p", "ph": "p", "bh": "p", "t": "t", "d": "t", "đ": "t", "dh": "t", "s": "t", "z": "t", "th": "t", "sh": "t", "ch": "t", "tch": "t", "sch": "t", "tsch": "t", "ts": "t", "tz": "t", "cz": "t", "sz": "t", "zh": "t", "j": "t", "ç": "t", "gi": "t", "c": "c", "k": "c", "ck": "c", "g": "c", "gh": "c", "kh": "c", "q": "c", "x": "c", "l": "n", "r": "c"}; const SYLLABLE_RE = new RegExp("^(?:ngh|ng|nh|ch|tr|th|ph|kh|gh|gi|qu|[bcdđghklmnprstvx])?(?:(?:uyê|iê|yê|ươ|uô|oa|oă|oe|uâ|uê|uơ|uy|[aăâeêioôơuưy])(?:ng|nh|ch|[mnptc])?|iêu|yêu|ươi|ươu|uôi|oai|oay|oao|oeo|uây|uya|uyu|ai|ay|ây|oi|ôi|ơi|ui|ưi|ao|au|âu|eo|êu|iu|ưu|ia|ua|ưa|uê|uơ)$"); const SYLLABLE_RE = new RegExp("^(?:ngh|ng|nh|ch|tr|th|ph|kh|gh|gi|qu|[bcdđghklmnprstvx])?(?:(?:uyê|iê|yê|ươ|uô|oa|oă|oe|uâ|uê|uơ|uy|[aăâeêioôơuưy])(?:ng|nh|ch|[mnptc])?|iêu|yêu|ươi|ươu|uôi|oai|oay|oao|oeo|uây|uya|uyu|ai|ay|ây|oi|ôi|ơi|ui|ưi|ao|au|âu|eo|êu|iu|ưu|ia|ua|ưa|uê|uơ|oo(?:ng|c))$"); const F_SILENT_E = new RegExp("(?:c|g|v|ll|tl|ak|it|id|in|il|ir|ur|ar|iz|as|am|yp|at)e$"); // </lexicon> const LT = "A-Za-zÀ-ỹ"; const alt = (keys) => [...keys].sort((a, b) => b.length - a.length || (a < b ? -1 : 1)).map((k) => k.replace(/[.*+?^${}()|[\]\\\/]/g, "\\$&")).join("|");39 unmodified lines return s.replace(TITLE, (_, t) => ABBREV[t] + " "); }
const F_BASE = { ø: "o", æ: "e", œ: "e", ß: "s", ł: "l", ð: "đ", þ: "t", ı: "i" }; const F_V = "aăâeêioôơuưy"; const TONE_MARK = ["", "", "̀", "́", "̉", "̃", "̣"]; const isV = (c) => c !== undefined && c !== "" && F_V.includes(c);
function addTone(syl, tone) { if (tone < 2) return syl; let vs = [...syl].map((ch, i) => (isV(ch) ? i : -1)).filter((i) => i >= 0); if (syl.startsWith("qu") && vs.length > 1) vs = vs.slice(1); let i = vs[0]; for (const j of vs) if ("ăâêôơư".includes(syl[j])) i = j; if (vs.length > 1 && "ou".includes(syl[vs[0]]) && "aey".includes(syl[vs[1]]) && i === vs[0]) i = vs[1]; return (syl.slice(0, i + 1) + TONE_MARK[tone] + syl.slice(i + 1)).normalize("NFC"); }
function respellForeign(word) { let chars = []; for (const ch of word.toLowerCase().normalize("NFC")) { let [base, tone] = stripTone(ch); if (!has(F_ONSET, base) && !isV(base)) base = F_BASE[base] || base.normalize("NFD").slice(0, 1); if (isV(base)) chars.push([base, tone]); else if (has(F_ONSET, base)) chars.push([base, 1]); } const plain = chars.map((c) => c[0]).join(""); if (chars.length > 3 && chars[chars.length - 1][1] === 1 && F_SILENT_E.test(plain) && [...plain.slice(0, -1)].some(isV)) chars = chars.slice(0, -1); const src = chars; chars = src.filter((c, i) => !(i && c[0] === src[i - 1][0] && !isV(c[0]))); const text = chars.map((c) => c[0]).join(""); const segs = []; const n = text.length; let i = 0; while (i < n) { const ch = text[i]; if (isV(ch) && !(ch === "y" && i + 1 < n && isV(text[i + 1]) && (!segs.length || segs[segs.length - 1][0] === "V"))) { const two = text.slice(i, i + 2); const ok = two.length === 2 && has(F_VOWELS, two) && !("wy".includes(two[1]) && i + 2 < n && isV(text[i + 2])) && !(two === "ie" && i + 2 < n) && !(two === "ea" && i + 2 === n); const size = ok ? 2 : 1; segs.push(["V", F_VOWELS[text.slice(i, i + size)], Math.max(...chars.slice(i, i + size).map((c) => c[1]))]); i += size; continue; } let unit = ""; for (const size of [4, 3, 2, 1]) { unit = text.slice(i, i + size); if (unit.length === size && has(F_ONSET, unit)) break; } if (unit === "qu" && !(i + 2 < n && isV(text[i + 2]))) unit = "q"; if (unit === "gi" && !(i + 2 < n && isV(text[i + 2]))) unit = "g"; if (unit === "c" && i + 1 < n && "eêiy".includes(text[i + 1])) unit = "ç"; segs.push(["C", unit, 1]); i += unit === "q" || unit === "ç" ? 1 : unit.length; } if (!segs.some((x) => x[0] === "V")) return null; if (segs.filter((x) => x[0] === "V").length > 1) for (const x of segs) x[2] = 1; let clean = true; const syls = []; let pending = []; for (let [kind, val, tone] of segs) { if (kind === "C") { pending.push(val); continue; } const onset = pending.length ? pending.pop() : ""; if (pending.length && syls.length && syls[syls.length - 1][2] === "") { const first = pending.shift(); syls[syls.length - 1][2] = F_CODA[first] ?? ""; if (syls[syls.length - 1][2] === "") clean = false; if (["m", "n", "ng"].includes(syls[syls.length - 1][2]) && onset !== "r" && onset !== "l") { const rest = pending.filter((u) => !["d", "t", "p"].includes(u)); clean = clean && rest.length === pending.length; pending = rest; } } for (const u of pending) { const w = F_ONSET[u]; syls.push(w === "y" ? ["", "i", "", 1] : w === "w" ? ["", "u", "", 1] : [w === "k" ? "c" : w, "ơ", "", 2]); clean = false; } pending = []; let o = onset ? F_ONSET[onset] : ""; if (o === "y") { if (val !== "i") syls.push(["", "i", "", 1]); o = ""; } else if (o === "w") { const glide = ({ a: "oa", ai: "oai", ê: "oe", i: "uy", u: "u", ây: "uây" })[val]; if (glide) { o = ""; val = glide; } else o = "v"; } else if (o === "g" && "eêi".includes(val[0])) o = "gh"; else if (o === "qu" && val[0] === "u") o = "k"; syls.push([o, val, "", tone]); } if (pending.length) { const first = pending.shift(); const last = syls[syls.length - 1]; if (first === "r" && last[1] === "ê") last[1] = "ơ"; else if (first !== "r") { last[2] = F_CODA[first] ?? ""; clean = clean && last[2] !== ""; } if (pending.length) clean = false; } const out = []; for (let [o, v, c, tone] of syls) { if (c) { if (v.length === 1 || v === "oa" || v === "oe" || v === "uy") { v = F_CLOSED[v] ?? v; if ("iy".includes(v[v.length - 1]) || v === "ê") c = c === "ng" ? "nh" : c === "c" ? "ch" : c; else if (c === "nh" && !"ae".includes(v[v.length - 1])) c = "n"; } else { c = ""; clean = false; } } if (["p", "t", "c", "ch"].includes(c) && tone !== 3 && tone !== 6) tone = 3; out.push(addTone(o + v + c, tone)); } return [out.join(" "), clean]; }
function readUnknown(w) { if (isSyllable(w) || w.length < 2) return w; const r = respellForeign(w); if (r) return r[0]; const ls = [...w.toLowerCase()].map((ch) => stripTone(ch)[0]); if (w.length <= 4 && ls.every((c) => has(LETTERS, c))) return ls.map((c) => LETTERS[c]).join(" "); return w; }
function oneWord(w) { return (has(ABBREV, w) && ABBREV[w]) || (has(ENGLISH, w.toLowerCase()) && ENGLISH[w.toLowerCase()]) || readUnknown(w); }
function expandWords(s) { const shouting = !/\p{Ll}/u.test(s); return s.replace(WORD, (w) => {4 unmodified lines } const low = w.toLowerCase(); if (has(ENGLISH, low)) return ENGLISH[low]; if (w === w.toUpperCase() && w !== low && w.length <= 6 && !syll) if (w === w.toUpperCase() && w !== low && w.length <= 6 && !syll) { const r = w.length >= 4 ? respellForeign(w) : null; if (r && r[1]) return r[0]; return [...low].map((ch) => LETTERS[stripTone(ch)[0]] ?? ch).join(" "); return w; } const parts = w.match(/[A-ZĐ]?[^A-ZĐ]+|[A-ZĐ]+(?![^A-ZĐ])/g) ?? []; if (parts.length > 1 && w.length > 4 && parts.join("") === w && parts.every((p) => p.length > 1)) return parts.map(oneWord).join(" "); return readUnknown(w); }); }
function normalizeText(text) { let s = text.normalize("NFC") .replace(/[\u00d0\u0189]/g, "Đ").replace(/[\u00f0\u0256]/g, "đ") .replace(/[\u00ad\u200b-\u200d\u2060\ufeff]/g, "") .replaceAll("…", ".").replaceAll("–", "—") .replaceAll("“", "").replaceAll("”", "").replaceAll('"', ""); s = s.replace(/(ngày\s+)?\b(\d{1,2})\/(\d{1,2})\/(\d{4})\b/gi, (_, p, d, mo, y) =>51 unmodified lines525354555556575851 unmodified lines<body><div class="wrap"> <h1>TinyVoca-Vi <span class="badge">v0.2 · 4.77M params</span> <span class="badge">3 Voices</span> <span class="badge">24 kHz</span> <span class="badge">WASM 1-Thread · 100% Offline</span></h1> <p class="sub">Northern Vietnamese neural TTS (3 female voices in 1 ONNX file, voicing-gated pitch-locked HN-Vocos) running directly in your browser via ONNX Runtime Web + ~10 KB (gzipped) rule-based G2P. No server, no Python.</p> <p class="sub">Northern Vietnamese neural TTS (3 female voices in 1 ONNX file, voicing-gated pitch-locked HN-Vocos) running directly in your browser via ONNX Runtime Web + ~12 KB (gzipped) rule-based G2P. No server, no Python.</p>
<div class="card"> <div class="status-bar">g2p.js+142-5
index.html+1-1