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

public Class IntersectionSimilarity<T>

extends Object
implements SimilarityScore<IntersectionResult>
Class Inheritance
All Implemented Interfaces
org.apache.commons.text.similarity.SimilarityScore, org.apache.commons.text.similarity.ObjectSimilarityScore, java.util.function.BiFunction
Type Parameters
<T>
the type of the elements extracted from the character sequence
Imports
java.util.Collection, .HashMap, .Map, .Map.Entry, .Set, java.util.function.Function

Measures the intersection of two sets created from a pair of character sequences.

It is assumed that the type T correctly conforms to the requirements for storage within a Set or HashMap. Ideally the type is immutable and implements Object#equals(Object) and Object#hashCode().

Since
1.7
See Also
Set, HashMap

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
IntersectionSimilarity.BagCount

Mutable counter class for storing the count of elements.

private class
IntersectionSimilarity.TinyBag

A minimal implementation of a Bag that can store elements and a count.

Field Summary

Modifier and TypeField and Description
private final Function<CharSequence, Collection<T>>
converter

The converter used to create the elements from the characters.

Constructor Summary

AccessConstructor and Description
public
IntersectionSimilarity(final Function<CharSequence, Collection<T>>
the converter used to create the elements from the characters.
converter
)

Create a new intersection similarity using the provided converter.

Method Summary

Modifier and TypeMethod and Description
public IntersectionResult

Returns:

The intersection result.
apply
(final CharSequence
first character sequence.
left
,
final CharSequence
second character sequence.
right
)

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

Calculates the intersection of two character sequences passed as input.

private static <
the type of the elements in the set.
T
>
int

Returns:

The intersection of A and B.
getIntersection
(final Set<T>
the set A.
setA
,
final Set<T>
the set B.
setB
)

Computes the intersection between two sets.

private int

Returns:

The intersection of A and B.
getIntersection
(final IntersectionSimilarity<T>.TinyBag
the bag A.
bagA
,
final IntersectionSimilarity<T>.TinyBag
the bag B.
bagB
)

Computes the intersection between two bags.

private IntersectionSimilarity<T>.TinyBag

Returns:

The bag.
toBag
(final Collection<T>
the objects.
objects
)

Converts the collection to a bag.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

converterback to summary
private final Function<CharSequence, Collection<T>> converter

The converter used to create the elements from the characters.

Constructor Detail

IntersectionSimilarityback to summary
public IntersectionSimilarity(final Function<CharSequence, Collection<T>> converter)

Create a new intersection similarity using the provided converter.

If the converter returns a Set then the intersection result will not include duplicates. Any other Collection is used to produce a result that will include duplicates in the intersect and union.

Parameters
converter:Function<CharSequence, Collection<T>>

the converter used to create the elements from the characters.

Exceptions
IllegalArgumentException:
if the converter is null.

Method Detail

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

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

Calculates the intersection of two character sequences passed as input.

Parameters
left:CharSequence

first character sequence.

right:CharSequence

second character sequence.

Returns:IntersectionResult

The intersection result.

Annotations
@Override
Exceptions
IllegalArgumentException:
if either input sequence is null.
getIntersectionback to summary
private static <T> int getIntersection(final Set<T> setA, final Set<T> setB)

Computes the intersection between two sets. This is the count of all the elements that are within both sets.

Parameters
<T>
the type of the elements in the set.
setA:Set<T>

the set A.

setB:Set<T>

the set B.

Returns:int

The intersection of A and B.

getIntersectionback to summary
private int getIntersection(final IntersectionSimilarity<T>.TinyBag bagA, final IntersectionSimilarity<T>.TinyBag bagB)

Computes the intersection between two bags. This is the sum of the minimum count of each element that is within both sets.

Parameters
bagA:IntersectionSimilarity<T>.TinyBag

the bag A.

bagB:IntersectionSimilarity<T>.TinyBag

the bag B.

Returns:int

The intersection of A and B.

toBagback to summary
private IntersectionSimilarity<T>.TinyBag toBag(final Collection<T> objects)

Converts the collection to a bag. The bag will contain the count of each element in the collection.

Parameters
objects:Collection<T>

the objects.

Returns:IntersectionSimilarity<T>.TinyBag

The bag.

org.apache.commons.text.similarity back to summary

private final Class IntersectionSimilarity.BagCount

extends Object
Class Inheritance
  • java.lang.Object
  • org.apache.commons.text.similarity.IntersectionSimilarity.BagCount

Mutable counter class for storing the count of elements.

Field Summary

Modifier and TypeField and Description
private int
count

The count.

private static final IntersectionSimilarity.BagCount
ZERO

Private, mutable but must be used as immutable.

Constructor Summary

AccessConstructor and Description
private

Method Summary

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

countback to summary
private int count

The count.

ZEROback to summary
private static final IntersectionSimilarity.BagCount ZERO

Private, mutable but must be used as immutable.

Constructor Detail

BagCountback to summary
private BagCount()
org.apache.commons.text.similarity back to summary

private final Class IntersectionSimilarity.TinyBag

extends Object
Class Inheritance
  • java.lang.Object
  • org.apache.commons.text.similarity.IntersectionSimilarity.TinyBag

A minimal implementation of a Bag that can store elements and a count.

For the intended purpose the Bag does not have to be a Collection. It does not even have to know its own size.

Field Summary

Modifier and TypeField and Description
private final Map<T, IntersectionSimilarity.BagCount>
map

The backing map.

Constructor Summary

AccessConstructor and Description
private
TinyBag(final int
the initial capacity
initialCapacity
)

Create a new tiny bag.

Method Summary

Modifier and TypeMethod and Description
private void
add(final T
the object to add.
object
)

Adds a new element to the bag, incrementing its count in the underlying map.

private Set<Map.Entry<T, IntersectionSimilarity.BagCount>>

Returns:

The Set view.
entrySet
()

Returns a Set view of the mappings contained in this bag.

private int

Returns:

The number of occurrences of the object, zero if not found.
getCount
(final Object
the object to search for.
object
)

Returns the number of occurrence of the given element in this bag by looking up its count in the underlying map.

private int

Returns:

The unique element size.
uniqueElementSize
()

Gets the number of unique elements in the bag.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

mapback to summary
private final Map<T, IntersectionSimilarity.BagCount> map

The backing map.

Constructor Detail

TinyBagback to summary
private TinyBag(final int initialCapacity)

Create a new tiny bag.

Parameters
initialCapacity:int

the initial capacity

Method Detail

addback to summary
private void add(final T object)

Adds a new element to the bag, incrementing its count in the underlying map.

Parameters
object:T

the object to add.

entrySetback to summary
private Set<Map.Entry<T, IntersectionSimilarity.BagCount>> entrySet()

Returns a Set view of the mappings contained in this bag.

Returns:Set<Map.Entry<T, IntersectionSimilarity.BagCount>>

The Set view.

getCountback to summary
private int getCount(final Object object)

Returns the number of occurrence of the given element in this bag by looking up its count in the underlying map.

Parameters
object:Object

the object to search for.

Returns:int

The number of occurrences of the object, zero if not found.

uniqueElementSizeback to summary
private int uniqueElementSize()

Gets the number of unique elements in the bag.

Returns:int

The unique element size.