http://blog.csdn.net/cscmaker/article/details/7990600
realization of the algorithm of calculating similarity by cosine method
(1) cosine similarity
The similarity between the two vectors is measured by measuring the cosine of the corners between them. The cosine of the 0-degree angle is 1, and the cosine of any other angle is no greater than 1, and its minimum value is-1. Thus the cosine of the angle between the two vectors determines whether the two vectors are roughly pointing in the same direction. Therefore, it is commonly used for file comparisons.
See Wikipedia introduction (Click to open the link)
(2) The number of occurrences of the term is used as the value of the vector space (IDF---Inverse document frequency) in the implementation of the algorithm.
Import Java.util.HashMap;
Import Java.util.Iterator;
Import Java.util.Map;
public class Similardegreebycos
{
/*
* Calculates the similarity of two strings (English characters), simple cosine calculation, no weights added
*/
public static double Getsimilardegree (string str1, String str2)
{
Create a vector space model, using the map implementation, the primary key is a word term, the value is an array of length 2, and holds the number of occurrences of the corresponding term in the string.
map<string, int[]> vectorspace = new hashmap<string, int[]> ();
int[] Itemcountarray = null;//in order to avoid frequent local variables, the Itemcountarray is declared here
Break a string with a space separator
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);
}
}
Calculate Similarity degree
Double Vector1modulo = 0.00;//The modulus of the vector 1
Double Vector2modulo = 0.00;//The modulus of the vector 2
Double vectorproduct = 0.00; Vector product
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);
Returns the degree of similarity
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 and 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));
}
}
Realization of the algorithm of calculating similarity by cosine method