Merge branch 'exclude_word' into for_webui

This commit is contained in:
misyaguziya
2024-09-12 23:14:29 +09:00
2 changed files with 59 additions and 1 deletions

View File

@@ -690,6 +690,17 @@ class Config:
self._AUTH_KEYS[key] = value self._AUTH_KEYS[key] = value
saveJson(self.PATH_CONFIG, inspect.currentframe().f_code.co_name, self.AUTH_KEYS) saveJson(self.PATH_CONFIG, inspect.currentframe().f_code.co_name, self.AUTH_KEYS)
@property
@json_serializable('USE_EXCLUDE_WORDS')
def USE_EXCLUDE_WORDS(self):
return self._USE_EXCLUDE_WORDS
@USE_EXCLUDE_WORDS.setter
def USE_EXCLUDE_WORDS(self, value):
if isinstance(value, bool):
self._USE_EXCLUDE_WORDS = value
saveJson(self.PATH_CONFIG, inspect.currentframe().f_code.co_name, value)
@property @property
@json_serializable('USE_TRANSLATION_FEATURE') @json_serializable('USE_TRANSLATION_FEATURE')
def USE_TRANSLATION_FEATURE(self): def USE_TRANSLATION_FEATURE(self):
@@ -1129,6 +1140,7 @@ class Config:
self._AUTH_KEYS = { self._AUTH_KEYS = {
"DeepL_API": None, "DeepL_API": None,
} }
self._USE_EXCLUDE_WORDS = True
self._USE_TRANSLATION_FEATURE = True self._USE_TRANSLATION_FEATURE = True
self._CTRANSLATE2_WEIGHT_TYPE = "Small" self._CTRANSLATE2_WEIGHT_TYPE = "Small"
self._USE_WHISPER_FEATURE = False self._USE_WHISPER_FEATURE = False

View File

@@ -2,6 +2,7 @@ from typing import Callable, Union
from time import sleep from time import sleep
from subprocess import Popen from subprocess import Popen
from threading import Thread from threading import Thread
import re
from config import config from config import config
from model import model from model import model
from utils import getKeyByValue, isUniqueStrings, printLog, printResponse from utils import getKeyByValue, isUniqueStrings, printLog, printResponse
@@ -292,6 +293,43 @@ def stopThreadingTranscriptionReceiveMessageOnOpenConfigWindow():
th_stopTranscriptionReceiveMessage.start() th_stopTranscriptionReceiveMessage.start()
# func message box # func message box
def replaceExclamationsWithRandom(text):
# ![...] にマッチする正規表現
pattern = r'!\[(.*?)\]'
# 乱数と置換部分を保存する辞書
replacement_dict = {}
num = 4096
# マッチした部分を4096から始まる整数に置換する。置換毎に4097, 4098, ... と増える
def replace(match):
original = match.group(1)
nonlocal num
rand_value = hex(num)
replacement_dict[rand_value] = original
num += 1
return f" ${rand_value} "
# 文章内の ![] の部分を置換
replaced_text = re.sub(pattern, replace, text)
return replaced_text, replacement_dict
def restoreText(escaped_text, escape_dict):
# 大文字小文字を無視して置換するために、正規表現を使う
for escape_seq, char in escape_dict.items():
# escaped_text の部分を pattern で置換
pattern = re.escape(f"${escape_seq}") + r"|\$\s+" + re.escape(escape_seq)
escaped_text = re.sub(pattern, char, escaped_text, flags=re.IGNORECASE)
return escaped_text
def removeExclamations(text):
# ![...] を [...] に置換する正規表現
pattern = r'!\[(.*?)\]'
# ![...] の部分を [] 内のテキストに置換
cleaned_text = re.sub(pattern, r'\1', text)
return cleaned_text
class ChatMessage: class ChatMessage:
def __init__(self, action:Callable[[dict], None]) -> None: def __init__(self, action:Callable[[dict], None]) -> None:
self.action = action self.action = action
@@ -306,7 +344,15 @@ class ChatMessage:
if config.ENABLE_TRANSLATION is False: if config.ENABLE_TRANSLATION is False:
pass pass
else: else:
translation, success = model.getInputTranslate(message) 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:
translation, success = model.getInputTranslate(message)
if success is False: if success is False:
changeToCTranslate2Process() changeToCTranslate2Process()
self.action("error_translation_engine", { self.action("error_translation_engine", {