Improve a couple of the modules

This commit is contained in:
DazedAnon 2025-02-10 17:17:19 -06:00
parent 53df5c4525
commit 64b4e2f15f
5 changed files with 199 additions and 177 deletions

View file

@ -229,7 +229,8 @@ from PIL import Image, ImageDraw, ImageFont
def stringToImageOutline(text, width, height, font_path="fonts/TsunagiGothic.ttf", scale_factor=4):
# Outline
outline_color = (0, 0, 0, 255)
outline_color = (255, 255, 255, 255)
text_color = (0, 0, 0, 255)
outline_thickness = 4
# Increase the resolution
@ -264,7 +265,7 @@ def stringToImageOutline(text, width, height, font_path="fonts/TsunagiGothic.ttf
draw.text((x + dx, y + dy), text, font=font, fill=outline_color)
# Draw the main text
draw.text((x, y), text, font=font, fill=(60, 160, 230, 255))
draw.text((x, y), text, font=font, fill=text_color)
# Resize back to the original dimensions to get a clearer text rendering
image = image.resize((width, height), Image.LANCZOS)

View file

@ -80,12 +80,12 @@ POSITION = 0
LEAVE = False
# Dialogue / Scroll / Choices (Main Codes)
CODE401 = True
CODE405 = True
CODE102 = True
CODE401 = False
CODE405 = False
CODE102 = False
# Optional
CODE101 = True # Turn this one when names exist in 101
CODE101 = False # Turn this one when names exist in 101
CODE408 = False # Warning, translates comments and can inflate costs.
# Variables
@ -256,6 +256,10 @@ def parseMap(data, filename):
totalTokens[0] += response[1][0]
totalTokens[1] += response[1][1]
event["name"] = response[0].replace('"', "")
if "<msgText:" in event["note"]:
tokensResponse = translateNote(event, r"<msgText:\"(.*?)\">", False)
totalTokens[0] += tokensResponse[0]
totalTokens[1] += tokensResponse[1]
for page in event["pages"]:
totalLines += len(page["list"])
@ -291,7 +295,7 @@ def parseMap(data, filename):
return [data, totalTokens, None]
def translateNote(event, regex):
def translateNote(event, regex, wordwrap=True):
# Regex String
jaString = event["note"]
match = re.findall(regex, jaString, re.DOTALL)
@ -314,8 +318,10 @@ def translateNote(event, regex):
tokens[1] += response[1][1]
# Textwrap
translatedText = textwrap.fill(translatedText, width=NOTEWIDTH)
translatedText = translatedText.replace('"', "")
if wordwrap:
translatedText = textwrap.fill(translatedText, width=NOTEWIDTH)
translatedText = translatedText.replace('"', "")
jaString = jaString.replace(initialJAString, translatedText)
event["note"] = jaString
i += 1
@ -1184,7 +1190,7 @@ def searchCodes(page, pbar, jobList, filename):
## Event Code: 122 [Set Variables]
if "code" in codeList[i] and codeList[i]["code"] == 122 and CODE122 is True:
# This is going to be the var being set. (IMPORTANT)
if codeList[i]["parameters"][0] not in list(range(18, 19)):
if codeList[i]["parameters"][0] not in list(range(20, 150)):
i += 1
continue
@ -1205,6 +1211,11 @@ def searchCodes(page, pbar, jobList, filename):
i += 1
continue
# Validate Japanese Text
if not re.search(LANGREGEX, jaString):
i += 1
continue
# Set String
matchedText = None
if len(re.findall(r"([\'\"\`])", jaString)) >= 2:
@ -1283,8 +1294,8 @@ def searchCodes(page, pbar, jobList, filename):
translatedText = translatedText.replace(char, "")
# Textwrap
translatedText = textwrap.fill(translatedText, 80)
translatedText = translatedText.replace("\n", "\\n")
# translatedText = textwrap.fill(translatedText, 80)
# translatedText = translatedText.replace("\n", "\\n")
# translatedText = re.sub(r"[\\]+c", r"\\\\c", translatedText)
translatedText = re.sub(r"[\\]+\*item", r"\\\\*item", translatedText)
@ -1313,6 +1324,7 @@ def searchCodes(page, pbar, jobList, filename):
"SoR_GabWindow": ("arg1", None),
"DarkPlasma_CharacterText": ("text", None),
"DTextPicture": ("text", None),
"TextPicture": ("text", None),
}
for key, (argVar, font) in headerMappings.items():
@ -2392,7 +2404,7 @@ def translateText(system, user, history, penalty, format, model=MODEL):
# History
if isinstance(history, list):
msg.append({"role": "assistant", "content": "Translation History:"})
msg.append({"role": "system", "content": "Translation History:"})
msg.extend([{"role": "assistant", "content": h} for h in history])
else:
msg.append({"role": "assistant", "content": history})

View file

@ -200,17 +200,21 @@ def translateUnity(data, pbar, filename, translatedList):
# Dialogue
while i < len(data):
# Lines
regex = r".*?=(.*)"
regex = r"(.*?)=(.*)"
match = re.search(regex, data[i])
if match != None and match.group(1) != "":
originalString = match.group(1)
if match:
leftString = match.group(1)
rightString = match.group(2)
# Validate Japanese Text
if not re.search(LANGREGEX, rightString) and IGNORETLTEXT:
i += 1
continue
# Pass 1
if translatedList == []:
# Grab Consecutive Strings
jaString = match.group(1)
# Remove textwrap
jaString = jaString.replace("\n", "")
jaString = leftString.replace("\n", "")
# Add String
stringList.append(jaString.strip())
@ -236,7 +240,7 @@ def translateUnity(data, pbar, filename, translatedList):
translatedText = translatedText.replace("=", "->")
# Set Data
data[i] = f"{originalString}{originalString}={translatedText}\n"
data[i] = f"{leftString}={translatedText}\n"
i += 1
# Nothing relevant. Skip Line.
@ -269,72 +273,43 @@ def translateUnity(data, pbar, filename, translatedList):
# Save some money and enter the character before translation
def getSpeaker(speaker):
if speaker not in str(NAMESLIST):
response = translateGPT(
speaker,
"Reply with the " + LANGUAGE + " translation of the NPC name.",
True,
)
response[0] = response[0].title()
response[0] = response[0].replace("'S", "'s")
match speaker:
case "ファイン":
return ["Fine", [0, 0]]
case "":
return ["", [0, 0]]
case _:
# Find Speaker
for i in range(len(NAMESLIST)):
if speaker == NAMESLIST[i][0]:
return [NAMESLIST[i][1], [0, 0]]
# Retry if name doesn't translate for some reason
if re.search(r"([a-zA-Z?])", response[0]) == None:
# Translate and Store Speaker
response = translateGPT(
speaker,
f"{speaker}",
"Reply with the " + LANGUAGE + " translation of the NPC name.",
False,
)
response[0] = response[0].title()
response[0] = response[0].replace("'S", "'s")
response[0] = response[0].replace("Speaker: ", "")
speakerList = [speaker, response[0]]
NAMESLIST.append(speakerList)
return response
# Find Speaker
else:
for i in range(len(NAMESLIST)):
if speaker == NAMESLIST[i][0]:
return [NAMESLIST[i][1], [0, 0]]
# Retry if name doesn't translate for some reason
if re.search(r"([a-zA-Z?])", response[0]) == None:
response = translateGPT(
f"{speaker}",
"Reply with the " + LANGUAGE + " translation of the NPC name.",
False,
)
response[0] = response[0].title()
response[0] = response[0].replace("'S", "'s")
speakerList = [speaker, response[0]]
NAMESLIST.append(speakerList)
return response
return [speaker, [0, 0]]
def subVars(jaString):
jaString = jaString.replace("\u3000", " ")
# Formatting
count = 0
codeList = re.findall(r"[\\]+[\w]+\[[a-zA-Z0-9\\\[\]\_,\s-]+\]", jaString)
codeList = set(codeList)
if len(codeList) != 0:
for var in codeList:
jaString = jaString.replace(var, "[FCode_" + str(count) + "]")
count += 1
# Put all lists in list and return
return [jaString, codeList]
def resubVars(translatedText, codeList):
# Fix Spacing and ChatGPT Nonsense
matchList = re.findall(r"\[\s?.+?\s?\]", translatedText)
if len(matchList) > 0:
for match in matchList:
text = match.strip()
translatedText = translatedText.replace(match, text)
# Formatting
count = 0
if len(codeList) != 0:
for var in codeList:
translatedText = translatedText.replace("[FCode_" + str(count) + "]", var)
count += 1
return translatedText
def batchList(input_list, batch_size):
if not isinstance(batch_size, int) or batch_size <= 0:
raise ValueError("batch_size must be a positive integer")
@ -372,9 +347,10 @@ def translateText(system, user, history, penalty, format, model=MODEL):
# History
if isinstance(history, list):
msg.extend([{"role": "system", "content": h} for h in history])
msg.append({"role": "system", "content": "Translation History:"})
msg.extend([{"role": "assistant", "content": h} for h in history])
else:
msg.append({"role": "system", "content": history})
msg.append({"role": "assistant", "content": history})
# Response Format
if format == "json":
@ -394,7 +370,7 @@ def translateText(system, user, history, penalty, format, model=MODEL):
return response
def cleanTranslatedText(translatedText, varResponse):
def cleanTranslatedText(translatedText):
placeholders = {
f"{LANGUAGE} Translation: ": "",
"Translation: ": "",
@ -405,15 +381,24 @@ def cleanTranslatedText(translatedText, varResponse):
"": '\\"',
"": '\\"',
"- ": "-",
"": "",
"": "]",
"": "[",
"é": "e",
"this guy": "this bastard",
"This guy": "This bastard",
"Placeholder Text": "",
# Add more replacements as needed
}
for target, replacement in placeholders.items():
translatedText = translatedText.replace(target, replacement)
# Remove Repeating Characters
pattern = re.compile(r"(.)\s*\1(?:\s*\1){" + str(20 - 1) + r",}")
translatedText = pattern.sub(lambda match: match.group(0).replace(" ", "")[:20], translatedText)
# Elongate Long Dashes (Since GPT Ignores them...)
translatedText = elongateCharacters(translatedText)
translatedText = resubVars(translatedText, varResponse[1])
return translatedText
@ -434,6 +419,8 @@ def elongateCharacters(text):
def extractTranslation(translatedTextList, is_list):
try:
translatedTextList = re.sub(r'\\"+\"([^,\n}])', r'\\"\1', translatedTextList)
translatedTextList = re.sub(r"(?<![\\])\"+(?![\n,])", r'"', translatedTextList)
line_dict = json.loads(translatedTextList)
# If it's a batch (i.e., list), extract with tags; otherwise, return the single item.
string_list = list(line_dict.values())
@ -443,7 +430,7 @@ def extractTranslation(translatedTextList, is_list):
return string_list[0]
except Exception as e:
print(f"extractTranslation Error: {e}")
PBAR.write(f"extractTranslation Error: {e} on String {translatedTextList}")
return None
@ -470,91 +457,117 @@ def countTokens(system, user, history):
@retry(exceptions=Exception, tries=5, delay=5)
def translateGPT(text, history, fullPromptFlag):
global PBAR, MISMATCH, FILENAME
if text:
with open("log/translationHistory.txt", "a+", encoding="utf-8") as logFile:
mismatch = False
totalTokens = [0, 0]
if isinstance(text, list):
format = "json"
tList = batchList(text, BATCHSIZE)
else:
format = "text"
tList = [text]
mismatch = False
totalTokens = [0, 0]
if isinstance(text, list):
format = "json"
tList = batchList(text, BATCHSIZE)
else:
format = "text"
tList = [text]
for index, tItem in enumerate(tList):
# Things to Check before starting translation
if not re.search(LANGREGEX, str(tItem)):
if PBAR is not None:
PBAR.update(len(tItem))
if isinstance(tItem, list):
for j in range(len(tItem)):
tItem[j] = cleanTranslatedText(tItem[j])
tList[index] = tItem
else:
tList[index] = cleanTranslatedText(tItem)
history = tItem[-MAXHISTORY:]
continue
for index, tItem in enumerate(tList):
# Before sending to translation, if we have a list of items, add the formatting
if isinstance(tItem, list):
payload = {f"Line{i+1}": string for i, string in enumerate(tItem)}
payload = json.dumps(payload, indent=4, ensure_ascii=False)
varResponse = subVars(payload)
subbedT = varResponse[0]
else:
varResponse = subVars(tItem)
subbedT = varResponse[0]
# Before sending to translation, if we have a list of items, add the formatting
if isinstance(tItem, list):
for j in range(len(tItem)):
if not tItem[j]:
tItem[j] = tItem[j].replace("", "Placeholder Text")
payload = {f"Line{i+1}": string for i, string in enumerate(tItem)}
payload = json.dumps(payload, indent=4, ensure_ascii=False)
varResponse = [payload, []]
subbedT = varResponse[0]
else:
varResponse = [tItem, []]
subbedT = varResponse[0]
# Things to Check before starting translation
if not re.search(LANGREGEX, subbedT):
if PBAR is not None:
PBAR.update(len(tItem))
continue
# Create Message
system, user = createContext(fullPromptFlag, subbedT, format)
# Create Message
system, user = createContext(fullPromptFlag, subbedT, format)
# Calculate Estimate
if ESTIMATE:
estimate = countTokens(system, user, history)
totalTokens[0] += estimate[0]
totalTokens[1] += estimate[1]
continue
# Calculate Estimate
if ESTIMATE:
estimate = countTokens(system, user, history)
totalTokens[0] += estimate[0]
totalTokens[1] += estimate[1]
continue
# Translating
response = translateText(system, user, history, 0.05, format)
# Translating
response = translateText(system, user, history, 0.05, format)
translatedText = response.choices[0].message.content
totalTokens[0] += response.usage.prompt_tokens
totalTokens[1] += response.usage.completion_tokens
# Check Translation
translatedText = cleanTranslatedText(translatedText, varResponse)
if isinstance(tItem, list):
extractedTranslations = extractTranslation(translatedText, True)
if extractedTranslations == None or len(tItem) != len(extractedTranslations):
# Mismatch. Try Again
response = translateText(system, user, history, 0.05, format, MODEL)
# Set Tokens
translatedText = response.choices[0].message.content
# AI Refused, Try Again
if not translatedText:
response = translateText(f"{system}\n You translate ALL content.", user, history, 0.1, format)
# Report Tokens
totalTokens[0] += response.usage.prompt_tokens
totalTokens[1] += response.usage.completion_tokens
# Formatting
translatedText = cleanTranslatedText(translatedText, varResponse)
if isinstance(tItem, list):
extractedTranslations = extractTranslation(translatedText, True)
if extractedTranslations == None or len(tItem) != len(extractedTranslations):
mismatch = True # Just here for breakpoint
# Check Translation
if translatedText:
translatedText = cleanTranslatedText(translatedText)
if isinstance(tItem, list):
extractedTranslations = extractTranslation(translatedText, True)
if extractedTranslations == None or len(tItem) != len(extractedTranslations):
# Mismatch. Try Again
response = translateText(system, user, history, 0.05, format, MODEL)
translatedText = response.choices[0].message.content
totalTokens[0] += response.usage.prompt_tokens
totalTokens[1] += response.usage.completion_tokens
# Set if no mismatch
if mismatch == False:
tList[index] = extractedTranslations
history = extractedTranslations[-10:] # Update history if we have a list
else:
history = text[-10:]
mismatch = False
if FILENAME not in MISMATCH:
MISMATCH.append(FILENAME)
# Formatting
translatedText = cleanTranslatedText(translatedText)
if isinstance(tItem, list):
extractedTranslations = extractTranslation(translatedText, True)
if extractedTranslations == None or len(tItem) != len(extractedTranslations):
mismatch = True # Just here for breakpoint
logFile.write(f"Input:\n{subbedT}\n")
logFile.write(f"Output:\n{translatedText}\n")
# Update Loading Bar
with LOCK:
if PBAR is not None:
PBAR.update(len(tItem))
# Set if no mismatch
if mismatch == False:
tList[index] = extractedTranslations
history = extractedTranslations[-MAXHISTORY:] # Update history if we have a list
else:
history = text[-MAXHISTORY:]
mismatch = False
if FILENAME not in MISMATCH:
MISMATCH.append(FILENAME)
# Update Loading Bar
with LOCK:
if PBAR is not None:
PBAR.update(len(tItem))
else:
# Ensure we're passing a single string to extractTranslation
tList[index] = translatedText.replace("Placeholder Text", "")
else:
PBAR.write(f"AI Refused:{tItem}\n")
# Combine if multilist
if isinstance(tList[0], list):
tList = [t for sublist in tList for t in sublist]
# Return
if format == "json":
return [tList, totalTokens]
else:
# Ensure we're passing a single string to extractTranslation
tList[index] = translatedText.replace("Placeholder Text", "")
# Combine if multilist
if isinstance(tList[0], list):
tList = [t for sublist in tList for t in sublist]
# Return
if format == "json":
return [tList, totalTokens]
return [tList[0], totalTokens]
else:
return [tList[0], totalTokens]
return [text, [0, 0]]

View file

@ -85,8 +85,8 @@ CODE101 = False
CODE102 = False
# Set String (Fragile but necessary)
CODE122 = True
CODE150 = False
CODE122 = False
CODE150 = True
# Other
CODE210 = False

View file

@ -1,16 +1,14 @@
Here are some vocabulary and terms so that you know the proper spelling and translation.
```
# Game Characters
アイ (Ai) - Female
シオン (Sion) - Female
エリス (Eris) - Female
杉浦 (Sugiura) - Female
獏木 (Bakuki) - Female
千里 (Chisato) - Female
ハルカ (Haruka) - Female
間根山 (Manemaya) - Female
ミズキ (Mizuki) - Female
ツヨシ (Tsuyoshi) - Male
リク (Riku) - Male
シエンナ (Sienna) - Female
ミラ (Mira) - Female
ライオネス (Lioness) - Male
モーングロウ (Mournglow)
デボラ (Deborah)
レジナルド (Reginald) - Male
クスルーツ (Kuslutz)
# Lewd Terms
マンコ (pussy)
@ -45,7 +43,7 @@ Here are some vocabulary and terms so that you know the proper spelling and tran
ねえさん (nee-san)
おじさん (oji-san)
# System
# Terms
初めから (Start)
逃げる (Escape)
大事なもの (Key Items)
@ -64,19 +62,15 @@ Here are some vocabulary and terms so that you know the proper spelling and tran
持っている数 (Owned)
ME 音量 (ME Volume)
回想する (Recollection)
# RPG
エクスポーション (EX Potion)
アスカロン (Ascalon)
刀 (Sword)
ゴブリン (Goblin)
信仰心 (Faith)
会話 (Conversation)
収集 (Collect)
討伐 (Subjugate)
破滅 (Destruction)
魂 (Soul)
巫女 (Shrine Maiden)
# Demons/Angels
# Demons/Angels/Monsters
悪魔 (Devil)
上級悪魔 (Arch Devil)
歪魔 (Distorted Devil)
@ -94,13 +88,15 @@ ME 音量 (ME Volume)
飛天魔 (Nephilim)
堕天使 (Fallen Angel)
鬼 (Oni)
魔 (Demon)
妖怪 (Yokai)
式神 (Shikigami)
# Terms
幽霊 (Ghost)
デーモン (Daemon)
ローバー (Roper)
# Other
w ((lol))
巫女 (Shrine Maiden)
2万 (20,000)
```