Example usage for org.apache.lucene.search.spell SpellChecker suggestSimilar

List of usage examples for org.apache.lucene.search.spell SpellChecker suggestSimilar

Introduction

In this page you can find the example usage for org.apache.lucene.search.spell SpellChecker suggestSimilar.

Prototype

public String[] suggestSimilar(String word, int numSug, IndexReader ir, String field, SuggestMode suggestMode)
        throws IOException 

Source Link

Document

Calls #suggestSimilar(String,int,IndexReader,String,SuggestMode,float) suggestSimilar(word, numSug, ir, suggestMode, field, this.accuracy)

Usage

From source file:com.appeligo.lucene.DidYouMeanIndexer.java

License:Apache License

public static void createSpellIndex(String field, Directory originalIndexDirectory,
        Directory spellIndexDirectory) throws IOException {

    IndexReader indexReader = null;//  w w w.j  a va2s  .c  o  m
    try {
        indexReader = IndexReader.open(originalIndexDirectory);
        Dictionary dictionary = new LuceneDictionary(indexReader, field);
        SpellChecker spellChecker = new SpellChecker(spellIndexDirectory);
        spellChecker.indexDictionary(dictionary);
        if (log.isDebugEnabled()) {
            spellChecker = new SpellChecker(spellIndexDirectory); // need to re-open to see it work
            log.debug("Does 'next' exist in the dictionary? " + spellChecker.exist("next"));
            StringBuilder sb = new StringBuilder();
            for (String s : spellChecker.suggestSimilar("noxt", 5, indexReader, "compositeField", true)) {
                sb.append(s + ", ");
            }
            log.debug("Best suggestions for 'noxt': " + sb);
        }
    } finally {
        if (indexReader != null) {
            indexReader.close();
        }
    }
}

From source file:org.capelin.transaction.dao.RecordDao.java

License:GNU General Public License

/**
 * Spell check the term from the field in index, return similar terms. If
 * the keyword is not tokenized, this will give the full name and function
 * like browse.//from   w  w  w  .  j a  v a2 s.  co  m
 * 
 * @param field
 * @param term
 * @return
 */
public String[] spellcheck(String field, String term) {
    SearchFactory searchFactory = Search.getFullTextSession(getSession()).getSearchFactory();
    DirectoryProvider<?> recordProvider = searchFactory.getDirectoryProviders(recordClass)[0];
    ReaderProvider readerProvider = searchFactory.getReaderProvider();
    IndexReader reader = readerProvider.openReader(recordProvider);
    String[] similars = null;
    try {
        SpellChecker spellchecker = new SpellChecker(recordProvider.getDirectory());
        spellchecker.indexDictionary(new LuceneDictionary(reader, field));
        spellchecker.setAccuracy(0.0001f);
        similars = spellchecker.suggestSimilar(term, getPageSize(), reader, field, true);
    } catch (IOException e) {
        log.error("Index not found: " + e);
    } finally {
        readerProvider.closeReader(reader);
    }
    return similars;
}