Created
July 27, 2022 05:36
-
-
Save wfng92/27f99e027cdbf8f14a6d18b99312897d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from sentence_transformers import SentenceTransformer, util | |
import torch | |
model = SentenceTransformer('paraphrase-multilingual-MiniLM-L12-v2', device='cpu', cache_folder='/') | |
# Corpus with example sentences | |
corpus = [ | |
'I am a boy', | |
'What are you doing?', | |
'Can you help me?', | |
'A man is riding a horse.', | |
'A woman is playing violin.', | |
'A monkey is chasing after a goat', | |
'The quick brown fox jumps over the lazy dog' | |
] | |
# Query sentences: | |
queries = ['I am in need of assistance', '我是男孩子', 'Qué estás haciendo'] | |
corpus_embedding = model.encode(corpus, convert_to_tensor=True, normalize_embeddings=True) | |
top_k = min(5, len(corpus)) | |
for query in queries: | |
query_embedding = model.encode(query, convert_to_tensor=True, normalize_embeddings=True) | |
hits = util.semantic_search(query_embedding, corpus_embedding, score_function=util.dot_score) | |
hits = hits[0] | |
print("Query:", query) | |
print("---------------------------") | |
for hit in hits[:top_k]: | |
print(f"{round(hit['score'], 3)} | {corpus[hit['corpus_id']]}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment