本文整理汇总了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());
}