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);
}
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);
}
Aggregations