public String getMatchingPhrase(List<String> phrases, String userInput) { // Pad with spaces to help avoid in string searches String userInputPadded = " " + userInput + " "; double bestSimilarity = 0; String bestPhrase = ""; int allowedDistance = 0; for (String phrase : phrases) { // Pad with spaces to help avoid in string searches String phrasePadded = " " + phrase.toLowerCase() + " "; // Get maximum allowed edit distance based on FUZZY_ERROR_RATE allowedDistance = getAllowedDistance(userInputPadded, phrasePadded); // Run the fuzzy substring matcher FuzzySubstringResults result = FuzzyMatching.Substring(userInputPadded, phrasePadded); if (result.levenshteinDistance <= allowedDistance) { // Input contains a close-enough recognized phrase if (result.similarity > bestSimilarity) { // Keep track of the highest similarity phrase bestSimilarity = result.similarity; bestPhrase = phrase; } } } if (bestSimilarity > 0) { return bestPhrase; } else { return bestPhrase; } }
public List<String> getMatchingPhrases(List<String> phrases, String userInput) { // Pad with spaces to help avoid in string searches String userInputPadded = " " + userInput + " "; List<String> matches = new ArrayList<>(); int allowedDistance = 0; for (String phrase : phrases) { // Pad with spaces to help avoid in string searches String phrasePadded = " " + phrase.toLowerCase() + " "; // Get maximum allowed edit distance based on FUZZY_ERROR_RATE allowedDistance = getAllowedDistance(userInputPadded, phrasePadded); // Run the fuzzy substring matcher FuzzySubstringResults result = FuzzyMatching.Substring(userInputPadded, phrasePadded); if (result.levenshteinDistance <= allowedDistance) { // Input contains a close-enough recognized phrase matches.add(phrase); } } return matches; }