Extend ruff format line length

This commit is contained in:
DazedAnon 2024-12-21 13:00:26 -06:00
parent cc65eac115
commit a20788ca11
25 changed files with 252 additions and 831 deletions

View file

@ -117,19 +117,14 @@ def getResultString(translatedData, translationTime, filename):
Fore.YELLOW + "[Input: " + str(translatedData[1][0]) + "]"
"[Output: "
+ str(translatedData[1][1])
+ "]" "[Cost: ${:,.4f}".format(
(translatedData[1][0] * 0.001 * INPUTAPICOST)
+ (translatedData[1][1] * 0.001 * OUTPUTAPICOST)
)
+ "]" "[Cost: ${:,.4f}".format((translatedData[1][0] * 0.001 * INPUTAPICOST) + (translatedData[1][1] * 0.001 * OUTPUTAPICOST))
+ "]"
)
timeString = Fore.BLUE + "[" + str(round(translationTime, 1)) + "s]"
if translatedData[2] == None:
# Success
return (
filename + ": " + totalTokenstring + timeString + Fore.GREEN + " \u2713 " + Fore.RESET
)
return filename + ": " + totalTokenstring + timeString + Fore.GREEN + " \u2713 " + Fore.RESET
else:
# Fail
@ -138,16 +133,7 @@ def getResultString(translatedData, translationTime, filename):
except Exception as e:
traceback.print_exc()
errorString = str(e) + Fore.RED
return (
filename
+ ": "
+ totalTokenstring
+ timeString
+ Fore.RED
+ " \u2717 "
+ errorString
+ Fore.RESET
)
return filename + ": " + totalTokenstring + timeString + Fore.RED + " \u2717 " + errorString + Fore.RESET
def parseText(data, filename):
@ -201,10 +187,7 @@ def translateLines(linesList, pbar):
speakerMatch = re.findall(r"s\[[0-9]+\] = \"([^]+)\"", linesList[i - 1])
if len(speakerMatch) > 0:
# If there isn't any Japanese in the text just skip
if (
re.search(r"[一-龠]+|[ぁ-ゔ]+|[ァ-ヴー]+", jaString)
and "_" not in speakerMatch[0]
):
if re.search(r"[一-龠]+|[ぁ-ゔ]+|[ァ-ヴー]+", jaString) and "_" not in speakerMatch[0]:
speaker = speakerMatch[0]
else:
speaker = ""
@ -219,18 +202,13 @@ def translateLines(linesList, pbar):
linesList[i] = re.sub(r"(s\[[0-9]+\]) = \"(.+)\"", r'\1 = ""', linesList[i])
linesList[i] = linesList[i].replace(";", "")
start = i
while (
len(linesList) > i + 1
and re.search(r"s\[[0-9]+\] = \"\s+(.*)\"", linesList[i + 1]) != None
):
while len(linesList) > i + 1 and re.search(r"s\[[0-9]+\] = \"\s+(.*)\"", linesList[i + 1]) != None:
multiLine = True
i += 1
match = re.findall(r"s\[[0-9]+\] = \"\s+(.*)\"", linesList[i])
currentGroup.append(match[0])
if insertBool is True:
linesList[i] = re.sub(
r"(s\[[0-9]+\]) = \"\s+(.+)\"", r'\1 = ""', linesList[i]
)
linesList[i] = re.sub(r"(s\[[0-9]+\]) = \"\s+(.+)\"", r'\1 = ""', linesList[i])
linesList[i] = linesList[i].replace(";", "")
i += 1
@ -527,11 +505,7 @@ def extractTranslation(translatedTextList, is_list):
pattern = r"<Line(\d+)>[\\]*`?(.*?)[\\]*?`?</?Line\d+>"
# If it's a batch (i.e., list), extract with tags; otherwise, return the single item.
if is_list:
return [
re.findall(pattern, line)[0][1]
for line in translatedTextList
if re.search(pattern, line)
]
return [re.findall(pattern, line)[0][1] for line in translatedTextList if re.search(pattern, line)]
else:
matchList = re.findall(pattern, translatedTextList)
return matchList[0][1] if matchList else translatedTextList

View file

@ -124,19 +124,14 @@ def getResultString(translatedData, translationTime, filename):
Fore.YELLOW + "[Input: " + str(translatedData[1][0]) + "]"
"[Output: "
+ str(translatedData[1][1])
+ "]" "[Cost: ${:,.4f}".format(
(translatedData[1][0] * 0.001 * INPUTAPICOST)
+ (translatedData[1][1] * 0.001 * OUTPUTAPICOST)
)
+ "]" "[Cost: ${:,.4f}".format((translatedData[1][0] * 0.001 * INPUTAPICOST) + (translatedData[1][1] * 0.001 * OUTPUTAPICOST))
+ "]"
)
timeString = Fore.BLUE + "[" + str(round(translationTime, 1)) + "s]"
if translatedData[2] == None:
# Success
return (
filename + ": " + totalTokenstring + timeString + Fore.GREEN + " \u2713 " + Fore.RESET
)
return filename + ": " + totalTokenstring + timeString + Fore.GREEN + " \u2713 " + Fore.RESET
else:
# Fail
@ -145,16 +140,7 @@ def getResultString(translatedData, translationTime, filename):
except Exception as e:
traceback.print_exc()
errorString = str(e) + Fore.RED
return (
filename
+ ": "
+ totalTokenstring
+ timeString
+ Fore.RED
+ " \u2717 "
+ errorString
+ Fore.RESET
)
return filename + ": " + totalTokenstring + timeString + Fore.RED + " \u2717 " + errorString + Fore.RESET
def parseJSON(data, filename):

View file

@ -27,9 +27,7 @@ INPUTAPICOST = 0.002 # Depends on the model https://openai.com/pricing
OUTPUTAPICOST = 0.002
PROMPT = Path("prompt.txt").read_text(encoding="utf-8")
VOCAB = Path("vocab.txt").read_text(encoding="utf-8")
THREADS = int(
os.getenv("threads")
) # Controls how many threads are working on a single file (May have to drop this)
THREADS = int(os.getenv("threads")) # Controls how many threads are working on a single file (May have to drop this)
LOCK = threading.Lock()
WIDTH = int(os.getenv("width"))
LISTWIDTH = int(os.getenv("listWidth"))
@ -98,19 +96,14 @@ def getResultString(translatedData, translationTime, filename):
Fore.YELLOW + "[Input: " + str(translatedData[1][0]) + "]"
"[Output: "
+ str(translatedData[1][1])
+ "]" "[Cost: ${:,.4f}".format(
(translatedData[1][0] * 0.001 * INPUTAPICOST)
+ (translatedData[1][1] * 0.001 * OUTPUTAPICOST)
)
+ "]" "[Cost: ${:,.4f}".format((translatedData[1][0] * 0.001 * INPUTAPICOST) + (translatedData[1][1] * 0.001 * OUTPUTAPICOST))
+ "]"
)
timeString = Fore.BLUE + "[" + str(round(translationTime, 1)) + "s]"
if translatedData[2] is None:
# Success
return (
filename + ": " + totalTokenstring + timeString + Fore.GREEN + " \u2713 " + Fore.RESET
)
return filename + ": " + totalTokenstring + timeString + Fore.GREEN + " \u2713 " + Fore.RESET
else:
# Fail
@ -118,16 +111,7 @@ def getResultString(translatedData, translationTime, filename):
raise translatedData[2]
except Exception as e:
errorString = str(e) + Fore.RED
return (
filename
+ ": "
+ totalTokenstring
+ timeString
+ Fore.RED
+ " \u2717 "
+ errorString
+ Fore.RESET
)
return filename + ": " + totalTokenstring + timeString + Fore.RED + " \u2717 " + errorString + Fore.RESET
def parseText(data, filename):
@ -356,13 +340,7 @@ def translateGPT(t, history, fullPromptFlag):
system = PROMPT
user = "Line to Translate = " + subbedT
else:
system = (
"Output ONLY the "
+ LANGUAGE
+ " translation in the following format: `Translation: <"
+ LANGUAGE.upper()
+ "_TRANSLATION>`"
)
system = "Output ONLY the " + LANGUAGE + " translation in the following format: `Translation: <" + LANGUAGE.upper() + "_TRANSLATION>`"
user = "Line to Translate = " + subbedT
# Create Message List
@ -412,10 +390,7 @@ def translateGPT(t, history, fullPromptFlag):
translatedText = translatedText.replace("", "!")
# Return Translation
if (
len(translatedText) > 15 * len(t)
or "I'm sorry, but I'm unable to assist with that translation" in translatedText
):
if len(translatedText) > 15 * len(t) or "I'm sorry, but I'm unable to assist with that translation" in translatedText:
raise Exception
else:
return [translatedText, totalTokens]

View file

@ -63,6 +63,7 @@ BAR_FORMAT = "{l_bar}{bar:10}{r_bar}{bar:-10b}"
POSITION = 0
LEAVE = False
PBAR = None
ENCODING = "cp932"
def handleCSV(filename, estimate):
@ -70,7 +71,7 @@ def handleCSV(filename, estimate):
ESTIMATE = estimate
if not ESTIMATE:
with open("translated/" + filename, "w+t", newline="", encoding="utf-8-sig") as writeFile:
with open("translated/" + filename, "w+t", newline="", encoding=ENCODING) as writeFile:
# Translate
start = time.time()
translatedData = openFiles(filename, writeFile)
@ -104,14 +105,14 @@ def handleCSV(filename, estimate):
def openFiles(filename, writeFile):
with open("files/" + filename, "r", encoding="utf-8-sig") as readFile, writeFile:
with open("files/" + filename, "r", encoding="cp932") as readFile, writeFile:
translatedData = parseCSV(readFile, writeFile, filename)
return translatedData
def openFilesEstimate(filename):
with open("files/" + filename, "r", encoding="utf-8-sig") as readFile:
with open("files/" + filename, "r", encoding="cp932") as readFile:
translatedData = parseCSV(readFile, "", filename)
return translatedData
@ -123,19 +124,14 @@ def getResultString(translatedData, translationTime, filename):
Fore.YELLOW + "[Input: " + str(translatedData[1][0]) + "]"
"[Output: "
+ str(translatedData[1][1])
+ "]" "[Cost: ${:,.4f}".format(
(translatedData[1][0] * 0.001 * INPUTAPICOST)
+ (translatedData[1][1] * 0.001 * OUTPUTAPICOST)
)
+ "]" "[Cost: ${:,.4f}".format((translatedData[1][0] * 0.001 * INPUTAPICOST) + (translatedData[1][1] * 0.001 * OUTPUTAPICOST))
+ "]"
)
timeString = Fore.BLUE + "[" + str(round(translationTime, 1)) + "s]"
if translatedData[2] is None:
# Success
return (
filename + ": " + totalTokenstring + timeString + Fore.GREEN + " \u2713 " + Fore.RESET
)
return filename + ": " + totalTokenstring + timeString + Fore.GREEN + " \u2713 " + Fore.RESET
else:
# Fail
try:
@ -143,16 +139,7 @@ def getResultString(translatedData, translationTime, filename):
except Exception as e:
traceback.print_exc()
errorString = str(e) + Fore.RED
return (
filename
+ ": "
+ totalTokenstring
+ timeString
+ Fore.RED
+ " \u2717 "
+ errorString
+ Fore.RESET
)
return filename + ": " + totalTokenstring + timeString + Fore.RED + " \u2717 " + errorString + Fore.RESET
def parseCSV(readFile, writeFile, filename):
@ -161,8 +148,8 @@ def parseCSV(readFile, writeFile, filename):
global LOCK
format = ""
while format not in ["1", "2", "3"]:
format = input("\n\nSelect the CSV Format:\n\n1. Translator++\n2. Single\n3. Multiple\n")
while format not in ["1", "2", "3", "4"]:
format = input("\n\nSelect the CSV Format:\n\n1. Translator++\n2. Single\n3. Multiple\n4. Speaker&Text\n")
match format:
case "1":
format = "1"
@ -170,6 +157,8 @@ def parseCSV(readFile, writeFile, filename):
format = "2"
case "3":
format = "3"
case "4":
format = "4"
# Get total for progress bar
totalLines = len(readFile.readlines())
@ -275,7 +264,7 @@ def translateCSV(data, pbar, writer, filename, translatedList, format):
# Iterate
i += 1
# All Format
# In Place Format
case "3":
# Set columns to translate. Leave empty to translate all.
targetColumns = []
@ -315,6 +304,52 @@ def translateCSV(data, pbar, writer, filename, translatedList, format):
# Iterate
i += 1
# Speaker & Text Format
case "4":
# Set columns to translate. Leave empty to translate all.
speakerColumn = 8
textColumn = 20
speaker = ""
if len(data[i]) > textColumn:
# Speaker
if data[i][speakerColumn]:
speakerResponse = getSpeaker(data[i][speakerColumn])
totalTokens[0] += speakerResponse[1][0]
totalTokens[1] += speakerResponse[1][1]
speaker = speakerResponse[0]
data[i][speakerColumn] = speaker
# Check if Translated
jaString = data[i][textColumn]
# Remove Textwrap
jaString = jaString.replace("\\n", " ")
# Pass 1
if not translatedList:
stringList.append(jaString)
# Pass 2
else:
# Grab and Pop
translatedText = translatedList[0]
translatedList.pop(0)
# Remove speaker
if speaker:
translatedText = re.sub(r"^\[?(.+?)\]?\s?[|:]\s?", "", translatedText)
# Add Wordwrap
translatedText = textwrap.fill(translatedText, WIDTH)
translatedText = translatedText.replace("\n", "\\n")
# Set Data
data[i][textColumn] = translatedText
# Iterate
i += 1
# EOF
if len(stringList) > 0:
# Set Progress
@ -369,6 +404,10 @@ def getSpeaker(speaker):
if speaker == NAMESLIST[i][0]:
return [NAMESLIST[i][1], [0, 0]]
# If there isn't any Japanese in the text just skip
if not re.search(r"[一-龠ぁ-ゔァ-ヴーa---\uFF61-\uFF9F]+", speaker):
return [speaker, [0, 0]]
# Translate and Store Speaker
response = translateGPT(
f"{speaker}",
@ -600,9 +639,7 @@ def translateGPT(text, history, fullPromptFlag):
translatedText = cleanTranslatedText(translatedText, varResponse)
if isinstance(tItem, list):
extractedTranslations = extractTranslation(translatedText, True)
if extractedTranslations == None or len(tItem) != len(
extractedTranslations
):
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")
@ -610,9 +647,7 @@ def translateGPT(text, history, fullPromptFlag):
# Set if no mismatch
if mismatch == False:
tList[index] = extractedTranslations
history = extractedTranslations[
-MAXHISTORY:
] # Update history if we have a list
history = extractedTranslations[-MAXHISTORY:] # Update history if we have a list
else:
history = text[-MAXHISTORY:]
mismatch = False

View file

@ -110,19 +110,14 @@ def getResultString(translatedData, translationTime, filename):
Fore.YELLOW + "[Input: " + str(translatedData[1][0]) + "]"
"[Output: "
+ str(translatedData[1][1])
+ "]" "[Cost: ${:,.4f}".format(
(translatedData[1][0] * 0.001 * INPUTAPICOST)
+ (translatedData[1][1] * 0.001 * OUTPUTAPICOST)
)
+ "]" "[Cost: ${:,.4f}".format((translatedData[1][0] * 0.001 * INPUTAPICOST) + (translatedData[1][1] * 0.001 * OUTPUTAPICOST))
+ "]"
)
timeString = Fore.BLUE + "[" + str(round(translationTime, 1)) + "s]"
if translatedData[2] == None:
# Success
return (
filename + ": " + totalTokenstring + timeString + Fore.GREEN + " \u2713 " + Fore.RESET
)
return filename + ": " + totalTokenstring + timeString + Fore.GREEN + " \u2713 " + Fore.RESET
else:
# Fail
@ -131,16 +126,7 @@ def getResultString(translatedData, translationTime, filename):
except Exception as e:
traceback.print_exc()
errorString = str(e) + Fore.RED
return (
filename
+ ": "
+ totalTokenstring
+ timeString
+ Fore.RED
+ " \u2717 "
+ errorString
+ Fore.RESET
)
return filename + ": " + totalTokenstring + timeString + Fore.RED + " \u2717 " + errorString + Fore.RESET
def openFiles(filename):
@ -208,9 +194,7 @@ def translateEushully(data, pbar, filename, translatedList):
originalString = match.group(2)
jaString = match.group(2)
currentGroup = [jaString]
while "end-text-line" in data[i + 1] and any(
x in data[i + 2] for x in ["show-text"]
):
while "end-text-line" in data[i + 1] and any(x in data[i + 2] for x in ["show-text"]):
match = re.search(regex, data[i + 2])
if match != None:
currentGroup.append(match.group(2))

View file

@ -137,19 +137,14 @@ def getResultString(translatedData, translationTime, filename):
Fore.YELLOW + "[Input: " + str(translatedData[1][0]) + "]"
"[Output: "
+ str(translatedData[1][1])
+ "]" "[Cost: ${:,.4f}".format(
(translatedData[1][0] * 0.001 * INPUTAPICOST)
+ (translatedData[1][1] * 0.001 * OUTPUTAPICOST)
)
+ "]" "[Cost: ${:,.4f}".format((translatedData[1][0] * 0.001 * INPUTAPICOST) + (translatedData[1][1] * 0.001 * OUTPUTAPICOST))
+ "]"
)
timeString = Fore.BLUE + "[" + str(round(translationTime, 1)) + "s]"
if translatedData[2] is None:
# Success
return (
filename + ": " + totalTokenstring + timeString + Fore.GREEN + " \u2713 " + Fore.RESET
)
return filename + ": " + totalTokenstring + timeString + Fore.GREEN + " \u2713 " + Fore.RESET
else:
# Fail
try:
@ -157,16 +152,7 @@ def getResultString(translatedData, translationTime, filename):
except Exception as e:
traceback.print_exc()
errorString = str(e) + Fore.RED
return (
filename
+ ": "
+ totalTokenstring
+ timeString
+ Fore.RED
+ " \u2717 "
+ errorString
+ Fore.RESET
)
return filename + ": " + totalTokenstring + timeString + Fore.RED + " \u2717 " + errorString + Fore.RESET
def getFontSize(text, image_width, image_height, font_path):
@ -262,9 +248,7 @@ def processImagesDir(directory_path, imageList):
line = line.replace(":", "")
line = line.replace("/", "")
line = line.replace("?", "")
imageList[0].append(
line
) # Using strip() to remove any extra newlines or spaces
imageList[0].append(line) # Using strip() to remove any extra newlines or spaces
imageList[1].append([104, 15])
except FileNotFoundError:
print(f"The file at {file_path} was not found.")

View file

@ -109,19 +109,14 @@ def getResultString(translatedData, translationTime, filename):
Fore.YELLOW + "[Input: " + str(translatedData[1][0]) + "]"
"[Output: "
+ str(translatedData[1][1])
+ "]" "[Cost: ${:,.4f}".format(
(translatedData[1][0] * 0.001 * INPUTAPICOST)
+ (translatedData[1][1] * 0.001 * OUTPUTAPICOST)
)
+ "]" "[Cost: ${:,.4f}".format((translatedData[1][0] * 0.001 * INPUTAPICOST) + (translatedData[1][1] * 0.001 * OUTPUTAPICOST))
+ "]"
)
timeString = Fore.BLUE + "[" + str(round(translationTime, 1)) + "s]"
if translatedData[2] == None:
# Success
return (
filename + ": " + totalTokenstring + timeString + Fore.GREEN + " \u2713 " + Fore.RESET
)
return filename + ": " + totalTokenstring + timeString + Fore.GREEN + " \u2713 " + Fore.RESET
else:
# Fail
@ -130,16 +125,7 @@ def getResultString(translatedData, translationTime, filename):
except Exception as e:
traceback.print_exc()
errorString = str(e) + Fore.RED
return (
filename
+ ": "
+ totalTokenstring
+ timeString
+ Fore.RED
+ " \u2717 "
+ errorString
+ Fore.RESET
)
return filename + ": " + totalTokenstring + timeString + Fore.RED + " \u2717 " + errorString + Fore.RESET
def openFiles(filename):
@ -195,10 +181,7 @@ def translateIris(data, pbar, filename, translatedList):
if "#MSG," in data[i] or "#MSG\n" in data[i] or voice == True:
i += 1
# Speaker
if (
re.search(r'^ ?([^#\/."、。*!\(\)\[\] \n]+)\n', data[i])
and len(data[i]) < 30
):
if re.search(r'^ ?([^#\/."、。*!\(\)\[\] \n]+)\n', data[i]) and len(data[i]) < 30:
match = re.search(r"(.*)", data[i])
if match != None:
speaker = match.group(1)

View file

@ -109,19 +109,14 @@ def getResultString(translatedData, translationTime, filename):
Fore.YELLOW + "[Input: " + str(translatedData[1][0]) + "]"
"[Output: "
+ str(translatedData[1][1])
+ "]" "[Cost: ${:,.4f}".format(
(translatedData[1][0] * 0.001 * INPUTAPICOST)
+ (translatedData[1][1] * 0.001 * OUTPUTAPICOST)
)
+ "]" "[Cost: ${:,.4f}".format((translatedData[1][0] * 0.001 * INPUTAPICOST) + (translatedData[1][1] * 0.001 * OUTPUTAPICOST))
+ "]"
)
timeString = Fore.BLUE + "[" + str(round(translationTime, 1)) + "s]"
if translatedData[2] == None:
# Success
return (
filename + ": " + totalTokenstring + timeString + Fore.GREEN + " \u2713 " + Fore.RESET
)
return filename + ": " + totalTokenstring + timeString + Fore.GREEN + " \u2713 " + Fore.RESET
else:
# Fail
@ -130,16 +125,7 @@ def getResultString(translatedData, translationTime, filename):
except Exception as e:
traceback.print_exc()
errorString = str(e) + Fore.RED
return (
filename
+ ": "
+ totalTokenstring
+ timeString
+ Fore.RED
+ " \u2717 "
+ errorString
+ Fore.RESET
)
return filename + ": " + totalTokenstring + timeString + Fore.RED + " \u2717 " + errorString + Fore.RESET
def openFiles(filename):
@ -187,9 +173,7 @@ def translateJS(data, pbar):
modifiedStringList[j] = modifiedStringList[j].replace(r"\\\\\\\\n", r" ")
# Translate
response = translateGPT(
modifiedStringList, f"Reply with the {LANGUAGE} translation", True, pbar
)
response = translateGPT(modifiedStringList, f"Reply with the {LANGUAGE} translation", True, pbar)
translatedList = response[0]
tokens[0] = response[1][0]
tokens[0] = response[1][1]

View file

@ -116,19 +116,14 @@ def getResultString(translatedData, translationTime, filename):
Fore.YELLOW + "[Input: " + str(translatedData[1][0]) + "]"
"[Output: "
+ str(translatedData[1][1])
+ "]" "[Cost: ${:,.4f}".format(
(translatedData[1][0] * 0.001 * INPUTAPICOST)
+ (translatedData[1][1] * 0.001 * OUTPUTAPICOST)
)
+ "]" "[Cost: ${:,.4f}".format((translatedData[1][0] * 0.001 * INPUTAPICOST) + (translatedData[1][1] * 0.001 * OUTPUTAPICOST))
+ "]"
)
timeString = Fore.BLUE + "[" + str(round(translationTime, 1)) + "s]"
if translatedData[2] == None:
# Success
return (
filename + ": " + totalTokenstring + timeString + Fore.GREEN + " \u2713 " + Fore.RESET
)
return filename + ": " + totalTokenstring + timeString + Fore.GREEN + " \u2713 " + Fore.RESET
else:
# Fail
@ -137,16 +132,7 @@ def getResultString(translatedData, translationTime, filename):
except Exception as e:
traceback.print_exc()
errorString = str(e) + Fore.RED
return (
filename
+ ": "
+ totalTokenstring
+ timeString
+ Fore.RED
+ " \u2717 "
+ errorString
+ Fore.RESET
)
return filename + ": " + totalTokenstring + timeString + Fore.RED + " \u2717 " + errorString + Fore.RESET
def parseJSON(data, filename):
@ -541,9 +527,7 @@ def translateGPT(text, history, fullPromptFlag):
translatedText = cleanTranslatedText(translatedText, varResponse)
if isinstance(tItem, list):
extractedTranslations = extractTranslation(translatedText, True)
if extractedTranslations == None or len(tItem) != len(
extractedTranslations
):
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")
@ -551,9 +535,7 @@ def translateGPT(text, history, fullPromptFlag):
# Set if no mismatch
if mismatch == False:
tList[index] = extractedTranslations
history = extractedTranslations[
-MAXHISTORY:
] # Update history if we have a list
history = extractedTranslations[-MAXHISTORY:] # Update history if we have a list
else:
history = text[-MAXHISTORY:]
mismatch = False

View file

@ -85,9 +85,7 @@ def handleKansen(filename, estimate):
else:
try:
with open(
"translated/" + filename, "w", encoding="shift_jis", errors="ignore"
) as outFile:
with open("translated/" + filename, "w", encoding="shift_jis", errors="ignore") as outFile:
start = time.time()
translatedData = openFiles(filename)
@ -111,19 +109,14 @@ def getResultString(translatedData, translationTime, filename):
Fore.YELLOW + "[Input: " + str(translatedData[1][0]) + "]"
"[Output: "
+ str(translatedData[1][1])
+ "]" "[Cost: ${:,.4f}".format(
(translatedData[1][0] * 0.001 * INPUTAPICOST)
+ (translatedData[1][1] * 0.001 * OUTPUTAPICOST)
)
+ "]" "[Cost: ${:,.4f}".format((translatedData[1][0] * 0.001 * INPUTAPICOST) + (translatedData[1][1] * 0.001 * OUTPUTAPICOST))
+ "]"
)
timeString = Fore.BLUE + "[" + str(round(translationTime, 1)) + "s]"
if translatedData[2] == None:
# Success
return (
filename + ": " + totalTokenstring + timeString + Fore.GREEN + " \u2713 " + Fore.RESET
)
return filename + ": " + totalTokenstring + timeString + Fore.GREEN + " \u2713 " + Fore.RESET
else:
# Fail
@ -132,16 +125,7 @@ def getResultString(translatedData, translationTime, filename):
except Exception as e:
traceback.print_exc()
errorString = str(e) + Fore.RED
return (
filename
+ ": "
+ totalTokenstring
+ timeString
+ Fore.RED
+ " \u2717 "
+ errorString
+ Fore.RESET
)
return filename + ": " + totalTokenstring + timeString + Fore.RED + " \u2717 " + errorString + Fore.RESET
def openFiles(filename):
@ -624,11 +608,7 @@ def extractTranslation(translatedTextList, is_list):
pattern = r"`?<Line(\d+)>([\\]*.*?[\\]*?)<\/?Line\d+>`?"
# If it's a batch (i.e., list), extract with tags; otherwise, return the single item.
if is_list:
return [
re.findall(pattern, line)[0][1]
for line in translatedTextList
if re.search(pattern, line)
]
return [re.findall(pattern, line)[0][1] for line in translatedTextList if re.search(pattern, line)]
else:
matchList = re.findall(pattern, translatedTextList)
return matchList[0][1] if matchList else translatedTextList

View file

@ -123,19 +123,14 @@ def getResultString(translatedData, translationTime, filename):
Fore.YELLOW + "[Input: " + str(translatedData[1][0]) + "]"
"[Output: "
+ str(translatedData[1][1])
+ "]" "[Cost: ${:,.4f}".format(
(translatedData[1][0] * 0.001 * INPUTAPICOST)
+ (translatedData[1][1] * 0.001 * OUTPUTAPICOST)
)
+ "]" "[Cost: ${:,.4f}".format((translatedData[1][0] * 0.001 * INPUTAPICOST) + (translatedData[1][1] * 0.001 * OUTPUTAPICOST))
+ "]"
)
timeString = Fore.BLUE + "[" + str(round(translationTime, 1)) + "s]"
if translatedData[2] == None:
# Success
return (
filename + ": " + totalTokenstring + timeString + Fore.GREEN + " \u2713 " + Fore.RESET
)
return filename + ": " + totalTokenstring + timeString + Fore.GREEN + " \u2713 " + Fore.RESET
else:
# Fail
@ -144,16 +139,7 @@ def getResultString(translatedData, translationTime, filename):
except Exception as e:
traceback.print_exc()
errorString = str(e) + Fore.RED
return (
filename
+ ": "
+ totalTokenstring
+ timeString
+ Fore.RED
+ " \u2717 "
+ errorString
+ Fore.RESET
)
return filename + ": " + totalTokenstring + timeString + Fore.RED + " \u2717 " + errorString + Fore.RESET
def openFiles(filename):
@ -584,9 +570,7 @@ def translateGPT(text, history, fullPromptFlag):
translatedText = cleanTranslatedText(translatedText, varResponse)
if isinstance(tItem, list):
extractedTranslations = extractTranslation(translatedText, True)
if extractedTranslations == None or len(tItem) != len(
extractedTranslations
):
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")
@ -594,9 +578,7 @@ def translateGPT(text, history, fullPromptFlag):
# Set if no mismatch
if mismatch == False:
tList[index] = extractedTranslations
history = extractedTranslations[
-MAXHISTORY:
] # Update history if we have a list
history = extractedTranslations[-MAXHISTORY:] # Update history if we have a list
else:
history = text[-MAXHISTORY:]
mismatch = False

View file

@ -119,19 +119,14 @@ def getResultString(translatedData, translationTime, filename):
Fore.YELLOW + "[Input: " + str(translatedData[1][0]) + "]"
"[Output: "
+ str(translatedData[1][1])
+ "]" "[Cost: ${:,.4f}".format(
(translatedData[1][0] * 0.001 * INPUTAPICOST)
+ (translatedData[1][1] * 0.001 * OUTPUTAPICOST)
)
+ "]" "[Cost: ${:,.4f}".format((translatedData[1][0] * 0.001 * INPUTAPICOST) + (translatedData[1][1] * 0.001 * OUTPUTAPICOST))
+ "]"
)
timeString = Fore.BLUE + "[" + str(round(translationTime, 1)) + "s]"
if translatedData[2] == None:
# Success
return (
filename + ": " + totalTokenstring + timeString + Fore.GREEN + " \u2713 " + Fore.RESET
)
return filename + ": " + totalTokenstring + timeString + Fore.GREEN + " \u2713 " + Fore.RESET
else:
# Fail
@ -140,16 +135,7 @@ def getResultString(translatedData, translationTime, filename):
except Exception as e:
traceback.print_exc()
errorString = str(e) + Fore.RED
return (
filename
+ ": "
+ totalTokenstring
+ timeString
+ Fore.RED
+ " \u2717 "
+ errorString
+ Fore.RESET
)
return filename + ": " + totalTokenstring + timeString + Fore.RED + " \u2717 " + errorString + Fore.RESET
def parseJSON(data, filename):

View file

@ -119,19 +119,14 @@ def getResultString(translatedData, translationTime, filename):
Fore.YELLOW + "[Input: " + str(translatedData[1][0]) + "]"
"[Output: "
+ str(translatedData[1][1])
+ "]" "[Cost: ${:,.4f}".format(
(translatedData[1][0] * 0.001 * INPUTAPICOST)
+ (translatedData[1][1] * 0.001 * OUTPUTAPICOST)
)
+ "]" "[Cost: ${:,.4f}".format((translatedData[1][0] * 0.001 * INPUTAPICOST) + (translatedData[1][1] * 0.001 * OUTPUTAPICOST))
+ "]"
)
timeString = Fore.BLUE + "[" + str(round(translationTime, 1)) + "s]"
if translatedData[2] == None:
# Success
return (
filename + ": " + totalTokenstring + timeString + Fore.GREEN + " \u2713 " + Fore.RESET
)
return filename + ": " + totalTokenstring + timeString + Fore.GREEN + " \u2713 " + Fore.RESET
else:
# Fail
@ -140,16 +135,7 @@ def getResultString(translatedData, translationTime, filename):
except Exception as e:
traceback.print_exc()
errorString = str(e) + Fore.RED
return (
filename
+ ": "
+ totalTokenstring
+ timeString
+ Fore.RED
+ " \u2717 "
+ errorString
+ Fore.RESET
)
return filename + ": " + totalTokenstring + timeString + Fore.RED + " \u2717 " + errorString + Fore.RESET
def openFiles(filename):
@ -600,9 +586,7 @@ def translateGPT(text, history, fullPromptFlag):
translatedText = cleanTranslatedText(translatedText, varResponse)
if isinstance(tItem, list):
extractedTranslations = extractTranslation(translatedText, True)
if extractedTranslations == None or len(tItem) != len(
extractedTranslations
):
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")
@ -610,9 +594,7 @@ def translateGPT(text, history, fullPromptFlag):
# Set if no mismatch
if mismatch == False:
tList[index] = extractedTranslations
history = extractedTranslations[
-MAXHISTORY:
] # Update history if we have a list
history = extractedTranslations[-MAXHISTORY:] # Update history if we have a list
else:
history = text[-MAXHISTORY:]
mismatch = False

View file

@ -103,19 +103,14 @@ def getResultString(translatedData, translationTime, filename):
Fore.YELLOW + "[Input: " + str(translatedData[1][0]) + "]"
"[Output: "
+ str(translatedData[1][1])
+ "]" "[Cost: ${:,.4f}".format(
(translatedData[1][0] * 0.001 * INPUTAPICOST)
+ (translatedData[1][1] * 0.001 * OUTPUTAPICOST)
)
+ "]" "[Cost: ${:,.4f}".format((translatedData[1][0] * 0.001 * INPUTAPICOST) + (translatedData[1][1] * 0.001 * OUTPUTAPICOST))
+ "]"
)
timeString = Fore.BLUE + "[" + str(round(translationTime, 1)) + "s]"
if translatedData[2] == None:
# Success
return (
filename + ": " + totalTokenstring + timeString + Fore.GREEN + " \u2713 " + Fore.RESET
)
return filename + ": " + totalTokenstring + timeString + Fore.GREEN + " \u2713 " + Fore.RESET
else:
# Fail
@ -124,16 +119,7 @@ def getResultString(translatedData, translationTime, filename):
except Exception as e:
traceback.print_exc()
errorString = str(e) + Fore.RED
return (
filename
+ ": "
+ totalTokenstring
+ timeString
+ Fore.RED
+ " \u2717 "
+ errorString
+ Fore.RESET
)
return filename + ": " + totalTokenstring + timeString + Fore.RED + " \u2717 " + errorString + Fore.RESET
def openFiles(filename):
@ -274,9 +260,7 @@ def translateRegex(data, translatedList):
# 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
)
translatedText = pattern.sub(lambda match: match.group(0).replace(" ", "")[:20], translatedText)
# Textwrap
translatedText = textwrap.fill(translatedText, width=WIDTH)
@ -338,9 +322,7 @@ def translateRegex(data, translatedList):
# Choice List
if choiceList:
response = translateGPT(
choiceList, "Reply with the English TL of the Dialogue Choice", True
)
response = translateGPT(choiceList, "Reply with the English TL of the Dialogue Choice", True)
tokens[0] += response[1][0]
tokens[1] += response[1][1]
choiceListTL = response[0]
@ -591,9 +573,7 @@ def translateGPT(text, history, fullPromptFlag):
extractedTranslations = extractTranslation(translatedText, True)
if extractedTranslations == None or len(tItem) != len(extractedTranslations):
# Mismatch. Try Again
response = translateText(
system, user, history, 0.05, format, "gpt-4-turbo-2024-04-09"
)
response = translateText(system, user, history, 0.05, format, "gpt-4-turbo-2024-04-09")
translatedText = response.choices[0].message.content
totalTokens[0] += response.usage.prompt_tokens
totalTokens[1] += response.usage.completion_tokens
@ -602,9 +582,7 @@ def translateGPT(text, history, fullPromptFlag):
translatedText = cleanTranslatedText(translatedText, varResponse)
if isinstance(tItem, list):
extractedTranslations = extractTranslation(translatedText, True)
if extractedTranslations == None or len(tItem) != len(
extractedTranslations
):
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")
@ -612,9 +590,7 @@ def translateGPT(text, history, fullPromptFlag):
# Set if no mismatch
if mismatch == False:
tList[index] = extractedTranslations
history = extractedTranslations[
-MAXHISTORY:
] # Update history if we have a list
history = extractedTranslations[-MAXHISTORY:] # Update history if we have a list
else:
history = text[-MAXHISTORY:]
mismatch = False

View file

@ -114,19 +114,14 @@ def getResultString(translatedData, translationTime, filename):
Fore.YELLOW + "[Input: " + str(translatedData[1][0]) + "]"
"[Output: "
+ str(translatedData[1][1])
+ "]" "[Cost: ${:,.4f}".format(
(translatedData[1][0] * 0.001 * INPUTAPICOST)
+ (translatedData[1][1] * 0.001 * OUTPUTAPICOST)
)
+ "]" "[Cost: ${:,.4f}".format((translatedData[1][0] * 0.001 * INPUTAPICOST) + (translatedData[1][1] * 0.001 * OUTPUTAPICOST))
+ "]"
)
timeString = Fore.BLUE + "[" + str(round(translationTime, 1)) + "s]"
if translatedData[2] == None:
# Success
return (
filename + ": " + totalTokenstring + timeString + Fore.GREEN + " \u2713 " + Fore.RESET
)
return filename + ": " + totalTokenstring + timeString + Fore.GREEN + " \u2713 " + Fore.RESET
else:
# Fail
@ -135,16 +130,7 @@ def getResultString(translatedData, translationTime, filename):
except Exception as e:
traceback.print_exc()
errorString = str(e) + Fore.RED
return (
filename
+ ": "
+ totalTokenstring
+ timeString
+ Fore.RED
+ " \u2717 "
+ errorString
+ Fore.RESET
)
return filename + ": " + totalTokenstring + timeString + Fore.RED + " \u2717 " + errorString + Fore.RESET
def openFiles(filename):
@ -524,9 +510,7 @@ def translateGPT(text, history, fullPromptFlag):
translatedText = cleanTranslatedText(translatedText, varResponse)
if isinstance(tItem, list):
extractedTranslations = extractTranslation(translatedText, True)
if extractedTranslations == None or len(tItem) != len(
extractedTranslations
):
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")
@ -534,9 +518,7 @@ def translateGPT(text, history, fullPromptFlag):
# Set if no mismatch
if mismatch == False:
tList[index] = extractedTranslations
history = extractedTranslations[
-MAXHISTORY:
] # Update history if we have a list
history = extractedTranslations[-MAXHISTORY:] # Update history if we have a list
else:
history = text[-MAXHISTORY:]
mismatch = False

View file

@ -217,19 +217,14 @@ def getResultString(translatedData, translationTime, filename):
Fore.YELLOW + "[Input: " + str(translatedData[1][0]) + "]"
"[Output: "
+ str(translatedData[1][1])
+ "]" "[Cost: ${:,.4f}".format(
(translatedData[1][0] * 0.001 * INPUTAPICOST)
+ (translatedData[1][1] * 0.001 * OUTPUTAPICOST)
)
+ "]" "[Cost: ${:,.4f}".format((translatedData[1][0] * 0.001 * INPUTAPICOST) + (translatedData[1][1] * 0.001 * OUTPUTAPICOST))
+ "]"
)
timeString = Fore.BLUE + "[" + str(round(translationTime, 1)) + "s]"
if translatedData[2] is None:
# Success
return (
filename + ": " + totalTokenstring + timeString + Fore.GREEN + " \u2713 " + Fore.RESET
)
return filename + ": " + totalTokenstring + timeString + Fore.GREEN + " \u2713 " + Fore.RESET
else:
# Fail
try:
@ -237,16 +232,7 @@ def getResultString(translatedData, translationTime, filename):
except Exception as e:
traceback.print_exc()
errorString = str(e) + Fore.RED
return (
filename
+ ": "
+ totalTokenstring
+ timeString
+ Fore.RED
+ " \u2717 "
+ errorString
+ Fore.RESET
)
return filename + ": " + totalTokenstring + timeString + Fore.RED + " \u2717 " + errorString + Fore.RESET
def parseMap(data, filename):
@ -272,11 +258,7 @@ def parseMap(data, filename):
with ThreadPoolExecutor(max_workers=THREADS) as executor:
for key in events:
if key is not None:
futures = [
executor.submit(searchCodes, page, pbar, [], filename)
for page in events[key]["pages"]
if page is not None
]
futures = [executor.submit(searchCodes, page, pbar, [], filename) for page in events[key]["pages"] if page is not None]
for future in as_completed(futures):
try:
totalTokensFuture = future.result()
@ -359,11 +341,7 @@ def parseCommonEvents(data, filename):
with tqdm(bar_format=BAR_FORMAT, position=POSITION, leave=LEAVE) as pbar:
pbar.desc = filename
with ThreadPoolExecutor(max_workers=THREADS) as executor:
futures = [
executor.submit(searchCodes, page, pbar, [], filename)
for page in data
if page is not None
]
futures = [executor.submit(searchCodes, page, pbar, [], filename) for page in data if page is not None]
for future in as_completed(futures):
try:
totalTokensFuture = future.result()
@ -391,11 +369,7 @@ def parseTroops(data, filename):
for troop in data:
if troop is not None:
with ThreadPoolExecutor(max_workers=THREADS) as executor:
futures = [
executor.submit(searchCodes, page, pbar, [], filename)
for page in troop["pages"]
if page is not None
]
futures = [executor.submit(searchCodes, page, pbar, [], filename) for page in troop["pages"] if page is not None]
for future in as_completed(futures):
try:
totalTokensFuture = future.result()
@ -481,11 +455,7 @@ def parseScenario(data, filename):
with tqdm(bar_format=BAR_FORMAT, position=POSITION, leave=LEAVE) as pbar:
pbar.desc = filename
with ThreadPoolExecutor(max_workers=THREADS) as executor:
futures = [
executor.submit(searchCodes, page[1], pbar, [], filename)
for page in data.items()
if page[1] is not None
]
futures = [executor.submit(searchCodes, page[1], pbar, [], filename) for page in data.items() if page[1] is not None]
for future in as_completed(futures):
try:
totalTokensFuture = future.result()
@ -600,9 +570,7 @@ def searchNames(data, pbar, context):
totalTokens[0] += tokensResponse[0]
totalTokens[1] += tokensResponse[1]
if "Switch Shop Description" in data[i]["note"]:
tokensResponse = translateNote(
data[i], r"<Switch Shop Description>\n(.*)\n"
)
tokensResponse = translateNote(data[i], r"<Switch Shop Description>\n(.*)\n")
totalTokens[0] += tokensResponse[0]
totalTokens[1] += tokensResponse[1]
if "<MapText:" in data[i]["note"]:
@ -623,9 +591,7 @@ def searchNames(data, pbar, context):
number = 1
while number < 5:
if f"message{number}" in data[i]:
if len(data[i][f"message{number}"]) > 0 and data[i][f"message{number}"][
0
] in ["", "", "", "", ""]:
if len(data[i][f"message{number}"]) > 0 and data[i][f"message{number}"][0] in ["", "", "", "", ""]:
msgResponse = translateGPT(
"Taro" + data[i][f"message{number}"],
"reply with only the gender neutral "
@ -641,9 +607,7 @@ def searchNames(data, pbar, context):
else:
msgResponse = translateGPT(
data[i][f"message{number}"],
"reply with only the gender neutral "
+ LANGUAGE
+ " translation",
"reply with only the gender neutral " + LANGUAGE + " translation",
False,
)
data[i][f"message{number}"] = msgResponse[0]
@ -715,9 +679,7 @@ def searchNames(data, pbar, context):
data[j]["nickname"] = translatedNicknameBatch[0]
translatedNicknameBatch.pop(0)
if data[j]["profile"] != "":
data[j]["profile"] = textwrap.fill(
translatedProfileBatch[0], LISTWIDTH
)
data[j]["profile"] = textwrap.fill(translatedProfileBatch[0], LISTWIDTH)
translatedProfileBatch.pop(0)
# If Batch is empty. Move on.
@ -760,9 +722,7 @@ def searchNames(data, pbar, context):
data[j]["name"] = translatedNameBatch[0]
translatedNameBatch.pop(0)
if "description" in data[j] and data[j]["description"] != "":
data[j]["description"] = textwrap.fill(
translatedDescriptionBatch[0], LISTWIDTH
)
data[j]["description"] = textwrap.fill(translatedDescriptionBatch[0], LISTWIDTH)
translatedDescriptionBatch.pop(0)
# If Batch is empty. Move on.
@ -1147,15 +1107,11 @@ def searchCodes(page, pbar, jobList, filename):
# Remove speaker
if speaker != "":
matchSpeakerList = re.findall(
r"^\[?(.+?)\]?\s?[|:]\s?", translatedText
)
matchSpeakerList = re.findall(r"^\[?(.+?)\]?\s?[|:]\s?", translatedText)
if len(matchSpeakerList) > 0:
newSpeaker = matchSpeakerList[0]
nametag = nametag.replace(speaker, newSpeaker)
translatedText = re.sub(
r"^\[?(.+?)\]?\s?[|:]\s?", "", translatedText
)
translatedText = re.sub(r"^\[?(.+?)\]?\s?[|:]\s?", "", translatedText)
# Fix '- '
translatedText = translatedText.replace("- ", "-")
@ -1474,9 +1430,7 @@ def searchCodes(page, pbar, jobList, filename):
# Center Text
if acExist:
translatedText = (
f'\\ac {translatedText.replace('\n', '\n\\ac ')}'
)
translatedText = f'\\ac {translatedText.replace('\n', '\n\\ac ')}'
# Set
codeList[i]["p"][3][argVar] = translatedText
@ -1604,11 +1558,7 @@ def searchCodes(page, pbar, jobList, filename):
speaker = ""
## Event Code: 355 or 655 Scripts [Optional]
if (
"c" in codeList[i]
and (codeList[i]["c"] == 355 or codeList[i]["c"] == 655)
and CODE355655 is True
):
if "c" in codeList[i] and (codeList[i]["c"] == 355 or codeList[i]["c"] == 655) and CODE355655 is True:
jaString = codeList[i]["p"][0]
regex = r"BattleManager\._logWindow.addText\('(.*)'"
@ -1627,9 +1577,7 @@ def searchCodes(page, pbar, jobList, filename):
translatedText = translatedText.replace("'", "\\'")
# Set
codeList[i]["p"][0] = codeList[i]["p"][0].replace(
finalJAString, translatedText
)
codeList[i]["p"][0] = codeList[i]["p"][0].replace(finalJAString, translatedText)
list355655.pop(0)
## Event Code: 408 (Script)
@ -1803,9 +1751,7 @@ def searchCodes(page, pbar, jobList, filename):
if len(matchList) > 0:
# Translate
text = matchList[0]
response = translateGPT(
text, "Reply with the " + LANGUAGE + " Translation", False
)
response = translateGPT(text, "Reply with the " + LANGUAGE + " Translation", False)
translatedText = response[0]
totalTokens[0] += response[1][0]
totalTokens[1] += response[1][1]
@ -1819,9 +1765,7 @@ def searchCodes(page, pbar, jobList, filename):
if len(matchList) > 0:
# Translate
text = matchList[0]
response = translateGPT(
text, "Reply with the " + LANGUAGE + " Translation", False
)
response = translateGPT(text, "Reply with the " + LANGUAGE + " Translation", False)
translatedText = response[0]
totalTokens[0] += response[1][0]
totalTokens[1] += response[1][1]
@ -1836,9 +1780,7 @@ def searchCodes(page, pbar, jobList, filename):
if len(matchList) > 0:
# Translate
text = matchList[0]
response = translateGPT(
text, "Reply with the " + LANGUAGE + " Translation", False
)
response = translateGPT(text, "Reply with the " + LANGUAGE + " Translation", False)
translatedText = response[0]
totalTokens[0] += response[1][0]
totalTokens[1] += response[1][1]
@ -1926,8 +1868,7 @@ def searchCodes(page, pbar, jobList, filename):
if len(textHistory) > 0:
response = translateGPT(
choiceList,
"This will be a dialogue option.\nPrevious text for context: "
+ str(textHistory),
"This will be a dialogue option.\nPrevious text for context: " + str(textHistory),
True,
)
translatedTextList = response[0]
@ -1948,9 +1889,7 @@ def searchCodes(page, pbar, jobList, filename):
totalTokens[0] += response[1][0]
totalTokens[1] += response[1][1]
if translatedText != "":
translatedText = (
varList[choice] + translatedText[0].upper() + translatedText[1:]
)
translatedText = varList[choice] + translatedText[0].upper() + translatedText[1:]
else:
translatedText = varList[choice] + translatedText
codeList[i]["p"][0][choice] = translatedText
@ -2369,9 +2308,7 @@ def searchSystem(data, pbar):
for i in range(len(data["weapon_types"])):
response = translateGPT(
data["weapon_types"][i],
"Reply with only the "
+ LANGUAGE
+ " translation of the equipment type. No disclaimers.",
"Reply with only the " + LANGUAGE + " translation of the equipment type. No disclaimers.",
False,
)
totalTokens[0] += response[1][0]
@ -2656,9 +2593,7 @@ def translateGPT(text, history, fullPromptFlag):
translatedText = cleanTranslatedText(translatedText, varResponse)
if isinstance(tItem, list):
extractedTranslations = extractTranslation(translatedText, True)
if extractedTranslations == None or len(tItem) != len(
extractedTranslations
):
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")
@ -2666,9 +2601,7 @@ def translateGPT(text, history, fullPromptFlag):
# Set if no mismatch
if mismatch == False:
tList[index] = extractedTranslations
history = extractedTranslations[
-MAXHISTORY:
] # Update history if we have a list
history = extractedTranslations[-MAXHISTORY:] # Update history if we have a list
else:
history = text[-MAXHISTORY:]
mismatch = False

View file

@ -195,19 +195,14 @@ def getResultString(translatedData, translationTime, filename):
Fore.YELLOW + "[Input: " + str(translatedData[1][0]) + "]"
"[Output: "
+ str(translatedData[1][1])
+ "]" "[Cost: ${:,.4f}".format(
(translatedData[1][0] * 0.001 * INPUTAPICOST)
+ (translatedData[1][1] * 0.001 * OUTPUTAPICOST)
)
+ "]" "[Cost: ${:,.4f}".format((translatedData[1][0] * 0.001 * INPUTAPICOST) + (translatedData[1][1] * 0.001 * OUTPUTAPICOST))
+ "]"
)
timeString = Fore.BLUE + "[" + str(round(translationTime, 1)) + "s]"
if translatedData[2] is None:
# Success
return (
filename + ": " + totalTokenstring + timeString + Fore.GREEN + " \u2713 " + Fore.RESET
)
return filename + ": " + totalTokenstring + timeString + Fore.GREEN + " \u2713 " + Fore.RESET
else:
# Fail
try:
@ -215,16 +210,7 @@ def getResultString(translatedData, translationTime, filename):
except Exception as e:
traceback.print_exc()
errorString = str(e) + Fore.RED
return (
filename
+ ": "
+ totalTokenstring
+ timeString
+ Fore.RED
+ " \u2717 "
+ errorString
+ Fore.RESET
)
return filename + ": " + totalTokenstring + timeString + Fore.RED + " \u2717 " + errorString + Fore.RESET
def parseMap(data, filename):
@ -270,11 +256,7 @@ def parseMap(data, filename):
totalTokens[0] += response[0]
totalTokens[1] += response[1]
futures = [
executor.submit(searchCodes, page, pbar, [], filename)
for page in event["pages"]
if page is not None
]
futures = [executor.submit(searchCodes, page, pbar, [], filename) for page in event["pages"] if page is not None]
for future in as_completed(futures):
try:
totalTokensFuture = future.result()
@ -357,11 +339,7 @@ def parseCommonEvents(data, filename):
with tqdm(bar_format=BAR_FORMAT, position=POSITION, leave=LEAVE) as pbar:
pbar.desc = filename
with ThreadPoolExecutor(max_workers=THREADS) as executor:
futures = [
executor.submit(searchCodes, page, pbar, [], filename)
for page in data
if page is not None
]
futures = [executor.submit(searchCodes, page, pbar, [], filename) for page in data if page is not None]
for future in as_completed(futures):
try:
totalTokensFuture = future.result()
@ -389,11 +367,7 @@ def parseTroops(data, filename):
for troop in data:
if troop is not None:
with ThreadPoolExecutor(max_workers=THREADS) as executor:
futures = [
executor.submit(searchCodes, page, pbar, [], filename)
for page in troop["pages"]
if page is not None
]
futures = [executor.submit(searchCodes, page, pbar, [], filename) for page in troop["pages"] if page is not None]
for future in as_completed(futures):
try:
totalTokensFuture = future.result()
@ -480,11 +454,7 @@ def parseScenario(data, filename):
with tqdm(bar_format=BAR_FORMAT, position=POSITION, leave=LEAVE) as pbar:
pbar.desc = filename
with ThreadPoolExecutor(max_workers=THREADS) as executor:
futures = [
executor.submit(searchCodes, page[1], pbar, [], filename)
for page in data.items()
if page[1] is not None
]
futures = [executor.submit(searchCodes, page[1], pbar, [], filename) for page in data.items() if page[1] is not None]
for future in as_completed(futures):
try:
totalTokensFuture = future.result()
@ -595,9 +565,7 @@ def searchNames(data, pbar, context):
totalTokens[0] += tokensResponse[0]
totalTokens[1] += tokensResponse[1]
if "Switch Shop Description" in data[i]["note"]:
tokensResponse = translateNote(
data[i], r"<Switch Shop Description>\n(.*)\n"
)
tokensResponse = translateNote(data[i], r"<Switch Shop Description>\n(.*)\n")
totalTokens[0] += tokensResponse[0]
totalTokens[1] += tokensResponse[1]
if "<MapText:" in data[i]["note"]:
@ -634,9 +602,7 @@ def searchNames(data, pbar, context):
else:
msgResponse = translateGPT(
data[i][f"message{number}"],
"reply with only the gender neutral "
+ LANGUAGE
+ " translation",
"reply with only the gender neutral " + LANGUAGE + " translation",
False,
)
data[i][f"message{number}"] = msgResponse[0]
@ -709,9 +675,7 @@ def searchNames(data, pbar, context):
data[j]["nickname"] = translatedNicknameBatch[0]
translatedNicknameBatch.pop(0)
if data[j]["profile"] != "":
data[j]["profile"] = textwrap.fill(
translatedProfileBatch[0], LISTWIDTH
)
data[j]["profile"] = textwrap.fill(translatedProfileBatch[0], LISTWIDTH)
translatedProfileBatch.pop(0)
# If Batch is empty. Move on.
@ -754,9 +718,7 @@ def searchNames(data, pbar, context):
data[j]["name"] = translatedNameBatch[0]
translatedNameBatch.pop(0)
if "description" in data[j] and data[j]["description"] != "":
data[j]["description"] = textwrap.fill(
translatedDescriptionBatch[0], LISTWIDTH
)
data[j]["description"] = textwrap.fill(translatedDescriptionBatch[0], LISTWIDTH)
translatedDescriptionBatch.pop(0)
# If Batch is empty. Move on.
@ -872,11 +834,7 @@ def searchCodes(page, pbar, jobList, filename):
nametag = ""
## Event Code: 401 Show Text
if (
"code" in codeList[i]
and codeList[i]["code"] in [401, 405, -1]
and (CODE401 or CODE405)
):
if "code" in codeList[i] and codeList[i]["code"] in [401, 405, -1] and (CODE401 or CODE405):
# Save Code and starting index (j)
code = codeList[i]["code"]
j = i
@ -892,10 +850,7 @@ def searchCodes(page, pbar, jobList, filename):
continue
# Validate Japanese Text
if (
not re.search(r"[一-龠ぁ-ゔァ-ヴーa---\uFF61-\uFF9F]+", jaString)
and IGNORETLTEXT
):
if not re.search(r"[一-龠ぁ-ゔァ-ヴーa---\uFF61-\uFF9F]+", jaString) and IGNORETLTEXT:
i += 1
continue
@ -957,9 +912,7 @@ def searchCodes(page, pbar, jobList, filename):
and len(codeList[i + 1]["parameters"]) > 0
and len(codeList[i + 1]["parameters"][0]) > 0
):
if codeList[i + 1]["parameters"] != "" and codeList[i + 1]["parameters"][
0
].strip()[0] in [
if codeList[i + 1]["parameters"] != "" and codeList[i + 1]["parameters"][0].strip()[0] in [
"",
'"',
"(",
@ -977,9 +930,7 @@ def searchCodes(page, pbar, jobList, filename):
totalTokens[1] += response[1][1]
# Set Data
codeList[i]["parameters"][0] = nametag + jaString.replace(
speakerList[0], speaker
)
codeList[i]["parameters"][0] = nametag + jaString.replace(speakerList[0], speaker)
nametag = ""
# Iterate to next string
@ -1127,15 +1078,11 @@ def searchCodes(page, pbar, jobList, filename):
# Remove speaker
if speaker != "":
matchSpeakerList = re.findall(
r"^\[?(.+?)\]?\s?[|:]\s?", translatedText
)
matchSpeakerList = re.findall(r"^\[?(.+?)\]?\s?[|:]\s?", translatedText)
if len(matchSpeakerList) > 0:
newSpeaker = matchSpeakerList[0]
nametag = nametag.replace(speaker, newSpeaker)
translatedText = re.sub(
r"^\[?(.+?)\]?\s?[|:]\s?", "", translatedText
)
translatedText = re.sub(r"^\[?(.+?)\]?\s?[|:]\s?", "", translatedText)
# Fix '- '
translatedText = translatedText.replace("- ", "-")
@ -1458,9 +1405,7 @@ def searchCodes(page, pbar, jobList, filename):
# Center Text
if acExist:
translatedText = (
f'\\ac {translatedText.replace('\n', '\n\\ac ')}'
)
translatedText = f'\\ac {translatedText.replace('\n', '\n\\ac ')}'
# Check and Set Font
if "fontSize" in codeList[i]["parameters"][3]:
@ -1592,11 +1537,7 @@ def searchCodes(page, pbar, jobList, filename):
speaker = ""
## Event Code: 355 or 655 Scripts [Optional]
if (
"code" in codeList[i]
and (codeList[i]["code"] == 355 or codeList[i]["code"] == 655)
and CODE355655 is True
):
if "code" in codeList[i] and (codeList[i]["code"] == 355 or codeList[i]["code"] == 655) and CODE355655 is True:
jaString = codeList[i]["parameters"][0]
regex = r'.*subject=(.*?)"'
@ -1615,9 +1556,7 @@ def searchCodes(page, pbar, jobList, filename):
translatedText = translatedText.replace("'", "\\'")
# Set
codeList[i]["parameters"][0] = codeList[i]["parameters"][0].replace(
finalJAString, translatedText
)
codeList[i]["parameters"][0] = codeList[i]["parameters"][0].replace(finalJAString, translatedText)
list355655.pop(0)
## Event Code: 408 (Script)
@ -1791,9 +1730,7 @@ def searchCodes(page, pbar, jobList, filename):
if len(matchList) > 0:
# Translate
text = matchList[0]
response = translateGPT(
text, "Reply with the " + LANGUAGE + " Translation", False
)
response = translateGPT(text, "Reply with the " + LANGUAGE + " Translation", False)
translatedText = response[0]
totalTokens[0] += response[1][0]
totalTokens[1] += response[1][1]
@ -1807,9 +1744,7 @@ def searchCodes(page, pbar, jobList, filename):
if len(matchList) > 0:
# Translate
text = matchList[0]
response = translateGPT(
text, "Reply with the " + LANGUAGE + " Translation", False
)
response = translateGPT(text, "Reply with the " + LANGUAGE + " Translation", False)
translatedText = response[0]
totalTokens[0] += response[1][0]
totalTokens[1] += response[1][1]
@ -1824,9 +1759,7 @@ def searchCodes(page, pbar, jobList, filename):
if len(matchList) > 0:
# Translate
text = matchList[0]
response = translateGPT(
text, "Reply with the " + LANGUAGE + " Translation", False
)
response = translateGPT(text, "Reply with the " + LANGUAGE + " Translation", False)
translatedText = response[0]
totalTokens[0] += response[1][0]
totalTokens[1] += response[1][1]
@ -1914,8 +1847,7 @@ def searchCodes(page, pbar, jobList, filename):
if len(textHistory) > 0:
response = translateGPT(
choiceList,
"This will be a dialogue option.\nPrevious text for context: "
+ str(textHistory),
"This will be a dialogue option.\nPrevious text for context: " + str(textHistory),
True,
)
translatedTextList = response[0]
@ -1936,9 +1868,7 @@ def searchCodes(page, pbar, jobList, filename):
totalTokens[0] += response[1][0]
totalTokens[1] += response[1][1]
if translatedText != "":
translatedText = (
varList[choice] + translatedText[0].upper() + translatedText[1:]
)
translatedText = varList[choice] + translatedText[0].upper() + translatedText[1:]
else:
translatedText = varList[choice] + translatedText
codeList[i]["parameters"][0][choice] = translatedText
@ -2360,9 +2290,7 @@ def searchSystem(data, pbar):
for i in range(len(data["equipTypes"])):
response = translateGPT(
data["equipTypes"][i],
"Reply with only the "
+ LANGUAGE
+ " translation of the equipment type. No disclaimers.",
"Reply with only the " + LANGUAGE + " translation of the equipment type. No disclaimers.",
False,
)
totalTokens[0] += response[1][0]
@ -2644,9 +2572,7 @@ def translateGPT(text, history, fullPromptFlag):
translatedText = cleanTranslatedText(translatedText, varResponse)
if isinstance(tItem, list):
extractedTranslations = extractTranslation(translatedText, True)
if extractedTranslations == None or len(tItem) != len(
extractedTranslations
):
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")
@ -2654,9 +2580,7 @@ def translateGPT(text, history, fullPromptFlag):
# Set if no mismatch
if mismatch == False:
tList[index] = extractedTranslations
history = extractedTranslations[
-MAXHISTORY:
] # Update history if we have a list
history = extractedTranslations[-MAXHISTORY:] # Update history if we have a list
else:
history = text[-MAXHISTORY:]
mismatch = False

View file

@ -111,19 +111,14 @@ def getResultString(translatedData, translationTime, filename):
Fore.YELLOW + "[Input: " + str(translatedData[1][0]) + "]"
"[Output: "
+ str(translatedData[1][1])
+ "]" "[Cost: ${:,.4f}".format(
(translatedData[1][0] * 0.001 * INPUTAPICOST)
+ (translatedData[1][1] * 0.001 * OUTPUTAPICOST)
)
+ "]" "[Cost: ${:,.4f}".format((translatedData[1][0] * 0.001 * INPUTAPICOST) + (translatedData[1][1] * 0.001 * OUTPUTAPICOST))
+ "]"
)
timeString = Fore.BLUE + "[" + str(round(translationTime, 1)) + "s]"
if translatedData[2] == None:
# Success
return (
filename + ": " + totalTokenstring + timeString + Fore.GREEN + " \u2713 " + Fore.RESET
)
return filename + ": " + totalTokenstring + timeString + Fore.GREEN + " \u2713 " + Fore.RESET
else:
# Fail
@ -132,16 +127,7 @@ def getResultString(translatedData, translationTime, filename):
except Exception as e:
traceback.print_exc()
errorString = str(e) + Fore.RED
return (
filename
+ ": "
+ totalTokenstring
+ timeString
+ Fore.RED
+ " \u2717 "
+ errorString
+ Fore.RESET
)
return filename + ": " + totalTokenstring + timeString + Fore.RED + " \u2717 " + errorString + Fore.RESET
def openFiles(filename):
@ -515,9 +501,7 @@ def translateGPT(text, history, fullPromptFlag):
translatedText = cleanTranslatedText(translatedText, varResponse)
if isinstance(tItem, list):
extractedTranslations = extractTranslation(translatedText, True)
if extractedTranslations == None or len(tItem) != len(
extractedTranslations
):
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")
@ -525,9 +509,7 @@ def translateGPT(text, history, fullPromptFlag):
# Set if no mismatch
if mismatch == False:
tList[index] = extractedTranslations
history = extractedTranslations[
-MAXHISTORY:
] # Update history if we have a list
history = extractedTranslations[-MAXHISTORY:] # Update history if we have a list
else:
history = text[-MAXHISTORY:]
mismatch = False

View file

@ -27,9 +27,7 @@ LANGUAGE = os.getenv("language").capitalize()
INPUTAPICOST = 0.002 # Depends on the model https://openai.com/pricing
OUTPUTAPICOST = 0.002
PROMPT = Path("prompt.txt").read_text(encoding="utf-8")
THREADS = int(
os.getenv("threads")
) # Controls how many threads are working on a single file (May have to drop this)
THREADS = int(os.getenv("threads")) # Controls how many threads are working on a single file (May have to drop this)
LOCK = threading.Lock()
WIDTH = int(os.getenv("width"))
LISTWIDTH = int(os.getenv("listWidth"))
@ -97,19 +95,14 @@ def getResultString(translatedData, translationTime, filename):
Fore.YELLOW + "[Input: " + str(translatedData[1][0]) + "]"
"[Output: "
+ str(translatedData[1][1])
+ "]" "[Cost: ${:,.4f}".format(
(translatedData[1][0] * 0.001 * INPUTAPICOST)
+ (translatedData[1][1] * 0.001 * OUTPUTAPICOST)
)
+ "]" "[Cost: ${:,.4f}".format((translatedData[1][0] * 0.001 * INPUTAPICOST) + (translatedData[1][1] * 0.001 * OUTPUTAPICOST))
+ "]"
)
timeString = Fore.BLUE + "[" + str(round(translationTime, 1)) + "s]"
if translatedData[2] is None:
# Success
return (
filename + ": " + totalTokenstring + timeString + Fore.GREEN + " \u2713 " + Fore.RESET
)
return filename + ": " + totalTokenstring + timeString + Fore.GREEN + " \u2713 " + Fore.RESET
else:
# Fail
@ -117,16 +110,7 @@ def getResultString(translatedData, translationTime, filename):
raise translatedData[2]
except Exception as e:
errorString = str(e) + Fore.RED
return (
filename
+ ": "
+ totalTokenstring
+ timeString
+ Fore.RED
+ " \u2717 "
+ errorString
+ Fore.RESET
)
return filename + ": " + totalTokenstring + timeString + Fore.RED + " \u2717 " + errorString + Fore.RESET
def openFiles(filename):
@ -232,9 +216,7 @@ def translateTyrano(data, pbar):
if len(textHistory) > 0:
response = translateGPT(
matchList[0],
"Past Translated Text: "
+ textHistory[len(textHistory) - 1]
+ "\n\nReply in the style of a dialogue option.",
"Past Translated Text: " + textHistory[len(textHistory) - 1] + "\n\nReply in the style of a dialogue option.",
True,
)
else:
@ -253,17 +235,13 @@ def translateTyrano(data, pbar):
translatedText = translatedText.replace("'", "\\'")
# Set Data
translatedText = data[i].replace(
matchList[0], translatedText.replace(" ", "\u00a0")
)
translatedText = data[i].replace(matchList[0], translatedText.replace(" ", "\u00a0"))
data[i] = translatedText
# Grab Lines
matchList = re.findall(r"^([^\n;@*\{\[].+[^;'{}\[]$)", data[i])
if len(matchList) > 0 and (
re.search(r"^\[(.+)\sstorage=.+\],", data[i - 1])
or re.search(r"^\[(.+)\]$", data[i - 1])
or re.search(r"^《(.+)》", data[i - 1])
re.search(r"^\[(.+)\sstorage=.+\],", data[i - 1]) or re.search(r"^\[(.+)\]$", data[i - 1]) or re.search(r"^《(.+)》", data[i - 1])
):
currentGroup.append(matchList[0])
if len(data) > i + 1:
@ -566,13 +544,7 @@ def translateGPT(t, history, fullPromptFlag):
system = PROMPT
user = "Line to Translate = " + subbedT
else:
system = (
"Output ONLY the "
+ LANGUAGE
+ " translation in the following format: `Translation: <"
+ LANGUAGE.upper()
+ "_TRANSLATION>`"
)
system = "Output ONLY the " + LANGUAGE + " translation in the following format: `Translation: <" + LANGUAGE.upper() + "_TRANSLATION>`"
user = "Line to Translate = " + subbedT
# Create Message List
@ -622,10 +594,7 @@ def translateGPT(t, history, fullPromptFlag):
translatedText = translatedText.replace("", "!")
# Return Translation
if (
len(translatedText) > 15 * len(t)
or "I'm sorry, but I'm unable to assist with that translation" in translatedText
):
if len(translatedText) > 15 * len(t) or "I'm sorry, but I'm unable to assist with that translation" in translatedText:
raise Exception
else:
return [translatedText, totalTokens]

View file

@ -103,19 +103,14 @@ def getResultString(translatedData, translationTime, filename):
Fore.YELLOW + "[Input: " + str(translatedData[1][0]) + "]"
"[Output: "
+ str(translatedData[1][1])
+ "]" "[Cost: ${:,.4f}".format(
(translatedData[1][0] * 0.001 * INPUTAPICOST)
+ (translatedData[1][1] * 0.001 * OUTPUTAPICOST)
)
+ "]" "[Cost: ${:,.4f}".format((translatedData[1][0] * 0.001 * INPUTAPICOST) + (translatedData[1][1] * 0.001 * OUTPUTAPICOST))
+ "]"
)
timeString = Fore.BLUE + "[" + str(round(translationTime, 1)) + "s]"
if translatedData[2] == None:
# Success
return (
filename + ": " + totalTokenstring + timeString + Fore.GREEN + " \u2713 " + Fore.RESET
)
return filename + ": " + totalTokenstring + timeString + Fore.GREEN + " \u2713 " + Fore.RESET
else:
# Fail
@ -124,16 +119,7 @@ def getResultString(translatedData, translationTime, filename):
except Exception as e:
traceback.print_exc()
errorString = str(e) + Fore.RED
return (
filename
+ ": "
+ totalTokenstring
+ timeString
+ Fore.RED
+ " \u2717 "
+ errorString
+ Fore.RESET
)
return filename + ": " + totalTokenstring + timeString + Fore.RED + " \u2717 " + errorString + Fore.RESET
def openFiles(filename):
@ -253,9 +239,7 @@ def translateTxt(data, translatedList):
# Choice List
if choiceList:
response = translateGPT(
choiceList, "Reply with the English TL of the Dialogue Choice", True
)
response = translateGPT(choiceList, "Reply with the English TL of the Dialogue Choice", True)
tokens[0] += response[1][0]
tokens[1] += response[1][1]
choiceListTL = response[0]
@ -515,9 +499,7 @@ def translateGPT(text, history, fullPromptFlag):
translatedText = cleanTranslatedText(translatedText, varResponse)
if isinstance(tItem, list):
extractedTranslations = extractTranslation(translatedText, True)
if extractedTranslations == None or len(tItem) != len(
extractedTranslations
):
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")
@ -525,9 +507,7 @@ def translateGPT(text, history, fullPromptFlag):
# Set if no mismatch
if mismatch == False:
tList[index] = extractedTranslations
history = extractedTranslations[
-MAXHISTORY:
] # Update history if we have a list
history = extractedTranslations[-MAXHISTORY:] # Update history if we have a list
else:
history = text[-MAXHISTORY:]
mismatch = False

View file

@ -103,19 +103,14 @@ def getResultString(translatedData, translationTime, filename):
Fore.YELLOW + "[Input: " + str(translatedData[1][0]) + "]"
"[Output: "
+ str(translatedData[1][1])
+ "]" "[Cost: ${:,.4f}".format(
(translatedData[1][0] * 0.001 * INPUTAPICOST)
+ (translatedData[1][1] * 0.001 * OUTPUTAPICOST)
)
+ "]" "[Cost: ${:,.4f}".format((translatedData[1][0] * 0.001 * INPUTAPICOST) + (translatedData[1][1] * 0.001 * OUTPUTAPICOST))
+ "]"
)
timeString = Fore.BLUE + "[" + str(round(translationTime, 1)) + "s]"
if translatedData[2] == None:
# Success
return (
filename + ": " + totalTokenstring + timeString + Fore.GREEN + " \u2713 " + Fore.RESET
)
return filename + ": " + totalTokenstring + timeString + Fore.GREEN + " \u2713 " + Fore.RESET
else:
# Fail
@ -124,16 +119,7 @@ def getResultString(translatedData, translationTime, filename):
except Exception as e:
traceback.print_exc()
errorString = str(e) + Fore.RED
return (
filename
+ ": "
+ totalTokenstring
+ timeString
+ Fore.RED
+ " \u2717 "
+ errorString
+ Fore.RESET
)
return filename + ": " + totalTokenstring + timeString + Fore.RED + " \u2717 " + errorString + Fore.RESET
def openFiles(filename):
@ -186,9 +172,7 @@ def translateTyrano(data, translatedList):
while i < len(data):
voice = False
lineRegexNoSpeaker = (
r"^([^\[#;*@\n]+)\[l\]\[[rp]\]|^([^\[#;*@\n]+)\[[rpl]\]|^([^\[#;*@_\n]+)\n$"
)
lineRegexNoSpeaker = r"^([^\[#;*@\n]+)\[l\]\[[rp]\]|^([^\[#;*@\n]+)\[[rpl]\]|^([^\[#;*@_\n]+)\n$"
lineRegexSpeaker = r"^#(.*)"
furiganaRegex = r"(\[ruby\stext=(.*?)\])"
choiceRegex = r'\[glink.+?text="(.*?)"'
@ -329,9 +313,7 @@ def translateTyrano(data, translatedList):
# Choice List
if choiceList:
response = translateGPT(
choiceList, "Reply with the English TL of the Dialogue Choice", True
)
response = translateGPT(choiceList, "Reply with the English TL of the Dialogue Choice", True)
tokens[0] += response[1][0]
tokens[1] += response[1][1]
choiceListTL = response[0]
@ -591,9 +573,7 @@ def translateGPT(text, history, fullPromptFlag):
translatedText = cleanTranslatedText(translatedText, varResponse)
if isinstance(tItem, list):
extractedTranslations = extractTranslation(translatedText, True)
if extractedTranslations == None or len(tItem) != len(
extractedTranslations
):
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")
@ -601,9 +581,7 @@ def translateGPT(text, history, fullPromptFlag):
# Set if no mismatch
if mismatch == False:
tList[index] = extractedTranslations
history = extractedTranslations[
-MAXHISTORY:
] # Update history if we have a list
history = extractedTranslations[-MAXHISTORY:] # Update history if we have a list
else:
history = text[-MAXHISTORY:]
mismatch = False

View file

@ -119,19 +119,14 @@ def getResultString(translatedData, translationTime, filename):
Fore.YELLOW + "[Input: " + str(translatedData[1][0]) + "]"
"[Output: "
+ str(translatedData[1][1])
+ "]" "[Cost: ${:,.4f}".format(
(translatedData[1][0] * 0.001 * INPUTAPICOST)
+ (translatedData[1][1] * 0.001 * OUTPUTAPICOST)
)
+ "]" "[Cost: ${:,.4f}".format((translatedData[1][0] * 0.001 * INPUTAPICOST) + (translatedData[1][1] * 0.001 * OUTPUTAPICOST))
+ "]"
)
timeString = Fore.BLUE + "[" + str(round(translationTime, 1)) + "s]"
if translatedData[2] == None:
# Success
return (
filename + ": " + totalTokenstring + timeString + Fore.GREEN + " \u2713 " + Fore.RESET
)
return filename + ": " + totalTokenstring + timeString + Fore.GREEN + " \u2713 " + Fore.RESET
else:
# Fail
@ -140,16 +135,7 @@ def getResultString(translatedData, translationTime, filename):
except Exception as e:
traceback.print_exc()
errorString = str(e) + Fore.RED
return (
filename
+ ": "
+ totalTokenstring
+ timeString
+ Fore.RED
+ " \u2717 "
+ errorString
+ Fore.RESET
)
return filename + ": " + totalTokenstring + timeString + Fore.RED + " \u2717 " + errorString + Fore.RESET
def openFiles(filename):

View file

@ -159,19 +159,14 @@ def getResultString(translatedData, translationTime, filename):
Fore.YELLOW + "[Input: " + str(translatedData[1][0]) + "]"
"[Output: "
+ str(translatedData[1][1])
+ "]" "[Cost: ${:,.4f}".format(
(translatedData[1][0] * 0.001 * INPUTAPICOST)
+ (translatedData[1][1] * 0.001 * OUTPUTAPICOST)
)
+ "]" "[Cost: ${:,.4f}".format((translatedData[1][0] * 0.001 * INPUTAPICOST) + (translatedData[1][1] * 0.001 * OUTPUTAPICOST))
+ "]"
)
timeString = Fore.BLUE + "[" + str(round(translationTime, 1)) + "s]"
if translatedData[2] is None:
# Success
return (
filename + ": " + totalTokenstring + timeString + Fore.GREEN + " \u2713 " + Fore.RESET
)
return filename + ": " + totalTokenstring + timeString + Fore.GREEN + " \u2713 " + Fore.RESET
else:
# Fail
try:
@ -179,16 +174,7 @@ def getResultString(translatedData, translationTime, filename):
except Exception as e:
traceback.print_exc()
errorString = str(e) + Fore.RED
return (
filename
+ ": "
+ totalTokenstring
+ timeString
+ Fore.RED
+ " \u2717 "
+ errorString
+ Fore.RESET
)
return filename + ": " + totalTokenstring + timeString + Fore.RED + " \u2717 " + errorString + Fore.RESET
def parseOther(data, filename):
@ -248,11 +234,7 @@ def parseMap(data, filename):
with ThreadPoolExecutor(max_workers=THREADS) as executor:
for event in events:
if event is not None:
futures = [
executor.submit(searchCodes, page["list"], pbar, None, filename)
for page in event["pages"]
if page is not None
]
futures = [executor.submit(searchCodes, page["list"], pbar, None, filename) for page in event["pages"] if page is not None]
for future in as_completed(futures):
try:
totalTokensFuture = future.result()
@ -388,11 +370,7 @@ def searchCodes(events, pbar, jobList, filename):
### Event Code: 210 Common Event
if codeList[i]["code"] == 210 and CODE210 == True:
# Speaker Event
if (
"stringArgs" in codeList[i]
and codeList[i]["intArgs"][0] == None
and len(codeList[i]["stringArgs"]) == 2
):
if "stringArgs" in codeList[i] and codeList[i]["intArgs"][0] == None and len(codeList[i]["stringArgs"]) == 2:
response = getSpeaker(codeList[i]["stringArgs"][1])
totalTokens[1] += response[1][0]
totalTokens[1] += response[1][1]
@ -401,11 +379,7 @@ def searchCodes(events, pbar, jobList, filename):
codeList[i]["stringArgs"][1] = response[0]
# Logs
elif (
"stringArgs" in codeList[i]
and codeList[i]["intArgs"][0] == 500220
and len(codeList[i]["stringArgs"]) == 2
):
elif "stringArgs" in codeList[i] and codeList[i]["intArgs"][0] == 500220 and len(codeList[i]["stringArgs"]) == 2:
# Grab String
jaString = codeList[i]["stringArgs"][1]
initialJAString = jaString
@ -499,9 +473,7 @@ def searchCodes(events, pbar, jobList, filename):
totalTokens[1] += response[1][1]
# Set String
codeList[i]["stringArgs"][0] = codeList[i]["stringArgs"][0].replace(
jaString, translatedText
)
codeList[i]["stringArgs"][0] = codeList[i]["stringArgs"][0].replace(jaString, translatedText)
### Event Code: 122 SetString
if codeList[i]["code"] == 150 and CODE150 == True:
@ -544,17 +516,9 @@ def searchCodes(events, pbar, jobList, filename):
codeList[i]["stringArgs"][0] = translatedText
### Event Code: 300 Common Events
if (
codeList[i]["code"] == 300
and CODE300 == True
and "stringArgs" in codeList[i]
and len(codeList[i]["stringArgs"]) > 1
):
if codeList[i]["code"] == 300 and CODE300 == True and "stringArgs" in codeList[i] and len(codeList[i]["stringArgs"]) > 1:
# Choices
if (
codeList[i]["stringArgs"][0] == "[共]汎用ウィンドウ生成"
or codeList[i]["stringArgs"][0] == "[共]選択生成"
):
if codeList[i]["stringArgs"][0] == "[共]汎用ウィンドウ生成" or codeList[i]["stringArgs"][0] == "[共]選択生成":
# Grab String
choiceList = codeList[i]["stringArgs"][1].split("\r\n")
@ -610,18 +574,13 @@ def searchCodes(events, pbar, jobList, filename):
# Validate size
if len(codeList[i]["stringArgs"]) == 4:
if (
codeList[i]["stringArgs"][1] == "┣所持アイテム個数"
and codeList[i]["stringArgs"][2] != ""
):
if codeList[i]["stringArgs"][1] == "┣所持アイテム個数" and codeList[i]["stringArgs"][2] != "":
# Grab String
jaString = codeList[i]["stringArgs"][2]
# Catch Vars that may break the TL
varString = ""
matchList = re.findall(
r"^[\\_]+[\w]+\[[a-zA-Z0-9\\\[\]\_,\s-]+\]", jaString
)
matchList = re.findall(r"^[\\_]+[\w]+\[[a-zA-Z0-9\\\[\]\_,\s-]+\]", jaString)
if len(matchList) != 0:
varString = matchList[0]
jaString = jaString.replace(matchList[0], "")
@ -733,9 +692,7 @@ def formatDramon(jaString):
jaStringList = re.split(imageRegex, jaString)
# Clean List
cleanedList = [
x for x in jaStringList if x is not None and x != "" and x != "\r\n" and x != "_SS_"
]
cleanedList = [x for x in jaStringList if x is not None and x != "" and x != "\r\n" and x != "_SS_"]
# Iterate Through List
j = 0
@ -748,12 +705,7 @@ def formatDramon(jaString):
and ".ogg" not in cleanedList[j]
):
# Setup @
if (
j > 0
and "@" not in cleanedList[j - 1]
and "/" not in cleanedList[j - 1]
and "_" not in cleanedList[j - 1]
):
if j > 0 and "@" not in cleanedList[j - 1] and "/" not in cleanedList[j - 1] and "_" not in cleanedList[j - 1]:
cleanedList[j - 1] = cleanedList[j - 1] + cleanedList[j + 1]
else:
cleanedList.insert(j, cleanedList[j + 1])
@ -1682,23 +1634,17 @@ def searchDB(events, pbar, jobList, filename):
totalTokens[0] += response[1][0]
totalTokens[1] += response[1][1]
# Desc 1
response = translateGPT(
npcList[1], "Reply with only the " + LANGUAGE + " translation", True
)
response = translateGPT(npcList[1], "Reply with only the " + LANGUAGE + " translation", True)
descListTL1 = response[0]
totalTokens[0] += response[1][0]
totalTokens[1] += response[1][1]
# Desc 2
response = translateGPT(
npcList[2], "Reply with only the " + LANGUAGE + " translation", True
)
response = translateGPT(npcList[2], "Reply with only the " + LANGUAGE + " translation", True)
descListTL2 = response[0]
totalTokens[0] += response[1][0]
totalTokens[1] += response[1][1]
# Desc 3
response = translateGPT(
npcList[3], "Reply with only the " + LANGUAGE + " translation", True
)
response = translateGPT(npcList[3], "Reply with only the " + LANGUAGE + " translation", True)
descListTL3 = response[0]
totalTokens[0] += response[1][0]
totalTokens[1] += response[1][1]
@ -1755,11 +1701,7 @@ def searchDB(events, pbar, jobList, filename):
totalTokens[1] += response[1][1]
# Check Mismatch
if (
len(nameListTL) != len(scenarioList[0])
or len(descListTL1) != len(scenarioList[1])
or len(descListTL2) != len(scenarioList[2])
):
if len(nameListTL) != len(scenarioList[0]) or len(descListTL1) != len(scenarioList[1]) or len(descListTL2) != len(scenarioList[2]):
with LOCK:
if filename not in MISMATCH:
MISMATCH.append(filename)
@ -1777,30 +1719,22 @@ def searchDB(events, pbar, jobList, filename):
pbar.refresh()
# Name
response = translateGPT(
itemList[0], "Reply with only the " + LANGUAGE + " translation", True
)
response = translateGPT(itemList[0], "Reply with only the " + LANGUAGE + " translation", True)
nameListTL = response[0]
totalTokens[0] += response[1][0]
totalTokens[1] += response[1][1]
# Desc 1
response = translateGPT(
itemList[1], "Reply with only the " + LANGUAGE + " translation", True
)
response = translateGPT(itemList[1], "Reply with only the " + LANGUAGE + " translation", True)
descListTL1 = response[0]
totalTokens[0] += response[1][0]
totalTokens[1] += response[1][1]
# Desc 2
response = translateGPT(
itemList[2], "Reply with only the " + LANGUAGE + " translation", True
)
response = translateGPT(itemList[2], "Reply with only the " + LANGUAGE + " translation", True)
descListTL2 = response[0]
totalTokens[0] += response[1][0]
totalTokens[1] += response[1][1]
# Desc 3
response = translateGPT(
itemList[3], "Reply with only the " + LANGUAGE + " translation", True
)
response = translateGPT(itemList[3], "Reply with only the " + LANGUAGE + " translation", True)
descListTL3 = response[0]
totalTokens[0] += response[1][0]
totalTokens[1] += response[1][1]
@ -1838,9 +1772,7 @@ def searchDB(events, pbar, jobList, filename):
totalTokens[0] += response[1][0]
totalTokens[1] += response[1][1]
# Desc 1
response = translateGPT(
armorList[1], "Reply with only the " + LANGUAGE + " translation", True
)
response = translateGPT(armorList[1], "Reply with only the " + LANGUAGE + " translation", True)
descListTL1 = response[0]
totalTokens[0] += response[1][0]
totalTokens[1] += response[1][1]
@ -1873,9 +1805,7 @@ def searchDB(events, pbar, jobList, filename):
totalTokens[0] += response[1][0]
totalTokens[1] += response[1][1]
# Desc 1
response = translateGPT(
enemyList[1], "Reply with only the " + LANGUAGE + " translation", True
)
response = translateGPT(enemyList[1], "Reply with only the " + LANGUAGE + " translation", True)
descListTL1 = response[0]
totalTokens[0] += response[1][0]
totalTokens[1] += response[1][1]
@ -1919,11 +1849,7 @@ def searchDB(events, pbar, jobList, filename):
totalTokens[1] += response[1][1]
# Check Mismatch
if (
len(nameListTL) != len(weaponsList[0])
or len(descListTL1) != len(weaponsList[1])
or len(descListTL2) != len(weaponsList[2])
):
if len(nameListTL) != len(weaponsList[0]) or len(descListTL1) != len(weaponsList[1]) or len(descListTL2) != len(weaponsList[2]):
with LOCK:
if filename not in MISMATCH:
MISMATCH.append(filename)
@ -2143,9 +2069,7 @@ def searchDB(events, pbar, jobList, filename):
pbar.refresh()
# Name
response = translateGPT(
itemList[0], "Reply with only the " + LANGUAGE + " translation", True
)
response = translateGPT(itemList[0], "Reply with only the " + LANGUAGE + " translation", True)
nameListTL = response[0]
totalTokens[0] += response[1][0]
totalTokens[1] += response[1][1]
@ -2169,9 +2093,7 @@ def searchDB(events, pbar, jobList, filename):
pbar.refresh()
# Name
response = translateGPT(
dbNameList[0], "Reply with only the " + LANGUAGE + " translation", True
)
response = translateGPT(dbNameList[0], "Reply with only the " + LANGUAGE + " translation", True)
nameListTL = response[0]
totalTokens[0] += response[1][0]
totalTokens[1] += response[1][1]
@ -2453,9 +2375,7 @@ def translateGPT(text, history, fullPromptFlag):
translatedText = cleanTranslatedText(translatedText, varResponse)
if isinstance(tItem, list):
extractedTranslations = extractTranslation(translatedText, True)
if extractedTranslations == None or len(tItem) != len(
extractedTranslations
):
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")
@ -2463,9 +2383,7 @@ def translateGPT(text, history, fullPromptFlag):
# Set if no mismatch
if mismatch == False:
tList[index] = extractedTranslations
history = extractedTranslations[
-MAXHISTORY:
] # Update history if we have a list
history = extractedTranslations[-MAXHISTORY:] # Update history if we have a list
else:
history = text[-MAXHISTORY:]
mismatch = False

View file

@ -86,9 +86,7 @@ def handleWOLF2(filename, estimate):
else:
try:
with open(
"translated/" + filename, "w", encoding="shift_jis", errors="ignore"
) as outFile:
with open("translated/" + filename, "w", encoding="shift_jis", errors="ignore") as outFile:
start = time.time()
translatedData = openFiles(filename)
@ -112,19 +110,14 @@ def getResultString(translatedData, translationTime, filename):
Fore.YELLOW + "[Input: " + str(translatedData[1][0]) + "]"
"[Output: "
+ str(translatedData[1][1])
+ "]" "[Cost: ${:,.4f}".format(
(translatedData[1][0] * 0.001 * INPUTAPICOST)
+ (translatedData[1][1] * 0.001 * OUTPUTAPICOST)
)
+ "]" "[Cost: ${:,.4f}".format((translatedData[1][0] * 0.001 * INPUTAPICOST) + (translatedData[1][1] * 0.001 * OUTPUTAPICOST))
+ "]"
)
timeString = Fore.BLUE + "[" + str(round(translationTime, 1)) + "s]"
if translatedData[2] == None:
# Success
return (
filename + ": " + totalTokenstring + timeString + Fore.GREEN + " \u2713 " + Fore.RESET
)
return filename + ": " + totalTokenstring + timeString + Fore.GREEN + " \u2713 " + Fore.RESET
else:
# Fail
@ -133,16 +126,7 @@ def getResultString(translatedData, translationTime, filename):
except Exception as e:
traceback.print_exc()
errorString = str(e) + Fore.RED
return (
filename
+ ": "
+ totalTokenstring
+ timeString
+ Fore.RED
+ " \u2717 "
+ errorString
+ Fore.RESET
)
return filename + ": " + totalTokenstring + timeString + Fore.RED + " \u2717 " + errorString + Fore.RESET
def openFiles(filename):
@ -239,9 +223,7 @@ def translateWOLF(data, translatedList, pbar, filename):
# Grab Consecutive Strings
currentGroup.append(data[i])
i += 1
while (
i < len(data) and r"/" not in data[i] and "@" not in data[i] and data[i] != "\n"
):
while i < len(data) and r"/" not in data[i] and "@" not in data[i] and data[i] != "\n":
currentGroup.append(data[i])
i += 1
@ -264,9 +246,7 @@ def translateWOLF(data, translatedList, pbar, filename):
# Pass 2
else:
# Insert Strings
while (
i < len(data) and r"/" not in data[i] and "@" not in data[i] and data[i] != "\n"
):
while i < len(data) and r"/" not in data[i] and "@" not in data[i] and data[i] != "\n":
data.pop(i)
# Get Text
@ -558,9 +538,7 @@ def translateGPT(text, history, fullPromptFlag):
translatedText = cleanTranslatedText(translatedText, varResponse)
if isinstance(tItem, list):
extractedTranslations = extractTranslation(translatedText, True)
if extractedTranslations == None or len(tItem) != len(
extractedTranslations
):
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")
@ -568,9 +546,7 @@ def translateGPT(text, history, fullPromptFlag):
# Set if no mismatch
if mismatch == False:
tList[index] = extractedTranslations
history = extractedTranslations[
-MAXHISTORY:
] # Update history if we have a list
history = extractedTranslations[-MAXHISTORY:] # Update history if we have a list
else:
history = text[-MAXHISTORY:]
mismatch = False

View file

@ -1 +1 @@
line-length = 100
line-length = 150