一.下载中文维基百科数据https://dumps.wikimedia.org/zhwiki/
并使用gensim中的wikicorpus解析提取xml中的内容

二.利用opencc繁体转简体

三.利用jieba对转换后的文本进行分词,去停词

四.利用gensim中的word2vec训练分词后的文本

五.测试

python代码如下:

#!/user/bin/python
#coding:utf-8
__author__ = 'yan.shi'
from gensim.corpora import WikiCorpus
import opencc
import jieba
import codecs
from gensim.models import Word2Vec
from gensim.models.word2vec import LineSentence
import multiprocessing '''
读取中文wiki语料库,并解析提取xml中的内容
'''
def dataprocess():
space=b' '
i=0
output=open('E:\zhwiki-articles.txt','wb')
wiki=WikiCorpus('E:\zhwiki-latest-pages-articles.xml.bz2',lemmatize=False,dictionary={})
for text in wiki.get_texts():
output.write(space.join(text)+b'\n')
i=i+1
if(i%10000==0):
print('Saved '+str(i)+' articles')
output.close()
print('Finished Saved '+str(i)+' articles') '''
加载停用词表
'''
def createstoplist(stoppath):
print('load stopwords...')
stoplist=[line.strip() for line in codecs.open(stoppath,'r',encoding='utf-8').readlines()]
stopwords={}.fromkeys(stoplist)
return stopwords '''
过滤英文
'''
def isAlpha(word):
try:
return word.encode('ascii').isalpha()
except UnicodeEncodeError:
return False '''
opencc繁体转简体,jieba中文分词
'''
def trans_seg():
stopwords=createstoplist('E:\stopwords.txt')
cc=opencc.OpenCC('t2s')
i=0
with codecs.open('E:\zhwiki-segment.txt','w','utf-8') as wopen:
print('开始...')
with codecs.open('E:\wiki-utf8.txt','r','utf-8') as ropen:
while True:
line=ropen.readline().strip()
i+=1
print('line '+str(i))
text=''
for char in line.split():
if isAlpha(char):
continue
char=cc.convert(char)
text+=char
words=jieba.cut(text)
seg=''
for word in words:
if word not in stopwords:
if len(word)>1 and isAlpha(word)==False: #去掉长度小于1的词和英文
if word !='\t':
seg+=word+' '
wopen.write(seg+'\n')
print('结束!') '''
利用gensim中的word2vec训练词向量
'''
def word2vec():
print('Start...')
rawdata='G:\python workspace\zhwiki-segment.txt'
modelpath='G:\python workspace\modeldata.model'
#vectorpath='E:\word2vec\vector'
model=Word2Vec(LineSentence(rawdata),size=400,window=5,min_count=5,workers=multiprocessing.cpu_count())#参数说明,gensim函数库的Word2Vec的参数说明
model.save(modelpath)
#model.wv.save_word2vec_format(vectorpath,binary=False)
print("Finished!") def wordsimilarity():
model=Word2Vec.load('E:\word2vec\modeldata.model')
semi=''
try:
semi=model.most_similar('日本'.decode('utf-8'),topn=10)#python3以上就不需要decode
except KeyError:
print('The word not in vocabulary!') #print(model[u'日本'])#打印词向量
for term in semi:
print('%s,%s' %(term[0],term[1])) if __name__=='__main__':
#dataprocess()
#trans_seg()
#word2vec()
wordsimilarity()

日本的相关词:

使用word2vec对中文维基百科数据进行处理的更多相关文章

  1. Windows下基于python3使用word2vec训练中文维基百科语料(二)

    在上一篇对中文维基百科语料处理将其转换成.txt的文本文档的基础上,我们要将为文本转换成向量,首先都要对文本进行预处理 步骤四:由于得到的中文维基百科中有许多繁体字,所以我们现在就是将繁体字转换成简体 ...

  2. Windows下基于python3使用word2vec训练中文维基百科语料(一)

    在进行自然语言处理之前,首先需要一个语料,这里选择维基百科中文语料,由于维基百科是 .xml.bz2文件,所以要将其转换成.txt文件,下面就是相关步骤: 步骤一:下载维基百科中文语料 https:/ ...

  3. Windows下基于python3使用word2vec训练中文维基百科语料(三)

    对前两篇获取到的词向量模型进行使用: 代码如下: import gensim model = gensim.models.Word2Vec.load('wiki.zh.text.model') fla ...

  4. 中文维基百科分类提取(jwpl)--构建知识图谱数据获取

    首先感谢 : 1.https://blog.csdn.net/qq_39023569/article/details/88556301 2.https://www.cnblogs.com/Cheris ...

  5. 使用JWPL (Java Wikipedia Library)操作维基百科数据

    使用JWPL (Java Wikipedia Library)操作维基百科数据 1. JWPL介绍 JWPL(Java Wikipedia Library)是一个开源的访问wikipeida数据的Ja ...

  6. 110G离线维基百科数据免费拿

    110G离线维基百科数据免费拿.. 资料获取方式,关注公总号RaoRao1994,查看往期精彩-所有文章,即可获取资源下载链接 更多资源获取,请关注公总号RaoRao1994

  7. 中英文维基百科语料上的Word2Vec实验

    最近试了一下Word2Vec, GloVe 以及对应的python版本 gensim word2vec 和 python-glove,就有心在一个更大规模的语料上测试一下,自然而然维基百科的语料进入了 ...

  8. JWPL工具处理维基百科wikipedia数据用于NLP

    JWPL处理维基百科数据用于NLP 处理zhwiki JWPL是一个Wikipedia处理工具,主要功能是将Wikipedia dump的文件经过处理.优化导入mysql数据库,用于NLP过程.以下以 ...

  9. 搜索和浏览离线 Wikipedia 维基百科(中/英)数据工具

    为什么使用离线维基百科?一是因为最近英文维基百科被封,无法访问:二是不受网络限制,使用方便,缺点是不能及时更新,可能会有不影响阅读的乱码. 目前,主要有两种工具用来搜索和浏览离线维基百科数据:Kiwi ...

随机推荐

  1. java中start()、yield、setDeamon()

    本节主要说明以下三个问题 start()的启动顺序不代表线程的启动顺序 yeild的作用 守护线程 1.start()与线程启动顺序 package foreverly.cn.chapter1; pu ...

  2. 【DP 好题】hihoCoder #1520 古老数字

    题目链接 这道题的要点是状态转移的顺序. 要从低位向高位进行状态转移. Implementation string s; cin >> s; reverse(all(s)); int x, ...

  3. WINDOWS7 系统中建立文件夹映射

    如何在WIN7中建立文件夹映射,还有以及MKLINK的具体使用方法: 步骤如下: 1.以映射d盘1文件夹为例: 2.按win+r,输入cmd,点击确定: 3.提示符后输入mklink /J " ...

  4. 从入门到自闭之Python高阶函数

    高阶函数:内部帮忙做了一个for循环 filter:筛选过滤 语法: filter(function,iterable) function: 1.指定过滤规则(函数的内存地址) 2.用来筛选的函数,在 ...

  5. win10子系统Ubuntu重置

    重置: 在win10命令行下执行: lxrun /uninstall /full 安装: win+R打开bash 执行命令: lxrun /install /y

  6. 使用zookeeper报错 stat is not executed because it is not in the whitelist. envi is not executed because it is not in the whitelist.

    在使用四字命令或者zk ui界面查看zookeeper集群时,出现如下提示: stat is not executed because it is not in the whitelist. envi ...

  7. 比特(bit)和字节(Byte)

    比特(bit)和字节(Byte) 基础的内容就不说了,这里是一个小的学习笔记 比特和字节的写法差异与应用场景 标准的写法中,正如标题中写的那样,是通过大小写来区分比特和字节的:比特的b应该是小写,而字 ...

  8. O005、远程管理 KVM 虚机

    参考https://www.cnblogs.com/CloudMan6/p/5256018.html   上一节我们通过 virt-manager 在本地主机上创建并管理 KVM 虚机,其实 virt ...

  9. Vue的nextTick是什么?

    公司做之前项目的时候,遇到了一些比较困惑的问题,后来研究明白了nextTick的用法. 我们先看两种情况: 第一种: export default { data () { return { msg: ...

  10. js截取字符串相关的知识点

    截取字符串中的数字 1.使用parseInt() var str ="4500元"; var num = parseInt(str); console.log(num);//450 ...