字符串余弦相似度的java简单实现


不分词 纯字符串计算  代码如下:

public static double similarScoreCos(String text1, String text2){
	if(text1 == null || text2 == null){
	        //只要有一个文本为null,规定相似度分值为0,表示完全不相等
	        return 0.0;
	}else if("".equals(text1)&&"".equals(text2)) return 1.0;
	Set ASII=new TreeSet<>();
	Map text1Map=new HashMap<>();
	Map text2Map=new HashMap<>();
	for(int i=0;i


使用分词,代码如下:


       /**
	 * text1,text2为已分词字符串 余弦相似度匹配度
	 * @param text1 已分词字符串 eg:这只 皮靴 号码 大了
	 * @param text2 已分词字符串  eg:这只 皮靴 号码 小了
	 * @return
	 */
	public double similarScoreCosWord(String text1, String text2){
		if(text1 == null || text2 == null)
	        return 0.0;
	    else if("".equals(text1)&&"".equals(text2)) return 1.0;
		Set word = new TreeSet<>();
		String[] text1Array = text1.split(" ");
		String[] text2Array = text2.split(" ");
		Map text1Map = new HashMap<>();
		Map text2Map = new HashMap<>();
		for(int i=0;i


仅供学习


你可能感兴趣的:(字符串余弦相似度的java简单实现)