CoolFace
Apppublic

Kafke/Code-Realize-TTS

sourceHugging Faceupdated 3y agoView on Hugging Face
0likes
japanese.py154 linesDownload Raw Back to text
1import re2from unidecode import unidecode3import pyopenjtalk4 5 6# Regular expression matching Japanese without punctuation marks:7_japanese_characters = re.compile(8    r'[A-Za-z\d\u3005\u3040-\u30ff\u4e00-\u9fff\uff11-\uff19\uff21-\uff3a\uff41-\uff5a\uff66-\uff9d]')9 10# Regular expression matching non-Japanese characters or punctuation marks:11_japanese_marks = re.compile(12    r'[^A-Za-z\d\u3005\u3040-\u30ff\u4e00-\u9fff\uff11-\uff19\uff21-\uff3a\uff41-\uff5a\uff66-\uff9d]')13 14# List of (symbol, Japanese) pairs for marks:15_symbols_to_japanese = [(re.compile('%s' % x[0]), x[1]) for x in [16    ('%', 'パーセント')17]]18 19# List of (romaji, ipa) pairs for marks:20_romaji_to_ipa = [(re.compile('%s' % x[0]), x[1]) for x in [21    ('ts', 'ʦ'),22    ('u', 'ɯ'),23    ('j', 'ʥ'),24    ('y', 'j'),25    ('ni', 'n^i'),26    ('nj', 'n^'),27    ('hi', 'çi'),28    ('hj', 'ç'),29    ('f', 'ɸ'),30    ('I', 'i*'),31    ('U', 'ɯ*'),32    ('r', 'ɾ')33]]34 35# List of (romaji, ipa2) pairs for marks:36_romaji_to_ipa2 = [(re.compile('%s' % x[0]), x[1]) for x in [37    ('u', 'ɯ'),38    ('ʧ', 'tʃ'),39    ('j', 'dʑ'),40    ('y', 'j'),41    ('ni', 'n^i'),42    ('nj', 'n^'),43    ('hi', 'çi'),44    ('hj', 'ç'),45    ('f', 'ɸ'),46    ('I', 'i*'),47    ('U', 'ɯ*'),48    ('r', 'ɾ')49]]50 51# List of (consonant, sokuon) pairs:52_real_sokuon = [(re.compile('%s' % x[0]), x[1]) for x in [53    (r'Q([↑↓]*[kg])', r'k#\1'),54    (r'Q([↑↓]*[tdjʧ])', r't#\1'),55    (r'Q([↑↓]*[sʃ])', r's\1'),56    (r'Q([↑↓]*[pb])', r'p#\1')57]]58 59# List of (consonant, hatsuon) pairs:60_real_hatsuon = [(re.compile('%s' % x[0]), x[1]) for x in [61    (r'N([↑↓]*[pbm])', r'm\1'),62    (r'N([↑↓]*[ʧʥj])', r'n^\1'),63    (r'N([↑↓]*[tdn])', r'n\1'),64    (r'N([↑↓]*[kg])', r'ŋ\1')65]]66 67 68def symbols_to_japanese(text):69    for regex, replacement in _symbols_to_japanese:70        text = re.sub(regex, replacement, text)71    return text72 73 74def japanese_to_romaji_with_accent(text):75    '''Reference https://r9y9.github.io/ttslearn/latest/notebooks/ch10_Recipe-Tacotron.html'''76    text = symbols_to_japanese(text)77    sentences = re.split(_japanese_marks, text)78    marks = re.findall(_japanese_marks, text)79    text = ''80    for i, sentence in enumerate(sentences):81        if re.match(_japanese_characters, sentence):82            if text != '':83                text += ' '84            labels = pyopenjtalk.extract_fullcontext(sentence)85            for n, label in enumerate(labels):86                phoneme = re.search(r'\-([^\+]*)\+', label).group(1)87                if phoneme not in ['sil', 'pau']:88                    text += phoneme.replace('ch', 'ʧ').replace('sh',89                                                               'ʃ').replace('cl', 'Q')90                else:91                    continue92                # n_moras = int(re.search(r'/F:(\d+)_', label).group(1))93                a1 = int(re.search(r"/A:(\-?[0-9]+)\+", label).group(1))94                a2 = int(re.search(r"\+(\d+)\+", label).group(1))95                a3 = int(re.search(r"\+(\d+)/", label).group(1))96                if re.search(r'\-([^\+]*)\+', labels[n + 1]).group(1) in ['sil', 'pau']:97                    a2_next = -198                else:99                    a2_next = int(100                        re.search(r"\+(\d+)\+", labels[n + 1]).group(1))101                # Accent phrase boundary102                if a3 == 1 and a2_next == 1:103                    text += ' '104                # Falling105                elif a1 == 0 and a2_next == a2 + 1:106                    text += '↓'107                # Rising108                elif a2 == 1 and a2_next == 2:109                    text += '↑'110        if i < len(marks):111            text += unidecode(marks[i]).replace(' ', '')112    return text113 114 115def get_real_sokuon(text):116    for regex, replacement in _real_sokuon:117        text = re.sub(regex, replacement, text)118    return text119 120 121def get_real_hatsuon(text):122    for regex, replacement in _real_hatsuon:123        text = re.sub(regex, replacement, text)124    return text125 126 127def japanese_to_ipa(text):128    text = japanese_to_romaji_with_accent(text).replace('...', '…')129    text = re.sub(130        r'([aiueo])\1+', lambda x: x.group(0)[0]+'ː'*(len(x.group(0))-1), text)131    text = get_real_sokuon(text)132    text = get_real_hatsuon(text)133    for regex, replacement in _romaji_to_ipa:134        text = re.sub(regex, replacement, text)135    return text136 137 138def japanese_to_ipa2(text):139    text = japanese_to_romaji_with_accent(text).replace('...', '…')140    text = get_real_sokuon(text)141    text = get_real_hatsuon(text)142    for regex, replacement in _romaji_to_ipa2:143        text = re.sub(regex, replacement, text)144    return text145 146 147def japanese_to_ipa3(text):148    text = japanese_to_ipa2(text).replace('n^', 'ȵ').replace(149        'ʃ', 'ɕ').replace('*', '\u0325').replace('#', '\u031a')150    text = re.sub(151        r'([aiɯeo])\1+', lambda x: x.group(0)[0]+'ː'*(len(x.group(0))-1), text)152    text = re.sub(r'((?:^|\s)(?:ts|tɕ|[kpt]))', r'\1ʰ', text)153    return text154