目录

下载数据

从以下地址下载中文维基百科数据:

处理bz2文件

使用以下命令处理bz2文件, 得到了大约23万多篇中文语料的text格式的语料:wiki.zh.text,大概750多M。

python process_wiki.py zhwiki-latest-pages-articles.xml.bz2 wiki.zh.text

process_wiki.py的代码如下,代码来自https://raw.githubusercontent.com/panyang/Wikipedia_Word2vec/master/v1/process_wiki.py:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Author: Pan Yang (panyangnlp@gmail.com)
# Copyrigh 2017

from __future__ import print_function

import logging
import os.path
import six
import sys

from gensim.corpora import WikiCorpus

if __name__ == '__main__':
    program = os.path.basename(sys.argv[0])
    logger = logging.getLogger(program)

    logging.basicConfig(format='%(asctime)s: %(levelname)s: %(message)s')
    logging.root.setLevel(level=logging.INFO)
    logger.info("running %s" % ' '.join(sys.argv))

    # check and process input arguments
    if len(sys.argv) != 3:
        print("Using: python process_wiki.py enwiki.xxx.xml.bz2 wiki.en.text")
        sys.exit(1)
    inp, outp = sys.argv[1:3]
    space = " "
    i = 0

    output = open(outp, 'w')
    wiki = WikiCorpus(inp, lemmatize=False, dictionary={})
    for text in wiki.get_texts():
        if six.PY3:
            output.write(bytes(' '.join(text), 'utf-8').decode('utf-8') + '\n')
        #   ###another method###
        #    output.write(
        #            space.join(map(lambda x:x.decode("utf-8"), text)) + '\n')
        else:
            output.write(space.join(text) + "\n")
        i = i + 1
        if (i % 10000 == 0):
            logger.info("Saved " + str(i) + " articles")

    output.close()
    logger.info("Finished Saved " + str(i) + " articles")

繁转简

安装opencc,然后将wiki.zh.text中的繁体字转化位简体字,命令如下:

opencc -i wiki.zh.text -o wiki.zh.text.jian -c zht2zhs.ini

代码部分

参考