From 668a5509cd5801326d0ef81bd8556665b4c3cbdc Mon Sep 17 00:00:00 2001 From: ThyWolf Date: Mon, 8 May 2023 21:55:06 -0700 Subject: [PATCH] update support scripts --- Scripts/cleanseTerms.py | 30 ++++++++++++++++++++++++++++++ Scripts/translateAutoSync.py | 16 +++++++++++++++- 2 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 Scripts/cleanseTerms.py diff --git a/Scripts/cleanseTerms.py b/Scripts/cleanseTerms.py new file mode 100644 index 0000000000..530f0f8800 --- /dev/null +++ b/Scripts/cleanseTerms.py @@ -0,0 +1,30 @@ +import os +import re + +def unpack_record(record): + term = "" + text = "" + try: + (term, text) = record.split("=", 1) + text = text.strip() + except: + term = record + + return term, text if text != "" else "EMPTY" + +def upper_repl(match): + return match.group(1) + "=" + match.group(2).upper() + match.group(3) + +for root, dirs, files in os.walk('.'): + for file in files: + if file.endswith('.txt'): + filename = os.path.join(root, file) + print(f"sorting {filename}") + with open(filename, "rt", encoding="utf-8") as f: + data = f.readlines() + data[0] = data[0].replace('', '') + for idx, record in enumerate(data): + data[idx] = re.sub(r"(.+?)=(.)(.*)", upper_repl, data[idx]) + data.sort() + with open(filename, "wt", encoding="utf-8") as f: + f.writelines(data) \ No newline at end of file diff --git a/Scripts/translateAutoSync.py b/Scripts/translateAutoSync.py index 25d83c23e8..1009622876 100644 --- a/Scripts/translateAutoSync.py +++ b/Scripts/translateAutoSync.py @@ -8,6 +8,9 @@ import os import codecs +from deep_translator import GoogleTranslator + +CHARS_MAX = 4500 def unpack_record(record): term = "" @@ -20,6 +23,16 @@ def unpack_record(record): return term, text if text != "" else "EMPTY" +def translate_text(text, code): + text = text.replace("\\n", "{99}") + if len(text) > 3 and len(text) <= CHARS_MAX: + translated = GoogleTranslator(source="auto", target=code).translate(text) + else: + translated = text + translated = translated.replace("{99}", "\\n") + + return translated + def readRecord(filename): # read file and split with "=" to dict dic = {} @@ -48,7 +61,8 @@ def sync_file(input_file, output_file, code): # compare for key, value in inputDict.items(): if key not in outputDict: - outputDict[key] = value + # outputDict[key] = value + outputDict[key] = translate_text(value, code) print(f"\t+ {output_file} add:{key}={value}") # write with open(output_file, "wt", encoding="utf-8") as f: