Download code/kana.py from davidguzmanr/VITS-JSUT-Japanese: direct link, hf CLI and curl.
- Browser
- Download file 2.35 kB
-
https://huggingface.co/davidguzmanr/VITS-JSUT-Japanese/resolve/main/code/kana.py
- Command line
-
hf download hf://davidguzmanr/VITS-JSUT-Japanese/code/kana.py
-
curl -L -o kana.py https://huggingface.co/davidguzmanr/VITS-JSUT-Japanese/resolve/main/code/kana.py
2.35 kB
| """Japanese text front end for the character-level VITS: kanji/kana text -> katakana as pronounced (pyopenjtalk), with the | |
| Japanese punctuation mapped to the ASCII marks that train_vits.py keeps as tokens. prepare_jsut.py applies it to the training | |
| transcripts and synthesize.py to the listening-test sentences, so training and synthesis see the same symbols. | |
| Why a front end at all: train_vits.py builds its vocabulary from every letter of the transcripts. That works for the Open Bible | |
| scripts (38-92 symbols, each seen thousands of times; Devanagari writes its vowels and the Arabic text is fully vowelled), but JSUT | |
| has 2,723 distinct characters, half of them seen fewer than 10 times, and a kanji is read differently from word to word | |
| (学生 ガクセー, 出生率 シュッショーリツ). Katakana leaves ~90 symbols. pyopenjtalk writes the pronunciation, not the spelling: | |
| particles は/へ come out as ワ/エ, long vowels as ー, numbers and Latin letters are read out (100 -> ヒャク, NHK -> エヌエイチケイ). | |
| python vits/kana.py "木曜日、停戦会談は、何の進展もないまま終了しました。" | |
| """ | |
| import re, sys, unicodedata | |
| import pyopenjtalk | |
| # pauses and sentence intonation: kept as the ASCII marks in train_vits.py's punctuation list | |
| PUNCT = {"、": ",", ",": ",", "。": ".", ".": ".", "?": "?", "!": "!", "…": ",", "―": ",", "—": ","} | |
| KANA = re.compile(r"[ァ-ヺー]") # katakana U+30A1-U+30FA and the long-vowel mark U+30FC | |
| KEEP = re.compile(r"[ァ-ヺー,.?!]") | |
| def convert(text): | |
| """(kana, unconverted): the katakana pronunciation of `text` with ASCII punctuation, and the letters or digits pyopenjtalk | |
| could not read (e.g. a kanji missing from its dictionary), which are dropped from the kana. Brackets, middle dots, spaces | |
| and symbols carry no sound and are dropped silently.""" | |
| raw = "".join(PUNCT.get(ch, ch) for ch in pyopenjtalk.g2p(text, kana=True)) | |
| unconverted = [ch for ch in raw if not KEEP.match(ch) and unicodedata.category(ch)[0] in "LN"] | |
| return "".join(ch for ch in raw if KEEP.match(ch)), unconverted | |
| def to_kana(text): | |
| return convert(text)[0] | |
| if __name__ == "__main__": | |
| for t in sys.argv[1:]: | |
| kana, bad = convert(t) | |
| print(f"{t}\n -> {kana}" + (f"\n unconverted: {''.join(bad)}" if bad else "")) | |