本文整理匯總了Java中org.apache.commons.lang.StringUtils.getLevenshteinDistance方法的典型用法代碼示例。如果您正苦於以下問題:Java StringUtils.getLevenshteinDistance方法的具體用法?Java StringUtils.getLevenshteinDistance怎麽用?Java StringUtils.getLevenshteinDistance使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.apache.commons.lang.StringUtils
的用法示例。
在下文中一共展示了StringUtils.getLevenshteinDistance方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: find
import org.apache.commons.lang.StringUtils; //導入方法依賴的package包/類
/**
* Locates the best match for the given pattern in the given set of candidate items.
*
* @return The match if exactly 1 match found, null if no matches or multiple matches.
*/
public String find(String pattern, Collection<String> items) {
this.pattern = pattern;
matches.clear();
candidates.clear();
if (items.contains(pattern)) {
matches.add(pattern);
return pattern;
}
if (pattern.length() == 0) {
return null;
}
Pattern camelCasePattern = getPatternForName(pattern);
Pattern normalisedCamelCasePattern = Pattern.compile(camelCasePattern.pattern(), Pattern.CASE_INSENSITIVE);
String normalisedPattern = pattern.toUpperCase();
Set<String> caseInsensitiveMatches = new TreeSet<String>();
Set<String> caseSensitiveCamelCaseMatches = new TreeSet<String>();
Set<String> caseInsensitiveCamelCaseMatches = new TreeSet<String>();
for (String candidate : items) {
if (candidate.equalsIgnoreCase(pattern)) {
caseInsensitiveMatches.add(candidate);
}
if (camelCasePattern.matcher(candidate).matches()) {
caseSensitiveCamelCaseMatches.add(candidate);
continue;
}
if (normalisedCamelCasePattern.matcher(candidate).lookingAt()) {
caseInsensitiveCamelCaseMatches.add(candidate);
continue;
}
if (StringUtils.getLevenshteinDistance(normalisedPattern, candidate.toUpperCase()) <= Math.min(3, pattern.length() / 2)) {
candidates.add(candidate);
}
}
if (!caseInsensitiveMatches.isEmpty()) {
matches.addAll(caseInsensitiveMatches);
} else if (!caseSensitiveCamelCaseMatches.isEmpty()) {
matches.addAll(caseSensitiveCamelCaseMatches);
} else {
matches.addAll(caseInsensitiveCamelCaseMatches);
}
if (matches.size() == 1) {
return matches.first();
}
return null;
}
示例2: getNormEditDistance
import org.apache.commons.lang.StringUtils; //導入方法依賴的package包/類
/**
* @param tokenB
* a word.
* @param tokenQ
* another word.
* @return the normalized edit distance between tokenB and tokenQ.
*/
public static float getNormEditDistance(String tokenB, String tokenQ) {
if (tokenQ.isEmpty() || tokenB.isEmpty())
return 1;
int lev = StringUtils.getLevenshteinDistance(tokenB, tokenQ);
return (float) lev / (float) Math.max(tokenB.length(), tokenQ.length());
}