|
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.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 endsWithAny(s: str, keys):
|
|
for x in keys:
|
|
if s.endswith(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\生命的备件\ks', ext='.ks')
|
|
b = r'D:\datasets\tmp'
|
|
|
|
|
|
|
|
sc = {}
|
|
|
|
_n = {
|
|
"虎太郎": "虎太郎",
|
|
"竜次": "龙次",
|
|
"恵璃": "恵璃",
|
|
"大宮": "大宫",
|
|
"小金井": "小金井",
|
|
"璃亜": "璃亚",
|
|
"恭也": "恭也",
|
|
"母": "母",
|
|
"通行人": "路人",
|
|
"OL": "OL",
|
|
"友里恵": "友里恵",
|
|
"晴信": "晴信",
|
|
"恵璃&璃亜": "恵璃&璃亚",
|
|
"露天商": "露天商",
|
|
"千秋": "千秋",
|
|
"千秋の恋人": "千秋的恋人",
|
|
"医師": "医师",
|
|
"竜次\"": "龙次"
|
|
}
|
|
|
|
|
|
for path in a:
|
|
name = path[path.rindex('\\'):]
|
|
name = '0'
|
|
if name not in sc:
|
|
sc[name] = []
|
|
print(name)
|
|
|
|
|
|
with open(path, 'r', encoding='utf-16-le') as f:
|
|
data = list(filter(lambda x: x and (not x.startswith(';')),
|
|
(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.endswith('[np]'):
|
|
continue
|
|
|
|
n: str = data[w_i-1]
|
|
if n.startswith('*p'):
|
|
n = '旁白'
|
|
else:
|
|
if n.startswith('@nm'):
|
|
tmp = args2map(getCmdArgs(n))
|
|
if 'rt' in tmp:
|
|
n = tmp['rt']
|
|
else:
|
|
n = tmp['t']
|
|
|
|
if n in _n:
|
|
n = _n[n]
|
|
else:
|
|
_n[n] = clearT(n)
|
|
print(line)
|
|
|
|
if '「' in line:
|
|
line = line[line.index('「'):]
|
|
|
|
else:
|
|
print(line)
|
|
|
|
line = removeWait(line)
|
|
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) |