求大佬帮忙看一下python程序没问题但是运行不出结果也不报错
import numpy as np
import gensim
import pandas as pd
import jieba
import xlrd
from xlutils.copy import copy
model = gensim.models.word2vec.Word2Vec.load(‘improve’)
#此函数计算某词对于模型中各个词的转移概率p(wk|wi)
def predict_proba(oword, iword):
#获取输入词的词向量
iword_vec = model[iword]
#获取保存权重的词的词库
oword = model.wv.vocab[oword]
oword_l = model.trainables.syn1[oword.point].T
dot = np.dot(iword_vec, oword_l)
lprob = -sum(np.logaddexp(0, -dot) + oword.code*dot)
return lprob
#各个词对于某词wi转移概率的乘积即为p(content|wi),
#如果p(content|wi)越大就说明在出现wi这个词的条件下,此内容概率越大,
#那么把所有词的p(content|wi)按照大小降序排列,越靠前的词就越重要,越应该看成是本文的关键词。
from collections import Counter
def keywords(s):
#抽出s中和与训练的model重叠的词
s = [w for w in s if w in model]
ws = {w:sum([predict_proba(u, w) for u in s]) for w in s}
return Counter(ws).most_common()
list=[]
data = pd.read_csv(‘附件2.csv’, encoding=‘utf-8’)
data1 = data[‘留言详情’]
counter = 0
for index, line in enumerate(data1):
counter += 1
for i in range(0, counter):
x = pd.Series(keywords(jieba.cut(data1[i])))
list.append(x[0:1])
fen1 = pd.read_csv(‘fujian1.csv’,encoding=‘gbk’)
fen1_1 = fen1[‘一级分类’].drop_duplicates()#去重
counter1 = 0
for index, line in enumerate(fen1_1):
counter1 += 1
for i in range(0,counter):
list_1=[]
for j in range(0,counter1):
sim1 = model.similarity(list[i][0],fen1_1[j])
list_1.append(sim1)
x=max(list_1)
for m in range(0,counter1):
list_2=[]
if x == list_1[m]:
y = m
list_2.append(fen1_1[y])
sh =xlrd.open_workbook(r’附件二 (测试结果).xls’,formatting_info=True)
xl = copy(sh)
shtc = sh.get_sheet(0)
for n in range(0,counter):
shtc.write(n,0,list[n][0])
shtc.write(n,1,list_2[n])
xl.save(r’一级分类.xls’)