判断两字符串相似度

/**
 * <h5>功能:判断两字符串相似度(最小为0,最大为1)</h5>
 * 
 * @param strOne 
 * @param strTwo
 * @return 两字符串相似度(最小为0,最大为1)
 */
public static double SimlarityString(String strOne, String strTwo) {
    Set<String> seta = new HashSet<String>();
    Set<String> setb = new HashSet<String>();

    for (int i = 0; i < strOne.length(); i++) {
        seta.add(strOne.substring(i, i + 1));
    }
    for (int i = 0; i < strTwo.length(); i++) {
        setb.add(strTwo.substring(i, i + 1));
    }
    double x = 0;
    double y = 0;
    if (seta.size() != 0 && setb.size() != 0) {
        if (seta.size() >= setb.size()) {
            y = setb.size();
        } else {
            y = seta.size();
        }

        for (Object obja : seta) {
            for (Object objb : setb) {
                if (obja.equals(objb)) {
                    x++;
                }
            }
        }
        double z = 0.0;
        try {
            z = x / y;
        } catch (Exception e) {
        }
        return z;
    } else {
        return 0;
    }
}

猜你喜欢

转载自blog.51cto.com/1197822/2312449