當前位置: 首頁>>代碼示例>>Java>>正文


Java StringUtils.getLevenshteinDistance方法代碼示例

本文整理匯總了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;
}
 
開發者ID:lxxlxx888,項目名稱:Reer,代碼行數:59,代碼來源:NameMatcher.java

示例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());
}
 
開發者ID:marcocor,項目名稱:smaph,代碼行數:14,代碼來源:SmaphUtils.java


注:本文中的org.apache.commons.lang.StringUtils.getLevenshteinDistance方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。