ScoringResult
- class curses_fzf.ScoringResult(query: str, candidate: str)[source]
ScoringResult encapsules information and toolage to help with your own scoring functions.
The only important field is
score,FuzzyFinderwill take it into account when it decides which items to show and in which order. If it is0the element will not be displayed inFuzzyFinder.filteredlist. Positive values will sort the items from high to lowscore.The second field to notice is
matches, which is a list of tuples containing the starting index of all matches inside thecandidatestring along with the matched substring. If set, this information will be used byFuzzyFinderto highlight the matched substrings in theFuzzyFinder.filteredlist of query results.The intended way to set those fields is
add_match(). It adds the given score toScoringResult.scoreand the given position and match as a tuple toScoringResult.matches. However feel free to directly manipulate these fields as your scoring logic requires. Especially settingscoreto0is a common way to filter an item out of the results.- Parameters:
query (int) – This is the
FuzzyFinder.querystring entered by the user.candidate (int) – This is a single item’s
display()representation from theFuzzyFinder.all_itemslist.
- SEPARATORS: Set[str] = {'\t', ' ', '"', "'", '(', ')', ',', '-', '.', '/', ':', ';', '<', '>', '[', '\\', ']', '_', '`', '{', '|', '}'}
A set of characters that commonly indicate “word boundaries” in generic text and paths. Those are chosen similarly to fzf’s original heuristics.
- add_match(position: int, match: str, score: int) None[source]
Add position and match as a tuple to
ScoringResult.matches. The given score is added toScoringResult.score.This is the intended way to add to the
scoreand keep track of thematches, but feel free to directly manipulate these fields as your scoring logic requires. Especially settingscoreto0is a common way to filter an item out of the results.- Parameters:
position (int) – The starting index of the match in the original
candidatestring.match (str) – The matched substring, starting at the given position.
score (int) – This value is added to
ScoringResult.score.
- candidate: str
The original candidate string parameter as given to the constructor. This is a single item’s
display()representation from theFuzzyFinder.all_itemslist.
- candidate_words_with_index: List[Tuple[str, int]]
The
candidate_lowerstring split on whitespaces. Each element is a tuple with the word and its starting index in the originalcandidatestring.
- check_query_empty() bool[source]
Check if the
querystring is empty. If it is empty, the score is set to100to keep all items in the original order.- Returns:
Trueif thequerystring is empty,Falseotherwise.
- Return type:
bool
- find_best_word_match(word: str) Tuple[str, int, int, int] | None[source]
Find the best match for the given query word among the
candidate_words_with_index. The best match is the one that is closest to a full word (highest percentage of the word matched). If multiple matches have the same percentage, the one with the match closer to the word’s beginning is preferred.If a match is found, the index of the matched word in the
candidateis added toScoringResult._already_matched_wordsto prevent it from being matched again to another query word.- Parameters:
word (str) – The query word (an item from
query_words_with_index) for which to find the best match amongcandidate_words_with_index.- Returns:
- A tuple containing:
the matched word from
candidate_words_with_indexits position inside
candidatestringthe position of the match inside the matched word
the percentage of the
candidateword thequeryword actually matches
Returns
Noneif no match was found at all.- Return type:
Optional[Tuple[str, int, int, int]]
- greedy_match_positions() List[int][source]
Find the positions of each single query characters in the candidate string using a greedy forward scan. This is a common first step in many fuzzy matching algorithms and can be used to quickly filter out candidates that don’t match at all or to get a baseline score for more complex scoring functions.
- Returns:
- A list of positions where the query characters match in
the candidate.
- Return type:
List[int]
- is_boundary(position: int) bool[source]
Check if the given position in the
candidatestring is a boundary.A boundary is defined as:
the beginning of the string
a position after a separator character (space, tab, slash, dash, dot, etc.)
a camelCase transition (lowercase followed by uppercase)
an alpha<->digit transition
Those criteria are chosen similarly to fzf’s original heuristics.
- Parameters:
position (int) – The index in the
candidatestring to check.- Returns:
Trueif the position is a boundary,Falseotherwise.- Return type:
bool
- matches: List[Tuple[int, str]]
A list of tuples representing each substring match of the
queryin thecandidatestring. The first tuple entry is the starting index of the match in the originalcandidatestring. The second entry is the matched substring. Theadd_match()method is meant to add to matches, but feel free to directly manipulate this field as your scoring logic requires.
- merge_positions_to_substrings(positions: List[int]) List[Tuple[int, str]][source]
Convert matched character positions to (start, substring) tuples for
add_match()ormatches. Example: positions [3,4,5, 10,11] -> [(3, candidate[3:6]), (10, candidate[10:12])]
- query: str
The original query string parameter as given to the constructor. This is the
FuzzyFinder.querystring entered by the user.
- query_words_with_index: List[Tuple[str, int]]
The
query_lowerstring split on whitespaces. Each element is a tuple with the word and its starting index in the originalquerystring.
- score: int
The resulting fuzzy score. This is the field that
FuzzyFinderwill take into account when it decides which items to show. A score of0will filter the item out of the resultingFuzzyFinder.filteredlist. Remaining items are sorted from high to low score. Theadd_match()method is meant to add to score, but feel free to directly manipulate this field as your scoring logic requires.