众所周知,Aishell的transcipt并没有给你切割好,只有一个将所有lable整合在一个txt的文件,如图所示: 似乎kaldi有自带的脚本,但是我还没搞懂,所以自己写了一个python脚本来实现,比较难搞的是编码问题,由于windows系统是“GBK”编码,而Linux系统是“UTF-8”编码,我很傻,又写了一个转码的代码,其实直接在linux下跑这段脚本就可以避免转码问题了,具体代码如下:
f = open('/你的目录/data_aishell/transcript/aishell_transcript_v0.8.txt', 'r', encoding='utf-8')
flag = False
for i in f:
filename = ''
file_context = ''
for s in i:
if flag and s != '\n':
file_context = file_context + s
elif flag and s == '\n':
desktop_path = "/你的目录/zdb_data/data/train/"
full_path = desktop_path + filename + '.txt'
file = open(full_path, 'w')
file.write(file_context)
filename = ''
file_context = ''
file.close()
flag = False
if s != ' ' and flag is False:
filename = filename + s
if filename.strip() == "BAC009S0723W0495.wav":
print("执行到了BAC009S0723W0495.wav")
break
elif s == ' ':
flag = True
print("分割完成!")
f.close()
处理结果如下图:
上述是处理train文件夹内的切割数据,接下来是dev部分:
f = open('C:\\Users\\Administrator\\Desktop\\aishell_transcript_v0.8.txt', 'r',
encoding='utf-8')
flag = False
from_dev = False
for i in f:
filename = ''
file_context = ''
for s in i:
if flag and s != '\n':
file_context = file_context + s
elif flag and s == '\n':
desktop_path = "C:\\Users\\Administrator\\Desktop\\test\\"
if filename == "BAC009S0764W0121":
print("到了test文件夹内,结束切割!")
exit()
if from_dev is True:
full_path = desktop_path + filename + '.txt'
file = open(full_path, 'w')
file.write(file_context)
file.close()
filename = ''
file_context = ''
flag = False
if s != ' ':
if flag is False:
filename = filename + s
else:
if filename.strip() != "BAC009S0724W0121":
continue
elif filename.strip() == "BAC009S0724W0121":
print("到了BAC009S0724W0121.wav")
from_dev = True
elif s == ' ':
flag = True
print("分割完成!")
f.close()
结果如下图:
test部分很简单,就是处理剩下的所有数据,只不过需要设置一下从什么地方开始切割。 代码如下:
f = open('C:\\Users\\Administrator\\Desktop\\aishell_transcript_v0.8.txt', 'r',
encoding='utf-8')
flag = False
for i in f:
filename = ''
file_context = ''
for s in i:
if flag and s != '\n':
file_context = file_context + s
elif flag and s == '\n':
desktop_path = "C:\\Users\\Administrator\\Desktop\\test\\"
if from_dev is True:
full_path = desktop_path + filename + '.txt'
file = open(full_path, 'w')
file.write(file_context)
file.close()
filename = ''
file_context = ''
flag = False
if s != ' ':
if flag is False:
filename = filename + s
else:
if filename.strip() != "BAC009S0764W0121":
continue
elif filename.strip() == "BAC009S0764W0121":
print("到了BAC009S0764W0121.wav")
from_dev = True
elif s == ' ':
flag = True
print("分割完成!")
f.close()
结果如下: 上述代码均为速写代码,难免会有很多疏漏,如果您发现有不对或者不好的地方,还请赐教。
|