import gensim
import jieba
import numpy as np
from scipy.linalg import norm
model_file = 'C:/Users/Administrator/Desktop//word2vec/news_12g_baidubaike_20g_novel_90g_embedding_64.bin'
model = gensim.models.KeyedVectors.load_word2vec_format(model_file, binary=True)
def vector_similarity(s1, s2):
def sentence_vector(s):
words = jieba.lcut(s)
v = np.zeros(64)
for word in words:
v += model[word]
v /= len(words)
return v
v1, v2 = sentence_vector(s1), sentence_vector(s2)
return np.dot(v1, v2) / (norm(v1) * norm(v2))
s1 = '简欧欧式沙发椅卧室咖啡厅小户型双人沙发美式复古皮艺沙发'
s2 = '布艺沙发简约小户型沙发组合可拆洗转角出租房三人客厅整装家具'
vector_similarity(s1, s2)
输出为:
0.8733292588236009
备注:利用的是现成的.bin模型文件,稍后我会把文件传上来。
遇到的问题:我在运行过程中,发现运行速度非常慢,不知道是为什么。