Datasets:

Languages:
Chinese
Tags:
Not-For-All-Audiences
License:
Limour's picture
Upload 2 files
95c569c verified
raw
history blame
3.95 kB
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.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 listRfind_idx(_i, _data, _condition):
for j in range(_i - 1, -1, -1):
if _condition(_data[j]):
return j
return -1
def listRfind(_i, _data, _condition, _filter, delta=1):
return list(filter(_filter, _data[max(listRfind_idx(_i, _data, _condition) + delta, 0):_i + 1]))
def startsWithCmd(s: str, _chrs=None):
if _chrs is None:
_chrs = {'_', '@'}
cmd = startsWithAlnum(s, _chrs)
if cmd:
return s.startswith(cmd + ' ')
else:
return False
# =================
a = get_all_files_in_directory(r'E:\tmp\幼性反应', ext='.txt')
b = r'D:\datasets\tmp'
# =================
sc = {}
_n = {}
# =================
for path in a:
name = path[path.rindex('\\'):]
if name not in sc:
sc[name] = []
print(name)
# =================
with open(path, 'r', encoding='utf-8') as f:
data = list(filter(lambda x: not x.startswith(';'), (x.rstrip() for x in f.readlines())))
csel = {}
w_i = -1
while w_i < len(data) - 1:
w_i += 1
line = data[w_i]
if line.startswith('select'):
while w_i < len(data) - 1:
w_i += 1
line = data[w_i]
if not line:
break
line = line.split(',*')
assert len(line) == 2
csel[line[1].rstrip(',')] = '选择:' + clearT(line[0].strip('"')[1:])
# print(csel)
continue
if line.startswith('*') and line.isascii():
line = line[1:]
if line in csel:
sc[name].append('旁白' + ':' + csel[line])
continue
if not line.endswith('\\'):
continue
tmp = listRfind(w_i, data,
lambda x: x.startswith('dwave ') or x.endswith('\\') or not x,
lambda x: (x.startswith('dwave ') or not startsWithCmd(x)) and not(x.startswith('bgm')),
delta=0)
if (len(tmp) > 1 and tmp[0].endswith('\\')) or not tmp[0]:
tmp = tmp[1:]
if not tmp:
continue
if tmp[0].startswith('dwave '):
n = '由佳'
tmp = tmp[1:]
assert tmp
else:
n = '旁白'
d = '\\n'.join(tmp)
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)