def find_nearest_word(self,represent, topk:int=10,stopwords:list=[]):
"""
????(???????????)???????
:param stopwords: ?????????????
:param represent:
:param topk:
:return:
"""
array1=np.empty(200)
if isinstance(represent,str) and represent in self:
array1=self[represent]
stopwords.append(represent)
elif isinstance(represent,np.ndarray) :
array1=represent
else:
raise NotImplementedError
result_cos=cosine_similarity(np.reshape(array1,(1,array1.shape[-1])),self._matrix)
result_cos=np.reshape(result_cos,result_cos.shape[-1])
result_sort=result_cos.argsort()[-1*topk:][::-1]
# [[self.idx2word[idx],result_cos[idx]] for idx in result_sort]
# found={}
# for item in result_sort:
# found[self.idx2word[item]]=result[item]
# sortlist=sorted(found.items(), key=lambda d: d[1],reverse=True)
#print(found)
return [[self.idx2word[idx],result_cos[idx]] for idx in result_sort if self.idx2word[idx] not in stopwords and sum([ 1 if stop.startswith(self.idx2word[idx]) else 0 for stop in stopwords])==0 ] #[item for item in sortlist if sum([len(item[0].replace(stop,''))>=2 for stop in stopwords]) ==0]
评论列表
文章目录