Search in sources :

Example 1 with BoundExtractedResult

use of me.xdrop.fuzzywuzzy.model.BoundExtractedResult in project BotCommands by freya022.

the class AutocompleteAlgorithms method fuzzyMatching.

/**
 * Sorts the item with fuzzing matching, the {@value OptionData#MAX_CHOICES} most relevant results should appear at the top
 * <br>This algorithm is different from {@link #fuzzyMatchingWithContinuity(Collection, ToStringFunction, CommandAutoCompleteInteractionEvent)}
 *
 * @param items            The items to sort
 * @param toStringFunction The function to transform an item into a String
 * @param event            The autocompletion event
 * @param <T>              Type of the items
 * @return A list of extract results with the scores of each item
 */
public static <T> List<BoundExtractedResult<T>> fuzzyMatching(Collection<T> items, ToStringFunction<T> toStringFunction, CommandAutoCompleteInteractionEvent event) {
    final List<T> list = items.stream().sorted(Comparator.comparing(toStringFunction::apply)).toList();
    final AutoCompleteQuery autoCompleteQuery = event.getFocusedOption();
    // First sort the results by similarities but by taking into account an incomplete input
    final List<BoundExtractedResult<T>> bigLengthDiffResults = FuzzySearch.extractTop(autoCompleteQuery.getValue(), list, toStringFunction, FuzzySearch::partialRatio, OptionData.MAX_CHOICES);
    // Then sort the results by similarities but don't take length into account
    return FuzzySearch.extractTop(autoCompleteQuery.getValue(), bigLengthDiffResults.stream().map(BoundExtractedResult::getReferent).toList(), toStringFunction, FuzzySearch::ratio, OptionData.MAX_CHOICES);
}
Also used : AutoCompleteQuery(net.dv8tion.jda.api.interactions.AutoCompleteQuery) BoundExtractedResult(me.xdrop.fuzzywuzzy.model.BoundExtractedResult) FuzzySearch(me.xdrop.fuzzywuzzy.FuzzySearch)

Aggregations

FuzzySearch (me.xdrop.fuzzywuzzy.FuzzySearch)1 BoundExtractedResult (me.xdrop.fuzzywuzzy.model.BoundExtractedResult)1 AutoCompleteQuery (net.dv8tion.jda.api.interactions.AutoCompleteQuery)1