gensim中的word2vec

安装gensim前要装python,numpy, scipy, 通过pip list检查
开始安装gensim

sudo pip install gensim

参考文档:http://www.jianshu.com/p/6d542ff65b1e
    http://kexue.fm/archives/4316/

文档http://www.jianshu.com/p/6d542ff65b1e上的两个python程序有错误, 我已经改正,内容见python文件

对文件编码格式处理
cat news_tensite_xml.dat | iconv -f gbk -t utf-8 -c | grep "<content>"  > corpus.txt
分词

python word_segment.py corpus.txt corpus_seg.txt

训练模型

python train_word2vec_model.py seg_title_keyword_abstracts.txt agriculture.model agriculture.vector
python train_word2vec_model.py seg_qa.txt qa.model qa.vector

我的训练模型中的参数也和文档不一样,见train_word2vec_model.py的26行
 model = Word2Vec(LineSentence(inp),size=400,window=5, sg=1, hs=1,min_count=5,workers=multiprocessing.cpu_count())

dir_seg.py文件

# -*- encoding:utf-8 -*-
import jieba  # 导入jieba模块
import re
import os

jieba.load_userdict("/home/hadoop/ext.dic")  # 加载自定义词典
jieba.del_word(‘延长‘)
import jieba.posseg as pseg

#对文件夹下的所有文件进行分词,分词结果存入一个文件中
def splitSentence(fold, outputFile):
    # 把停用词做成字典
    # stopwords = {}
    # fstop = open(‘stop_words.txt‘, ‘r‘)
    # for eachWord in fstop:
    #     stopwords[eachWord.strip().decode(‘utf-8‘, ‘ignore‘)] = eachWord.strip().decode(‘utf-8‘, ‘ignore‘)
    # fstop.close()
    if not os.path.isdir(fold):
        print ‘不是一个文件夹,请检查!‘
    else:
        filelist = os.listdir(fold)
        for f in filelist:
            of = fold + ‘/‘ + f
            fin = open(of, ‘r‘)  # 以读的方式打开文件
            fout = open(outputFile, ‘a‘)  # 以写得方式打开文件
            jieba.enable_parallel(4)  # 并行分词
            for eachLine in fin:
                line = eachLine.strip().decode(‘utf-8‘, ‘ignore‘)  # 去除每行首尾可能出现的空格,并转为Unicode进行处理
                line1 = re.sub("[0-9\s+\.\!\/_,$%^*()?;;:-【】+\"\‘]+|[+——!,;:。?、[email protected]#¥%……&*()]+".decode("utf8"), "".decode("utf8"),
                               line)
                wordList = list(jieba.cut(line1))  # 用结巴分词,对每行内容进行分词
                outStr = ‘‘
                for word in wordList:
                    #if word not in stopwords:
                    outStr += word
                    outStr += ‘ ‘
                fout.write(outStr.strip().encode(‘utf-8‘) + ‘\n‘)  # 将分词好的结果写入到输出文件
            fin.close()
            fout.close()
            print f,‘ 分词结束!‘
        print  ‘分词完成!‘

splitSentence(‘/home/hadoop/article‘, ‘/home/hadoop/tt.txt‘)

word_segment.py文件

# -*- coding: utf-8 -*-
# word_segment.py用于语料分词

import logging
import os.path
import sys
import re
import jieba

reload(sys)
sys.setdefaultencoding( "utf-8" )

# 先用正则将<content>和</content>去掉
def reTest(content):
  reContent = re.sub(‘<content>|</content>‘,‘‘,content)
  return reContent

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 globals()[‘__doc__‘] % locals()
    sys.exit(1)
  inp, outp = sys.argv[1:3]
  space = " "
  i = 0

finput = open(inp)
  foutput = open(outp,‘w‘)
  for line in finput:
    line_seg = jieba.cut(reTest(line))
    foutput.write(space.join(line_seg))
    i = i + 1
    if (i % 1000 == 0):
      logger.info("Saved " + str(i) + " articles_seg")

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

# -*- coding: utf-8 -*-
# train_word2vec_model.py用于训练模型

import logging
import os.path
import sys
import multiprocessing

from gensim.models import Word2Vec
from gensim.models.word2vec import LineSentence

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)
  logging.info("running %s" % ‘ ‘.join(sys.argv))

if len(sys.argv) < 4:
    print globals()[‘__doc__‘] % locals()
    sys.exit(1)

inp,outp,outp2 = sys.argv[1:4]

model = Word2Vec(LineSentence(inp),size=400,window=5, sg=1, hs=1,min_count=5,workers=multiprocessing.cpu_count())

model.save(outp)
  model.wv.save_word2vec_format(outp2,binary=False)

模型的运用:
# coding:utf-8
import sys
reload(sys)
sys.setdefaultencoding( "utf-8" )
from gensim.models import Word2Vec
import logging,gensim,os
import pandas as pd

#模型的加载
model = Word2Vec.load(‘/home/hadoop/agriculture.model‘)

class  similarmodel:

def get_simiar_keyword(self, word):
        str = ‘‘
        try:

simi = pd.Series(model.most_similar(word))
        except BaseException:
            str = ‘‘
        else:

for i in simi:
                temp = i[0]+ ‘;‘
                str += temp

return str

si = similarmodel()
print si.get_simiar_keyword(u‘水稻‘)

求top105的关键词

# coding:utf-8

import xlsxwriter

import sys
import csv
import re
import collections as col

from simular import similarmodel

reload(sys)
sys.setdefaultencoding( "utf-8" )

#提取top100的关键词
zh_pattern = re.compile(u‘[\u4e00-\u9fa5]+‘)

def contain_zh(word):
    global zh_pattern
    match = zh_pattern.search(word)

return match

filename = ‘/home/hadoop/caas_casdd_utf8.csv‘
filedistination = ‘/home/hadoop/top100keyword.xlsx‘

def getfromcsv(csvfile, txtfile):
    # csvfile:要读取的csv文件,注意csv的编码为utf-8,提前可用editplus工具转为utf-8
    # txtfile:写入目标文件
    list_keyword = []
    with open(filename, ‘r‘) as readf:
        with open(filedistination, ‘w‘) as fw:
            reader = csv.DictReader(readf)
            # 抽取abstracts列
            for row in reader:
                temp = row[‘keyword‘].decode(‘utf-8‘)
                sp = re.split(u‘[;,;,;,/“”、。()()]‘, temp)
                for w in sp:
                    if contain_zh(w):
                        # print w
                        list_keyword.append(w)
                        # fw.write(w.encode(‘utf-8‘)+‘\n‘)
                        # print temp,‘\n‘

i = 0
            for w in list_keyword:
                i = i + 1
                # print w
                # fw.write(w.encode(‘utf-8‘) + ‘\n‘)

print i

# print list_keyword

c = col.Counter(list_keyword)
    top100 = c.most_common(105)

# for i in top100:
    #     print i[0], ‘ ‘, i[1]

# print c.most_common(10)
    # print  str(list(c.most_common(100))).replace(‘u\‘‘, ‘\‘‘).decode("unicode-escape")

workbook = xlsxwriter.Workbook(filedistination)  # 建立文件

worksheet = workbook.add_worksheet()  # 建立sheet

worksheet.write(‘A1‘, u‘关键词‘)  # 向A1写入
    worksheet.write(‘B1‘, u‘词频‘)  # 向B1写入
    worksheet.write(‘C1‘, u‘相关词‘)  # 向C1写入

row = 1
    colomn = 0

si = similarmodel()

# print si.get_simiar_keyword(u‘杂交水稻‘)
    for i in top100:
        worksheet.write(row, colomn, i[0])
        worksheet.write(row, colomn + 1, i[1])
        worksheet.write(row, colomn + 2, si.get_simiar_keyword((i[0])))
        print si.get_simiar_keyword((i[0]))
        row += 1

workbook.close()

getfromcsv(filename, filedistination)

原文地址:https://www.cnblogs.com/herosoft/p/8134071.html

时间: 2024-07-30 07:55:03

gensim中的word2vec的相关文章

gensim中word2vec

from gensim.models import Word2Vec Word2Vec(self, sentences=None, size=100, alpha=0.025, window=5, min_count=5, max_vocab_size=None, sample=1e-3, seed=1, workers=3, min_alpha=0.0001, sg=0, hs=0, negative=5, cbow_mean=1, hashfxn=hash, iter=5, null_wor

【python gensim使用】word2vec词向量处理英文语料

word2vec介绍 word2vec官网:https://code.google.com/p/word2vec/ word2vec是google的一个开源工具,能够根据输入的词的集合计算出词与词之间的距离. 它将term转换成向量形式,可以把对文本内容的处理简化为向量空间中的向量运算,计算出向量空间上的相似度,来表示文本语义上的相似度. word2vec计算的是余弦值,距离范围为0-1之间,值越大代表两个词关联度越高. 词向量:用Distributed Representation表示词,通常

【python gensim使用】word2vec词向量处理中文语料

word2vec介绍 word2vec官网:https://code.google.com/p/word2vec/ word2vec是google的一个开源工具,能够根据输入的词的集合计算出词与词之间的距离. 它将term转换成向量形式,可以把对文本内容的处理简化为向量空间中的向量运算,计算出向量空间上的相似度,来表示文本语义上的相似度. word2vec计算的是余弦值,距离范围为0-1之间,值越大代表两个词关联度越高. 词向量:用Distributed Representation表示词,通常

NLP:Gensim库之word2vec

Gensim是一款开源的第三方Python工具包,用于从原始的非结构化的文本中,无监督地学习到文本隐层的主题向量表达.它支持包括TF-IDF,LSA,LDA,和word2vec在内的多种主题模型算法,支持流式训练,并提供了诸如相似度计算,信息检索等一些常用任务的API接口. 1.实现类 class gensim.models.Word2Vec(sentences=None, size=100, alpha=0.025, window=5, min_count=5, max_vocab_size=

gensim加载word2vec训练结果(bin文件)并进行相似度实验

# -*- coding: utf-8 -*- import gensim # 导入模型 model = gensim.models.KeyedVectors.load_word2vec_format('vectors.bin', binary=True) # 得到两组词的相似度 list1 = [u'核能'] list2 = [u'电能'] list3 = [u'电力'] list_sim1 = model.n_similarity(list1, list2) print list_sim1

用gensim学习word2vec

在word2vec原理篇中,我们对word2vec的两种模型CBOW和Skip-Gram,以及两种解法Hierarchical Softmax和Negative Sampling做了总结.这里我们就从实践的角度,使用gensim来学习word2vec. 1. gensim安装与概述 gensim是一个很好用的Python NLP的包,不光可以用于使用word2vec,还有很多其他的API可以用.它封装了google的C语言版的word2vec.当然我们可以可以直接使用C语言版的word2vec来

文本分类实战(一)—— word2vec预训练词向量

1 大纲概述 文本分类这个系列将会有十篇左右,包括基于word2vec预训练的文本分类,与及基于最新的预训练模型(ELMo,BERT等)的文本分类.总共有以下系列: word2vec预训练词向量 textCNN 模型 charCNN 模型 Bi-LSTM 模型 Bi-LSTM + Attention 模型 RCNN 模型 Adversarial LSTM 模型 Transformer 模型 ELMo 预训练模型 BERT 预训练模型 所有代码均在textClassifier仓库中, 觉得有帮助,

个性化召回算法实践(五)——item2vec

item2vec将用户的行为序列转化成item组成的句子,模仿word2vec训练word embedding将item embedding.基本思想是把原来高维稀疏的表示方式(one_hot)映射到低维稠密的向量空间中,这样我们就可以用这个低维向量来表示该项目(电影),进而通过计算两个低维向量之间的相似度来衡量两个项目之间的相似性. embedding就是用一个低维的向量表示一个物体,可以是一个词,或是一个商品,或是一个电影等等.这个embedding向量的性质是能使距离相近的向量对应的物体有

Python与自然语言处理(二)基于Gensim的Word2Vec

继续学习摸索,看到很多博客都在研究Word2Vec,感觉挺有意思,我也来尝试一下. 实验环境:Python3,Java8 Word2Vec的输入是句子序列,而每个句子又是一个单词列表,由于没有这样结构的现成输入,所以决定自己动手对原始语料进行预处理. NLPIR是一个汉语分词系统,挺感谢张华平博士,便利了我们的文本处理工作.下载地址:http://ictclas.nlpir.org/newsdownloads?DocId=389 这里还有一个自然语言处理与信息检索共享平台(感觉挺好的,有资料,还