Phoneme Example Translation ------- ------- ----------- AA odd AA D AE at AE T AH hut HH AH T AO ought AO T AW cow K AW AY hide HH AY D B be B IY CH cheese CH IY Z D dee D IY DH thee DH IY EH Ed EH D ER hurt HH ER T EY ate EY T F fee F IY G green G R IY N HH he HH IY IH it IH T IY eat IY T JH gee JH IY K key K IY L lee L IY M me M IY N knee N IY NG ping P IH NG OW oat OW T OY toy T OY P pee P IY R read R IY D S sea S IY SH she SH IY T tea T IY TH theta TH EY T AH UH hood HH UH D UW two T UW V vee V IY W we W IY Y yield Y IY L D Z zee Z IY ZH seizure S IY ZH ER
alt_re = re.compile(r'\([0-9]+\)') cmu_dict = {} text = _clean_text(text) text = re.sub(r"([?.!,])", r" \1", text)
# 文件是从官网下载的,所以文件编码格式要用latin-1 withopen(cmu_dict_path, 'r', encoding='latin-1') as cmu_file: for line in cmu_file: iflen(line) and (line[0] >= "A"and line[0] <= "Z"or line[0] == "'"): parts = line.split(' ') word = re.sub(alt_re, '', parts[0])
# 这里要将非cmu音素的干扰排除 pronunciation = " " temps = parts[1].strip().split(' ') for temp in temps: if temp notin symbols_set: pronunciation = None break if pronunciation: pronunciation = ' '.join(temps) if word in cmu_dict: cmu_dict[word].append(pronunciation) else: cmu_dict[word] = [pronunciation]
cmu_result = [] for word in text.split(' '): # 因为同一个单词,它的发音音素可能不一样,所以存在多个 # 音素分词,我这里就单纯的取第一个,后面再改进和优化 cmu_word = cmu_dict.get(word.upper(), [word])[0] if cmu_word != word: cmu_result.append("{" + cmu_word + "}") else: cmu_result.append(cmu_word)
SIL sil <SPOKEN_NOISE> sil 啊 aa a1 啊 aa a2 啊 aa a4 啊 aa a5 啊啊啊 aa a2 aa a2 aa a2 啊啊啊 aa a5 aa a5 aa a5 阿 aa a1 阿 ee e1 阿尔 aa a1 ee er3 阿根廷 aa a1 g en1 t ing2 阿九 aa a1 j iu3 阿克 aa a1 k e4 阿拉伯数字 aa a1 l a1 b o2 sh u4 z iy4 阿拉法特 aa a1 l a1 f a3 t e4