From c0401374e5bb3565ce8bbd452fa74033269c9646 Mon Sep 17 00:00:00 2001 From: Dazed Date: Sun, 2 Apr 2023 23:43:28 -0500 Subject: [PATCH] feat: add wordwrap --- main.py | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/main.py b/main.py index d8bc018..6c73d4f 100644 --- a/main.py +++ b/main.py @@ -1,5 +1,6 @@ import os import re +import textwrap from googletrans import Translator from pathlib import Path import json @@ -45,6 +46,7 @@ def parse401(data): translatedBatchList.append(translateGPT(batch)) translatedText = ''.join(translatedBatchList) translatedTextList = translatedText.split('/p') + translatedTextList = wordwrapList(translatedTextList) translatedTextList = matchListSize(untranslatedTextList, translatedTextList) # Write to new json file @@ -54,8 +56,11 @@ def parse401(data): for page in event['pages']: for command in page['list']: if command['code'] == 401: - command['parameters'][0] = translatedTextList[i] - i += 1 + if command['parameters'][0] == '': + del command['parameters'][0] + else: + command['parameters'][0] = translatedTextList[i] + i += 1 with open('file.json', 'w') as f: json.dump(data, f) @@ -98,7 +103,7 @@ def translateGPT(t): t = pipe.join(t) response = openai.ChatCompletion.create( - temperature=0.2, + temperature=0, model="gpt-3.5-turbo", messages=[ {"role": "system", "content": system}, @@ -115,4 +120,12 @@ def matchListSize(list1, list2): return list2 +def wordwrapList(list): + wrappedList = [] + + for item in list: + wrappedList.append(textwrap.fill(item, width=50)) + + return wrappedList + main()