Skip to content

Instantly share code, notes, and snippets.

@vinicius85
Last active November 20, 2021 17:06
Show Gist options
  • Save vinicius85/5687341 to your computer and use it in GitHub Desktop.
Save vinicius85/5687341 to your computer and use it in GitHub Desktop.
Lucene StringDistance sample
import java.util.ArrayList;
import java.util.List;
import org.apache.lucene.search.spell.JaroWinklerDistance;
import org.apache.lucene.search.spell.LevensteinDistance;
import org.apache.lucene.search.spell.NGramDistance;
import org.apache.lucene.search.spell.StringDistance;
public class TestLuceneDistanceImplementations
{
public static void main(String[] args)
{
//Lucene StringDistance implementations
LevensteinDistance levenstein = new LevensteinDistance();
JaroWinklerDistance jaroWinkler = new JaroWinklerDistance();
NGramDistance nGram = new NGramDistance();
List<StringDistance> distanceList = new ArrayList<StringDistance>();
distanceList.add( levenstein );
distanceList.add( jaroWinkler );
distanceList.add( nGram );
// Dictionary is a set of words assumed correctly spelled
String[] dictionary = {
"notebook",
"notebooks",
"netbook" };
// Misspelled word. User entry
String misspelled = "notebuk";
for ( StringDistance distanceAlgorithm : distanceList )
{
System.out.println( distanceAlgorithm.getClass() );
for ( String word : dictionary )
{
float distance = distanceAlgorithm.getDistance( word, misspelled );
System.out.println( "misspelled=" + misspelled + "\tword=" + word + "\tdistance=" + distance );
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment