使用word2vec对中文维基百科数据进行处理
一.下载中文维基百科数据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对中文维基百科数据进行处理的更多相关文章
- Windows下基于python3使用word2vec训练中文维基百科语料(二)
在上一篇对中文维基百科语料处理将其转换成.txt的文本文档的基础上,我们要将为文本转换成向量,首先都要对文本进行预处理 步骤四:由于得到的中文维基百科中有许多繁体字,所以我们现在就是将繁体字转换成简体 ...
- Windows下基于python3使用word2vec训练中文维基百科语料(一)
在进行自然语言处理之前,首先需要一个语料,这里选择维基百科中文语料,由于维基百科是 .xml.bz2文件,所以要将其转换成.txt文件,下面就是相关步骤: 步骤一:下载维基百科中文语料 https:/ ...
- Windows下基于python3使用word2vec训练中文维基百科语料(三)
对前两篇获取到的词向量模型进行使用: 代码如下: import gensim model = gensim.models.Word2Vec.load('wiki.zh.text.model') fla ...
- 中文维基百科分类提取(jwpl)--构建知识图谱数据获取
首先感谢 : 1.https://blog.csdn.net/qq_39023569/article/details/88556301 2.https://www.cnblogs.com/Cheris ...
- 使用JWPL (Java Wikipedia Library)操作维基百科数据
使用JWPL (Java Wikipedia Library)操作维基百科数据 1. JWPL介绍 JWPL(Java Wikipedia Library)是一个开源的访问wikipeida数据的Ja ...
- 110G离线维基百科数据免费拿
110G离线维基百科数据免费拿.. 资料获取方式,关注公总号RaoRao1994,查看往期精彩-所有文章,即可获取资源下载链接 更多资源获取,请关注公总号RaoRao1994
- 中英文维基百科语料上的Word2Vec实验
最近试了一下Word2Vec, GloVe 以及对应的python版本 gensim word2vec 和 python-glove,就有心在一个更大规模的语料上测试一下,自然而然维基百科的语料进入了 ...
- JWPL工具处理维基百科wikipedia数据用于NLP
JWPL处理维基百科数据用于NLP 处理zhwiki JWPL是一个Wikipedia处理工具,主要功能是将Wikipedia dump的文件经过处理.优化导入mysql数据库,用于NLP过程.以下以 ...
- 搜索和浏览离线 Wikipedia 维基百科(中/英)数据工具
为什么使用离线维基百科?一是因为最近英文维基百科被封,无法访问:二是不受网络限制,使用方便,缺点是不能及时更新,可能会有不影响阅读的乱码. 目前,主要有两种工具用来搜索和浏览离线维基百科数据:Kiwi ...
随机推荐
- Codeforces 1194A. Remove a Progression
传送门 再一次题目看错浪费一小时...退役算了 自己手玩一下发现划掉的都是奇数,最后所有奇数都划掉了,证明也挺显然的 所以直接输出 $2m$ 即可 #include<iostream> # ...
- 使用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 ...
- 在CentOS 7系统下升级 Jenkins版本
使用yum方式安装的war文件路径:/usr/lib/jenkins/jenkins.war 查看war包所在的目录 find / -name jenkins.war 停止Jenkins 服务 sys ...
- 配置Mysql远程连接
一.赋予某个用户权限 1.赋予权限格式:grant 权限 on 数据库对象 to 用户@IP(或者相应正则) 注:可以赋予select,delete,update,insert,index等权限精确到 ...
- div+css布局教程(1)
margin:Margin属性用于设置两个元素之间的距离. 后面如果只有两个参数的话,第一个表示top和bottom,第二个表示left和right因为0 auto,表示上下边界为0,左右则根据宽度自 ...
- thinkphp3.2.3 自定义路由实践
使用了很久的tp3,却没发现还有这么可玩性的功能. 官方文档:要使用路由功能,前提是你的URL支持PATH_INFO(或者兼容URL模式也可以,采用普通URL模式的情况下不支持路由功能),并且在应用( ...
- 【Activiti】为每一个流程绑定相应的业务对象的2种方法
方式1: 在保存每一个流程实例时,设置多个流程变量,通过多个流程变量的组合来过滤筛选符合该组合条件的流程实例,以后在需要查询对应业务对象所对应的流程实例时,只需查询包含该流程变量的值的流程实例即可. ...
- Nginx之常用操作
1) 将XXX.com 重定向到 www.XXX.com server { client_max_body_size 20m; listen ; server_name www.xxx.com xxx ...
- scrapy-redis 实现分布式爬虫
分布式爬虫 一 介绍 原来scrapy的Scheduler维护的是本机的任务队列(存放Request对象及其回调函数等信息)+本机的去重队列(存放访问过的url地址) 所以实现分布式爬取的关键就是,找 ...
- ps aux详解(进程状态说明)
linux上进程有5种状态: 1. 运行(正在运行或在运行队列中等待) 2. 中断(休眠中, 受阻, 在等待某个条件的形成或接受到信号) 3. 不可中断(收到信号不唤醒和不可运行, 进程必须等待直到有 ...