Last active
November 20, 2021 17:06
-
-
Save vinicius85/5687341 to your computer and use it in GitHub Desktop.
Lucene StringDistance sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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