余弦相似度比对方法

余弦相似度

公式

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-qM8rsQlL-1628574413802)(F:/ZNV/%E7%AC%94%E8%AE%B0%E5%9B%BE%E7%89%87/%E4%BD%99%E5%BC%A6%E7%9B%B8%E4%BC%BC%E5%BA%A6/image-20210810103549211.png)]

公式推导

img

简单代码理解

public class test {

    Map<Character, int[]> vectorMap = new HashMap<Character, int[]>();
    int[] tempArray = null;

    public test(String source, String target) {

        for (Character sch : source.toCharArray()) {

            if (vectorMap.containsKey(sch)) {

                vectorMap.get(sch)[0]++;
            }
            //用将字符转化为向量
            else {

                tempArray = new int[2];
                tempArray[0] = 1;
                tempArray[1] = 0;
                vectorMap.put(sch, tempArray);
            }

        }


        for (Character tch : target.toCharArray()) {

            if (vectorMap.containsKey(tch)) {

                vectorMap.get(tch)[1]++;
            }
            //用将字符转化为向量
            else {

                tempArray = new int[2];
                tempArray[0] = 0;
                tempArray[1] = 1;
                vectorMap.put(tch, tempArray);
            }

        }


    }

    // 求余弦相似度
    public double sim() {
        double result = 0;
        result = pointMulti(vectorMap) / sqrtMulti(vectorMap);
        return result;
    }


    // 求平方和
    private double squares(Map<Character, int[]> paramMap) {
        double result1 = 0;
        double result2 = 0;
        Set<Character> keySet = paramMap.keySet();
        for (Character character : keySet) {
            int temp[] = paramMap.get(character);
            result1 += (temp[0] * temp[0]);
            result2 += (temp[1] * temp[1]);
        }
        return result1 * result2;
    }

    // 点乘法
    private double pointMulti(Map<Character, int[]> paramMap) {
        double result = 0;
        Set<Character> keySet = paramMap.keySet();
        for (Character character : keySet) {
            int temp[] = paramMap.get(character);
            result += (temp[0] * temp[1]);
        }
        return result;
    }

	// 开方
    private double sqrtMulti(Map<Character, int[]> paramMap) {
        double result = 0;
        result = squares(paramMap);
        result = Math.sqrt(result);
        return result;
    }


    public static void main(String[] args) {
        String s1 = "我是中国人";
        String s3 = "中国人";
        String s4 = "我是中国人";
        String s2 = "66666";
        test t2 = new test(s1, s2);
        test t4 = new test(s1, s4);
        test t3 = new test(s1, s3);
        System.out.println("==相似度===" + t2.sim());
        System.out.println("==相似度===" + t4.sim());
        System.out.println("==相似度===" + t3.sim());


    }
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

友培

数据皆开源!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值