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.strip().strip(r'\n').replace('\n', '\\n') return _clearT clearT = clearT() import re reg_rq = re.compile(r'(.+?)') def removeQ(_s:str): return reg_rq.sub('',_s).strip() # ================= a = get_all_files_in_directory(r'E:\tmp\LOOPERS\Text', ext='.txt') b = r'D:\datasets\tmp' # ================= sc = {} _n = { "魔法师": "魔法师", "少年": "少年", "母亲": "母亲", "护士": "护士", "少女": "少女", "父亲": "父亲", "平良": "平良", "昼田": "昼田", "玲央奈": "玲央奈", "女子": "女子", "男子": "男子", "大妈A": "大妈A", "大妈B": "大妈B", "大妈C": "大妈C", "梦幻般的少女": "梦幻般的少女", "米娅": "米娅", "昼田的父亲": "昼田的父亲", "急救员": "急救员", "护士A": "护士A", "医生": "医生", "护士B": "护士B", "护士C": "护士C", "护士D": "护士D", "西门": "西门", "声音": "声音", "莉塔朋德": "莉塔朋德", "霍莉": "霍莉", "让": "让", "平良/昼田": "平良&昼田", "售货员A": "售货员A", "售货员B": "售货员B", "售货员C": "售货员C", "路人A": "路人A", "路人B": "路人B", "路人C": "路人C", "路人D": "路人D", "路人E": "路人E", "路人F": "路人F", "路人G": "路人G", "路人H": "路人H", "女": "女", "玲奈央": "玲奈央", "平良/让": "平良&让", "米娅的母亲": "米娅的母亲", "主刀医生": "主刀医生", "主导医生": "主导医生", "全员": "全员", "「我更想追随平良所相信的希望……!」": "米娅", "「我得向平良、道歉……我得道歉……」": "米娅", "库洛": "库洛", "凯": "凯", "上班族": "上班族", "主妇": "主妇", "大学生": "大学生", "教师": "教师", "「―——对不起,对不起……」": "「―——对不起,对不起......」", "所有人": "所有人", "平良/米娅": "平良&米娅" } # ================= for path in a: name = path[path.rindex('\\'):] name = 'loopers' if name not in sc: sc[name] = [] print(name) # ================= with open(path, 'r', encoding='utf-8') as f: data = list(filter(lambda x: x, (x.strip() for x in f.readlines()))) w_i = -1 while w_i < len(data) - 1: w_i += 1 line: str = data[w_i] if line in _n: if line.startswith('「'): pass elif len(line) - len(_n[line]) > 4: pass elif data[w_i + 1].startswith('『'): assert removeQ(data[w_i + 1]).endswith('』') data[w_i + 1] = f'「{data[w_i + 1]}」' print(data[w_i + 1]) continue elif not (data[w_i + 1].startswith('「') or data[w_i + 1].startswith('(')): pass else: continue # print(line) if line.startswith('「'): n = data[w_i-1] if n in _n: n = _n[n] else: pass _n[n] = clearT(n).replace('・', '&') print(n, line) tmp = [] while not (removeQ(line).endswith('」')): tmp.append(line) w_i += 1 line: str = data[w_i] if line in _n: if data[w_i + 1].startswith('「') or data[w_i + 1].startswith('('): w_i -= 1 break else: tmp.append(line) line = '\\n'.join(tmp) elif line.startswith('(') and data[w_i-1] in _n: n = data[w_i-1] n = _n[n] tmp = [] while not line.endswith(')'): tmp.append(line) w_i += 1 line: str = data[w_i] if line in _n: if data[w_i + 1].startswith('「') or data[w_i + 1].startswith('('): w_i -= 1 break else: tmp.append(line) line = '\\n'.join(tmp) else: n = '旁白' d = clearT(line) if d: sc[name].append(n + ':' + d) # ================= for k, v in sc.items(): if v: 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=4)