Many fixes to \\nw codes

This commit is contained in:
Dazed 2023-10-29 14:40:36 -05:00
parent b94f171d84
commit 261f9600a4
3 changed files with 178 additions and 82 deletions

View file

@ -81,7 +81,7 @@ def openFiles(filename):
data = json.load(f) data = json.load(f)
# Map Files # Map Files
if 'script' in filename: if 'test' in filename:
translatedData = parseJSON(data, filename) translatedData = parseJSON(data, filename)
else: else:
@ -128,19 +128,20 @@ def translateJSON(data, pbar):
maxHistory = MAXHISTORY maxHistory = MAXHISTORY
tokens = 0 tokens = 0
for key, value in data.items(): for item in data:
# Remove any textwrap # Remove any textwrap
if FIXTEXTWRAP == True: if FIXTEXTWRAP == True:
value = re.sub(r'@b', ' ', value) jaString = item['value']
jaString = jaString.replace('\n', '')
# Translate # Translate
if value == '': if jaString != '':
response = translateGPT(key, 'Past Translated Text: ' + '|\n\n'.join(textHistory), True) response = translateGPT(jaString, 'Past Translated Text: ' + '|\n\n'.join(textHistory), True)
tokens += response[1] tokens += response[1]
translatedText = response[0] translatedText = response[0]
textHistory.append('\"' + translatedText + '\"') textHistory.append('\"' + translatedText + '\"')
else: else:
translatedText = value translatedText = jaString
textHistory.append('\"' + translatedText + '\"') textHistory.append('\"' + translatedText + '\"')
# Textwrap # Textwrap
@ -148,7 +149,7 @@ def translateJSON(data, pbar):
translatedText = translatedText.replace('\n', '@b') translatedText = translatedText.replace('\n', '@b')
# Set Data # Set Data
data[key] = translatedText item['value'] = translatedText
# Keep textHistory list at length maxHistory # Keep textHistory list at length maxHistory
if len(textHistory) > maxHistory: if len(textHistory) > maxHistory:

View file

@ -12,7 +12,7 @@ from modules.tyrano import handleTyrano
from modules.json import handleJSON from modules.json import handleJSON
from modules.kansen import handleKansen from modules.kansen import handleKansen
THREADS = 9 # For GPT4 rate limit will be hit if you have more than 1 thread. THREADS = 5 # For GPT4 rate limit will be hit if you have more than 1 thread.
# Info Message # Info Message
print(Fore.LIGHTYELLOW_EX + "WARNING: Once a translation starts do not close it unless you want to lose your\ print(Fore.LIGHTYELLOW_EX + "WARNING: Once a translation starts do not close it unless you want to lose your\

View file

@ -23,10 +23,10 @@ openai.api_key = os.getenv('key')
APICOST = .002 # Depends on the model https://openai.com/pricing APICOST = .002 # Depends on the model https://openai.com/pricing
PROMPT = Path('prompt.txt').read_text(encoding='utf-8') PROMPT = Path('prompt.txt').read_text(encoding='utf-8')
THREADS = 10 # For GPT4 rate limit will be hit if you have more than 1 thread. THREADS = 10
LOCK = threading.Lock() LOCK = threading.Lock()
WIDTH = 50 WIDTH = 50
LISTWIDTH = 60 LISTWIDTH = 90
MAXHISTORY = 10 MAXHISTORY = 10
ESTIMATE = '' ESTIMATE = ''
TOTALCOST = 0 TOTALCOST = 0
@ -492,6 +492,7 @@ def searchCodes(page, pbar):
nametag = '' nametag = ''
match = [] match = []
syncIndex = 0 syncIndex = 0
CLFlag = False
global LOCK global LOCK
global NAMESLIST global NAMESLIST
@ -616,32 +617,32 @@ def searchCodes(page, pbar):
# Remove nametag from final string # Remove nametag from final string
finalJAString = finalJAString.replace(nametag, '') finalJAString = finalJAString.replace(nametag, '')
elif '\\nw' in finalJAString or '\\NW' in finalJAString: elif '\\nw' in finalJAString or '\\NW' in finalJAString:
matchList = re.findall(r'([\\]+[nN][wW]\[(.+?)\])(.+)', finalJAString) matchList = re.findall(r'([\\]+[nN][wW]\[(.+?)\]+)(.+)', finalJAString)
if len(matchList) != 0: if len(matchList) != 0:
response = translateGPT(matchList[0][1], 'Reply with only the english translation of the NPC name', True) response = translateGPT(matchList[0][1], 'Reply with only the english translation of the NPC name', True)
tokens += response[1]
speaker = response[0].strip('.')
# Set Nametag and Remove from Final String
nametag = matchList[0][0].replace(matchList[0][1], speaker)
finalJAString = finalJAString.replace(matchList[0][0], '')
else: else:
print('wtf') print('wtf')
tokens += response[1]
speaker = response[0].strip('.')
# Set Nametag and Remove from Final String
nametag = matchList[0][0].replace(matchList[0][1], speaker)
finalJAString = finalJAString.replace(matchList[0][0], '')
# Set next item as dialogue # Set next item as dialogue
if (codeList[j + 1]['code'] == 401 and len(codeList[j + 1]['parameters']) > 0) or (codeList[j + 1]['code'] == 0 and len(codeList[j + 1]['parameters']) > 0): # if (codeList[j + 1]['code'] == 401 and len(codeList[j + 1]['parameters']) > 0) or (codeList[j + 1]['code'] == 0 and len(codeList[j + 1]['parameters']) > 0):
# Set name var to top of list # # Set name var to top of list
codeList[j]['parameters'][0] = nametag # codeList[j]['parameters'][0] = nametag
codeList[j]['code'] = code # codeList[j]['code'] = code
j += 1 # j += 1
codeList[j]['parameters'][0] = finalJAString # codeList[j]['parameters'][0] = finalJAString
codeList[j]['code'] = code # codeList[j]['code'] = code
nametag = '' # nametag = ''
else: # else:
# Set nametag in string # Set nametag in string
codeList[j]['parameters'][0] = nametag + finalJAString codeList[j]['parameters'][0] = nametag + finalJAString
codeList[j]['code'] = code codeList[j]['code'] = code
### Only for Specific games where name is surrounded by brackets. ### Only for Specific games where name is surrounded by brackets.
# elif '【' in finalJAString: # elif '【' in finalJAString:
# matchList = re.findall(r'(.+?【(.+?)】.+?)(「.+)', finalJAString) # matchList = re.findall(r'(.+?【(.+?)】.+?)(「.+)', finalJAString)
@ -685,7 +686,6 @@ def searchCodes(page, pbar):
finalJAString = finalJAString.replace('', '-') finalJAString = finalJAString.replace('', '-')
finalJAString = finalJAString.replace('', '...') finalJAString = finalJAString.replace('', '...')
finalJAString = finalJAString.replace(' ', '') finalJAString = finalJAString.replace(' ', '')
finalJAString = finalJAString.replace('\\#', '')
# Remove any RPGMaker Code at start # Remove any RPGMaker Code at start
ffMatchList = re.findall(r'[\\]+[fF]+\[.+?\]', finalJAString) ffMatchList = re.findall(r'[\\]+[fF]+\[.+?\]', finalJAString)
@ -699,14 +699,17 @@ def searchCodes(page, pbar):
if len(rcodeMatch) > 0: if len(rcodeMatch) > 0:
for match in rcodeMatch: for match in rcodeMatch:
finalJAString = finalJAString.replace(match[0],match[1]) finalJAString = finalJAString.replace(match[0],match[1])
# # Formatting Codes
if finalJAString == 'あ、\\!あんな風に、おちんちん入ってるんだ...': # Formatting Codes
print('t') formatMatch = re.findall(r'[\\]+[!><.|#^]', finalJAString)
formatMatch = re.findall(r'[\\]+[!><.]', finalJAString)
if len(formatMatch) > 0: if len(formatMatch) > 0:
for match in formatMatch: for match in formatMatch:
finalJAString = finalJAString.replace(match, '') finalJAString = finalJAString.replace(match, '')
# Center Lines
if '\\CL' in finalJAString:
finalJAString = finalJAString.replace('\\CL', '')
CLFlag = True
# Translate # Translate
if speaker == '' and finalJAString != '': if speaker == '' and finalJAString != '':
@ -727,19 +730,20 @@ def searchCodes(page, pbar):
translatedText = finalJAString translatedText = finalJAString
# Textwrap # Textwrap
if '\n' not in translatedText and '<br>' not in translatedText: if FIXTEXTWRAP == True:
translatedText = textwrap.fill(translatedText, width=WIDTH) translatedText = textwrap.fill(translatedText, width=WIDTH)
if BRFLAG == True: if BRFLAG == True:
translatedText = translatedText.replace('\n', '<br>') translatedText = translatedText.replace('\n', '<br>')
# Add Beginning Text # Add Beginning Text
if CLFlag:
translatedText = '\\CL' + translatedText
CLFlag = False
translatedText = nametag + translatedText translatedText = nametag + translatedText
nametag = '' nametag = ''
# Set Data # Set Data
translatedText = translatedText.replace('\"', '') translatedText = translatedText.replace('\"', '')
translatedText = translatedText.replace('\\CL ', '\\CL')
translatedText = translatedText.replace('\\CL', '\\CL ')
codeList[i]['parameters'][0] = '' codeList[i]['parameters'][0] = ''
codeList[i]['code'] = 0 codeList[i]['code'] = 0
codeList[j]['parameters'][0] = translatedText codeList[j]['parameters'][0] = translatedText
@ -757,7 +761,7 @@ def searchCodes(page, pbar):
if codeList[i]['code'] == 122 and CODE122 == True: if codeList[i]['code'] == 122 and CODE122 == True:
# This is going to be the var being set. (IMPORTANT) # This is going to be the var being set. (IMPORTANT)
varNum = codeList[i]['parameters'][0] varNum = codeList[i]['parameters'][0]
if varNum != 319: if varNum != 11:
continue continue
jaString = codeList[i]['parameters'][4] jaString = codeList[i]['parameters'][4]
@ -884,31 +888,30 @@ def searchCodes(page, pbar):
## Event Code: 101 [Name] [Optional] ## Event Code: 101 [Name] [Optional]
if codeList[i]['code'] == 101 and CODE101 == True: if codeList[i]['code'] == 101 and CODE101 == True:
jaString = codeList[i]['parameters'][0] # Grab String
jaString = ''
if len(codeList[i]['parameters']) > 4:
jaString = codeList[i]['parameters'][4]
if type(jaString) != str: if type(jaString) != str:
continue continue
# Force Speaker # Force Speaker
if 'natu' in jaString: matchList = re.findall(r'(\w+)\\?', jaString)
speaker = 'Natsu' if len(matchList) > 0:
continue if 'エスカ' in jaString:
elif 'kotohana' in jaString: speaker = 'Esuka'
speaker = 'Kotohana' codeList[i]['parameters'][4] = jaString.replace(matchList[0], speaker)
continue continue
elif 'aoi' in jaString: elif 'シュウ' in jaString:
speaker = 'Aoi' speaker = 'Shuu'
continue codeList[i]['parameters'][4] = jaString.replace(matchList[0], speaker)
elif 'misaki' in jaString: continue
speaker = 'Misaki' elif 'ワルチン総統' in jaString:
continue speaker = 'President Waltin'
elif 'koume' in jaString: codeList[i]['parameters'][4] = jaString.replace(matchList[0], speaker)
speaker = 'Koume' continue
continue else:
elif 'titose' in jaString: speaker = ''
speaker = 'Chitose'
continue
else:
speaker = 'Unknown'
# Definitely don't want to mess with files # Definitely don't want to mess with files
if '_' in jaString: if '_' in jaString:
@ -957,11 +960,11 @@ def searchCodes(page, pbar):
continue continue
# Want to translate this script # Want to translate this script
if codeList[i]['code'] == 355 and '$gameSystem.addLog' not in jaString: if codeList[i]['code'] == 355 and '.setProfile' not in jaString:
continue continue
# Don't want to touch certain scripts # Don't want to touch certain scripts
if codeList[i]['code'] == 655 and '$gameSystem.addLog' not in jaString: if codeList[i]['code'] == 655 and '.setProfile' not in jaString:
continue continue
# Need to remove outside code and put it back later # Need to remove outside code and put it back later
@ -1089,7 +1092,7 @@ def searchCodes(page, pbar):
jaString = re.sub(r'\n', '_', jaString) jaString = re.sub(r'\n', '_', jaString)
# Capture Arguments and text # Capture Arguments and text
dtextList = re.findall(r'D_TEXT\s(.+?)\s|D_TEXT\s(.+)', jaString) dtextList = re.findall(r'D_TEXT\s(.+)\s|D_TEXT\s(.+)', jaString)
if len(dtextList) > 0: if len(dtextList) > 0:
if dtextList[0][0] != '': if dtextList[0][0] != '':
dtext = dtextList[0][0] dtext = dtextList[0][0]
@ -1097,9 +1100,6 @@ def searchCodes(page, pbar):
dtext = dtextList[0][1] dtext = dtextList[0][1]
originalDTEXT = dtext originalDTEXT = dtext
# Remove underscores
dtext = re.sub(r'_', ' ', dtext)
# Using this to keep track of 401's in a row. Throws IndexError at EndOfList (Expected Behavior) # Using this to keep track of 401's in a row. Throws IndexError at EndOfList (Expected Behavior)
currentGroup.append(dtext) currentGroup.append(dtext)
@ -1111,7 +1111,7 @@ def searchCodes(page, pbar):
codeList[i]['parameters'][0] = '' codeList[i]['parameters'][0] = ''
i += 1 i += 1
jaString = codeList[i]['parameters'][0] jaString = codeList[i]['parameters'][0]
dtextList = re.findall(r'D_TEXT\s(.+?)\s|D_TEXT\s(.+)', jaString) dtextList = re.findall(r'D_TEXT\s(.+)\s|D_TEXT\s(.+)', jaString)
if len(dtextList) > 0: if len(dtextList) > 0:
if dtextList[0][0] != '': if dtextList[0][0] != '':
dtext = dtextList[0][0] dtext = dtextList[0][0]
@ -1128,14 +1128,14 @@ def searchCodes(page, pbar):
# Clear Group # Clear Group
currentGroup = [] currentGroup = []
# Remove any textwrap
jaString = re.sub(r'\n', '_', jaString)
# Translate # Translate
response = translateGPT(finalJAString, 'Reply with the English Translation.', True) response = translateGPT(finalJAString, 'Reply with the English Translation.', True)
translatedText = response[0] translatedText = response[0]
tokens += response[1] tokens += response[1]
# Textwrap
translatedText = textwrap.fill(translatedText, width=20, drop_whitespace=False)
# Remove characters that may break scripts # Remove characters that may break scripts
charList = ['.', '\"'] charList = ['.', '\"']
for char in charList: for char in charList:
@ -1144,6 +1144,9 @@ def searchCodes(page, pbar):
# Cant have spaces? # Cant have spaces?
translatedText = translatedText.replace(' ', '_') translatedText = translatedText.replace(' ', '_')
# Fix spacing after ___
translatedText = translatedText.replace('__\n', '__')
# Put Args Back # Put Args Back
translatedText = jaString.replace(originalDTEXT, translatedText) translatedText = jaString.replace(originalDTEXT, translatedText)
@ -1214,6 +1217,68 @@ def searchCodes(page, pbar):
else: else:
continue continue
if 'PushGab ' in jaString:
# Remove any textwrap
jaString = re.sub(r'\n', '_', jaString)
# Capture Arguments and text
infoList = re.findall(r'PushGab [0-9]+ (.+)', jaString)
if len(infoList) > 0:
info = infoList[0]
originalInfo = info
# Remove underscores
info = re.sub(r'_', ' ', info)
# Using this to keep track of 401's in a row. Throws IndexError at EndOfList (Expected Behavior)
currentGroup.append(info)
while (codeList[i+1]['code'] == 356):
# Want to translate this script
if 'PushGab ' not in codeList[i+1]['parameters'][0]:
break
codeList[i]['parameters'][0] = ''
i += 1
jaString = codeList[i]['parameters'][0]
infoList = re.findall(r'PushGab [0-9]+ (.+)', jaString)
if len(infoList) > 0:
dtext = infoList[0]
currentGroup.append(info)
# Join up 356 groups for better translation.
if len(currentGroup) > 0:
finalJAString = ' '.join(currentGroup)
else:
finalJAString = info
# Clear Group
currentGroup = []
# Remove any textwrap
jaString = re.sub(r'\n', '_', jaString)
# Translate
response = translateGPT(finalJAString, 'Reply with the English Translation.', True)
translatedText = response[0]
tokens += response[1]
# Remove characters that may break scripts
charList = ['.', '\"']
for char in charList:
translatedText = translatedText.replace(char, '')
# Cant have spaces?
translatedText = translatedText.replace(' ', '_')
# Put Args Back
translatedText = jaString.replace(originalInfo, translatedText)
# Set Data
codeList[i]['parameters'][0] = translatedText
else:
continue
### Event Code: 102 Show Choice ### Event Code: 102 Show Choice
if codeList[i]['code'] == 102 and CODE102 == True: if codeList[i]['code'] == 102 and CODE102 == True:
for choice in range(len(codeList[i]['parameters'][0])): for choice in range(len(codeList[i]['parameters'][0])):
@ -1420,7 +1485,7 @@ def searchSS(state, pbar):
def searchSystem(data, pbar): def searchSystem(data, pbar):
tokens = 0 tokens = 0
context = 'Reply with only the english translation of the UI textbox.\nTranslate "逃げる" as "Escape".\nTranslate"大事なもの" as "Key Items.\nTranslate "最強装備" as "Optimize".\nTranslate "攻撃力" as "Attack".\nTranslate "最大HP" as "Max HP".\nTranslate "経験値" as "EXP".\n Translate "購入する" as "Buy"' context = '\nTranslate "逃げる" as "Escape".\nTranslate"大事なもの" as "Key Items.\nTranslate "最強装備" as "Optimize".\nTranslate "攻撃力" as "Attack".\nTranslate "最大HP" as "Max HP".\nTranslate "経験値" as "EXP".\n Translate "購入する" as "Buy".\nReply with only the english translation of the UI textbox."'
# Title # Title
response = translateGPT(data['gameTitle'], ' Reply with the English translation of the game title name', False) response = translateGPT(data['gameTitle'], ' Reply with the English translation of the game title name', False)
@ -1470,7 +1535,7 @@ def searchSystem(data, pbar):
# Messages # Messages
messages = (data['terms']['messages']) messages = (data['terms']['messages'])
for key, value in messages.items(): for key, value in messages.items():
response = translateGPT(value, 'Reply with only the english translation of the battle text.\nTranslate "常時ダッシュ" as "Always Dash".', False) response = translateGPT(value, 'Reply with only the english translation of the battle text.\nTranslate "常時ダッシュ" as "Always Dash"\nTranslate "次の%1まで" as Next %1.', False)
translatedText = response[0] translatedText = response[0]
# Remove characters that may break scripts # Remove characters that may break scripts
@ -1507,11 +1572,11 @@ def subVars(jaString):
# Names # Names
count = 0 count = 0
nameList = re.findall(r'[\\]+[nN]\[[0-9]+\]', jaString) nameList = re.findall(r'[\\]+[nN]\[.+?\]+', jaString)
nameList = set(nameList) nameList = set(nameList)
if len(nameList) != 0: if len(nameList) != 0:
for name in nameList: for name in nameList:
jaString = jaString.replace(name, '[Name' + str(count) + ']') jaString = jaString.replace(name, '[N' + str(count) + ']')
count += 1 count += 1
# Variables # Variables
@ -1523,8 +1588,19 @@ def subVars(jaString):
jaString = jaString.replace(var, '[Var' + str(count) + ']') jaString = jaString.replace(var, '[Var' + str(count) + ']')
count += 1 count += 1
# Formatting
count = 0
if '笑えるよね.' in jaString:
print('t')
formatList = re.findall(r'[\\]+CL', jaString)
formatList = set(formatList)
if len(formatList) != 0:
for var in formatList:
jaString = jaString.replace(var, '[FCode' + str(count) + ']')
count += 1
# Put all lists in list and return # Put all lists in list and return
allList = [iconList, colorList, nameList, varList] allList = [iconList, colorList, nameList, varList, formatList]
return [jaString, allList] return [jaString, allList]
def resubVars(translatedText, allList): def resubVars(translatedText, allList):
@ -1553,7 +1629,7 @@ def resubVars(translatedText, allList):
count = 0 count = 0
if len(allList[2]) != 0: if len(allList[2]) != 0:
for var in allList[2]: for var in allList[2]:
translatedText = translatedText.replace('[Name' + str(count) + ']', var) translatedText = translatedText.replace('[N' + str(count) + ']', var)
count += 1 count += 1
# Vars # Vars
@ -1563,6 +1639,13 @@ def resubVars(translatedText, allList):
translatedText = translatedText.replace('[Var' + str(count) + ']', var) translatedText = translatedText.replace('[Var' + str(count) + ']', var)
count += 1 count += 1
# Formatting
count = 0
if len(allList[4]) != 0:
for var in allList[4]:
translatedText = translatedText.replace('[FCode' + str(count) + ']', var)
count += 1
# Remove Color Variables Spaces # Remove Color Variables Spaces
# if '\\c' in translatedText: # if '\\c' in translatedText:
# translatedText = re.sub(r'\s*(\\+c\[[1-9]+\])\s*', r' \1', translatedText) # translatedText = re.sub(r'\s*(\\+c\[[1-9]+\])\s*', r' \1', translatedText)
@ -1586,13 +1669,25 @@ def translateGPT(t, history, fullPromptFlag):
return(t, 0) return(t, 0)
"""Translate text using GPT""" """Translate text using GPT"""
context = 'Eroge Names Context: (Name: サクラ == Sakura\nGender: Female,\n\nName: ルリカ == Rurika\nGender: Female,\n\nName: ケンイチ == Kenichi\nGender: Male)' context = '```\
Character Names Context:\
Name: クノ == Kuno - Gender: Female\
Name: ノンナ == Nonna - Gender: Female\
Name: フロス == Fross - Gender: Female\
Name: シア == Shia - Gender: Female\
Name: ウカ == Uka - Gender: Female\
Name: プラエ == Plae - Gender: Female\
Name: サナ == Sana - Gender: Female\
Name: サリア == Saria - Gender: Female\
Name: 太郎 == Taro - Gender: Male\
```'
if fullPromptFlag: if fullPromptFlag:
system = PROMPT system = PROMPT.replace('\n', '')
user = 'Line to Translate: ' + subbedT user = 'Line to Translate = ' + subbedT
else: else:
system = 'You are an expert translator who translates everything to English. Reply with only the English Translation of the text.' system = 'You are an expert translator who translates everything to English. Reply with only the English Translation of the text.'
user = 'Line to Translate: ' + subbedT user = 'Line to Translate = ' + subbedT
response = openai.ChatCompletion.create( response = openai.ChatCompletion.create(
temperature=0, temperature=0,
frequency_penalty=0.2, frequency_penalty=0.2,
@ -1617,10 +1712,10 @@ def translateGPT(t, history, fullPromptFlag):
# Remove Placeholder Text # Remove Placeholder Text
translatedText = translatedText.replace('English Translation: ', '') translatedText = translatedText.replace('English Translation: ', '')
translatedText = translatedText.replace('Translation: ', '') translatedText = translatedText.replace('Translation: ', '')
translatedText = translatedText.replace('Line to Translate: ', '') translatedText = translatedText.replace('Line to Translate = ', '')
translatedText = translatedText.replace('English Translation:', '') translatedText = translatedText.replace('English Translation:', '')
translatedText = translatedText.replace('Translation:', '') translatedText = translatedText.replace('Translation:', '')
translatedText = translatedText.replace('Line to Translate:', '') translatedText = translatedText.replace('Line to Translate = ', '')
translatedText = re.sub(r'\n\nPast Translated Text:.*', '', translatedText, 0, re.DOTALL) translatedText = re.sub(r'\n\nPast Translated Text:.*', '', translatedText, 0, re.DOTALL)
translatedText = re.sub(r'Note:.*', '', translatedText) translatedText = re.sub(r'Note:.*', '', translatedText)
translatedText = translatedText.replace('', '') translatedText = translatedText.replace('', '')