Skip to content

Commit 2144570

Browse files
committed
Added cosine similarity
1 parent 44dd2b8 commit 2144570

File tree

2 files changed

+121
-1
lines changed

2 files changed

+121
-1
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ Currently implemeted:
77
- Jaro-Winkler similarity;
88
- Longest Common Subsequence edit distance;
99
- Q-Gram (Ukkonen);
10-
- n-Gram distance (Kondrak).
10+
- n-Gram distance (Kondrak);
11+
- Jaccard index;
12+
- Sorensen-Dice coefficient;
13+
- Cosine similarity.
1114

1215
## Download
1316
Using maven:
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
/*
2+
* The MIT License
3+
*
4+
* Copyright 2015 Thibault Debatty.
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*/
24+
25+
package info.debatty.java.stringsimilarity;
26+
27+
/**
28+
* Implements Cosine Similarity.
29+
* The strings are first transformed in vectors of occurences of k-shingles
30+
* (sequences of k characters). In this n-dimensional space, the similarity
31+
* between the two strings is the cosine of their respective vectors.
32+
* @author Thibault Debatty
33+
*/
34+
public class Cosine implements StringSimilarityInterface {
35+
36+
/**
37+
* @param args the command line arguments
38+
*/
39+
public static void main(String[] args) {
40+
Cosine cos = new Cosine(3);
41+
42+
// ABC BCE
43+
// 1 0
44+
// 1 1
45+
// angle = 45°
46+
// => similarity = .71
47+
48+
System.out.println(cos.similarity("ABC", "ABCE"));
49+
50+
cos = new Cosine(2);
51+
// AB BA
52+
// 2 1
53+
// 1 1
54+
// similarity = .95
55+
System.out.println(cos.similarity("ABAB", "BAB"));
56+
}
57+
58+
private int k;
59+
60+
public Cosine(int k) {
61+
this.k = k;
62+
}
63+
64+
public Cosine() {
65+
this.k = 3;
66+
}
67+
68+
/**
69+
* Computes the cosine similarity of s1 and s2.
70+
* The strings are first converted to vectors in the space of k-shingles.
71+
* The cosine similarity is computed as V1 . V2 / (|V1| * |V2|)
72+
* @param s1
73+
* @param s2
74+
* @return Cosine similarity
75+
*/
76+
public double similarity(String s1, String s2) {
77+
KShingling ks = new KShingling(this.k);
78+
ks.parse(s1);
79+
ks.parse(s2);
80+
81+
int[] v1 = ks.profileOf(s1);
82+
int[] v2 = ks.profileOf(s2);
83+
84+
return dotProduct(v1, v2) / (norm(v1) * norm(v2));
85+
86+
}
87+
88+
public double distance(String s1, String s2) {
89+
return 1.0 - similarity(s1, s2);
90+
}
91+
92+
/**
93+
* Compute the norm L2 : sqrt(Sum_i( v_i^2))
94+
* @param v
95+
* @return L2 norm
96+
*/
97+
protected static double norm(int[] v) {
98+
double agg = 0;
99+
100+
for (int i = 0; i < v.length; i++) {
101+
agg += (v[i] * v[i]);
102+
}
103+
104+
return Math.sqrt(agg);
105+
}
106+
107+
protected static double dotProduct(int[] v1, int[] v2) {
108+
double agg = 0;
109+
110+
for (int i = 0; i < v1.length; i++) {
111+
agg += (v1[i] * v2[i]);
112+
}
113+
114+
return agg;
115+
}
116+
117+
}

0 commit comments

Comments
 (0)