diff --git a/modules/rpgmakermvmz.py b/modules/rpgmakermvmz.py index 8692792..a3e4150 100644 --- a/modules/rpgmakermvmz.py +++ b/modules/rpgmakermvmz.py @@ -512,7 +512,9 @@ def searchNames(data, pbar, context): profileList = [] nicknameList = [] descriptionList = [] - noteList = [] + # For batching all note types + notesBatch = [] # List of (i, regex, match_text, note_type) + notesBatchMap = [] # List of (i, regex, match_text, note_type, groupidx) i = 0 # Counter j = 0 # Counter 2 filling = False @@ -540,13 +542,77 @@ def searchNames(data, pbar, context): # Names with open("translations.txt", "a", encoding="utf-8") as file: file.write(f"\n#{context}\n") + + # --- Batching pass: collect all note texts for all note types --- + note_regexes = [ + (r"", False), + (r"", False), + (r"", False), + (r"", False), + (r"", False), + (r"", False), + (r"", False), + (r"", False), + (r"", False), + (r"\n(.*)\n", False), + (r"", False), + (r"WATs:(.+?)>", False), + (r"ADTs?:(.+?)>", False), + (r"", False), + (r"", False), + (r"]+)", True), + (r"]+)", True), + (r"]+)", True), + (r"", True), + (r"", True), + (r"", False), + (r"<拡張説明:(.+?)>", False), + (r"\n(.+?)\n<", False), + ] + # For each entry, collect all note matches + for idx, entry in enumerate(data): + if entry is None or "note" not in entry or not entry["note"]: + continue + note = entry["note"] + for regex, wordwrap in note_regexes: + matches = re.findall(regex, note, re.DOTALL) + for m in matches: + match_text = m if isinstance(m, str) else m[0] + notesBatch.append(match_text) + notesBatchMap.append((idx, regex, match_text, wordwrap)) + + # --- Batch translate all notes --- + translatedNotesBatch = [] + if notesBatch: + response = translateGPT(notesBatch, f"Reply with only the {LANGUAGE} translation of the note text.", True) + translatedNotesBatch = response[0] + totalTokens[0] += response[1][0] + totalTokens[1] += response[1][1] + + # --- Insert translated notes back --- + note_insert_idx = 0 + for idx, regex, match_text, wordwrap in notesBatchMap: + if note_insert_idx >= len(translatedNotesBatch): + break + translated = translatedNotesBatch[note_insert_idx] + if wordwrap: + translated = dazedwrap.wrapText(translated, width=NOTEWIDTH) + translated = translated.replace('"', "") + # Use a safe literal match for the replacement (no re.escape, just str.replace) + data[idx]["note"] = data[idx]["note"].replace(match_text, translated, 1) + note_insert_idx += 1 + + # Now continue with the rest of the batching logic for names, descriptions, etc. + i = 0 + filling = False + batchFull = False + mismatch = False while i < len(data) or filling == True: if i < len(data): # Empty Data if data[i] is None or data[i]["name"] == "": i += 1 continue - # Filling up Batch filling = True if context in "Actors": @@ -557,16 +623,6 @@ def searchNames(data, pbar, context): nicknameList.append(data[i]["nickname"]) if "profile" in data[i] and data[i]["profile"]: profileList.append(data[i]["profile"].replace("\n", " ")) - - # Notes - if "") - totalTokens[0] += tokensResponse[0] - totalTokens[1] += tokensResponse[1] - if "PE拡張" in data[i]["note"]: - tokensResponse = translateNote(data[i], r"") - totalTokens[0] += tokensResponse[0] - totalTokens[1] += tokensResponse[1] i += 1 else: batchFull = True @@ -577,80 +633,6 @@ def searchNames(data, pbar, context): description = data[i]["description"] description = description.replace("\n", " ") descriptionList.append(description) - if "") - totalTokens[0] += tokensResponse[0] - totalTokens[1] += tokensResponse[1] - if "") - totalTokens[0] += tokensResponse[0] - totalTokens[1] += tokensResponse[1] - if "") - totalTokens[0] += tokensResponse[0] - totalTokens[1] += tokensResponse[1] - if "") - totalTokens[0] += tokensResponse[0] - totalTokens[1] += tokensResponse[1] - if "") - totalTokens[0] += tokensResponse[0] - totalTokens[1] += tokensResponse[1] - if "") - totalTokens[0] += tokensResponse[0] - totalTokens[1] += tokensResponse[1] - if "") - totalTokens[0] += tokensResponse[0] - totalTokens[1] += tokensResponse[1] - if "Switch Shop Description" in data[i]["note"]: - tokensResponse = translateNote(data[i], r"\n(.*)\n") - totalTokens[0] += tokensResponse[0] - totalTokens[1] += tokensResponse[1] - if "") - totalTokens[0] += tokensResponse[0] - totalTokens[1] += tokensResponse[1] - if "") - totalTokens[0] += tokensResponse[0] - totalTokens[1] += tokensResponse[1] - if "") - totalTokens[0] += tokensResponse[0] - totalTokens[1] += tokensResponse[1] - if "") - totalTokens[0] += tokensResponse[0] - totalTokens[1] += tokensResponse[1] - if "") - totalTokens[0] += tokensResponse[0] - totalTokens[1] += tokensResponse[1] - if "sub_1" in data[i]["note"]: - tokensResponse = translateNote(data[i], r"]+)", True) - totalTokens[0] += tokensResponse[0] - totalTokens[1] += tokensResponse[1] - if "sub_2" in data[i]["note"]: - tokensResponse = translateNote(data[i], r"]+)", True) - totalTokens[0] += tokensResponse[0] - totalTokens[1] += tokensResponse[1] - if "sub_3" in data[i]["note"]: - tokensResponse = translateNote(data[i], r"]+)", True) - totalTokens[0] += tokensResponse[0] - totalTokens[1] += tokensResponse[1] - if "infowindow" in data[i]["note"]: - tokensResponse = translateNote(data[i], r"", True) - totalTokens[0] += tokensResponse[0] - totalTokens[1] += tokensResponse[1] - if "ExtendDesc" in data[i]["note"]: - tokensResponse = translateNote(data[i], r"", True) - totalTokens[0] += tokensResponse[0] - totalTokens[1] += tokensResponse[1] - - i += 1 else: batchFull = True @@ -659,8 +641,7 @@ def searchNames(data, pbar, context): nameList.append(data[i]["name"]) if "description" in data[i] and data[i]["description"]: descriptionList.append(data[i]["description"].replace("\n", " ")) - - # Messages + # Messages (unchanged) number = 1 while number < 5: if f"message{number}" in data[i] and data[i][f"message{number}"]: @@ -676,7 +657,6 @@ def searchNames(data, pbar, context): totalTokens[0] += msgResponse[1][0] totalTokens[1] += msgResponse[1][1] number += 1 - else: msgResponse = translateGPT( data[i][f"message{number}"], @@ -689,42 +669,12 @@ def searchNames(data, pbar, context): number += 1 else: number += 1 - - # Notes - if "") - totalTokens[0] += tokensResponse[0] - totalTokens[1] += tokensResponse[1] - if "") - totalTokens[0] += tokensResponse[0] - totalTokens[1] += tokensResponse[1] - if "<拡張説明:" in data[i]["note"]: - tokensResponse = translateNote(data[i], r"<拡張説明:(.+?)>") - totalTokens[0] += tokensResponse[0] - totalTokens[1] += tokensResponse[1] - if "" in data[i]["note"]: - tokensResponse = translateNote(data[i], r"\n(.+?)\n<") - totalTokens[0] += tokensResponse[0] - totalTokens[1] += tokensResponse[1] - i += 1 else: batchFull = True if context in ["Enemies", "Classes", "MapInfos"]: if len(nameList) < BATCHSIZE: nameList.append(data[i]["name"]) - - # Notes - if "note" in data[i]: - if "") - totalTokens[0] += tokensResponse[0] - totalTokens[1] += tokensResponse[1] - if "") - totalTokens[0] += tokensResponse[0] - totalTokens[1] += tokensResponse[1] i += 1 else: batchFull = True @@ -2311,23 +2261,45 @@ Translate 'Taroを倒した!' as 'Taro was defeated!'", False, ) - # Translate State Notes - if "help" in state["note"]: - noteResponse = translateNote(state, r"]*)>") - totalTokens[0] += noteResponse[0] - totalTokens[1] += noteResponse[1] - if "STATE_HELP" in state["note"]: - noteResponse = translateNote(state, r"\n(.*)\n") - totalTokens[0] += noteResponse[0] - totalTokens[1] += noteResponse[1] - if "ShowHoverState" in state["note"]: - noteResponse = translateNote(state, r"") - totalTokens[0] += noteResponse[0] - totalTokens[1] += noteResponse[1] - if "") - totalTokens[0] += noteResponse[0] - totalTokens[1] += noteResponse[1] + # --- Batching pass: collect all note texts for all note types --- + note_regexes = [ + (r"]*)>", False), + (r"\n(.*)\n", False), + (r"", False), + (r"", False), + ] + notesBatch = [] + notesBatchMap = [] + if "note" in state and state["note"]: + note = state["note"] + for regex, wordwrap in note_regexes: + matches = re.findall(regex, note, re.DOTALL) + for m in matches: + match_text = m if isinstance(m, str) else m[0] + notesBatch.append(match_text) + notesBatchMap.append((regex, match_text, wordwrap)) + + # --- Batch translate all notes --- + translatedNotesBatch = [] + if notesBatch: + response = translateGPT(notesBatch, f"Reply with only the {LANGUAGE} translation of the note text.", True) + translatedNotesBatch = response[0] + totalTokens[0] += response[1][0] + totalTokens[1] += response[1][1] + + # --- Insert translated notes back --- + note_insert_idx = 0 + if "note" in state and state["note"]: + for regex, match_text, wordwrap in notesBatchMap: + if note_insert_idx >= len(translatedNotesBatch): + break + translated = translatedNotesBatch[note_insert_idx] + if wordwrap: + translated = dazedwrap.wrapText(translated, width=NOTEWIDTH) + translated = translated.replace('"', "") + # Replace only the matched text in the note + state["note"] = re.sub(re.escape(match_text), translated, state["note"], count=1) + note_insert_idx += 1 # Count totalTokens totalTokens[0] += nameResponse[1][0] if nameResponse != "" else 0 diff --git a/vocab.txt b/vocab.txt index ac56d45..a458492 100644 --- a/vocab.txt +++ b/vocab.txt @@ -12,20 +12,20 @@ Here are some vocabulary and terms so that you know the proper spelling and tran カッツ (Katz) - Female シエル (Ciel) - Male ディック (Dick) - Male -ガンドレー (Gandore) - Male +ガンドレー (Gandrei) - Male デュオレクス (Duorex) - Male ガーラ (Gara) - Female ガゼル (Gazel) - Male ギタン (Gitan) - Male グリッド (Grid) - Male イスペリオ (Isperio) - Male -ケイトリン (Kaitlyn) - Female +ケイトリン (Caitlin) - Female コヴァ (Kova) - Male -レクス (Lex) - Male -リィネ (Lynn) - Female +レクス (Rex) - Male +リィネ (Rine) - Female ミラノ (Mirano) - Male ミシェラ (Mishela) - Female -ネリス (Nelis) - Female +ネリス (Neris) - Female オルネ (Orne) - Female オクヴァン (Oxvan) - Male ラジール (Ragil) - Male