下载维基百科原始语料
- wikidump 开源语料库(https://dumps.wikimedia.org/zhwiki/)
- 示例语料:zhwiki-20211220-pages-articles-multistream.xml.bz2 2.3 GB
处理.bz2为文本文件
- 命令行 改预处理文件(1_process.py)路径下:执行 python 1_process.py zhwiki-20211220-pages-articles-multistream.xml.bz2 wiki.zh.txt
- 含简繁转换
import logging
import os.path
import sys
from gensim.corpora import WikiCorpus
from opencc import OpenCC
tt = OpenCC('t2s') # 繁转简
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))
if len(sys.argv) < 3:
print(globals()['__doc__'], locals())
sys.exit(1)
inp, outp = sys.argv[1:3]
space = " "
i = 0
output = open("out.txt", "w", encoding='utf-8')
wiki = WikiCorpus(inp, dictionary=[]) # gensim里的维基百科处理类WikiCorpus
for text in wiki.get_texts(): # 通过get_texts将维基里的每篇文章转换位1行text文本,并且去掉了标点符号等内容
output.write(tt.convert(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.")
![](https://i-blog.csdnimg.cn/blog_migrate/fe1d3b2f21180b9b6e26118021c0af4d.png)
![](https://i-blog.csdnimg.cn/blog_migrate/3e2b4681c8eca4a2eaec170798b0bfd5.png)
重要参考
(11条消息) 自然语言处理(一) :Windows环境下 维基百科语料获取及解压_PapperTown的博客-CSDN博客https://blog.csdn.net/weixin_39563232/article/details/81128426?spm=1001.2014.3001.5502维基百科简体中文语料的提取 - 知乎 (zhihu.com)
https://zhuanlan.zhihu.com/p/39960476zhwiki dump progress on 20211220 (wikimedia.org)
https://dumps.wikimedia.org/zhwiki/20211220/