Refactor: improve regular expression performance during NLP
Improve performance by pre-compiling regular expressions that are executed many times.
This commit is contained in:
@@ -5,17 +5,7 @@ import cn2an
|
|||||||
from style_bert_vits2.nlp.symbols import PUNCTUATIONS
|
from style_bert_vits2.nlp.symbols import PUNCTUATIONS
|
||||||
|
|
||||||
|
|
||||||
def normalize_text(text: str) -> str:
|
__REPLACE_MAP = {
|
||||||
numbers = re.findall(r"\d+(?:\.?\d+)?", text)
|
|
||||||
for number in numbers:
|
|
||||||
text = text.replace(number, cn2an.an2cn(number), 1)
|
|
||||||
text = replace_punctuation(text)
|
|
||||||
return text
|
|
||||||
|
|
||||||
|
|
||||||
def replace_punctuation(text: str) -> str:
|
|
||||||
|
|
||||||
REPLACE_MAP = {
|
|
||||||
":": ",",
|
":": ",",
|
||||||
";": ",",
|
";": ",",
|
||||||
",": ",",
|
",": ",",
|
||||||
@@ -49,10 +39,21 @@ def replace_punctuation(text: str) -> str:
|
|||||||
"」": "'",
|
"」": "'",
|
||||||
}
|
}
|
||||||
|
|
||||||
text = text.replace("嗯", "恩").replace("呣", "母")
|
|
||||||
pattern = re.compile("|".join(re.escape(p) for p in REPLACE_MAP.keys()))
|
|
||||||
|
|
||||||
replaced_text = pattern.sub(lambda x: REPLACE_MAP[x.group()], text)
|
def normalize_text(text: str) -> str:
|
||||||
|
numbers = re.findall(r"\d+(?:\.?\d+)?", text)
|
||||||
|
for number in numbers:
|
||||||
|
text = text.replace(number, cn2an.an2cn(number), 1)
|
||||||
|
text = replace_punctuation(text)
|
||||||
|
return text
|
||||||
|
|
||||||
|
|
||||||
|
def replace_punctuation(text: str) -> str:
|
||||||
|
|
||||||
|
text = text.replace("嗯", "恩").replace("呣", "母")
|
||||||
|
pattern = re.compile("|".join(re.escape(p) for p in __REPLACE_MAP.keys()))
|
||||||
|
|
||||||
|
replaced_text = pattern.sub(lambda x: __REPLACE_MAP[x.group()], text)
|
||||||
|
|
||||||
replaced_text = re.sub(
|
replaced_text = re.sub(
|
||||||
r"[^\u4e00-\u9fa5" + "".join(PUNCTUATIONS) + r"]+", "", replaced_text
|
r"[^\u4e00-\u9fa5" + "".join(PUNCTUATIONS) + r"]+", "", replaced_text
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ from style_bert_vits2.constants import Languages
|
|||||||
from style_bert_vits2.logging import logger
|
from style_bert_vits2.logging import logger
|
||||||
from style_bert_vits2.nlp import bert_models
|
from style_bert_vits2.nlp import bert_models
|
||||||
from style_bert_vits2.nlp.japanese import pyopenjtalk_worker as pyopenjtalk
|
from style_bert_vits2.nlp.japanese import pyopenjtalk_worker as pyopenjtalk
|
||||||
from style_bert_vits2.nlp.japanese.mora_list import MORA_KATA_TO_MORA_PHONEMES
|
from style_bert_vits2.nlp.japanese.mora_list import MORA_KATA_TO_MORA_PHONEMES, VOWELS
|
||||||
from style_bert_vits2.nlp.japanese.normalizer import replace_punctuation
|
from style_bert_vits2.nlp.japanese.normalizer import replace_punctuation
|
||||||
from style_bert_vits2.nlp.symbols import PUNCTUATIONS
|
from style_bert_vits2.nlp.symbols import PUNCTUATIONS
|
||||||
|
|
||||||
@@ -144,7 +144,7 @@ def text_to_sep_kata(
|
|||||||
## 例外を送出しない場合
|
## 例外を送出しない場合
|
||||||
## 読めない文字は「'」として扱う
|
## 読めない文字は「'」として扱う
|
||||||
logger.warning(
|
logger.warning(
|
||||||
f"Cannot read: {word} in:\n{norm_text}, replaced with \"'\""
|
f'Cannot read: {word} in:\n{norm_text}, replaced with "\'"'
|
||||||
)
|
)
|
||||||
# word の文字数分「'」を追加
|
# word の文字数分「'」を追加
|
||||||
yomi = "'" * len(word)
|
yomi = "'" * len(word)
|
||||||
@@ -428,15 +428,23 @@ def __g2phone_tone_wo_punct(text: str) -> list[tuple[str, int]]:
|
|||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
|
__PYOPENJTALK_G2P_PROSODY_A1_PATTERN = re.compile(r"/A:([0-9\-]+)\+")
|
||||||
|
__PYOPENJTALK_G2P_PROSODY_A2_PATTERN = re.compile(r"\+(\d+)\+")
|
||||||
|
__PYOPENJTALK_G2P_PROSODY_A3_PATTERN = re.compile(r"\+(\d+)/")
|
||||||
|
__PYOPENJTALK_G2P_PROSODY_E3_PATTERN = re.compile(r"!(\d+)_")
|
||||||
|
__PYOPENJTALK_G2P_PROSODY_F1_PATTERN = re.compile(r"/F:(\d+)_")
|
||||||
|
__PYOPENJTALK_G2P_PROSODY_P3_PATTERN = re.compile(r"\-(.*?)\+")
|
||||||
|
|
||||||
|
|
||||||
def __pyopenjtalk_g2p_prosody(
|
def __pyopenjtalk_g2p_prosody(
|
||||||
text: str, drop_unvoiced_vowels: bool = True
|
text: str, drop_unvoiced_vowels: bool = True
|
||||||
) -> list[str]:
|
) -> list[str]:
|
||||||
"""
|
"""
|
||||||
ESPnet の実装から引用、変更点無し。「ん」は「N」なことに注意。
|
ESPnet の実装から引用、概ね変更点無し。「ん」は「N」なことに注意。
|
||||||
ref: https://github.com/espnet/espnet/blob/master/espnet2/text/phoneme_tokenizer.py
|
ref: https://github.com/espnet/espnet/blob/master/espnet2/text/phoneme_tokenizer.py
|
||||||
------------------------------------------------------------------------------------------
|
------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
Extract phoneme + prosoody symbol sequence from input full-context labels.
|
Extract phoneme + prosody symbol sequence from input full-context labels.
|
||||||
|
|
||||||
The algorithm is based on `Prosodic features control by symbols as input of
|
The algorithm is based on `Prosodic features control by symbols as input of
|
||||||
sequence-to-sequence acoustic modeling for neural TTS`_ with some r9y9's tweaks.
|
sequence-to-sequence acoustic modeling for neural TTS`_ with some r9y9's tweaks.
|
||||||
@@ -457,8 +465,8 @@ def __pyopenjtalk_g2p_prosody(
|
|||||||
modeling for neural TTS`: https://doi.org/10.1587/transinf.2020EDP7104
|
modeling for neural TTS`: https://doi.org/10.1587/transinf.2020EDP7104
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def _numeric_feature_by_regex(regex: str, s: str) -> int:
|
def _numeric_feature_by_regex(pattern: re.Pattern[str], s: str) -> int:
|
||||||
match = re.search(regex, s)
|
match = pattern.search(s)
|
||||||
if match is None:
|
if match is None:
|
||||||
return -50
|
return -50
|
||||||
return int(match.group(1))
|
return int(match.group(1))
|
||||||
@@ -471,7 +479,7 @@ def __pyopenjtalk_g2p_prosody(
|
|||||||
lab_curr = labels[n]
|
lab_curr = labels[n]
|
||||||
|
|
||||||
# current phoneme
|
# current phoneme
|
||||||
p3 = re.search(r"\-(.*?)\+", lab_curr).group(1) # type: ignore
|
p3 = __PYOPENJTALK_G2P_PROSODY_P3_PATTERN.search(lab_curr).group(1) # type: ignore
|
||||||
# deal unvoiced vowels as normal vowels
|
# deal unvoiced vowels as normal vowels
|
||||||
if drop_unvoiced_vowels and p3 in "AEIOU":
|
if drop_unvoiced_vowels and p3 in "AEIOU":
|
||||||
p3 = p3.lower()
|
p3 = p3.lower()
|
||||||
@@ -483,7 +491,9 @@ def __pyopenjtalk_g2p_prosody(
|
|||||||
phones.append("^")
|
phones.append("^")
|
||||||
elif n == N - 1:
|
elif n == N - 1:
|
||||||
# check question form or not
|
# check question form or not
|
||||||
e3 = _numeric_feature_by_regex(r"!(\d+)_", lab_curr)
|
e3 = _numeric_feature_by_regex(
|
||||||
|
__PYOPENJTALK_G2P_PROSODY_E3_PATTERN, lab_curr
|
||||||
|
)
|
||||||
if e3 == 0:
|
if e3 == 0:
|
||||||
phones.append("$")
|
phones.append("$")
|
||||||
elif e3 == 1:
|
elif e3 == 1:
|
||||||
@@ -496,14 +506,16 @@ def __pyopenjtalk_g2p_prosody(
|
|||||||
phones.append(p3)
|
phones.append(p3)
|
||||||
|
|
||||||
# accent type and position info (forward or backward)
|
# accent type and position info (forward or backward)
|
||||||
a1 = _numeric_feature_by_regex(r"/A:([0-9\-]+)\+", lab_curr)
|
a1 = _numeric_feature_by_regex(__PYOPENJTALK_G2P_PROSODY_A1_PATTERN, lab_curr)
|
||||||
a2 = _numeric_feature_by_regex(r"\+(\d+)\+", lab_curr)
|
a2 = _numeric_feature_by_regex(__PYOPENJTALK_G2P_PROSODY_A2_PATTERN, lab_curr)
|
||||||
a3 = _numeric_feature_by_regex(r"\+(\d+)/", lab_curr)
|
a3 = _numeric_feature_by_regex(__PYOPENJTALK_G2P_PROSODY_A3_PATTERN, lab_curr)
|
||||||
|
|
||||||
# number of mora in accent phrase
|
# number of mora in accent phrase
|
||||||
f1 = _numeric_feature_by_regex(r"/F:(\d+)_", lab_curr)
|
f1 = _numeric_feature_by_regex(__PYOPENJTALK_G2P_PROSODY_F1_PATTERN, lab_curr)
|
||||||
|
|
||||||
a2_next = _numeric_feature_by_regex(r"\+(\d+)\+", labels[n + 1])
|
a2_next = _numeric_feature_by_regex(
|
||||||
|
__PYOPENJTALK_G2P_PROSODY_A2_PATTERN, labels[n + 1]
|
||||||
|
)
|
||||||
# accent phrase border
|
# accent phrase border
|
||||||
if a3 == 1 and a2_next == 1 and p3 in "aeiouAEIOUNcl":
|
if a3 == 1 and a2_next == 1 and p3 in "aeiouAEIOUNcl":
|
||||||
phones.append("#")
|
phones.append("#")
|
||||||
@@ -560,9 +572,6 @@ def __handle_long(sep_phonemes: list[list[str]]) -> list[list[str]]:
|
|||||||
list[list[str]]: 長音記号を処理した音素のリストのリスト
|
list[list[str]]: 長音記号を処理した音素のリストのリスト
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# 母音の集合 (便宜上「ん」を含める)
|
|
||||||
VOWELS = {"a", "i", "u", "e", "o", "N"}
|
|
||||||
|
|
||||||
for i in range(len(sep_phonemes)):
|
for i in range(len(sep_phonemes)):
|
||||||
if len(sep_phonemes[i]) == 0:
|
if len(sep_phonemes[i]) == 0:
|
||||||
# 空白文字等でリストが空の場合
|
# 空白文字等でリストが空の場合
|
||||||
@@ -588,6 +597,15 @@ def __handle_long(sep_phonemes: list[list[str]]) -> list[list[str]]:
|
|||||||
return sep_phonemes
|
return sep_phonemes
|
||||||
|
|
||||||
|
|
||||||
|
__KATAKANA_PATTERN = re.compile(r"[\u30A0-\u30FF]+")
|
||||||
|
__MORA_PATTERN = re.compile(
|
||||||
|
"|".join(
|
||||||
|
map(re.escape, sorted(MORA_KATA_TO_MORA_PHONEMES.keys(), key=len, reverse=True))
|
||||||
|
)
|
||||||
|
)
|
||||||
|
__LONG_PATTERN = re.compile(r"(\w)(ー*)")
|
||||||
|
|
||||||
|
|
||||||
def __kata_to_phoneme_list(text: str) -> list[str]:
|
def __kata_to_phoneme_list(text: str) -> list[str]:
|
||||||
"""
|
"""
|
||||||
原則カタカナの `text` を受け取り、それをそのままいじらずに音素記号のリストに変換。
|
原則カタカナの `text` を受け取り、それをそのままいじらずに音素記号のリストに変換。
|
||||||
@@ -610,23 +628,20 @@ def __kata_to_phoneme_list(text: str) -> list[str]:
|
|||||||
if set(text).issubset(set(PUNCTUATIONS)):
|
if set(text).issubset(set(PUNCTUATIONS)):
|
||||||
return list(text)
|
return list(text)
|
||||||
# `text` がカタカナ(`ー`含む)のみからなるかどうかをチェック
|
# `text` がカタカナ(`ー`含む)のみからなるかどうかをチェック
|
||||||
if re.fullmatch(r"[\u30A0-\u30FF]+", text) is None:
|
if __KATAKANA_PATTERN.fullmatch(text) is None:
|
||||||
raise ValueError(f"Input must be katakana only: {text}")
|
raise ValueError(f"Input must be katakana only: {text}")
|
||||||
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:
|
def mora2phonemes(mora: str) -> str:
|
||||||
cosonant, vowel = MORA_KATA_TO_MORA_PHONEMES[mora]
|
consonant, vowel = MORA_KATA_TO_MORA_PHONEMES[mora]
|
||||||
if cosonant is None:
|
if consonant is None:
|
||||||
return f" {vowel}"
|
return f" {vowel}"
|
||||||
return f" {cosonant} {vowel}"
|
return f" {consonant} {vowel}"
|
||||||
|
|
||||||
spaced_phonemes = re.sub(pattern, lambda m: mora2phonemes(m.group()), text)
|
spaced_phonemes = __MORA_PATTERN.sub(lambda m: mora2phonemes(m.group()), text)
|
||||||
|
|
||||||
# 長音記号「ー」の処理
|
# 長音記号「ー」の処理
|
||||||
long_pattern = r"(\w)(ー*)"
|
|
||||||
long_replacement = lambda m: m.group(1) + (" " + m.group(1)) * len(m.group(2)) # type: ignore
|
long_replacement = lambda m: m.group(1) + (" " + m.group(1)) * len(m.group(2)) # type: ignore
|
||||||
spaced_phonemes = re.sub(long_pattern, long_replacement, spaced_phonemes)
|
spaced_phonemes = __LONG_PATTERN.sub(long_replacement, spaced_phonemes)
|
||||||
|
|
||||||
return spaced_phonemes.strip().split(" ")
|
return spaced_phonemes.strip().split(" ")
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
from style_bert_vits2.nlp.japanese.g2p import g2p
|
from style_bert_vits2.nlp.japanese.g2p import g2p
|
||||||
from style_bert_vits2.nlp.japanese.mora_list import (
|
from style_bert_vits2.nlp.japanese.mora_list import (
|
||||||
|
CONSONANTS,
|
||||||
MORA_KATA_TO_MORA_PHONEMES,
|
MORA_KATA_TO_MORA_PHONEMES,
|
||||||
MORA_PHONEMES_TO_MORA_KATA,
|
MORA_PHONEMES_TO_MORA_KATA,
|
||||||
)
|
)
|
||||||
@@ -33,15 +34,6 @@ def phone_tone2kata_tone(phone_tone: list[tuple[str, int]]) -> list[tuple[str, i
|
|||||||
カタカナと音高のリスト。
|
カタカナと音高のリスト。
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# 子音の集合
|
|
||||||
CONSONANTS = set(
|
|
||||||
[
|
|
||||||
consonant
|
|
||||||
for consonant, _ in MORA_KATA_TO_MORA_PHONEMES.values()
|
|
||||||
if consonant is not None
|
|
||||||
]
|
|
||||||
)
|
|
||||||
|
|
||||||
phone_tone = phone_tone[1:] # 最初の("_", 0)を無視
|
phone_tone = phone_tone[1:] # 最初の("_", 0)を無視
|
||||||
phones = [phone for phone, _ in phone_tone]
|
phones = [phone for phone, _ in phone_tone]
|
||||||
tones = [tone for _, tone in phone_tone]
|
tones = [tone for _, tone in phone_tone]
|
||||||
|
|||||||
@@ -234,3 +234,15 @@ MORA_KATA_TO_MORA_PHONEMES: dict[str, tuple[Optional[str], str]] = {
|
|||||||
kana: (consonant, vowel)
|
kana: (consonant, vowel)
|
||||||
for [kana, consonant, vowel] in __MORA_LIST_MINIMUM + __MORA_LIST_ADDITIONAL
|
for [kana, consonant, vowel] in __MORA_LIST_MINIMUM + __MORA_LIST_ADDITIONAL
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# 子音の集合
|
||||||
|
CONSONANTS = set(
|
||||||
|
[
|
||||||
|
consonant
|
||||||
|
for consonant, _ in MORA_KATA_TO_MORA_PHONEMES.values()
|
||||||
|
if consonant is not None
|
||||||
|
]
|
||||||
|
)
|
||||||
|
|
||||||
|
# 母音の集合 (便宜上「ん」を含める)
|
||||||
|
VOWELS = {"a", "i", "u", "e", "o", "N"}
|
||||||
|
|||||||
@@ -6,6 +6,81 @@ from num2words import num2words
|
|||||||
from style_bert_vits2.nlp.symbols import PUNCTUATIONS
|
from style_bert_vits2.nlp.symbols import PUNCTUATIONS
|
||||||
|
|
||||||
|
|
||||||
|
# 記号類の正規化マップ
|
||||||
|
__REPLACE_MAP = {
|
||||||
|
":": ",",
|
||||||
|
";": ",",
|
||||||
|
",": ",",
|
||||||
|
"。": ".",
|
||||||
|
"!": "!",
|
||||||
|
"?": "?",
|
||||||
|
"\n": ".",
|
||||||
|
".": ".",
|
||||||
|
"…": "...",
|
||||||
|
"···": "...",
|
||||||
|
"・・・": "...",
|
||||||
|
"·": ",",
|
||||||
|
"・": ",",
|
||||||
|
"、": ",",
|
||||||
|
"$": ".",
|
||||||
|
"“": "'",
|
||||||
|
"”": "'",
|
||||||
|
'"': "'",
|
||||||
|
"‘": "'",
|
||||||
|
"’": "'",
|
||||||
|
"(": "'",
|
||||||
|
")": "'",
|
||||||
|
"(": "'",
|
||||||
|
")": "'",
|
||||||
|
"《": "'",
|
||||||
|
"》": "'",
|
||||||
|
"【": "'",
|
||||||
|
"】": "'",
|
||||||
|
"[": "'",
|
||||||
|
"]": "'",
|
||||||
|
# NFKC 正規化後のハイフン・ダッシュの変種を全て通常半角ハイフン - \u002d に変換
|
||||||
|
"\u02d7": "\u002d", # ˗, Modifier Letter Minus Sign
|
||||||
|
"\u2010": "\u002d", # ‐, Hyphen,
|
||||||
|
# "\u2011": "\u002d", # ‑, Non-Breaking Hyphen, NFKC により \u2010 に変換される
|
||||||
|
"\u2012": "\u002d", # ‒, Figure Dash
|
||||||
|
"\u2013": "\u002d", # –, En Dash
|
||||||
|
"\u2014": "\u002d", # —, Em Dash
|
||||||
|
"\u2015": "\u002d", # ―, Horizontal Bar
|
||||||
|
"\u2043": "\u002d", # ⁃, Hyphen Bullet
|
||||||
|
"\u2212": "\u002d", # −, Minus Sign
|
||||||
|
"\u23af": "\u002d", # ⎯, Horizontal Line Extension
|
||||||
|
"\u23e4": "\u002d", # ⏤, Straightness
|
||||||
|
"\u2500": "\u002d", # ─, Box Drawings Light Horizontal
|
||||||
|
"\u2501": "\u002d", # ━, Box Drawings Heavy Horizontal
|
||||||
|
"\u2e3a": "\u002d", # ⸺, Two-Em Dash
|
||||||
|
"\u2e3b": "\u002d", # ⸻, Three-Em Dash
|
||||||
|
# "~": "-", # これは長音記号「ー」として扱うよう変更
|
||||||
|
# "~": "-", # これも長音記号「ー」として扱うよう変更
|
||||||
|
"「": "'",
|
||||||
|
"」": "'",
|
||||||
|
}
|
||||||
|
# 記号類の正規化パターン
|
||||||
|
__REPLACE_PATTERN = re.compile("|".join(re.escape(p) for p in __REPLACE_MAP.keys()))
|
||||||
|
# 句読点等の正規化パターン
|
||||||
|
__PUNCTUATION_CLEANUP_PATTERN = re.compile(
|
||||||
|
# ↓ ひらがな、カタカナ、漢字
|
||||||
|
r"[^\u3040-\u309F\u30A0-\u30FF\u4E00-\u9FFF\u3400-\u4DBF\u3005"
|
||||||
|
# ↓ 半角アルファベット(大文字と小文字)
|
||||||
|
+ r"\u0041-\u005A\u0061-\u007A"
|
||||||
|
# ↓ 全角アルファベット(大文字と小文字)
|
||||||
|
+ r"\uFF21-\uFF3A\uFF41-\uFF5A"
|
||||||
|
# ↓ ギリシャ文字
|
||||||
|
+ r"\u0370-\u03FF\u1F00-\u1FFF"
|
||||||
|
# ↓ "!", "?", "…", ",", ".", "'", "-", 但し`…`はすでに`...`に変換されている
|
||||||
|
+ "".join(PUNCTUATIONS) + r"]+", # fmt: skip
|
||||||
|
)
|
||||||
|
# 数字・通貨記号の正規化パターン
|
||||||
|
__CURRENCY_MAP = {"$": "ドル", "¥": "円", "£": "ポンド", "€": "ユーロ"}
|
||||||
|
__CURRENCY_PATTERN = re.compile(r"([$¥£€])([0-9.]*[0-9])")
|
||||||
|
__NUMBER_PATTERN = re.compile(r"[0-9]+(\.[0-9]+)?")
|
||||||
|
__NUMBER_WITH_SEPARATOR_PATTERN = re.compile("[0-9]{1,3}(,[0-9]{3})+")
|
||||||
|
|
||||||
|
|
||||||
def normalize_text(text: str) -> str:
|
def normalize_text(text: str) -> str:
|
||||||
"""
|
"""
|
||||||
日本語のテキストを正規化する。
|
日本語のテキストを正規化する。
|
||||||
@@ -62,80 +137,11 @@ def replace_punctuation(text: str) -> str:
|
|||||||
str: 正規化されたテキスト
|
str: 正規化されたテキスト
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# 記号類の正規化変換マップ
|
|
||||||
REPLACE_MAP = {
|
|
||||||
":": ",",
|
|
||||||
";": ",",
|
|
||||||
",": ",",
|
|
||||||
"。": ".",
|
|
||||||
"!": "!",
|
|
||||||
"?": "?",
|
|
||||||
"\n": ".",
|
|
||||||
".": ".",
|
|
||||||
"…": "...",
|
|
||||||
"···": "...",
|
|
||||||
"・・・": "...",
|
|
||||||
"·": ",",
|
|
||||||
"・": ",",
|
|
||||||
"、": ",",
|
|
||||||
"$": ".",
|
|
||||||
"“": "'",
|
|
||||||
"”": "'",
|
|
||||||
'"': "'",
|
|
||||||
"‘": "'",
|
|
||||||
"’": "'",
|
|
||||||
"(": "'",
|
|
||||||
")": "'",
|
|
||||||
"(": "'",
|
|
||||||
")": "'",
|
|
||||||
"《": "'",
|
|
||||||
"》": "'",
|
|
||||||
"【": "'",
|
|
||||||
"】": "'",
|
|
||||||
"[": "'",
|
|
||||||
"]": "'",
|
|
||||||
# NFKC 正規化後のハイフン・ダッシュの変種を全て通常半角ハイフン - \u002d に変換
|
|
||||||
"\u02d7": "\u002d", # ˗, Modifier Letter Minus Sign
|
|
||||||
"\u2010": "\u002d", # ‐, Hyphen,
|
|
||||||
# "\u2011": "\u002d", # ‑, Non-Breaking Hyphen, NFKC により \u2010 に変換される
|
|
||||||
"\u2012": "\u002d", # ‒, Figure Dash
|
|
||||||
"\u2013": "\u002d", # –, En Dash
|
|
||||||
"\u2014": "\u002d", # —, Em Dash
|
|
||||||
"\u2015": "\u002d", # ―, Horizontal Bar
|
|
||||||
"\u2043": "\u002d", # ⁃, Hyphen Bullet
|
|
||||||
"\u2212": "\u002d", # −, Minus Sign
|
|
||||||
"\u23af": "\u002d", # ⎯, Horizontal Line Extension
|
|
||||||
"\u23e4": "\u002d", # ⏤, Straightness
|
|
||||||
"\u2500": "\u002d", # ─, Box Drawings Light Horizontal
|
|
||||||
"\u2501": "\u002d", # ━, Box Drawings Heavy Horizontal
|
|
||||||
"\u2e3a": "\u002d", # ⸺, Two-Em Dash
|
|
||||||
"\u2e3b": "\u002d", # ⸻, Three-Em Dash
|
|
||||||
# "~": "-", # これは長音記号「ー」として扱うよう変更
|
|
||||||
# "~": "-", # これも長音記号「ー」として扱うよう変更
|
|
||||||
"「": "'",
|
|
||||||
"」": "'",
|
|
||||||
}
|
|
||||||
|
|
||||||
pattern = re.compile("|".join(re.escape(p) for p in REPLACE_MAP.keys()))
|
|
||||||
|
|
||||||
# 句読点を辞書で置換
|
# 句読点を辞書で置換
|
||||||
replaced_text = pattern.sub(lambda x: REPLACE_MAP[x.group()], text)
|
replaced_text = __REPLACE_PATTERN.sub(lambda x: __REPLACE_MAP[x.group()], text)
|
||||||
|
|
||||||
replaced_text = re.sub(
|
|
||||||
# ↓ ひらがな、カタカナ、漢字
|
|
||||||
r"[^\u3040-\u309F\u30A0-\u30FF\u4E00-\u9FFF\u3400-\u4DBF\u3005"
|
|
||||||
# ↓ 半角アルファベット(大文字と小文字)
|
|
||||||
+ r"\u0041-\u005A\u0061-\u007A"
|
|
||||||
# ↓ 全角アルファベット(大文字と小文字)
|
|
||||||
+ r"\uFF21-\uFF3A\uFF41-\uFF5A"
|
|
||||||
# ↓ ギリシャ文字
|
|
||||||
+ r"\u0370-\u03FF\u1F00-\u1FFF"
|
|
||||||
# ↓ "!", "?", "…", ",", ".", "'", "-", 但し`…`はすでに`...`に変換されている
|
|
||||||
+ "".join(PUNCTUATIONS) + r"]+",
|
|
||||||
# 上述以外の文字を削除
|
# 上述以外の文字を削除
|
||||||
"",
|
replaced_text = __PUNCTUATION_CLEANUP_PATTERN.sub("", replaced_text)
|
||||||
replaced_text,
|
|
||||||
)
|
|
||||||
|
|
||||||
return replaced_text
|
return replaced_text
|
||||||
|
|
||||||
@@ -151,13 +157,8 @@ def __convert_numbers_to_words(text: str) -> str:
|
|||||||
str: 変換されたテキスト
|
str: 変換されたテキスト
|
||||||
"""
|
"""
|
||||||
|
|
||||||
NUMBER_WITH_SEPARATOR_PATTERN = re.compile("[0-9]{1,3}(,[0-9]{3})+")
|
res = __NUMBER_WITH_SEPARATOR_PATTERN.sub(lambda m: m[0].replace(",", ""), text)
|
||||||
CURRENCY_MAP = {"$": "ドル", "¥": "円", "£": "ポンド", "€": "ユーロ"}
|
res = __CURRENCY_PATTERN.sub(lambda m: m[2] + __CURRENCY_MAP.get(m[1], m[1]), res)
|
||||||
CURRENCY_PATTERN = re.compile(r"([$¥£€])([0-9.]*[0-9])")
|
res = __NUMBER_PATTERN.sub(lambda m: num2words(m[0], lang="ja"), res)
|
||||||
NUMBER_PATTERN = re.compile(r"[0-9]+(\.[0-9]+)?")
|
|
||||||
|
|
||||||
res = NUMBER_WITH_SEPARATOR_PATTERN.sub(lambda m: m[0].replace(",", ""), text)
|
|
||||||
res = CURRENCY_PATTERN.sub(lambda m: m[2] + CURRENCY_MAP.get(m[1], m[1]), res)
|
|
||||||
res = NUMBER_PATTERN.sub(lambda m: num2words(m[0], lang="ja"), res)
|
|
||||||
|
|
||||||
return res
|
return res
|
||||||
|
|||||||
Reference in New Issue
Block a user