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)

你可能感兴趣的:(python,自然语言处理)