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

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

Introduction

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

Prototype

public void setAccuracy(float acc) 

Source Link

Document

Sets the accuracy 0 < minScore < 1; default #DEFAULT_ACCURACY

Usage

From source file:com.jaeksoft.searchlib.spellcheck.SpellCheck.java

License:Open Source License

public SpellCheck(ReaderLocal reader, SpellCheckRequest request, SpellCheckField spellCheckField)
        throws ParseException, SyntaxError, IOException, SearchLibException {
    fieldName = spellCheckField.getName();
    SpellChecker spellchecker = reader.getSpellChecker(fieldName);
    Set<String> wordSet = new LinkedHashSet<String>();

    Set<Term> set = request.getTermSet(spellCheckField.getName());
    for (Term term : set)
        if (term.field().equals(fieldName))
            wordSet.add(term.text());//from   w  w w  .j a va  2 s  . c  om
    int suggestionNumber = spellCheckField.getSuggestionNumber();
    float minScore = spellCheckField.getMinScore();
    synchronized (spellchecker) {
        spellchecker.setAccuracy(minScore);
        spellchecker.setStringDistance(spellCheckField.getStringDistance().getNewInstance());
        spellCheckItems = new ArrayList<SpellCheckItem>();
        for (String word : wordSet) {
            String[] suggestions = spellchecker.suggestSimilar(word, suggestionNumber);
            int s = 1;
            if (suggestions != null)
                s += suggestions.length;
            SuggestionItem[] suggestionItems = new SuggestionItem[s];
            int i = 0;
            suggestionItems[i++] = new SuggestionItem(word);
            if (suggestions != null) {
                for (String suggestion : suggestions)
                    suggestionItems[i++] = new SuggestionItem(suggestion);
                spellCheckItems.add(new SpellCheckItem(word, suggestionItems));
            }
        }
    }
    List<String> highers = new ArrayList<String>(spellCheckItems.size());
    for (SpellCheckItem spellcheckItem : spellCheckItems) {
        spellcheckItem.computeFrequency(reader, fieldName);
        String higher = spellcheckItem.getHigher();
        if (higher != null)
            highers.add(higher);
    }
    suggestion = StringUtils.join(highers, ' ');
}

From source file:fr.mael.microrss.dao.impl.GenericDaoImpl.java

License:Open Source License

/**
 * @see fr.mael.jmusic.dao.GenericDao#getSuggestions(java.lang.String)
 *///from ww w .  ja v a  2 s. c  o m
@Override
public String[] getSuggestions(String query) throws IOException {
    Directory directory = FSDirectory
            .open(new File(configuration.getIndexDir() + "/spell_" + getPersistentClass().getName()));
    SpellChecker spell = new SpellChecker(directory);
    spell.setStringDistance(new LevensteinDistance());
    spell.setAccuracy(configuration.getSuggestionAccuracy());
    return spell.suggestSimilar(query, configuration.getSuggestionNumber());
}

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 a 2s  . 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;
}