怎么把Aishell内的transcipt切割成多个.txt文件

该博客介绍了如何用Python脚本处理Aishell的transcript文件,将不同数据集(train、dev)的文本分别保存到单独的txt文件中。作者遇到了编码问题,首先解决了GBK到UTF-8的转换,并编写了脚本来识别并分割文件名和内容,最终成功实现了数据切割。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

众所周知,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/"  # 新创建的txt文件的存放路径
            full_path = desktop_path + filename + '.txt'  # 也可以创建一个.doc的word文档
            file = open(full_path, 'w')
            # file_context.encode('utf-8')
            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": # 这个是dev数据的第一个,也就是只把transcipt文件中train文件夹的部分切割出来
                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  # 是否执行到了dev文件夹内的第一个数据
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\\"  # 新创建的txt文件的存放路径,切记test后面加\\,否则就会和我一样在桌面生成上万个txt文件
            if filename == "BAC009S0764W0121":# 如果到了test的第一个文件,说明应该停止切割了
                print("到了test文件夹内,结束切割!")
                exit()
            if from_dev is True:
                full_path = desktop_path + filename + '.txt'  # 也可以创建一个.doc的word文档
                file = open(full_path, 'w')
                # file_context.encode('utf-8')
                file.write(file_context)  # msg也就是下面的Hello world!
                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\\"  # 新创建的txt文件的存放路径
            if from_dev is True:
                full_path = desktop_path + filename + '.txt'  # 也可以创建一个.doc的word文档
                file = open(full_path, 'w')
                # file_context.encode('utf-8')
                file.write(file_context)  # msg也就是下面的Hello world!
                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()

结果如下:
在这里插入图片描述
上述代码均为速写代码,难免会有很多疏漏,如果您发现有不对或者不好的地方,还请赐教。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Bean冷的心

你的鼓励将是我前进的动力~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值