File size: 2,347 Bytes
2768ce6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""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 ""))