word2vec 基于维基百科训练小结
首先去维基百科进行下载资料
wget https://dumps.wikimedia.org/zhwiki/20170301/zhwiki-20170301-pages-articles-multistream.xml.bz2
下载完成之后要进行对文本的处理
# 下载解压脚本
git clone https://github.com/attardi/wikiextractor.git wikiextractor
python wikiextractor/WikiExtractor.py -b 2000M -o zhwiki_extracted zhwiki-20170301-pages-articles.xml.bz2
# 解压抽取词汇
bzcat zhwiki-20170301-pages-articles-multistream.xml.bz2 | python WikiExtractor-zsy.py -b2000M -o extracted > vocabulary.txt
# 这个避免出未知错误
export LC_ALL=C
# 安装分词模块
pip install jieba
# 安装繁体中文转换简体中文
sudo apt-get install opencc
# 繁体 -> 简体
opencc -i wiki_00 -o wiki_00_chs -c zht2zhs.ini
# 或者
opencc -i wiki_00 -o wiki_00_chs -c t2s.json (mac版)
进行分词python脚本
#-*-coding:utf-8-*-
#coding:utf8
import jieba
fin = open('wiki_00_chs','r')
fou = open('output','w')
line = fin.readline()
while line:
newline = jieba.cut(line,cut_all=False)
str_out = ' '.join(newline).encode('utf-8').replace(',','').replace('。','').replace('?','').replace('!','') \
.replace('“','').replace('”','').replace(':','').replace('’','').replace('‘','').replace('-','') \
.replace('(','').replace(')','').replace('《','').replace('》','').replace(';','').replace('.','') \
.replace('、','').replace('_','').replace(',','').replace('?','').replace('!','')
print str_out,
print >> fou,str_out,
line = fin.readline()
fin.close()
fou.close()
这一步基本上要花费很久的时间,耐心等待就好,之后就可以使用是处理好的文档了。
克隆Google的项目word2vec
git clone https://github.com/dav/word2vec.git
把output
放在data
里面
修改script/demo-word.sh
text8 为output
或者其他的文件名。
所有的例子都在scripts里面
# 给脚本添加可执行权限
chmod +x demo-word.sh
./demo-word.sh
输入需要找到的词,并显示结果