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

public Class SimilarityScoreFrom<R>

extends Object
Class Inheritance
Type Parameters
<R>
This is the type of similarity score used by the SimilarityScore function.
Imports
org.apache.commons.lang3.Validate

Stores a SimilarityScore implementation and a CharSequence "left" string. The apply(CharSequence right) method accepts the "right" string and invokes the comparison function for the pair of strings.

The following is an example which finds the most similar string:

SimilarityScore<Integer> similarityScore = new LevenshteinDistance();
String target = "Apache";
SimilarityScoreFrom<Integer> similarityScoreFrom =
    new SimilarityScoreFrom<Integer>(similarityScore, target);
String mostSimilar = null;
Integer shortestDistance = null;

for (String test : new String[] { "Appaloosa", "a patchy", "apple" }) {
    Integer distance = similarityScoreFrom.apply(test);
    if (shortestDistance == null || distance < shortestDistance) {
        shortestDistance = distance;
        mostSimilar = test;
    }
}

System.out.println("The string most similar to \"" + target + "\" "
    + "is \"" + mostSimilar + "\" because "
    + "its distance is only " + shortestDistance + ".");
Since
1.0

Field Summary

Modifier and TypeField and Description
private final CharSequence
left

Left parameter used in distance function.

private final SimilarityScore<R>
similarityScore

Similarity score.

Constructor Summary

AccessConstructor and Description
public
SimilarityScoreFrom(final SimilarityScore<R>
This may not be null.
similarityScore
,
final CharSequence
This may be null here, but the SimilarityScore#compare(CharSequence left, CharSequence right) implementation may not accept nulls.
left
)

Constructs a new instance for a similarity score implementation and the "left" string.

Method Summary

Modifier and TypeMethod and Description
public R

Returns:

The similarity score between two CharSequences.
apply
(final CharSequence
the second CharSequence.
right
)

Compares "left" field against the "right" parameter using the "similarity score" implementation.

public CharSequence

Returns:

The left parameter.
getLeft
()

Gets the left parameter.

public SimilarityScore<R>

Returns:

The edit distance.
getSimilarityScore
()

Gets the edit distance.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

leftback to summary
private final CharSequence left

Left parameter used in distance function.

similarityScoreback to summary
private final SimilarityScore<R> similarityScore

Similarity score.

Constructor Detail

SimilarityScoreFromback to summary
public SimilarityScoreFrom(final SimilarityScore<R> similarityScore, final CharSequence left)

Constructs a new instance for a similarity score implementation and the "left" string.

Parameters
similarityScore:SimilarityScore<R>

This may not be null.

left:CharSequence

This may be null here, but the SimilarityScore#compare(CharSequence left, CharSequence right) implementation may not accept nulls.

Method Detail

applyback to summary
public R apply(final CharSequence right)

Compares "left" field against the "right" parameter using the "similarity score" implementation.

Parameters
right:CharSequence

the second CharSequence.

Returns:R

The similarity score between two CharSequences.

getLeftback to summary
public CharSequence getLeft()

Gets the left parameter.

Returns:CharSequence

The left parameter.

getSimilarityScoreback to summary
public SimilarityScore<R> getSimilarityScore()

Gets the edit distance.

Returns:SimilarityScore<R>

The edit distance.