|
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') |
|
|
|
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, _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: |
|
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 |
|
|
|
|
|
|
|
|
|
def listRfind_idx(_i, _data, _condition): |
|
for j in range(_i - 1, -1, -1): |
|
if _condition(_data[j]): |
|
return j |
|
return _i |
|
|
|
|
|
def listRfind(_i, _data, _condition, _filter, delta=1): |
|
return list(filter(_filter, _data[listRfind_idx(_i, _data, _condition) + delta:_i + 1])) |
|
|
|
|
|
|
|
a = get_all_files_in_directory(r'E:\tmp\苹果妹\ks', ext='.ks') |
|
b = r'D:\datasets\tmp' |
|
|
|
|
|
|
|
sc = {} |
|
|
|
_n = { |
|
"蓝里": "蓝里", |
|
"陆": "陆", |
|
"老婆婆": "老婆婆", |
|
"青年": "青年", |
|
"少女/ゆめ": "少女/梦", |
|
"陸": "陆", |
|
"少女の声/ゆめ": "少女的声音/梦", |
|
"梦": "梦", |
|
"ゆめ": "梦", |
|
"涟": "涟", |
|
"陆 ": "陆", |
|
"男1": "男1", |
|
"男2": "男2", |
|
"中年男性": "中年男性", |
|
"手汗很重的中年男性/中年男": "手汗很重的中年男性", |
|
"感受到命运的中年男性/中年男": "感受到命运的中年男性/", |
|
"邀请得很直接的中年男性/中年男": "邀请得很直接的中年男性", |
|
"死缠烂打的中年男性/中年男": "死缠烂打的中年男性", |
|
"呼吸急促的中年男性/中年男": "呼吸急促的中年男性", |
|
"就这样呼吸急促地靠近过来的中年男性/中年男": "就这样呼吸急促地靠近过来的中年男性", |
|
"少女/美羽": "少女/美羽", |
|
"美羽": "美羽", |
|
"アナウンサー": "广播", |
|
"未喜": "未喜", |
|
"笃史": "笃史", |
|
"工人": "工人", |
|
"声音/梦": "声音/梦", |
|
"男人": "男人", |
|
"铃声": "铃声", |
|
"声/美羽": "声/美羽", |
|
"アナウンス": "广播", |
|
"声音/梦的母亲": "声音/梦的母亲", |
|
"梦的母亲": "梦的母亲", |
|
"漣": "涟", |
|
"声/涟": "声/涟", |
|
"少女的声音/奈月": "少女的声音/奈月", |
|
"少女/奈月": "少女/奈月", |
|
"奈月": "奈月", |
|
"詩乃": "诗乃", |
|
"看護師": "护士", |
|
"诗乃": "诗乃", |
|
"诗乃乃": "诗乃乃", |
|
"少女A/部员A": "少女A/部员A", |
|
"少女B/部员B": "少女B/部员B", |
|
"少女C/部员C": "少女C/部员C", |
|
"声/詩乃の母": "声/诗乃的母亲", |
|
"诗乃的母亲": "诗乃的母亲", |
|
"诗乃的父亲": "诗乃的父亲", |
|
"八百屋": "八百屋", |
|
"女性的声音/漣": "女性的声音/涟", |
|
"少女的声音/詩乃": "少女的声音/诗乃", |
|
"少女/詩乃": "少女/诗乃", |
|
"女孩": "女孩", |
|
"母亲": "母亲", |
|
"男/篤史": "男/笃史", |
|
"少女的声音/未喜": "少女的声音/未喜", |
|
"少女/未喜": "少女/未喜", |
|
"少女的声音/藍里": "少女的声音/蓝里", |
|
"少女的声音/ゆめ": "少女的声音/梦", |
|
"女性/香澄": "女性/香澄", |
|
"声/未喜": "声/未喜", |
|
"裁判": "裁判", |
|
"声/藍里": "声/蓝里", |
|
"少女A/部員A": "少女A/部员A", |
|
"少女B/部員B": "少女B/部员B", |
|
"少女C/部員C": "少女C/部员C", |
|
"客A": "客A", |
|
"俳優A": "俳优A", |
|
"俳優B": "俳优B", |
|
"司会者": "司会者", |
|
"トム": "汤姆", |
|
"篤史": "笃史", |
|
"客B": "客B", |
|
"藍里": "蓝里", |
|
"香澄": "香澄", |
|
"店員": "店员", |
|
"主持人": "主持人", |
|
"客1": "客1", |
|
"声/香澄": "声/香澄", |
|
"露": "露", |
|
"少女/由美子": "少女/由美子", |
|
"由美子": "由美子", |
|
"声/詩乃": "声/诗乃", |
|
"部員A": "部员A", |
|
"部員B": "部员B", |
|
"部員C": "部员C", |
|
"部員D": "部员D", |
|
"蔬果商": "蔬果商", |
|
"子供": "小孩子", |
|
"母親": "母亲", |
|
"係員": "系员", |
|
"詩乃の母": "诗乃的母亲", |
|
"詩乃の父": "诗乃的父亲" |
|
} |
|
|
|
|
|
for path in a: |
|
name = path[path.rindex('\\'):] |
|
name = name[:name.rindex('_')] |
|
if name not in sc: |
|
sc[name] = [] |
|
print(name) |
|
|
|
|
|
with open(path, 'r', encoding='utf-16-le') as f: |
|
next(f) |
|
data = list(filter(lambda x: not x.startswith(';'), (x.strip() for x in f.readlines()))) |
|
|
|
w_i = -1 |
|
while w_i < len(data) - 1: |
|
w_i += 1 |
|
line = data[w_i] |
|
if not line.endswith('[np]'): |
|
continue |
|
tmp = listRfind(w_i, data, |
|
lambda x: x.startswith('@nm ') or x.endswith('[np]'), |
|
lambda x: x, delta=0) |
|
if len(tmp) > 1 and tmp[0].endswith('[np]'): |
|
tmp = tmp[1:] |
|
else: |
|
if not tmp: |
|
continue |
|
line = tmp[0] |
|
if line.startswith('@nm '): |
|
args = args2map(getCmdArgs(line)) |
|
n = args['t'] |
|
if not n and 'rt' in args: |
|
n = args['rt'] |
|
else: |
|
if 'rt' in args: |
|
n += '/' + args['rt'] |
|
if n in _n: |
|
n = _n[n] |
|
else: |
|
_n[n] = clearT(n) |
|
print(line) |
|
tmp = tmp[1:] |
|
else: |
|
n = '旁白' |
|
tmp = (removeWait(x) for x in tmp if not x.isascii()) |
|
d = '\\n'.join(removeWait(x) for x in tmp if x and not startsWithCmd(x, {'_', '@', '*', '['})) |
|
d = clearT(d) |
|
if d: |
|
sc[name].append(n + ':' + d) |
|
|
|
|
|
|
|
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) |
|
|