Search in sources :

Example 1 with FuzzySearch

use of me.xdrop.fuzzywuzzy.FuzzySearch 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)

Example 2 with FuzzySearch

use of me.xdrop.fuzzywuzzy.FuzzySearch in project BotCommands by freya022.

the class AutocompleteAlgorithms method fuzzyMatchingWithContinuity.

/**
 * Sorts the item with fuzzing matching, the {@value OptionData#MAX_CHOICES} most relevant results should appear at the top
 * <br>Additionally this removes items which do not start with the query string
 *
 * @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>> fuzzyMatchingWithContinuity(Collection<T> items, ToStringFunction<T> toStringFunction, CommandAutoCompleteInteractionEvent event) {
    final AutoCompleteQuery autoCompleteQuery = event.getFocusedOption();
    final String query = autoCompleteQuery.getValue();
    final List<T> list = items.stream().filter(s -> Utils.startsWithIgnoreCase(toStringFunction.apply(s), query)).sorted(Comparator.comparing(toStringFunction::apply)).toList();
    return FuzzySearch.extractTop(query, list, toStringFunction, FuzzySearch::ratio, OptionData.MAX_CHOICES);
}
Also used : AutoCompleteQuery(net.dv8tion.jda.api.interactions.AutoCompleteQuery) FuzzySearch(me.xdrop.fuzzywuzzy.FuzzySearch)

Aggregations

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