File size: 5,617 Bytes
7c34e0e |
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 |
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)
|