feat: Threading added
This commit is contained in:
parent
46158bad38
commit
0b3f640a73
5 changed files with 68 additions and 90 deletions
3
.gitignore
vendored
3
.gitignore
vendored
|
|
@ -1 +1,2 @@
|
|||
.env
|
||||
.env
|
||||
*.json
|
||||
0
files/.gitkeep
Normal file
0
files/.gitkeep
Normal file
152
main.py
152
main.py
|
|
@ -1,3 +1,4 @@
|
|||
from concurrent.futures import ThreadPoolExecutor
|
||||
import os
|
||||
import re
|
||||
import textwrap
|
||||
|
|
@ -5,102 +6,81 @@ import json
|
|||
from dotenv import load_dotenv
|
||||
import openai
|
||||
|
||||
#Globals
|
||||
load_dotenv()
|
||||
openai.organization = os.getenv('org')
|
||||
openai.api_key = os.getenv('key')
|
||||
pipe = '###'
|
||||
THREADS = 5
|
||||
|
||||
def main():
|
||||
# Load JSON file into a Python dictionary
|
||||
with open('Map013.json', 'r', encoding='utf-8') as f:
|
||||
data = json.load(f)
|
||||
parse401(data)
|
||||
# Open File (Threads)
|
||||
test = 0
|
||||
with ThreadPoolExecutor(max_workers=THREADS, thread_name_prefix='handle') as executor:
|
||||
for filename in os.listdir("files"):
|
||||
if filename.endswith('json'):
|
||||
executor.submit(handle, filename)
|
||||
|
||||
def count401Groups(data):
|
||||
"""
|
||||
Counts the number of groups of 401 in a dictionary of events.
|
||||
def handle(filename):
|
||||
with open('translated/' + filename, 'w', encoding='UTF-8') as outFile:
|
||||
with open('files/' + filename, 'r', encoding='UTF-8') as f:
|
||||
# Map Files
|
||||
if 'Map' in filename:
|
||||
translatedData = parseMap(json.load(f))
|
||||
json.dump(translatedData, outFile, ensure_ascii=False)
|
||||
print('Translated: {0}'.format(filename))
|
||||
|
||||
|
||||
Args:
|
||||
data (list): A list of events.
|
||||
def parseMap(data):
|
||||
test = 0
|
||||
with ThreadPoolExecutor(max_workers=THREADS, thread_name_prefix='parseMap') as executor:
|
||||
events = data['events']
|
||||
for event in events:
|
||||
if event is not None:
|
||||
executor.submit(handleParseMap, event, test)
|
||||
return data
|
||||
|
||||
Returns:
|
||||
int: The number of groups of 401.
|
||||
"""
|
||||
# Count 401 Groups
|
||||
events = data['events']
|
||||
groupCount = 0
|
||||
currentGroupCount = 0
|
||||
for event in events:
|
||||
if event is not None:
|
||||
for page in event['pages']:
|
||||
for command in page['list']:
|
||||
if command['code'] == 401:
|
||||
currentGroupCount += 1
|
||||
else:
|
||||
if currentGroupCount > 0:
|
||||
groupCount += 1
|
||||
currentGroupCount = 0
|
||||
if currentGroupCount > 1:
|
||||
groupCount += 1
|
||||
currentGroupCount = 0
|
||||
return groupCount
|
||||
def handleParseMap(event, test):
|
||||
print(test)
|
||||
test = test+1
|
||||
for page in event['pages']:
|
||||
searchCodes(page)
|
||||
return page
|
||||
|
||||
def parse401(data):
|
||||
"""
|
||||
Extracts the first parameter of all commands with code 401 from a dictionary of events.
|
||||
|
||||
Args:
|
||||
data (list): A list of events.
|
||||
|
||||
Returns:
|
||||
list: A list of strings containing the first parameter of all commands with code 401.
|
||||
"""
|
||||
# Extract 401 Text (Needed for Context)
|
||||
def searchCodes(page):
|
||||
translatedText = ''
|
||||
currentGroup = []
|
||||
textHistory = []
|
||||
try:
|
||||
for i in range(len(page['list'])):
|
||||
if page['list'][i]['code'] == 401:
|
||||
currentGroup.append(page['list'][i]['parameters'][0])
|
||||
|
||||
events = data['events']
|
||||
for event in events:
|
||||
if event is not None:
|
||||
for page in event['pages']:
|
||||
try:
|
||||
for i in range(len(page['list'])):
|
||||
if page['list'][i]['code'] == 401:
|
||||
currentGroup.append(page['list'][i]['parameters'][0])
|
||||
#textHistory.append(page['list'][i]['parameters'][0])
|
||||
|
||||
while (page['list'][i+1]['code'] == 401):
|
||||
del page['list'][i]
|
||||
currentGroup.append(page['list'][i]['parameters'][0])
|
||||
#textHistory.append(page['list'][i]['parameters'][0])
|
||||
else:
|
||||
if len(currentGroup) > 0:
|
||||
text = ''.join(currentGroup)
|
||||
text = text.replace('\\n', '')
|
||||
print('Translating' + text)
|
||||
translatedText = translateGPT(text, ' '.join(textHistory))
|
||||
textHistory.append(translatedText)
|
||||
|
||||
translatedText = textwrap.fill(translatedText, width=50)
|
||||
page['list'][i-1]['parameters'][0] = translatedText
|
||||
if len(textHistory) > 50:
|
||||
for i in range(len(currentGroup)):
|
||||
textHistory.pop(0)
|
||||
currentGroup = []
|
||||
except IndexError:
|
||||
print('End of List')
|
||||
|
||||
# Append leftover groups
|
||||
while (page['list'][i+1]['code'] == 401):
|
||||
del page['list'][i]
|
||||
currentGroup.append(page['list'][i]['parameters'][0])
|
||||
else:
|
||||
if len(currentGroup) > 0:
|
||||
translatedText = translateGPT(''.join(currentGroup), ' '.join(textHistory))
|
||||
text = ''.join(currentGroup)
|
||||
text = text.replace('\\n', '')
|
||||
print('Translating' + text)
|
||||
translatedText = translateGPT(text, ' '.join(textHistory))
|
||||
textHistory.append(translatedText)
|
||||
translatedText = textwrap.fill(translatedText, width=50)
|
||||
page['list'][i]['parameters'][0] = translatedText
|
||||
page['list'][i-1]['parameters'][0] = translatedText
|
||||
if len(textHistory) > 1:
|
||||
textHistory.pop(0)
|
||||
currentGroup = []
|
||||
except IndexError:
|
||||
print('End of List')
|
||||
|
||||
# Append leftover groups
|
||||
if len(currentGroup) > 0:
|
||||
translatedText = translateGPT(''.join(currentGroup), ' '.join(textHistory))
|
||||
translatedText = textwrap.fill(translatedText, width=50)
|
||||
page['list'][i]['parameters'][0] = translatedText
|
||||
currentGroup = []
|
||||
|
||||
with open('file.json', 'w', encoding='utf-8') as f:
|
||||
json.dump(data, f)
|
||||
|
||||
def translateGPT(t, history):
|
||||
|
||||
pattern = r'[一-龠]+|[ぁ-ゔ]+|[ァ-ヴー]+'
|
||||
|
|
@ -109,15 +89,15 @@ def translateGPT(t, history):
|
|||
|
||||
"""Translate text using GPT"""
|
||||
|
||||
system = "Context: " + history + "\n\n###\n\n You are a professional Japanese visual novel translator, \
|
||||
editor, and localizer. You always manage to convey the original meaning of the Japanese text to your output, \
|
||||
and localize it in a way that an average American would understand. \
|
||||
system = "Context: " + history + "\n\n###\n\n You are a professional Japanese visual novel translator,\
|
||||
editor, and localizer. You always manage to convey the original meaning of the Japanese text to your output,\
|
||||
and localize it in a way that an average American would understand.\
|
||||
The 'Context' at the top is previously translated text for the work.\
|
||||
You translate Onomatopoeia literally. \
|
||||
When I give you something to translate, answer with just the translation. \
|
||||
Translation Examples: \
|
||||
\\n<ルイ>そう、私はルイよ。= \\n<Rui> Yes, I'm Rui. \
|
||||
\\nそう、私はルイよ。= \\nYes, I'm Rui." \
|
||||
You translate Onomatopoeia literally.\
|
||||
When I give you something to translate, answer with just the translation.\
|
||||
Translation Examples:\
|
||||
\\n<ルイ>そう、私はルイよ。= \\n<Rui> Yes, I'm Rui.\
|
||||
\\nそう、私はルイよ。= \\nYes, I'm Rui."
|
||||
|
||||
response = openai.ChatCompletion.create(
|
||||
temperature=0,
|
||||
|
|
|
|||
|
|
@ -1,3 +0,0 @@
|
|||
{
|
||||
"Key": "Authorization: Bearer <OPEN_AI_KEY>"
|
||||
}
|
||||
0
translated/.gitkeep
Normal file
0
translated/.gitkeep
Normal file
Loading…
Reference in a new issue