Skip to content

Instantly share code, notes, and snippets.

@matiwinnetou
Last active July 6, 2016 20:49
Show Gist options
  • Save matiwinnetou/cd3e5370cb8dbc02349205c2056175d3 to your computer and use it in GitHub Desktop.
Save matiwinnetou/cd3e5370cb8dbc02349205c2056175d3 to your computer and use it in GitHub Desktop.
import org.apache.lucene.analysis.de.GermanAnalyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.*;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.search.spell.LuceneDictionary;
import org.apache.lucene.search.spell.SpellChecker;
import org.apache.lucene.search.suggest.Lookup;
import org.apache.lucene.search.suggest.analyzing.AnalyzingSuggester;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.RAMDirectory;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
public class LuceneSpellCheckerDemoService {
public static void main(String args[]) throws IOException {
Directory luceneDirectory = new RAMDirectory();
IndexWriter indexWriter = new IndexWriter(luceneDirectory, new IndexWriterConfig(new GermanAnalyzer()));
indexWriter.addDocument(createBerlin());
indexWriter.addDocument(createBremen());
indexWriter.addDocument(createTues());
indexWriter.addDocument(createTue());
indexWriter.commit();
indexWriter.close();
SpellChecker spellChecker = new SpellChecker(new RAMDirectory());
LuceneDictionary dictttt = new LuceneDictionary(DirectoryReader.open(luceneDirectory), "loc");
spellChecker.indexDictionary(dictttt, new IndexWriterConfig(new StandardAnalyzer()), true);
AnalyzingSuggester analyzingSuggester = new AnalyzingSuggester(new RAMDirectory(), "temp", new GermanAnalyzer());
analyzingSuggester.build(dictttt);
Arrays.asList(spellChecker.suggestSimilar("berlin", 5)).forEach(s -> System.out.println(s));
spellChecker.close();
List<Lookup.LookupResult> lookupResultList = analyzingSuggester.lookup("tue", false, 1);
lookupResultList.forEach(s -> System.out.println(s.key));
}
private static Document createBerlin() {
Document doc = new Document();
String loc = "berlin";
doc.add(new StringField("loc", loc, Field.Store.YES));
doc.add(new FloatPoint("lat", 12.12F));
doc.add(new FloatPoint("lon", 13.13F));
return doc;
}
private static Document createTue() {
Document doc = new Document();
String loc = "Tübingen";
doc.add(new StringField("loc", loc, Field.Store.YES));
doc.add(new FloatPoint("lat", 12.12F));
doc.add(new FloatPoint("lon", 13.13F));
return doc;
}
private static Document createTues() {
Document doc = new Document();
String loc = "Tuesmekistan";
doc.add(new StringField("loc", loc, Field.Store.YES));
doc.add(new FloatPoint("lat", 12.12F));
doc.add(new FloatPoint("lon", 13.13F));
return doc;
}
private static Document createBremen() {
Document doc = new Document();
String loc = "bremen";
doc.add(new StringField("loc", loc, Field.Store.YES));
doc.add(new FloatPoint("lat", 14.12F));
doc.add(new FloatPoint("lon", 14.13F));
return doc;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment