R/sem_nearest_neighbors.R
sem_nearest_neighbors.Rd
This function identifies the nearest neighbors of a given term or vector in a matrix based on cosine similarity.
sem_nearest_neighbors(x, matrix, n = 10)
A character or numeric vector representing the term or vector.
A numeric matrix or a sparse matrix against which the similarity is calculated.
Number of nearest neighbors to return.
A data frame with the ranks, terms, and their cosine similarity scores.
if (FALSE) {
api_url <- "https://api-inference.huggingface.co/pipeline/feature-extraction/sentence-transformers/all-MiniLM-L6-v2"
api_token <- ''
matrix <- api_huggingface_embeddings(tif,
text_hierarchy = c('doc_id', 'sentence_id'),
api_token = api_token,
api_url = '')
query <- api_huggingface_embeddings(query = "Where's the party at?",
api_token = api_token,
api_url = api_url)
neighbors <- sem_nearest_neighbors(x = query, matrix = matrix)
}