Refactor: remove old code that can be deleted and update where modules are imported

This commit is contained in:
tsukumi
2024-03-06 22:51:25 +00:00
parent f880641eb5
commit 1936344c0c
14 changed files with 52 additions and 473 deletions

View File

@@ -1,6 +1,6 @@
from text.symbols import *
from style_bert_vits2.text_processing.symbols import *
_symbol_to_id = {s: i for i, s in enumerate(symbols)}
_symbol_to_id = {s: i for i, s in enumerate(SYMBOLS)}
def cleaned_text_to_sequence(cleaned_text, tones, language):
@@ -11,9 +11,9 @@ def cleaned_text_to_sequence(cleaned_text, tones, language):
List of integers corresponding to the symbols in the text
"""
phones = [_symbol_to_id[symbol] for symbol in cleaned_text]
tone_start = language_tone_start_map[language]
tone_start = LANGUAGE_TONE_START_MAP[language]
tones = [i + tone_start for i in tones]
lang_id = language_id_map[language]
lang_id = LANGUAGE_ID_MAP[language]
lang_ids = [lang_id for i in phones]
return phones, tones, lang_ids

View File

@@ -4,7 +4,7 @@ import re
import cn2an
from pypinyin import lazy_pinyin, Style
from text.symbols import punctuation
from style_bert_vits2.text_processing.symbols import PUNCTUATIONS
from text.tone_sandhi import ToneSandhi
current_file_path = os.path.dirname(__file__)
@@ -60,14 +60,14 @@ def replace_punctuation(text):
replaced_text = pattern.sub(lambda x: rep_map[x.group()], text)
replaced_text = re.sub(
r"[^\u4e00-\u9fa5" + "".join(punctuation) + r"]+", "", replaced_text
r"[^\u4e00-\u9fa5" + "".join(PUNCTUATIONS) + r"]+", "", replaced_text
)
return replaced_text
def g2p(text):
pattern = r"(?<=[{0}])\s*".format("".join(punctuation))
pattern = r"(?<=[{0}])\s*".format("".join(PUNCTUATIONS))
sentences = [i for i in re.split(pattern, text) if i.strip() != ""]
phones, tones, word2ph = _g2p(sentences)
assert sum(word2ph) == len(phones)
@@ -119,7 +119,7 @@ def _g2p(segments):
# NOTE: post process for pypinyin outputs
# we discriminate i, ii and iii
if c == v:
assert c in punctuation
assert c in PUNCTUATIONS
phone = [c]
tone = "0"
word2ph.append(1)

View File

@@ -4,8 +4,7 @@ import re
from g2p_en import G2p
from transformers import DebertaV2Tokenizer
from text import symbols
from text.symbols import punctuation
from style_bert_vits2.text_processing.symbols import PUNCTUATIONS, SYMBOLS
current_file_path = os.path.dirname(__file__)
CMU_DICT_PATH = os.path.join(current_file_path, "cmudict.rep")
@@ -107,9 +106,9 @@ def post_replace_ph(ph):
}
if ph in rep_map.keys():
ph = rep_map[ph]
if ph in symbols:
if ph in SYMBOLS:
return ph
if ph not in symbols:
if ph not in SYMBOLS:
ph = "UNK"
return ph
@@ -399,13 +398,13 @@ def text_to_words(text):
if t.startswith(""):
words.append([t[1:]])
else:
if t in punctuation:
if t in PUNCTUATIONS:
if idx == len(tokens) - 1:
words.append([f"{t}"])
else:
if (
not tokens[idx + 1].startswith("")
and tokens[idx + 1] not in punctuation
and tokens[idx + 1] not in PUNCTUATIONS
):
if idx == 0:
words.append([])
@@ -433,7 +432,7 @@ def g2p(text):
if "'" in word:
word = ["".join(word)]
for w in word:
if w in punctuation:
if w in PUNCTUATIONS:
temp_phones.append(w)
temp_tones.append(0)
continue

View File

@@ -2,20 +2,18 @@
# compatible with Julius https://github.com/julius-speech/segmentation-kit
import re
import unicodedata
from pathlib import Path
import pyopenjtalk
from num2words import num2words
from transformers import AutoTokenizer
from style_bert_vits2.logging import logger
from text import punctuation
from text.japanese_mora_list import (
mora_kata_to_mora_phonemes,
mora_phonemes_to_mora_kata,
from style_bert_vits2.text_processing.japanese.mora_list import (
MORA_KATA_TO_MORA_PHONEMES,
MORA_PHONEMES_TO_MORA_KATA,
)
from style_bert_vits2.text_processing.japanese.user_dict import update_dict
from style_bert_vits2.text_processing.symbols import PUNCTUATIONS
# 最初にpyopenjtalkの辞書を更新
update_dict()
@@ -24,7 +22,7 @@ update_dict()
COSONANTS = set(
[
cosonant
for cosonant, _ in mora_kata_to_mora_phonemes.values()
for cosonant, _ in MORA_KATA_TO_MORA_PHONEMES.values()
if cosonant is not None
]
)
@@ -153,7 +151,7 @@ def replace_punctuation(text: str) -> str:
# ↓ ギリシャ文字
+ r"\u0370-\u03FF\u1F00-\u1FFF"
# ↓ "!", "?", "…", ",", ".", "'", "-", 但し`…`はすでに`...`に変換されている
+ "".join(punctuation) + r"]+",
+ "".join(PUNCTUATIONS) + r"]+",
# 上述以外の文字を削除
"",
replaced_text,
@@ -220,7 +218,7 @@ def g2p(
# sep_textから、各単語を1文字1文字分割して、文字のリストのリストを作る
sep_tokenized: list[list[str]] = []
for i in sep_text:
if i not in punctuation:
if i not in PUNCTUATIONS:
sep_tokenized.append(
tokenizer.tokenize(i)
) # ここでおそらく`i`が文字単位に分割される
@@ -268,7 +266,7 @@ def phone_tone2kata_tone(phone_tone: list[tuple[str, int]]) -> list[tuple[str, i
current_mora = ""
for phone, next_phone, tone, next_tone in zip(phones, phones[1:], tones, tones[1:]):
# zipの関係で最後の("_", 0)は無視されている
if phone in punctuation:
if phone in PUNCTUATIONS:
result.append((phone, tone))
continue
if phone in COSONANTS: # n以外の子音の場合
@@ -278,7 +276,7 @@ def phone_tone2kata_tone(phone_tone: list[tuple[str, int]]) -> list[tuple[str, i
else:
# phoneが母音もしくは「N」
current_mora += phone
result.append((mora_phonemes_to_mora_kata[current_mora], tone))
result.append((MORA_PHONEMES_TO_MORA_KATA[current_mora], tone))
current_mora = ""
return result
@@ -287,10 +285,10 @@ def kata_tone2phone_tone(kata_tone: list[tuple[str, int]]) -> list[tuple[str, in
"""`phone_tone2kata_tone()`の逆。"""
result: list[tuple[str, int]] = [("_", 0)]
for mora, tone in kata_tone:
if mora in punctuation:
if mora in PUNCTUATIONS:
result.append((mora, tone))
else:
cosonant, vowel = mora_kata_to_mora_phonemes[mora]
cosonant, vowel = MORA_KATA_TO_MORA_PHONEMES[mora]
if cosonant is None:
result.append((vowel, tone))
else:
@@ -387,7 +385,7 @@ def text2sep_kata(
assert yomi != "", f"Empty yomi: {word}"
if yomi == "":
# wordは正規化されているので、`.`, `,`, `!`, `'`, `-`, `--` のいずれか
if not set(word).issubset(set(punctuation)): # 記号繰り返しか判定
if not set(word).issubset(set(PUNCTUATIONS)): # 記号繰り返しか判定
# ここはpyopenjtalkが読めない文字等のときに起こる
if raise_yomi_error:
raise YomiError(f"Cannot read: {word} in:\n{norm_text}")
@@ -581,7 +579,7 @@ def align_tones(
result.append((phone, phone_tone_list[tone_index][1]))
# 探すindexを1つ進める
tone_index += 1
elif phone in punctuation:
elif phone in PUNCTUATIONS:
# phoneがpunctuationの場合 → (phone, 0)を追加
result.append((phone, 0))
else:
@@ -606,16 +604,16 @@ def kata2phoneme_list(text: str) -> list[str]:
`?` → ["?"]
`!?!?!?!?!` → ["!", "?", "!", "?", "!", "?", "!", "?", "!"]
"""
if set(text).issubset(set(punctuation)):
if set(text).issubset(set(PUNCTUATIONS)):
return list(text)
# `text`がカタカナ(`ー`含む)のみからなるかどうかをチェック
if re.fullmatch(r"[\u30A0-\u30FF]+", text) is None:
raise ValueError(f"Input must be katakana only: {text}")
sorted_keys = sorted(mora_kata_to_mora_phonemes.keys(), key=len, reverse=True)
sorted_keys = sorted(MORA_KATA_TO_MORA_PHONEMES.keys(), key=len, reverse=True)
pattern = "|".join(map(re.escape, sorted_keys))
def mora2phonemes(mora: str) -> str:
cosonant, vowel = mora_kata_to_mora_phonemes[mora]
cosonant, vowel = MORA_KATA_TO_MORA_PHONEMES[mora]
if cosonant is None:
return f" {vowel}"
return f" {cosonant} {vowel}"

View File

@@ -4,7 +4,7 @@ import torch
from transformers import AutoModelForMaskedLM, AutoTokenizer
from config import config
from text.japanese import text2sep_kata, text_normalize
from style_bert_vits2.text_processing.japanese.g2p import text_to_sep_kata
LOCAL_PATH = "./bert/deberta-v2-large-japanese-char-wwm"
@@ -22,10 +22,10 @@ def get_bert_feature(
):
# 各単語が何文字かを作る`word2ph`を使う必要があるので、読めない文字は必ず無視する
# でないと`word2ph`の結果とテキストの文字数結果が整合性が取れない
text = "".join(text2sep_kata(text, raise_yomi_error=False)[0])
text = "".join(text_to_sep_kata(text, raise_yomi_error=False)[0])
if assist_text:
assist_text = "".join(text2sep_kata(assist_text, raise_yomi_error=False)[0])
assist_text = "".join(text_to_sep_kata(assist_text, raise_yomi_error=False)[0])
if (
sys.platform == "darwin"
and torch.backends.mps.is_available()

View File

@@ -1,232 +0,0 @@
"""
VOICEVOXのソースコードからお借りして最低限に改造したコード。
https://github.com/VOICEVOX/voicevox_engine/blob/master/voicevox_engine/tts_pipeline/mora_list.py
"""
"""
以下のモーラ対応表はOpenJTalkのソースコードから取得し、
カタカナ表記とモーラが一対一対応するように改造した。
ライセンス表記:
-----------------------------------------------------------------
The Japanese TTS System "Open JTalk"
developed by HTS Working Group
http://open-jtalk.sourceforge.net/
-----------------------------------------------------------------
Copyright (c) 2008-2014 Nagoya Institute of Technology
Department of Computer Science
All rights reserved.
Redistribution and use in source and binary forms, with or
without modification, are permitted provided that the following
conditions are met:
- Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
- Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided
with the distribution.
- Neither the name of the HTS working group nor the names of its
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
"""
from typing import Optional
# (カタカナ, 子音, 母音)の順。子音がない場合はNoneを入れる。
# 但し「ン」と「ッ」は母音のみという扱いで、「ン」は「N」、「ッ」は「q」とする。
# 元々「ッ」は「cl」
# また「デェ = dy e」はpyopenjtalkの出力de eと合わないため削除
_mora_list_minimum: list[tuple[str, Optional[str], str]] = [
("ヴォ", "v", "o"),
("ヴェ", "v", "e"),
("ヴィ", "v", "i"),
("ヴァ", "v", "a"),
("", "v", "u"),
("", None, "N"),
("", "w", "a"),
("", "r", "o"),
("", "r", "e"),
("", "r", "u"),
("リョ", "ry", "o"),
("リュ", "ry", "u"),
("リャ", "ry", "a"),
("リェ", "ry", "e"),
("", "r", "i"),
("", "r", "a"),
("", "y", "o"),
("", "y", "u"),
("", "y", "a"),
("", "m", "o"),
("", "m", "e"),
("", "m", "u"),
("ミョ", "my", "o"),
("ミュ", "my", "u"),
("ミャ", "my", "a"),
("ミェ", "my", "e"),
("", "m", "i"),
("", "m", "a"),
("", "p", "o"),
("", "b", "o"),
("", "h", "o"),
("", "p", "e"),
("", "b", "e"),
("", "h", "e"),
("", "p", "u"),
("", "b", "u"),
("フォ", "f", "o"),
("フェ", "f", "e"),
("フィ", "f", "i"),
("ファ", "f", "a"),
("", "f", "u"),
("ピョ", "py", "o"),
("ピュ", "py", "u"),
("ピャ", "py", "a"),
("ピェ", "py", "e"),
("", "p", "i"),
("ビョ", "by", "o"),
("ビュ", "by", "u"),
("ビャ", "by", "a"),
("ビェ", "by", "e"),
("", "b", "i"),
("ヒョ", "hy", "o"),
("ヒュ", "hy", "u"),
("ヒャ", "hy", "a"),
("ヒェ", "hy", "e"),
("", "h", "i"),
("", "p", "a"),
("", "b", "a"),
("", "h", "a"),
("", "n", "o"),
("", "n", "e"),
("", "n", "u"),
("ニョ", "ny", "o"),
("ニュ", "ny", "u"),
("ニャ", "ny", "a"),
("ニェ", "ny", "e"),
("", "n", "i"),
("", "n", "a"),
("ドゥ", "d", "u"),
("", "d", "o"),
("トゥ", "t", "u"),
("", "t", "o"),
("デョ", "dy", "o"),
("デュ", "dy", "u"),
("デャ", "dy", "a"),
# ("デェ", "dy", "e"),
("ディ", "d", "i"),
("", "d", "e"),
("テョ", "ty", "o"),
("テュ", "ty", "u"),
("テャ", "ty", "a"),
("ティ", "t", "i"),
("", "t", "e"),
("ツォ", "ts", "o"),
("ツェ", "ts", "e"),
("ツィ", "ts", "i"),
("ツァ", "ts", "a"),
("", "ts", "u"),
("", None, "q"), # 「cl」から「q」に変更
("チョ", "ch", "o"),
("チュ", "ch", "u"),
("チャ", "ch", "a"),
("チェ", "ch", "e"),
("", "ch", "i"),
("", "d", "a"),
("", "t", "a"),
("", "z", "o"),
("", "s", "o"),
("", "z", "e"),
("", "s", "e"),
("ズィ", "z", "i"),
("", "z", "u"),
("スィ", "s", "i"),
("", "s", "u"),
("ジョ", "j", "o"),
("ジュ", "j", "u"),
("ジャ", "j", "a"),
("ジェ", "j", "e"),
("", "j", "i"),
("ショ", "sh", "o"),
("シュ", "sh", "u"),
("シャ", "sh", "a"),
("シェ", "sh", "e"),
("", "sh", "i"),
("", "z", "a"),
("", "s", "a"),
("", "g", "o"),
("", "k", "o"),
("", "g", "e"),
("", "k", "e"),
("グヮ", "gw", "a"),
("", "g", "u"),
("クヮ", "kw", "a"),
("", "k", "u"),
("ギョ", "gy", "o"),
("ギュ", "gy", "u"),
("ギャ", "gy", "a"),
("ギェ", "gy", "e"),
("", "g", "i"),
("キョ", "ky", "o"),
("キュ", "ky", "u"),
("キャ", "ky", "a"),
("キェ", "ky", "e"),
("", "k", "i"),
("", "g", "a"),
("", "k", "a"),
("", None, "o"),
("", None, "e"),
("ウォ", "w", "o"),
("ウェ", "w", "e"),
("ウィ", "w", "i"),
("", None, "u"),
("イェ", "y", "e"),
("", None, "i"),
("", None, "a"),
]
_mora_list_additional: list[tuple[str, Optional[str], str]] = [
("ヴョ", "by", "o"),
("ヴュ", "by", "u"),
("ヴャ", "by", "a"),
("", None, "o"),
("", None, "e"),
("", None, "i"),
("", "w", "a"),
("", "y", "o"),
("", "y", "u"),
("", "z", "u"),
("", "j", "i"),
("", "k", "e"),
("", "y", "a"),
("", None, "o"),
("", None, "e"),
("", None, "u"),
("", None, "i"),
("", None, "a"),
]
# 例: "vo" -> "ヴォ", "a" -> "ア"
mora_phonemes_to_mora_kata: dict[str, str] = {
(consonant or "") + vowel: kana for [kana, consonant, vowel] in _mora_list_minimum
}
# 例: "ヴォ" -> ("v", "o"), "ア" -> (None, "a")
mora_kata_to_mora_phonemes: dict[str, tuple[Optional[str], str]] = {
kana: (consonant, vowel)
for [kana, consonant, vowel] in _mora_list_minimum + _mora_list_additional
}

View File

@@ -1,187 +0,0 @@
punctuation = ["!", "?", "", ",", ".", "'", "-"]
pu_symbols = punctuation + ["SP", "UNK"]
pad = "_"
# chinese
zh_symbols = [
"E",
"En",
"a",
"ai",
"an",
"ang",
"ao",
"b",
"c",
"ch",
"d",
"e",
"ei",
"en",
"eng",
"er",
"f",
"g",
"h",
"i",
"i0",
"ia",
"ian",
"iang",
"iao",
"ie",
"in",
"ing",
"iong",
"ir",
"iu",
"j",
"k",
"l",
"m",
"n",
"o",
"ong",
"ou",
"p",
"q",
"r",
"s",
"sh",
"t",
"u",
"ua",
"uai",
"uan",
"uang",
"ui",
"un",
"uo",
"v",
"van",
"ve",
"vn",
"w",
"x",
"y",
"z",
"zh",
"AA",
"EE",
"OO",
]
num_zh_tones = 6
# japanese
ja_symbols = [
"N",
"a",
"a:",
"b",
"by",
"ch",
"d",
"dy",
"e",
"e:",
"f",
"g",
"gy",
"h",
"hy",
"i",
"i:",
"j",
"k",
"ky",
"m",
"my",
"n",
"ny",
"o",
"o:",
"p",
"py",
"q",
"r",
"ry",
"s",
"sh",
"t",
"ts",
"ty",
"u",
"u:",
"w",
"y",
"z",
"zy",
]
num_ja_tones = 2
# English
en_symbols = [
"aa",
"ae",
"ah",
"ao",
"aw",
"ay",
"b",
"ch",
"d",
"dh",
"eh",
"er",
"ey",
"f",
"g",
"hh",
"ih",
"iy",
"jh",
"k",
"l",
"m",
"n",
"ng",
"ow",
"oy",
"p",
"r",
"s",
"sh",
"t",
"th",
"uh",
"uw",
"V",
"w",
"y",
"z",
"zh",
]
num_en_tones = 4
# combine all symbols
normal_symbols = sorted(set(zh_symbols + ja_symbols + en_symbols))
symbols = [pad] + normal_symbols + pu_symbols
sil_phonemes_ids = [symbols.index(i) for i in pu_symbols]
# combine all tones
num_tones = num_zh_tones + num_ja_tones + num_en_tones
# language maps
language_id_map = {"ZH": 0, "JP": 1, "EN": 2}
num_languages = len(language_id_map.keys())
language_tone_start_map = {
"ZH": 0,
"JP": num_zh_tones,
"EN": num_zh_tones + num_ja_tones,
}
if __name__ == "__main__":
a = set(zh_symbols)
b = set(en_symbols)
print(sorted(a & b))