我想使用 fasttext 预训练模型来计算一组句子之间的句子相似度。谁能帮我?什么是最好的方法?
我通过训练一个 tfidf 模型来计算句子之间的相似度。写这样的代码。是否可以更改它并使用 fasttext 预训练模型?例如使用向量来训练 tfidf 模型?
def generate_tfidf_model(sentences):
print("generating TfIdf model")
texts = [[sentence for sentence in doc.split()] for doc in sentences]
dictionary = gensim.corpora.Dictionary(texts)
feature_cnt = len(dictionary.token2id)
mycorpus = [dictionary.doc2bow(doc, allow_update=True) for doc in texts]
tfidf_model = gensim.models.TfidfModel(mycorpus)
index = gensim.similarities.SparseMatrixSimilarity(tfidf_model[mycorpus]
, num_features = feature_cnt)
return tfidf_model, index, dictionary
def query_search(query, tfidf_model, index, dictionary):
query = normal_stemmer_sentence(query)
query_vector = dictionary.doc2bow(query.split())
similarity = index[tfidf_model[query_vector]]
return similarity