com.github.dylon.liblevenshtein.levenshtein.CandidateCollection Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of liblevenshtein Show documentation
Show all versions of liblevenshtein Show documentation
A library for spelling-correction based on Levenshtein Automata.
package com.github.dylon.liblevenshtein.levenshtein;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
/**
*
* WARNING This class is deprecated and will be removed in the next major
* release. Please migrate your code to use
* {@link com.github.dylon.liblevenshtein.levenshtein.factory.ICandidateFactory}
* and an instance of
* {@link com.github.dylon.liblevenshtein.levenshtein.ICandidateCollection}
* that can make use of it.
*
*
*
* Collection of all candidates from the dictionary that are no further than the
* specified distance from the query term.
*
*
* @param Kind of the spelling candidates returned from the dictionary.
*
* @author Dylon Edwards
* @since 2.1.0
*/
@Deprecated
public abstract class CandidateCollection
implements ICandidateCollection, Serializable {
private static final long serialVersionUID = 1L;
/**
* Maximum number of correction candidates that may be added to this
* ICandidateCollection.
*/
protected final int maxCandidates;
/**
* Correction candidates that have been added to this ICandidateCollection.
*/
protected final Collection candidates;
/**
* Constructs a new instance of ICandidateCollection, with a maximum threshold
* for the number of correction candidates it will accept.
* @param maxCandidates Maximum number of correction candidates that may be
* added to this collection.
*/
public CandidateCollection(final int maxCandidates) {
this.maxCandidates = maxCandidates;
// TODO: Consider specializing the data struction by , such as using
// an unsorted Dawg for , etc.
this.candidates = (maxCandidates < Integer.MAX_VALUE)
? new ArrayList<>(maxCandidates)
: new ArrayList<>();
}
/**
* {@inheritDoc}
*/
@Override
public Iterator iterator() {
return candidates.iterator();
}
/**
* {@inheritDoc}
*/
@Override
public String toString() {
return ReflectionToStringBuilder.toString(this, ToStringStyle.SHORT_PREFIX_STYLE);
}
/**
* Implementation of {@link CandidateCollection} that includes the distances
* with the spelling candidates, packaged in {@link Candidate}s.
* @author Dylon Edwards
* @since 2.1.0
*/
public static class WithDistance extends CandidateCollection {
private static final long serialVersionUID = 1L;
/**
* Constructs a new instance of ICandidateCollection, with a maximum
* threshold for the number of correction candidates it will accept.
* @param maxCandidates Maximum number of correction candidates that may be
* added to this collection.
*/
public WithDistance(final int maxCandidates) {
super(maxCandidates);
}
/**
* {@inheritDoc}
*/
@Override
public boolean offer(final String term, final int distance) {
if (candidates.size() == maxCandidates) {
return false;
}
candidates.add(new Candidate(term, distance));
return true;
}
}
/**
* Implementation of {@link CandidateCollection} that returns only the raw,
* spelling candidates.
* @author Dylon Edwards
* @since 2.1.0
*/
public static class WithoutDistance extends CandidateCollection {
private static final long serialVersionUID = 1L;
/**
* Constructs a new instance of ICandidateCollection, with a maximum
* threshold for the number of correction candidates it will accept.
* @param maxCandidates Maximum number of correction candidates that may be
* added to this collection.
*/
public WithoutDistance(final int maxCandidates) {
super(maxCandidates);
}
/**
* {@inheritDoc}
*/
@Override
public boolean offer(final String term, final int distance) {
if (candidates.size() == maxCandidates) {
return false;
}
candidates.add(term); // NOTE: distance is ignored ...
return true;
}
}
}