gensim word2vec把训练好的模型保存成txt
import gensim
import codecs
from gensim.models import word2vec
import re
from gensim.corpora.dictionary import Dictionary

import pickle
import logging

import numpy as np
# 引入日志配置
logging.basicConfig(format='%(asctime)s : %(levelname)s : %(message)s', level=logging.INFO)
sentences = word2vec.Text8Corpus('D:/csvtxt/corpus.txt')
model = word2vec.Word2Vec(sentences, size=100,min_count=1)###不过滤只出现1次的词
model.save('word2vec.model')
print(model.similarity('怎么','如何'))
###将模型保存为txt
file=codecs.open('D:/csvtxt/corpus.txt','r+',encoding='utf-8').read()
file1=re.sub('\r\n',' ',file)
file2=file1.split(' ')
vector=[]
for each in file2:
    line=list(model[each])
    lines=[str(i) for i in line]
    linestr=' '.join(lines)
    L=each+' '+linestr
    vector.append(L)
vect='\n'.join(vector)
ff=codecs.open('D:/csvtxt/xyz-add-wordvec.txt','w+',encoding='utf-8')
ff.write(vect)
阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

gensim word2vec把训练好的模型保存成txt

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭