1. 程式人生 > >判斷兩字符串相似度

判斷兩字符串相似度

except ring int set 最大 字符串相似度 public als ble

/** * <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; } }

判斷兩字符串相似度