#!/usr/bin/env python3 import os from unidecode import unidecode import config import parser japanese_ranges = [ (0x4E00, 0x9FFF), # Kanji (0x3040, 0x309F), # Hiragana (0x30A0, 0x30FF), # Katakana (0xFF00, 0xFFEF), # Full-width Roman characters and symbols (0x3000, 0x303F), # CJK symbols and punctuation (including 「」) ] def write_translated(): translation = parser.parse_to_tokens() output_filepath = os.path.join(config.get('output_path'), 'out.txt') outfile = open(output_filepath, 'w', encoding='shift_jisx0213') with open(config.get('original_path'), 'r', encoding='shift_jisx0213') as file: for line in file: found = False for start, end in japanese_ranges: if start <= ord(line[0]) <= end: found = True if found and len(translation) > 0: # outfile.write(unidecode(translation.pop(0)[1]).replace("\\","¥")) amount = line.count("@") + line.count("¥") outfile.write('`') for _ in range(amount): outfile.write( unidecode(translation.pop(0)[1]).replace("\\", "¥")) if amount > 0: outfile.write('\n') else: outfile.write(line) outfile.close()