📜  句子相似度python代码示例

📅  最后修改于: 2022-03-11 14:47:09.296000             🧑  作者: Mango

代码示例1
# sentence_transformer: Python module using Sentence BERT 
# check documentation in source link for further details
from sentence_transformers import SentenceTransformer
from sentence_transformers.util import pytorch_cos_sim # cosine silarity

# stsb-roberta-large is one of the possible pre-trained models
model = SentenceTransformer('stsb-roberta-large')

# list of sentences (type: str)
sentences = list(...) 

# calculate the embeddings
embeddings = bert_model.encode(sentences)

# example: cosine similarity among first and second sentences
cos_similarity = pytorch_cos_sim(embeddings[0], embeddings[1])