java 实现相似度算法

  • Post author:
  • Post category:java


通过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;

}