博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
余弦相似度计算
阅读量:7080 次
发布时间:2019-06-28

本文共 1681 字,大约阅读时间需要 5 分钟。

hot3.png

// 数据结构解析:<单词,二维数组>,其中单词表示公共词, // 二维数组一维度表示句子一的向量,另一维度表示句子二的向量 public class CosSimTextSim { Map<String, float[]> vectorMap = new HashMap<String, float[]>(); float[] tempArray = null;

public CosSimTextSim(String string1, String string2) {	String stri2 = null;	float stri1 = 0.00f;	for (String str2 : string2.split(" ")) {		if (vectorMap.containsKey(str2)) {			vectorMap.get(str2)[1]++;		} else {			tempArray = new float[2];			tempArray[0] = 1;			tempArray[1] = 0;			vectorMap.put(str2, tempArray);		}	}	for (String str2 : string2.split(" ")) {		if (vectorMap.containsKey(str2)) {			vectorMap.get(str2)[1]++;		} else {			tempArray = new float[2];			tempArray[0] = 0;			tempArray[1] = 1;			vectorMap.put(str2, tempArray);		}	}	for (Entry
entry : vectorMap.entrySet()) { }}// 求余弦相似度public double sim() { double result = 0; result = pointMulti(vectorMap) / sqrtMulti(vectorMap); return result;}private double sqrtMulti(Map
vectorMap2) { double result = 0; result = squares(vectorMap2); result = Math.sqrt(result); return result;}// 求平方和private double squares(Map
vectorMap2) { double result1 = 0; double result2 = 0; Set
keySet = vectorMap2.keySet(); for (String str : keySet) { float[] temp = vectorMap2.get(str); result1 += (temp[0] * temp[0]); result2 += (temp[1] * temp[1]); } return result1 * result2;}// 点乘法private double pointMulti(Map
vectorMap2) { double result = 0; Set
keySet = vectorMap2.keySet(); for (String str : keySet) { float[] temp = vectorMap2.get(str); result += (temp[0] * temp[1]); } return result;}

}

转载于:https://my.oschina.net/u/2354614/blog/1068953

你可能感兴趣的文章
使用jQuery封装实用函数
查看>>
求一个括号序列的合法子串个数
查看>>
其他杂记 我喜欢的歌曲
查看>>
java方法笔记
查看>>
新建URL,cookie技术
查看>>
sqlserver中分区函数 partition by的用法
查看>>
杭电oj2028、2034、2035、2041、2043-2046
查看>>
sql 查出一张表中重复的所有记录数据
查看>>
2017 GDOI
查看>>
[置顶] Java基础学习总结(34)——HTTP协议详解
查看>>
Python 字符串分割的方法
查看>>
Android 设置app 启动
查看>>
git常用命令
查看>>
在路上
查看>>
PUTTY与SecureCRT的比较
查看>>
作为Web开发人员,我为什么喜欢Google Chrome浏览器
查看>>
简单的求数组的最大值及最小值
查看>>
动态规划最后的坑
查看>>
回溯:最佳调度问题
查看>>
这道题目是链表倒转的重要总结
查看>>