Top Description Fields Constructors Methods
org.apache.commons.text.similarity

public Class JaroWinklerSimilarity

extends Object
implements SimilarityScore<Double>
Class Inheritance
All Implemented Interfaces
org.apache.commons.text.similarity.SimilarityScore, org.apache.commons.text.similarity.ObjectSimilarityScore, java.util.function.BiFunction
Imports
java.util.Arrays, .Objects

A similarity algorithm indicating the percentage of matched characters between two character sequences.

The Jaro measure is the weighted sum of percentage of matched characters from each file and transposed characters. Winkler increased this measure for matching initial characters.

This implementation is based on the Jaro Winkler similarity algorithm from https://en.wikipedia.org/wiki/Jaro%E2%80%93Winkler_distance.

This code has been adapted from Apache Commons Lang 3.3.

Since
1.7

Field Summary

Modifier and TypeField and Description
pack-priv static final JaroWinklerSimilarity
INSTANCE

The singleton instance.

Constructor Summary

AccessConstructor and Description
public
JaroWinklerSimilarity()

Creates a new instance.

Method Summary

Modifier and TypeMethod and Description
public Double

Returns:

result similarity.
apply
(final CharSequence
the first input, must not be null.
left
,
final CharSequence
the second input, must not be null.
right
)

Implements org.apache.commons.text.similarity.SimilarityScore.apply.

Computes the Jaro Winkler Similarity between two character sequences.

public <
The type of similarity score unit.
E
>
Double

Returns:

result similarity.
apply
(final SimilarityInput<E>
the first input, must not be null.
left
,
final SimilarityInput<E>
the second input, must not be null.
right
)

Implements org.apache.commons.text.similarity.ObjectSimilarityScore.apply.

Computes the Jaro Winkler Similarity between two character sequences.

protected static int[]

Returns:

mtp array containing: matches, half transpositions, and prefix.
matches
(final CharSequence
the first input to be matched.
first
,
final CharSequence
the second input to be matched.
second
)

Computes the Jaro-Winkler string matches, half transpositions, prefix array.

protected static <
The type of similarity score unit.
E
>
int[]

Returns:

mtp array containing: matches, half transpositions, and prefix.
matches
(final SimilarityInput<E>
the first input to be matched.
first
,
final SimilarityInput<E>
the second input to be matched.
second
)

Computes the Jaro-Winkler string matches, half transpositions, prefix array.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

INSTANCEback to summary
pack-priv static final JaroWinklerSimilarity INSTANCE

The singleton instance.

Constructor Detail

JaroWinklerSimilarityback to summary
public JaroWinklerSimilarity()

Creates a new instance.

Method Detail

applyback to summary
public Double apply(final CharSequence left, final CharSequence right)

Implements org.apache.commons.text.similarity.SimilarityScore.apply.

Computes the Jaro Winkler Similarity between two character sequences.

sim.apply(null, null)          = Throws IllegalArgumentException
sim.apply("foo", null)         = Throws IllegalArgumentException
sim.apply(null, "foo")         = Throws IllegalArgumentException
sim.apply("", "")              = 1.0
sim.apply("foo", "foo")        = 1.0
sim.apply("foo", "foo ")       = 0.94
sim.apply("foo", "foo  ")      = 0.91
sim.apply("foo", " foo ")      = 0.87
sim.apply("foo", "  foo")      = 0.51
sim.apply("", "a")             = 0.0
sim.apply("aaapppp", "")       = 0.0
sim.apply("frog", "fog")       = 0.93
sim.apply("fly", "ant")        = 0.0
sim.apply("elephant", "hippo") = 0.44
sim.apply("hippo", "elephant") = 0.44
sim.apply("hippo", "zzzzzzzz") = 0.0
sim.apply("hello", "hallo")    = 0.88
sim.apply("ABC Corporation", "ABC Corp") = 0.91
sim.apply("D N H Enterprises Inc", "D & H Enterprises, Inc.") = 0.95
sim.apply("My Gym Children's Fitness Center", "My Gym. Childrens Fitness") = 0.92
sim.apply("PENNSYLVANIA", "PENNCISYLVNIA") = 0.88
Parameters
left:CharSequence

the first input, must not be null.

right:CharSequence

the second input, must not be null.

Returns:Double

result similarity.

Annotations
@Override
Exceptions
IllegalArgumentException:
if either CharSequence input is null.
applyback to summary
public <E> Double apply(final SimilarityInput<E> left, final SimilarityInput<E> right)

Implements org.apache.commons.text.similarity.ObjectSimilarityScore.apply.

Computes the Jaro Winkler Similarity between two character sequences.

sim.apply(null, null)          = Throws IllegalArgumentException
sim.apply("foo", null)         = Throws IllegalArgumentException
sim.apply(null, "foo")         = Throws IllegalArgumentException
sim.apply("", "")              = 1.0
sim.apply("foo", "foo")        = 1.0
sim.apply("foo", "foo ")       = 0.94
sim.apply("foo", "foo  ")      = 0.91
sim.apply("foo", " foo ")      = 0.87
sim.apply("foo", "  foo")      = 0.51
sim.apply("", "a")             = 0.0
sim.apply("aaapppp", "")       = 0.0
sim.apply("frog", "fog")       = 0.93
sim.apply("fly", "ant")        = 0.0
sim.apply("elephant", "hippo") = 0.44
sim.apply("hippo", "elephant") = 0.44
sim.apply("hippo", "zzzzzzzz") = 0.0
sim.apply("hello", "hallo")    = 0.88
sim.apply("ABC Corporation", "ABC Corp") = 0.91
sim.apply("D N H Enterprises Inc", "D & H Enterprises, Inc.") = 0.95
sim.apply("My Gym Children's Fitness Center", "My Gym. Childrens Fitness") = 0.92
sim.apply("PENNSYLVANIA", "PENNCISYLVNIA") = 0.88
Parameters
<E>
The type of similarity score unit.
left:SimilarityInput<E>

the first input, must not be null.

right:SimilarityInput<E>

the second input, must not be null.

Returns:Double

result similarity.

Exceptions
IllegalArgumentException:
if either CharSequence input is null.
Since
1.13.0
matchesback to summary
protected static int[] matches(final CharSequence first, final CharSequence second)

Computes the Jaro-Winkler string matches, half transpositions, prefix array.

Parameters
first:CharSequence

the first input to be matched.

second:CharSequence

the second input to be matched.

Returns:int[]

mtp array containing: matches, half transpositions, and prefix.

matchesback to summary
protected static <E> int[] matches(final SimilarityInput<E> first, final SimilarityInput<E> second)

Computes the Jaro-Winkler string matches, half transpositions, prefix array.

Parameters
<E>
The type of similarity score unit.
first:SimilarityInput<E>

the first input to be matched.

second:SimilarityInput<E>

the second input to be matched.

Returns:int[]

mtp array containing: matches, half transpositions, and prefix.

Since
1.13.0