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)
阅读更多
上一篇beutifulsoup find_all_next(limit=n)通过某标签定位紧邻的元素
下一篇python 将两个向量合并为字典
想对作者说点什么? 我来说一句

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

关闭
关闭