Nlp 如何聚类关键字或获得关键字相似性时,我有他们的向量
我用Pickle方法(通过Bert as服务和Google的预训练模型)将python字典存储为向量文件,如下所示: (键)短语:(值)短语向量从 女布:1.3237-2.6354 1.7458 但我不知道如何像使用Gensim Word2Vec一样,从Bert as服务模型中获得短语与向量文件的相似性,因为后者配备了.similarity方法 你能给我一个建议,让短语/关键词相似性,或者用我的python Pickle字典向量文件对它们进行聚类吗 或者,是否有更好的方法将关键字作为服务进行聚类 以下代码显示如何获取短语/关键字的向量:Nlp 如何聚类关键字或获得关键字相似性时,我有他们的向量,nlp,word2vec,similarity,cosine-similarity,sentence-similarity,Nlp,Word2vec,Similarity,Cosine Similarity,Sentence Similarity,我用Pickle方法(通过Bert as服务和Google的预训练模型)将python字典存储为向量文件,如下所示: (键)短语:(值)短语向量从 女布:1.3237-2.6354 1.7458 但我不知道如何像使用Gensim Word2Vec一样,从Bert as服务模型中获得短语与向量文件的相似性,因为后者配备了.similarity方法 你能给我一个建议,让短语/关键词相似性,或者用我的python Pickle字典向量文件对它们进行聚类吗 或者,是否有更好的方法将关键字作为服务进行聚类
import Myutility
# the file Myutility includes the function save_model and load_model
import BertCommand
# the file Bertcommand includes the function to start Bert-as-service
client
WORD_PATH = 'E:/Works/testwords.txt'
WORD_FEATURE = 'E:/Works/word.google.vector'
word_vectors = {}
with open(WORD_PATH) as f:
lines = f.readlines()
for line in lines:
line = line.strip('\n')
if line:
word = line
print(line)
word_vectors[word]=None
for word in word_vectors:
try:
v = bc.encode([word])
word_vectors[word] = v
except:
pass
save_model(word_vectors,WORD_FEATURE)
如果我理解的很好,你还有每个短语的向量 然后,您可以简单地计算两个短语向量之间的余弦相似性 有关更多详细信息和实现(手动实现和sklearn实现),我建议使用以下链接: