欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java基于余弦方法实现的计算相似度算法示例

程序员文章站 2023-12-22 21:48:04
本文实例讲述了java基于余弦方法实现的计算相似度算法。分享给大家供大家参考,具体如下: (1)余弦相似性 通过测量两个向量之间的角的余弦值来度量它们之间的相似性。0度...

本文实例讲述了java基于余弦方法实现的计算相似度算法。分享给大家供大家参考,具体如下:

(1)余弦相似性

通过测量两个向量之间的角的余弦值来度量它们之间的相似性。0度角的余弦值是1,而其他任何角度的余弦值都不大于1;并且其最小值是-1。从而两个向量之间的角度的余弦值确定两个向量是否大致指向相同的方向。所以,它通常用于文件比较。

相关介绍可参考百度百科:余弦相似性

(2)算法实现的中未使用权重(idf ---逆文档频率),使用词项的出现次数作为向量空间的值。

import java.util.hashmap;
import java.util.iterator;
import java.util.map;
public class similardegreebycos
{
  /*
   * 计算两个字符串(英文字符)的相似度,简单的余弦计算,未添权重
   */
   public static double getsimilardegree(string str1, string str2)
   {
    //创建向量空间模型,使用map实现,主键为词项,值为长度为2的数组,存放着对应词项在字符串中的出现次数
     map<string, int[]> vectorspace = new hashmap<string, int[]>();
     int[] itemcountarray = null;//为了避免频繁产生局部变量,所以将itemcountarray声明在此
     //以空格为分隔符,分解字符串
     string strarray[] = str1.split(" ");
     for(int i=0; i<strarray.length; ++i)
     {
       if(vectorspace.containskey(strarray[i]))
         ++(vectorspace.get(strarray[i])[0]);
       else
       {
         itemcountarray = new int[2];
         itemcountarray[0] = 1;
         itemcountarray[1] = 0;
         vectorspace.put(strarray[i], itemcountarray);
       }
     }
     strarray = str2.split(" ");
     for(int i=0; i<strarray.length; ++i)
     {
       if(vectorspace.containskey(strarray[i]))
         ++(vectorspace.get(strarray[i])[1]);
       else
       {
         itemcountarray = new int[2];
         itemcountarray[0] = 0;
         itemcountarray[1] = 1;
         vectorspace.put(strarray[i], itemcountarray);
       }
     }
     //计算相似度
     double vector1modulo = 0.00;//向量1的模
     double vector2modulo = 0.00;//向量2的模
     double vectorproduct = 0.00; //向量积
     iterator iter = vectorspace.entryset().iterator();
     while(iter.hasnext())
     {
       map.entry entry = (map.entry)iter.next();
       itemcountarray = (int[])entry.getvalue();
       vector1modulo += itemcountarray[0]*itemcountarray[0];
       vector2modulo += itemcountarray[1]*itemcountarray[1];
       vectorproduct += itemcountarray[0]*itemcountarray[1];
     }
     vector1modulo = math.sqrt(vector1modulo);
     vector2modulo = math.sqrt(vector2modulo);
     //返回相似度
    return (vectorproduct/(vector1modulo*vector2modulo));
   }
   /*
   *
   */
   public static void main(string args[])
   {
     string str1 = "gold silver truck";
     string str2 = "shipment of gold damaged in a fire";
     string str3 = "delivery of silver arrived in a silver truck";
     string str4 = "shipment of gold arrived in a truck";
     string str5 = "gold gold gold gold gold gold";
     system.out.println(similardegreebycos.getsimilardegree(str1, str2));
     system.out.println(similardegreebycos.getsimilardegree(str1, str3));
     system.out.println(similardegreebycos.getsimilardegree(str1, str4));
     system.out.println(similardegreebycos.getsimilardegree(str1, str5));
   }
}

更多关于java算法相关内容感兴趣的读者可查看本站专题:《java数据结构与算法教程》、《java操作dom节点技巧总结》、《java文件与目录操作技巧汇总》和《java缓存操作技巧汇总

希望本文所述对大家java程序设计有所帮助。

上一篇:

下一篇: