File size: 14,341 Bytes
b5698d6 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 |
## built-in libraries
import os
import sys
import json
import asyncio
import re
import typing
## third-party libraries
from kairyou import Kairyou
from kairyou import Indexer
from kairyou.types import NameAndOccurrence
## custom modules
from models.kaiseki import Kaiseki
from models.kijiku import Kijiku
from handlers.json_handler import JsonHandler
from modules.common.toolkit import Toolkit
from modules.common.file_ensurer import FileEnsurer
from modules.common.logger import Logger
##-------------------start-of-Kudasai---------------------------------------------------------------------------------------------------------------------------------------------------------------------------
class Kudasai:
"""
Kudasai class is the main class for the Kudasai program. It handles all logic for CLI & Console versions of Kudasai.
"""
connection:bool
text_to_preprocess:str
replacement_json:dict
need_to_run_kairyou:bool = True
##-------------------start-of-boot()---------------------------------------------------------------------------------------------------------------------------------------------------------------------------
@staticmethod
def boot() -> None:
"""
Does some logging and sets up the console window, regardless of whether the user is running the CLI, WebGUI, or Console version of Kudasai.
"""
os.system("title " + "Kudasai")
Toolkit.clear_console()
FileEnsurer.setup_needed_files()
Logger.clear_log_file()
Logger.log_barrier()
Logger.log_action("Kudasai started")
Logger.log_action("Current version: " + Toolkit.CURRENT_VERSION)
Logger.log_barrier()
Logger.push_batch()
Logger.clear_batch()
try:
with open(FileEnsurer.config_kijiku_rules_path, "r") as kijiku_rules_file:
JsonHandler.current_kijiku_rules = json.load(kijiku_rules_file)
JsonHandler.validate_json()
assert JsonHandler.current_kijiku_rules != FileEnsurer.INVALID_KIJIKU_RULES_PLACEHOLDER
except:
print("Invalid kijiku_rules.json file. Please check the file for errors. If you are unsure, delete the file and run Kudasai again. Your kijiku rules file is located at: " + FileEnsurer.config_kijiku_rules_path)
Toolkit.pause_console()
raise Exception("Invalid kijiku_rules.json file. Please check the file for errors. If you are unsure, delete the file and run Kudasai again. Your kijiku rules file is located at: " + FileEnsurer.config_kijiku_rules_path)
##-------------------start-of-run_kairyou_indexer()---------------------------------------------------------------------------------------------------------------------------------------------------------------------------
@staticmethod
def run_kairyou_indexer(text_to_index:str, replacement_json:typing.Union[dict,str]) -> typing.Tuple[str, str]:
"""
Runs the Kairyou Indexer.
Parameters:
text_to_index (str): The text to index.
replacement_json (dict): The replacement json.
Returns:
text_to_index (str): The indexed text.
indexing_log (str): The log of the indexing process.
"""
Toolkit.clear_console()
knowledge_base = input("Please enter the path to the knowledge base you would like to use for the indexer (can be text, a path to a txt file, or a path to a directory of txt files):\n").strip('"')
## unique names is a list of named tuples, with the fields name and occurrence
unique_names, indexing_log = Indexer.index(text_to_index, knowledge_base, replacement_json)
## for each name in unique_names, we need to replace that name in the text_to_process with >>>name<<<
## but since it returns the occurrence of the name, we only need to replace that occurrence of the name in the text_to_process
## So if a name has 42 occurrences, but only the 3rd and 4th occurrence were flagged, we only need to replace the 3rd and 4th occurrence of the name in the text_to_process
text_to_index = Kudasai.mark_indexed_names(text_to_index, unique_names)
return text_to_index, indexing_log
##-------------------start-of-mark_indexed_names()---------------------------------------------------------------------------------------------------------------------------------------------------------------------------
@staticmethod
def mark_indexed_names(text:str, unique_names:typing.List[NameAndOccurrence]) -> str:
"""
Marks indexed names in the text.
This does not mark all names, but rather the specific occurrences of the names that were flagged by the indexer.
Parameters:
text (str): The text to mark.
unique_names (list - NameAndOccurrence): The list of unique names.
Returns:
str: The marked text.
"""
for name_tuple in unique_names:
name = name_tuple.name
pattern = re.compile(re.escape(name)) ## Prepare regex pattern, escaping the name to handle special characters
current_pos = 0
new_text = ""
last_end = 0
for match in pattern.finditer(text):
current_pos += 1
if(current_pos == name_tuple.occurrence):
new_text += text[last_end:match.start()] + f">>>{name}<<<"
last_end = match.end()
new_text += text[last_end:] # Append the rest of the text
text = new_text
return text
##-------------------start-of-run_kudasai()---------------------------------------------------------------------------------------------------------------------------------------------------------------------------
@staticmethod
async def run_kudasai() -> None:
"""
Runs the Kudasai program. Used for CLI and Console versions of Kudasai. Not used for the WebGUI version of Kudasai.
"""
Kudasai.handle_update_check()
if(Kudasai.need_to_run_kairyou):
indexing_log = ""
if(Kudasai.replacement_json not in ["", FileEnsurer.blank_rules_path, FileEnsurer.standard_read_json(FileEnsurer.blank_rules_path)] and input("Would you like to use Kairyou's Indexer to index the preprocessed text? (1 for yes, 2 for no)\n") == "1"):
Kudasai.text_to_preprocess, indexing_log = Kudasai.run_kairyou_indexer(Kudasai.text_to_preprocess, Kudasai.replacement_json)
preprocessed_text, preprocessing_log, error_log = Kairyou.preprocess(Kudasai.text_to_preprocess, Kudasai.replacement_json)
## Need to set this so auto-translation can use the preprocessed text
Kudasai.text_to_preprocess = preprocessed_text
## add index log to preprocessing log
if(indexing_log != ""):
preprocessing_log = indexing_log + "\n\n" + preprocessing_log
print(preprocessing_log)
timestamp = Toolkit.get_timestamp(is_archival=True)
FileEnsurer.write_kairyou_results(preprocessed_text, preprocessing_log, error_log, timestamp)
Toolkit.pause_console("\nPress any key to continue to Auto-Translation...")
Toolkit.clear_console()
else:
print("(Preprocessing skipped)")
await Kudasai.determine_autotranslation_module()
Toolkit.pause_console("\nPress any key to exit...")
##-------------------start-of-handle_update_check()---------------------------------------------------------------------------------------------------------------------------------------------------------------------------
@staticmethod
def handle_update_check() -> None:
"""
Checks for updates and prompts the user to update if there is an update available.
"""
Kudasai.connection, update_prompt = Toolkit.check_update()
if(update_prompt != ""):
print(update_prompt)
Toolkit.pause_console()
Toolkit.clear_console()
##-------------------start-of-determine_autotranslation_module()---------------------------------------------------------------------------------------------------------------------------------------------------------------------------
@staticmethod
async def determine_autotranslation_module() -> None:
"""
If the user is running the CLI or Console version of Kudasai, this function is called to determine which autotranslation module to use.
"""
if(not Kudasai.connection):
Toolkit.clear_console()
print("You are not connected to the internet. Please connect to the internet to use the autotranslation feature.\n")
Toolkit.pause_console()
exit()
pathing = ""
pathing_msg = "Please select an auto-translation module:\n\n1.Kaiseki (deepL)\n2.Kijiku (OpenAI/Gemini)\n3.Exit\n\n"
pathing = input(pathing_msg)
Toolkit.clear_console()
if(pathing == "1"):
Kudasai.run_kaiseki()
elif(pathing == "2"):
await Kudasai.run_kijiku()
else:
Toolkit.clear_console()
exit()
##-------------------start-of-run_kaiseki()---------------------------------------------------------------------------------------------------------------------------------------------------------------------------
@staticmethod
def run_kaiseki() -> None:
"""
If the user is running the CLI or Console version of Kudasai, this function is called to run the Kaiseki module.
"""
Logger.log_action("--------------------")
Logger.log_action("Kaiseki started")
Logger.log_action("--------------------")
Kaiseki.text_to_translate = [line for line in Kudasai.text_to_preprocess.splitlines()]
Kaiseki.translate()
Toolkit.clear_console()
print(Kaiseki.translation_print_result)
Kaiseki.write_kaiseki_results()
##-------------------start-of-run_kijiku()---------------------------------------------------------------------------------------------------------------------------------------------------------------------------
@staticmethod
async def run_kijiku() -> None:
"""
If the user is running the CLI or Console version of Kudasai, this function is called to run the Kijiku module.
"""
Logger.log_action("--------------------")
Logger.log_action("Kijiku started")
Logger.log_action("--------------------")
Toolkit.clear_console()
Kijiku.text_to_translate = [line for line in Kudasai.text_to_preprocess.splitlines()]
await Kijiku.translate()
Toolkit.clear_console()
print(Kijiku.translation_print_result)
Kijiku.write_kijiku_results()
##-------------------start-of-main()---------------------------------------------------------------------------------------------------------------------------------------------------------------------------
async def main() -> None:
"""
The main function of the Kudasai program.
"""
Kudasai.boot()
Toolkit.clear_console()
try:
if(len(sys.argv) <= 1):
await run_console_version()
elif(len(sys.argv) in [2, 3]):
await run_cli_version()
else:
print_usage_statement()
except Exception as e:
FileEnsurer.handle_critical_exception(e)
##-------------------start-of-run_console_version()---------------------------------------------------------------------------------------------------------------------------------------------------------------------------
async def run_console_version():
"""
Runs the console version of Kudasai.
"""
try:
path_to_text_to_preprocess = input("Please enter the path to the input file to be processed:\n").strip('"')
Kudasai.text_to_preprocess = FileEnsurer.standard_read_file(path_to_text_to_preprocess)
Toolkit.clear_console()
path_to_replacement_json = input("Please enter the path to the replacement json file:\n").strip('"')
Kudasai.replacement_json = FileEnsurer.standard_read_json(path_to_replacement_json if path_to_replacement_json else FileEnsurer.blank_rules_path)
Toolkit.clear_console()
except Exception as e:
print_usage_statement()
raise e
await Kudasai.run_kudasai()
Logger.push_batch()
##-------------------start-of-run_cli_version()---------------------------------------------------------------------------------------------------------------------------------------------------------------------------
async def run_cli_version():
"""
Runs the CLI version of Kudasai.
"""
try:
Kudasai.text_to_preprocess = FileEnsurer.standard_read_file(sys.argv[1].strip('"'))
Kudasai.replacement_json = FileEnsurer.standard_read_json(sys.argv[2].strip('"') if(len(sys.argv) == 3) else FileEnsurer.blank_rules_path)
except Exception as e:
print_usage_statement()
raise e
if(len(sys.argv) == 2):
Kudasai.need_to_run_kairyou = False
await Kudasai.run_kudasai()
Logger.push_batch()
##-------------------start-of-print_usage_statement()---------------------------------------------------------------------------------------------------------------------------------------------------------------------------
def print_usage_statement():
"""
Prints the usage statement for the CLI version of Kudasai.
"""
Logger.log_action("Usage: python Kudasai.py <input_file> <replacement_json>", output=True, omit_timestamp=True)
Logger.log_action("or run Kudasai.py without any arguments to run the console version.", output=True, omit_timestamp=True)
print("\n")
##-------------------start-of-submain()---------------------------------------------------------------------------------------------------------------------------------------------------------------------------
if(__name__ == '__main__'):
asyncio.run(main()) |