File size: 7,307 Bytes
9f85dcf |
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 |
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()
def startsWithAny(s: str, keys):
for x in keys:
if s.startswith(x):
return x
else:
return False
def startsWithAlnum(s: str, _chrs):
retn = ''
for char in s:
if (char.isalnum()) or (char in _chrs):
retn += char
else:
break
return retn
def startsWithCmd(s: str, _chrs=None):
if _chrs is None:
_chrs = {'_', '@'}
cmd = startsWithAlnum(s, _chrs)
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
def getCmdArgs(_cmd):
_args = ['']
_i = -1
_inQuota = ''
while _i < len(_cmd) - 1:
_i += 1
char = _cmd[_i]
if _inQuota:
_args[-1] += char
if char == _inQuota:
_inQuota = ''
elif char == '\\':
_i += 1
char = _cmd[_i]
_args[-1] += char
else:
pass
else:
if char == ' ':
_args.append('')
while _i < len(_cmd) - 1:
_i += 1
char = _cmd[_i]
if char != ' ':
_i -= 1
break
continue
elif char in {'"', "'"}:
_inQuota = char
else:
pass
_args[-1] += char
continue
return _args
def args2map(_args):
retn = {}
for _i in range(1, len(_args)):
_arg = _args[_i]
if '=' not in _arg:
if '=' in _arg:
_arg = _arg.replace('=', '=')
else:
continue
_idx = _arg.index('=')
_k = _arg[:_idx]
if '"' in _k or "'" in _k:
continue
_v = _arg[_idx + 1:]
if _v.startswith('"') or _v.startswith("'"):
_v = _v[1:len(_v) - 1]
retn[_k] = _v
return retn
# =================
a = get_all_files_in_directory(r'E:\tmp\时停5分\ks', ext='.ks')
b = r'D:\datasets\tmp'
# =================
sc = {}
_n = {
"心の声": "旁白",
"瑠璃/???": "瑠璃/?",
"悠真": "悠真",
"瑠璃": "瑠璃",
"胡桃/同班同学": "胡桃/同班同学",
"白亜/???": "白亚/?",
"女行人": "女路人",
"胡桃": "胡桃",
"奏斗": "奏斗",
"桜良/樱良/樱良": "樱良",
"白亜/白亚?": "白亚/?",
"白亜/白亚": "白亚",
"ノア/诺亚/???": "诺亚/?",
"ノア/诺亚/诺亚": "诺亚",
"花音/???": "花音/?",
"花音": "花音",
"梓/???": "梓/?",
"梓": "梓",
"瑠璃/悠真&瑠璃": "悠真&瑠璃",
"胡桃/???": "胡桃/?",
"奏斗/???": "奏斗/?",
"瑠璃/瑠璃&悠真": "瑠璃&悠真",
"悠真妈妈": "悠真妈妈",
"白亜": "白亚",
"瑠璃妈妈": "瑠璃妈妈",
"同班同学A": "同班同学A",
"同班同学B": "同班同学B",
"同班同学A&B": "同班同学A&B",
"讲师": "讲师",
"桜良/樱良/???": "樱良/?",
"瑠璃&樱良": "瑠璃&樱良",
"瑠璃妈妈/???": "瑠璃妈妈/?",
"瑠璃爸爸": "瑠璃爸爸",
"女同学A": "女同学A",
"女同学B": "女同学B",
"巡视员": "巡视员",
"客人": "客人",
"女路人": "女路人",
"ノア/诺亚": "诺亚",
"桜良/樱良": "樱良",
"搭讪男子A": "搭讪男子A",
"搭讪男子B": "搭讪男子B",
"梓/AZ": "梓/AZ",
"男子A": "男子A",
"男子B": "男子B",
"男子C": "男子C",
"迷之男子A": "迷之男子A",
"迷之男子B": "迷之男子B",
"迷之男子C": "迷之男子C",
"ノア/诺亚/悠真&诺亚": "悠真&诺亚",
"謎の男C/主管人员": "迷之男子C/主管人员",
"后辈女生": "后辈女生",
"三年女子/文艺社女生": "3年级女生/文艺社女生",
"編集長/梓的熟人": "编集长/梓的熟人",
"总编辑": "总编辑",
"花音奶奶": "花音奶奶",
"瑠璃&桜良&ノア&白亜&梓&胡桃&奏斗/大伙": "大伙",
"3年级女生": "3年级女生",
"老师": "老师",
"白亜/悠真&白亚": "悠真&白亚",
"白亚&樱良": "白亚&樱良",
"梓/护士": "梓/护士",
"护士": "护士",
"女孩": "女孩",
"男孩": "男孩",
"女学生A": "女学生A",
"女学生B": "女学生B"
}
# =================
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-8') as f:
data = list(filter(lambda x: x,
(x.rstrip() for x in f.readlines())))
# =================
w_i = -1
while w_i < len(data) - 1:
w_i += 1
line: str = data[w_i]
# =================
if not line.startswith('@Talk '):
continue
args = args2map(getCmdArgs(line))
n: str = args['name']
if n in _n:
n = _n[n]
else:
_n[n] = clearT(n)
print(line)
# =================
tmp = []
while w_i < len(data) - 1:
w_i += 1
line: str = data[w_i]
if startsWithCmd(line):
w_i -= 1
break
tmp.append(line)
line = '\\n'.join(tmp)
# =================
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) |