SemanticRanking[{text1, …},ref]
sorts textual items texti by semantic similarity to the reference string ref.
SemanticRanking[list,ref,prop]
returns the specified property prop.
SemanticRanking
SemanticRanking[{text1, …},ref]
sorts textual items texti by semantic similarity to the reference string ref.
SemanticRanking[list,ref,prop]
returns the specified property prop.
Details and Options
- SemanticRanking reorders the input textual list according to semantic similarity to a provided reference string.
- SemanticRanking[{text1, text2, text3,… }, ref] sorts texti from the most to least similar.
- Possible values for prop include:
-
"Ordering" similarity rank for each item "Item" items in the original order "Rank" item position in the ordered list "RankedItem" items reordered from most to least similar (default) "Relevance" numerical similarity between each item and the reference {prop1,…} a list of properties All a Tabular with all the properties - The following options can be given:
-
Method Automatic semantic ranking method - Possible values for Method include:
-
Automatic default local model Tiny, Small, Medium, Large predefined model sizes {"string",size} a named model of the specified size f a custom ranking function - A custom ranker f must operate on the reference string and a list of strings to produce a numerical vector of the same length. The higher the number, the more similar the pair is.
- Specific ranking models include:
-
"MiniLM" default (Medium) "MiniLM" ranker {"MiniLM",Tiny} MiniLM-V2 L-2 {"MiniLM",Small} MiniLM-V2 L-4 {"MiniLM",Medium} MiniLM-V2 L-6 {"MiniLM",Large} MiniLM-V2 L-12
Examples
open all close allBasic Examples (2)
Rerank single-word items according to similarity:
SemanticRanking[{"whale", "weasel", "shark", "goat", "cloud", "car", "turtle", "tuna"}, "fish"]Retrieve the relevance of each item:
SemanticRanking[{"whale", "weasel", "shark", "goat", "cloud", "car", "turtle", "tuna"}, "fish", "Relevance"]Rerank sentences according to similarity to a query:
faqAnswers = {"You can reset your password by clicking 'Forgot Password' on the login page.", "We ship orders within 2–3 business days.", "To cancel a subscription, go to your account settings.", "Our customer service is available 24/7 via chat and email.", "You can update your billing information under Payment Settings."};SemanticRanking[faqAnswers, "How do I change my credit card info?"]Scope (3)
Retrieve items in order of similarity from a reference text:
SemanticRanking[{"whale", "weasel", "shark", "goat", "cloud", "car", "turtle", "tuna"}, "fish"]Return a selected property of each item:
SemanticRanking[{"whale", "weasel", "shark", "goat", "cloud", "car", "turtle", "tuna"}, "fish", "Relevance"]SemanticRanking[{"whale", "weasel", "shark", "goat", "cloud", "car", "turtle", "tuna"}, "fish", {"Item", "Relevance"}]SemanticRanking[{"whale", "weasel", "shark", "goat", "cloud", "car", "turtle", "tuna"}, "fish", All]Options (1)
Method (1)
Specify a reranking function for species designations using a custom text extractor:
extractor = NetModel["SciBERT Trained on Semantic Scholar Data"];Use the extractor in the function:
reranker = Function[{reference, candidates}, EuclideanDistance[Mean[#], Mean[extractor[reference]]]& /@ extractor[candidates]];SemanticRanking[{"Mustela nivalis", "Carcharodon carcharias", "Capra aegagrus hircus", "Chelonia mydas", "Thunnus albacares", "Manis temminckii"}, "Fish", Method -> reranker]Interpreter["Entity"] /@ %Properties & Relations (1)
SemanticSearch automatically reranks the results using SemanticRanking:
index = CreateSemanticSearchIndex[{
"Jellyfish drift with ocean currents.",
"Starfish regenerate lost body parts.",
"Crayfish dig burrows in mud.",
"Cuttlefish change color for camouflage.",
"Silverfish infest homes, eating paper.",
"Salmon migrate upstream to spawn.",
"Starfish live on ocean floors.",
"Shellfish create shells from calcium."
}]query = "Fish"
semanticSearchReranked = SemanticSearch[index, query, MaxItems -> 5]This is equivalent to retrieving the "MinItems" for RerankingMethod without actually doing it:
extendedResults = SemanticSearch[index, query, MaxItems -> 10, RerankingMethod -> None]Then taking the first five results after using SemanticRanking:
rankingResults = SemanticRanking[extendedResults, query][[1 ;; 5]]semanticSearchReranked === rankingResultsPossible Issues (2)
Create several paragraphs of text:
definitions = Map[StringJoin[#, ": ", WordDefinition[#]]&, RandomWord[200]];Reranking such a large number of items is slow:
AbsoluteTiming[reranked = SemanticRanking[definitions, definitions[[10]]];]
First@rerankedInstead create a SemanticSearchIndex:
index = CreateSemanticSearchIndex[definitions]Use SemanticSearch to get a shortlist of the best matches:
(shortlist = SemanticSearch[index, definitions[[10]]];)//AbsoluteTimingAbsoluteTiming[result = SemanticRanking[shortlist, definitions[[10]]];]The closest result is identical:
First[result] == First[reranked]Words without context may not be ordered as expected:
SemanticRanking[{"weasel", "sole", "turtle", "tuna"}, "fish"]A larger ranking model may improve results:
SemanticRanking[{"weasel", "sole", "turtle", "tuna"}, "fish", Method -> {"MiniLM", Large}]Alternatively, add more context to make the results more reliable:
SemanticRanking[StringRiffle[{#, ":", WordDefinition[#]}]& /@ {"weasel", "sole", "turtle", "tuna"}, "fish"]See Also
Related Guides
History
Text
Wolfram Research (2025), SemanticRanking, Wolfram Language function, https://reference.wolfram.com/language/ref/SemanticRanking.html.
CMS
Wolfram Language. 2025. "SemanticRanking." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/SemanticRanking.html.
APA
Wolfram Language. (2025). SemanticRanking. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/SemanticRanking.html
BibTeX
@misc{reference.wolfram_2026_semanticranking, author="Wolfram Research", title="{SemanticRanking}", year="2025", howpublished="\url{https://reference.wolfram.com/language/ref/SemanticRanking.html}", note=[Accessed: 13-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_semanticranking, organization={Wolfram Research}, title={SemanticRanking}, year={2025}, url={https://reference.wolfram.com/language/ref/SemanticRanking.html}, note=[Accessed: 13-June-2026]}