def get_all_files_in_directory(directory, ext=''): import os import re custom_sort_key_re = re.compile('([0-9]+)') def custom_sort_key(s): # 将字符串中的数字部分转换为整数,然后进行排序 return [int(x) if x.isdigit() else x for x in custom_sort_key_re.split(s)] all_files = [] for root, dirs, files in os.walk(directory): for file in files: if file.endswith(ext): file_path = os.path.join(root, file) all_files.append(file_path) return sorted(all_files, key=custom_sort_key) def clearT(): import unicodedata from opencc import OpenCC def full2half(input_str): return ''.join([unicodedata.normalize('NFKC', char) for char in input_str]) cc = OpenCC('t2s') # 't2s'表示繁体转简体 def _clearT(s): s = cc.convert(full2half(s)) return s.lstrip('n').strip().strip(r'\n').replace('\n', '\\n') return _clearT clearT = clearT() def startsWithAny(s: str, keys): for x in keys: if s.startswith(x): return x else: return False def startsWithAlnum(s: str, _l = False): retn = '' for char in s: if char.isalnum() or (_l and char == '_'): retn += char else: break return retn def startsWithCmd(s: str): cmd = startsWithAlnum(s, _l=True) if cmd: return s.startswith(cmd + ' ') else: return False import re reg_removeWait = re.compile(r'\[.+?]') def removeWait(line): tmp = reg_removeWait.sub('', line) if tmp: return tmp else: return line # ================= a = get_all_files_in_directory(r'E:\tmp\哥特妄想\ks', ext='.ks') b = r'D:\datasets\tmp' # ================= sc = {} _n = { "貴路": "贵路", "ロー": "ロー", "警官": "警官", "絶火": "绝火", "塞門": "塞门", "スミス": "史密斯", "麗華": "丽华", "いつき": "丽华", "日木実": "日木实", "絃人": "弦人", "芦満": "芦満", "やすな": "安奈", "八宵": "八宵", "生徒全員": "全体学生", "吹摘": "吹摘", "くずは": "葛叶", # 阿倍野静奈 "根本": "根本", "剋弥": "克弥", "レイミア/???": "?/蕾米娅", "貴路子供": "贵路小时候", "teacher": "老师", "女性": "女性", "レイミア": "蕾米娅", "伯爵": "伯爵", "ティーチャー": "老师", "ヴァンダイン": "伯爵" } # ================= def tmp_filer(x): return not ((x and x.isascii()) or startsWithAny(x, ('@','[if]')) or (x.startswith('[') and x.endswith(']') and ' ' in x)) # ================= for path in a: name = path[path.rindex('\\'):] name = name[:2] if name not in sc: sc[name] = [] print(name) # ================= with open(path, 'r', encoding='utf-16-le') as f: print(next(f)) # BOM data = list(filter(lambda x: not x.startswith(';'), (x.strip() for x in f.readlines()))) data = list(removeWait(x) for x in filter(tmp_filer, data)) d_s = False for i, line in enumerate(data): if not line: d_s = False continue n = line.startswith('【') + line.endswith('】') if n == 2: n = line[1:len(line)-1] if n in _n: n = _n[n] else: _n[n] = clearT(n) print(line, n) nn = n d_s = True continue elif n == 1: n = line.strip('【】') assert n in _n nn = n d_s = True continue else: if line.startswith('「'): d_s = True d = line if d_s: nnn = nn else: nnn = '旁白' d = clearT(d) if d: sc[name].append(nnn + ':' + d) if line.endswith('」'): d_s = False # ================= for k, v in sc.items(): with open(b + f'\\{k}.txt', 'w', encoding='utf-8') as f: f.write('\n'.join(v)) # ================= import json tmp = json.dumps(_n, ensure_ascii=False, indent=2)