From ff610bc3f9399804d2758cbfbfab01fc6a7ee59a Mon Sep 17 00:00:00 2001 From: KakaruHayate Date: Thu, 21 Mar 2024 11:19:31 +0800 Subject: [PATCH] Revert "Merge pull request #821 from KamioRinn/Optimize-English-G2P" This reverts commit 7bc0836d9933402215ce529da7f50bd1a8c63f7f, reversing changes made to b451372316b2611c8607258982d22d9936cde696. --- GPT_SoVITS/text/english.py | 105 ++++++++----------------------------- 1 file changed, 21 insertions(+), 84 deletions(-) diff --git a/GPT_SoVITS/text/english.py b/GPT_SoVITS/text/english.py index 077d33c6..09e20bdd 100644 --- a/GPT_SoVITS/text/english.py +++ b/GPT_SoVITS/text/english.py @@ -8,13 +8,6 @@ from string import punctuation from text import symbols -import unicodedata -from builtins import str as unicode -from g2p_en.expand import normalize_numbers -from nltk.tokenize import TweetTokenizer -word_tokenize = TweetTokenizer().tokenize -from nltk import pos_tag - current_file_path = os.path.dirname(__file__) CMU_DICT_PATH = os.path.join(current_file_path, "cmudict.rep") CMU_DICT_FAST_PATH = os.path.join(current_file_path, "cmudict-fast.rep") @@ -195,6 +188,9 @@ def get_dict(): return g2p_dict +eng_dict = get_dict() + + def text_normalize(text): # todo: eng text normalize # 适配中文及 g2p_en 标点 @@ -208,16 +204,6 @@ def text_normalize(text): for p, r in rep_map.items(): text = re.sub(p, r, text) - # 来自 g2p_en 文本格式化处理 - # 增加大写兼容 - text = unicode(text) - text = normalize_numbers(text) - text = ''.join(char for char in unicodedata.normalize('NFD', text) - if unicodedata.category(char) != 'Mn') # Strip accents - text = re.sub("[^ A-Za-z'.,?!\-]", "", text) - text = re.sub(r"(?i)i\.e\.", "that is", text) - text = re.sub(r"(?i)e\.g\.", "for example", text) - return text @@ -234,85 +220,30 @@ class en_G2p(G2p): for word in ["AE", "AI", "AR", "IOS", "HUD", "OS"]: del self.cmu[word.lower()] - - def __call__(self, text): - # tokenization - words = word_tokenize(text) - tokens = pos_tag(words) # tuples of (word, tag) - - # steps - prons = [] - for o_word, pos in tokens: - # 还原 g2p_en 小写操作逻辑 - word = o_word.lower() - - if re.search("[a-z]", word) is None: - pron = [word] - # 先把单字母推出去 - elif len(word) == 1: - # 单读 A 发音修正, 这里需要原格式 o_word 判断大写 - if o_word == "A": - pron = ['EY1'] - else: - pron = self.cmu[word][0] - # g2p_en 原版多音字处理 - elif word in self.homograph2features: # Check homograph - pron1, pron2, pos1 = self.homograph2features[word] - if pos.startswith(pos1): - pron = pron1 - else: - pron = pron2 - else: - # 递归查找预测 - pron = self.qryword(word) - - prons.extend(pron) - prons.extend([" "]) - - return prons[:-1] + # "A" 落单不读 "AH0" 读 "EY1" + self.cmu['a'] = [['EY1']] - def qryword(self, word): - # 查字典, 单字母除外 - if len(word) > 1 and word in self.cmu: # lookup CMU dict - return self.cmu[word][0] - - # oov 长度小于等于 3 直接读字母 + def predict(self, word): + # 小写 oov 长度小于等于 3 直接读字母 if (len(word) <= 3): - phones = [] - for w in word: - # 单读 A 发音修正, 此处不存在大写的情况 - if w == "a": - phones.extend(['EY1']) - else: - phones.extend(self.cmu[w][0]) - return phones + return [phone for w in word for phone in self(w)] # 尝试分离所有格 if re.match(r"^([a-z]+)('s)$", word): - phones = self.qryword(word[:-2]) - # P T K F TH HH 无声辅音结尾 's 发 ['S'] - if phones[-1] in ['P', 'T', 'K', 'F', 'TH', 'HH']: - phones.extend(['S']) - # S Z SH ZH CH JH 擦声结尾 's 发 ['IH1', 'Z'] 或 ['AH0', 'Z'] - elif phones[-1] in ['S', 'Z', 'SH', 'ZH', 'CH', 'JH']: - phones.extend(['AH0', 'Z']) - # B D G DH V M N NG L R W Y 有声辅音结尾 's 发 ['Z'] - # AH0 AH1 AH2 EY0 EY1 EY2 AE0 AE1 AE2 EH0 EH1 EH2 OW0 OW1 OW2 UH0 UH1 UH2 IY0 IY1 IY2 AA0 AA1 AA2 AO0 AO1 AO2 - # ER ER0 ER1 ER2 UW0 UW1 UW2 AY0 AY1 AY2 AW0 AW1 AW2 OY0 OY1 OY2 IH IH0 IH1 IH2 元音结尾 's 发 ['Z'] - else: - phones.extend(['Z']) - return phones + phone = self(word[:-2]) + phone.extend(['Z']) + return phone # 尝试进行分词,应对复合词 comps = wordsegment.segment(word.lower()) # 无法分词的送回去预测 if len(comps)==1: - return self.predict(word) + return super().predict(word) # 可以分词的递归处理 - return [phone for comp in comps for phone in self.qryword(comp)] + return [phone for comp in comps for phone in self(comp)] _g2p = en_G2p() @@ -327,6 +258,12 @@ def g2p(text): if __name__ == "__main__": + # print(get_dict()) print(g2p("hello")) - print(g2p(text_normalize("e.g. I used openai's AI tool to draw a picture."))) - print(g2p(text_normalize("In this; paper, we propose 1 DSPGAN, a GAN-based universal vocoder."))) \ No newline at end of file + print(g2p("In this; paper, we propose 1 DSPGAN, a GAN-based universal vocoder.")) + # all_phones = set() + # for k, syllables in eng_dict.items(): + # for group in syllables: + # for ph in group: + # all_phones.add(ph) + # print(all_phones)