[中文语音识别后文本加标点] 维基百科数据下载和解析(xml->txt)

 

维基百科的中文语料库质量高、领域广泛而且开放,其每月会将所有条目打包供大家下载使用,可以点击: https://dumps.wikimedia.org/zhwiki/latest/zhwiki-latest-pages-articles.xml.bz2 直接下载最新版(也可以访问:https://dumps.wikimedia.org/zhwiki/ 获取历史版本)。

 

1、维基百科数据下载 (分享了一份百度网盘:链接:https://pan.baidu.com/s/1LgJvdhvJLScDZnwBSyIHwA  密码:wzgc)

         wget https://dumps.wikimedia.org/zhwiki/latest/zhwiki-latest-pages-articles.xml.bz2

 

2、将下载的维基百科xml转换为txt

        这里主要有两种方法:

         一种是使用gensim.corpora提供的接口(from gensim.corpora import WikiCorpus),这个有一个问题:会把标点过滤掉,不适合做文本加标点的任务,可用于训练word2vector。

        另一种方法:使用wikiextractor 。

下面详细介绍两种方法的使用。

(1)from gensim.corpora import WikiCorpus  (处理完的数据没有标点符号,并且比较干净)

import logging
import os.path
import sys
from optparse import OptionParser
from gensim.corpora import WikiCorpus


def parse_corpus(infile, outfile):
    '''parse the corpus of the infile into the outfile'''
    space = ' '
    i = 0
    with open(outfile, 'w', encoding='utf-8') as fout:
        wiki = WikiCorpus(infile, lemmatize=False, dictionary={})  # gensim中的维基百科处理类WikiCorpus
        for text in wiki.get_texts():
            fout.write(space.join(text) + '\n')
            i += 1
            if i % 10000 == 0:
                logger.info('Saved ' + str(i) + ' articles')


if __name__ == '__main__':
    program = os.path.basename(sys.argv[0])
    logging.basicConfig(level = logging.INFO, format = '%(asctime)s - %(levelname)s - %(message)s')
    logger = logging.getLogger(program)  # logging.getLogger(logger_name)
    logger.info('running ' + program + ': parse the chinese corpus')

    # parse the parameters
    parser = OptionParser()
    parser.add_option('-i','--input',dest='infile',default='zhwiki-latest-pages-articles.xml.bz2',help='input: Wiki corpus')
    parser.add_option('-o','--output',dest='outfile',default='corpus.zhwiki.txt',help='output: Wiki corpus')

    (options,args) = parser.parse_args()

    infile = options.infile
    outfile = options.outfile

    try:
        parse_corpus(infile, outfile)
        logger.info('Finished Saved ' + str(i) + 'articles')
    except Exception as err:
        logger.info(err)


# python parse_zhwiki_corpus.py -i zhwiki-latest-pages-articles.xml.bz2 -o corpus.zhwiki.txt

(2)wikiextractor       (处理完的数据含有比较符号,比较脏)         

git clone https://github.com/attardi/wikiextractor 
python wikiextractor/WikiExtractor.py zhwiki-latest-pages-articles.xml.bz2 -o wiki.txt

数据如下:

需要需要一个脚本进行合并: ( 输出到一个txt文件(corpus.zhwiki.txt) )

import os, sys

# 解析完的维基百科数据路径 
wiki_path = './wiki.txt/'

# 获取路径下面的所有文件
wiki_list = os.listdir(wiki_path)
# 或者文件下面的所有txt文件
for per_file in wiki_list:
    if per_file == '.DS_Store':
        continue
    # 文件路径
    file_path = os.path.join( wiki_path, per_file )
    txt_list = os.listdir(file_path)
    # 或者每一个txt 
    for per_txt in txt_list:
        if per_txt == '.DS_Store':
            continue
        # 每一个txt文件的路径
        txt_path = os.path.join( wiki_path, per_file, per_txt ) 
        # cat file0.txt >> file.txt   将file0.txt追加到file.txt的末尾  
        cms = 'cat {} >> corpus.zhwiki.txt'.format( txt_path )
        print (cms)
        os.system( cms )

 

总结:

上面的流程走完,在数据方面就完成很大一部分任务了,后面需要做的有:

(1)将繁体中文转为简体中文

(2)去除英文和空格

(3)选取合适的句子,对句子进行分词

(4)生成训练的数据:1、句子截取;2、提取词向量:训练word2vector模型; 3、标点映射标签。

 

  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

MachineLP

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值