Top Description Constructors Methods
info.debatty.java.stringsimilarity

public Class Cosine

extends ShingleBased
implements NormalizedStringDistance, NormalizedStringSimilarity
Class Inheritance
All Implemented Interfaces
info.debatty.java.stringsimilarity.interfaces.NormalizedStringSimilarity, info.debatty.java.stringsimilarity.interfaces.StringSimilarity, java.io.Serializable, info.debatty.java.stringsimilarity.interfaces.NormalizedStringDistance, info.debatty.java.stringsimilarity.interfaces.StringDistance
Annotations
@Immutable
Imports
info.debatty.java.stringsimilarity.interfaces.NormalizedStringSimilarity, .NormalizedStringDistance, java.util.Map, net.jcip.annotations.Immutable

The similarity between the two strings is the cosine of the angle between these two vectors representation. It is computed as V1 . V2 / (|V1| * |V2|) The cosine distance is computed as 1 - cosine similarity.
Author
Thibault Debatty

Constructor Summary

AccessConstructor and Description
public
Cosine(final int k)

Implements Cosine Similarity between strings.

public
Cosine()

Implements Cosine Similarity between strings.

Method Summary

Modifier and TypeMethod and Description
public final double

Returns:

1.0 - the cosine similarity in the range [0, 1]
distance
(final String
The first string to compare.
s1
,
final String
The second string to compare.
s2
)

Implements info.debatty.java.stringsimilarity.interfaces.StringDistance.distance.

Return 1.0 - similarity.

private static double
dotProduct(final Map<String, Integer> profile1, final Map<String, Integer> profile2)

private static double

Returns:

L2 norm
norm
(final Map<String, Integer> profile)

Compute the norm L2 : sqrt(Sum_i( v_i²)).

public final double

Returns:

The cosine similarity in the range [0, 1]
similarity
(final String
The first string to compare.
s1
,
final String
The second string to compare.
s2
)

Implements info.debatty.java.stringsimilarity.interfaces.StringSimilarity.similarity.

Compute the cosine similarity between strings.

public final double
similarity(final Map<String, Integer> profile1, final Map<String, Integer> profile2)

Compute similarity between precomputed profiles.

Inherited from info.debatty.java.stringsimilarity.ShingleBased:
getKgetProfile

Constructor Detail

Cosineback to summary
public Cosine(final int k)

Implements Cosine Similarity between strings. The strings are first transformed in vectors of occurrences of k-shingles (sequences of k characters). In this n-dimensional space, the similarity between the two strings is the cosine of their respective vectors.

Cosineback to summary
public Cosine()

Implements Cosine Similarity between strings. The strings are first transformed in vectors of occurrences of k-shingles (sequences of k characters). In this n-dimensional space, the similarity between the two strings is the cosine of their respective vectors. Default k is 3.

Method Detail

distanceback to summary
public final double distance(final String s1, final String s2)

Implements info.debatty.java.stringsimilarity.interfaces.StringDistance.distance.

Return 1.0 - similarity.

Parameters
s1:String

The first string to compare.

s2:String

The second string to compare.

Returns:double

1.0 - the cosine similarity in the range [0, 1]

Annotations
@Override
Exceptions
NullPointerException:
if s1 or s2 is null.
dotProductback to summary
private static double dotProduct(final Map<String, Integer> profile1, final Map<String, Integer> profile2)
normback to summary
private static double norm(final Map<String, Integer> profile)

Compute the norm L2 : sqrt(Sum_i( v_i²)).

Returns:double

L2 norm

similarityback to summary
public final double similarity(final String s1, final String s2)

Implements info.debatty.java.stringsimilarity.interfaces.StringSimilarity.similarity.

Compute the cosine similarity between strings.

Parameters
s1:String

The first string to compare.

s2:String

The second string to compare.

Returns:double

The cosine similarity in the range [0, 1]

Annotations
@Override
Exceptions
NullPointerException:
if s1 or s2 is null.
similarityback to summary
public final double similarity(final Map<String, Integer> profile1, final Map<String, Integer> profile2)

Compute similarity between precomputed profiles.