getIndexesMostSimilarEmbeddings function
Returns a sorted list of indexes of embeddings
that are most similar to
the provided embedding
(in descending order, most similar first).
The similarity is calculated using the provided similarityFunction
.
The default similarity function is cosineSimilarity.
Implementation
List<int> getIndexesMostSimilarEmbeddings(
final List<double> embedding,
final List<List<double>> embeddings, {
final double Function(List<double> a, List<double> b) similarityFunction =
cosineSimilarity,
}) {
final similarities = calculateSimilarity(
embedding,
embeddings,
similarityFunction: similarityFunction,
);
return List<int>.generate(embeddings.length, (i) => i)
..sort((a, b) => similarities[b].compareTo(similarities[a]));
}