Search in sources :

Example 1 with ExtractedResult

use of me.xdrop.fuzzywuzzy.model.ExtractedResult in project pokeraidbot by magnusmickelsson.

the class GymRepository method search.

public Gym search(User user, String query, String region) {
    if (region == null || StringUtils.isEmpty(query)) {
        throw new UserMessedUpException(user, localeService.getMessageFor(LocaleService.GYM_SEARCH, LocaleService.DEFAULT));
    }
    final Set<Gym> gyms = getAllGymsForRegion(region);
    final Locale localeForUser = localeService.getLocaleForUser(user);
    final Optional<Gym> gym = get(query, region);
    if (gym.isPresent()) {
        return gym.get();
    } else {
        // 70 seems like a reasonable cutoff here...
        List<ExtractedResult> candidates = extractTop(query, gyms.stream().map(s -> s.getName()).collect(Collectors.toList()), 6, 70);
        if (candidates.size() == 1) {
            return findByName(candidates.iterator().next().getString(), region);
        } else if (candidates.size() < 1) {
            throw new GymNotFoundException(query, localeService, LocaleService.SWEDISH, region);
        } else {
            List<Gym> matchingPartial = getMatchingPartial(query, region, candidates);
            if (matchingPartial.size() == 1) {
                return matchingPartial.get(0);
            }
            if (candidates.size() <= 5) {
                String possibleMatches = candidates.stream().map(s -> findByName(s.getString(), region).getName()).collect(Collectors.joining(", "));
                throw new UserMessedUpException(user, localeService.getMessageFor(LocaleService.GYM_SEARCH_OPTIONS, localeForUser, possibleMatches));
            } else {
                throw new UserMessedUpException(user, localeService.getMessageFor(LocaleService.GYM_SEARCH_MANY_RESULTS, localeForUser));
            }
        }
    }
}
Also used : ExtractedResult(me.xdrop.fuzzywuzzy.model.ExtractedResult) GymNotFoundException(pokeraidbot.domain.errors.GymNotFoundException) UserMessedUpException(pokeraidbot.domain.errors.UserMessedUpException)

Example 2 with ExtractedResult

use of me.xdrop.fuzzywuzzy.model.ExtractedResult in project pokeraidbot by magnusmickelsson.

the class GymRepository method getMatchingPartial.

private List<Gym> getMatchingPartial(String query, String region, List<ExtractedResult> candidates) {
    String cleanQuery = query.trim().replaceAll(" +", " ");
    List<Gym> mathingGyms = new ArrayList<>();
    for (ExtractedResult result : candidates) {
        if (containsIgnoreCase(result.getString(), cleanQuery)) {
            mathingGyms.add(findByName(result.getString(), region));
        }
    }
    return mathingGyms;
}
Also used : ExtractedResult(me.xdrop.fuzzywuzzy.model.ExtractedResult)

Aggregations

ExtractedResult (me.xdrop.fuzzywuzzy.model.ExtractedResult)2 GymNotFoundException (pokeraidbot.domain.errors.GymNotFoundException)1 UserMessedUpException (pokeraidbot.domain.errors.UserMessedUpException)1