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

1、维基百科数据下载

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 attardi/wikiextractor
python wikiextractor/WikiExtractor.py zhwiki-latest-pages-articles.xml.bz2 -o wiki.txt

数据如下:

0ad84aab2fe429fa81f4b8697621f3c8.png

需要需要一个脚本进行合并: ( 输出到一个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、标点映射标签。

原文链接:CSDN-专业IT技术社区-登录

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐