通過JAVA編程,使用余弦定理,相似度算法的實現
/**
* 計算帶有權重的相似度
* @param dict1:Map<String,Integer>:Map<特征詞,權重值>
* @param dict2:Map<String,Integer>:Map<特征詞,權重值>
* @return
*/
public double similarity(Map<String,Integer> dict1,Map<String,Integer> dict2){
double similarity = 0.0, numerator = 0.0, denominator1 = 0.0, denominator2 = 0.0;
if(dict1.size()==0||dict2.size()==0){
similarity = 0.0;
return similarity;
}
int value1=0;
int value2=0;
int num = 0;
for(String keyword:dict1.keySet()){
value1 = dict1.get(keyword);
if(dict2.containsKey(keyword)){
value2 = dict2.get(keyword);
dict2.remove(keyword);
num++;
}else{
value2 = 0;
}
numerator += value1 * value2;
denominator1 += value1 * value1;
denominator2 += value2 * value2;
}
for(String keyword:dict2.keySet()){
value2 = dict2.get(keyword);
denominator2 += value2 * value2;
}
similarity = numerator / (Math.sqrt(denominator1 * denominator2));
return similarity;
}