From a2b52658a80256d5feff3a0cb07e46a0bd415544 Mon Sep 17 00:00:00 2001 From: misyaguziya Date: Sat, 7 Sep 2024 01:13:13 +0900 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=A7=20[WIP/TEST]=20=E3=82=A8=E3=82=B9?= =?UTF-8?q?=E3=82=B1=E3=83=BC=E3=83=97=E6=96=87=E5=AD=97=E3=81=AB=E3=82=88?= =?UTF-8?q?=E3=82=8B=E7=BF=BB=E8=A8=B3=E3=81=97=E3=81=AA=E3=81=84=E6=96=87?= =?UTF-8?q?=E7=AB=A0=E3=82=92=E8=A8=AD=E5=AE=9A=E3=81=A7=E3=81=8D=E3=82=8B?= =?UTF-8?q?=E3=82=88=E3=81=86=E3=81=AB=E5=AE=9F=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit エスケープ時の置換文字を0x1000から順番に規定 --- src-python/webui_controller.py | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/src-python/webui_controller.py b/src-python/webui_controller.py index 3d137d50..619a24c2 100644 --- a/src-python/webui_controller.py +++ b/src-python/webui_controller.py @@ -286,19 +286,21 @@ def stopThreadingTranscriptionReceiveMessageOnOpenConfigWindow(): # func message box def replaceExclamationsWithRandom(text): - characters = string.ascii_letters + string.digits # ![...] にマッチする正規表現 pattern = r'!\[(.*?)\]' # 乱数と置換部分を保存する辞書 replacement_dict = {} - # マッチした部分をランダムな整数に置換し、その対応を辞書に保存 + num = 4096 + # マッチした部分を4096から始まる整数に置換する。置換毎に4097, 4098, ... と増える def replace(match): - original = match.group(1) # ![]内のテキストを取得 - rand_value = ''.join(random.choices(characters, k=8)) # 8文字のランダムな文字列を生成 - replacement_dict[rand_value] = original # 辞書に保存 - return str(rand_value) # 置換する値 + original = match.group(1) + nonlocal num + rand_value = hex(num) + replacement_dict[rand_value] = original + num += 1 + return str(rand_value) # 文章内の ![] の部分を置換 replaced_text = re.sub(pattern, replace, text) @@ -306,9 +308,10 @@ def replaceExclamationsWithRandom(text): return replaced_text, replacement_dict def restoreText(escaped_text, escape_dict): - # 辞書のキーに対応する値でテキスト内を置換する + # 大文字小文字を無視して置換するために、正規表現を使う for escape_seq, char in escape_dict.items(): - escaped_text = escaped_text.replace(escape_seq, char) + # escape_seq の部分を case-insensitive で置換 + escaped_text = re.sub(re.escape(escape_seq), char, escaped_text, flags=re.IGNORECASE) return escaped_text def removeExclamations(text): @@ -334,6 +337,7 @@ class ChatMessage: if config.USE_EXCLUDE_WORDS is True: replacement_message, replacement_dict = replaceExclamationsWithRandom(message) translation, success = model.getInputTranslate(replacement_message) + message = removeExclamations(message) translation = restoreText(translation, replacement_dict) else: