word2vec.py 文件源码

python
阅读 26 收藏 0 点赞 0 评论 0

项目:bot2017Fin 作者: AllanYiin 项目源码 文件源码
def get_antonyms(self,wordA:str, topk:int=10,ispositive:bool=True):
        seed=[['??','??'],['??','??'],['??','??'],['??','??'],['??','??']]
        proposal={}
        for pair in seed:
            if ispositive:
                result=self.analogy(pair[0],pair[1],wordA,topk)
                print(w2v.find_nearest_word((self[pair[0]] + self[pair[1]]) / 2, 3))
            else:
                result = self.analogy(pair[1], pair[0], wordA, topk)
                print(w2v.find_nearest_word((self[pair[0]] + self[pair[1]]) / 2, 3))

            for item in result:
                term_products = np.argwhere(self[wordA] * self[item[0]] < 0)
                #print(item[0] + ':' +wordA + str(term_products))
                #print(item[0] + ':' +wordA+'('+str(pair)+')  '+ str(len(term_products)))
                if len(term_products)>=self.dims/4:
                    if item[0] not in proposal:
                        proposal[item[0]] = item[1]
                    elif item[1]> proposal[item[0]]:
                        proposal[item[0]] +=item[1]
        for k,v in  proposal.items():
            proposal[k]=v/len(seed)
        sortitems=sorted(proposal.items(), key=lambda d: d[1],reverse=True)
        return  [sortitems[i] for i in range(min(topk,len(sortitems)))]
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号