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

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

Introduction

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

Prototype

public SpellChecker(Directory spellIndex) throws IOException 

Source Link

Document

Use the given directory as a spell checker index with a LevenshteinDistance as the default StringDistance .

Usage

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

License:Open Source License

/**
 * @see fr.mael.jmusic.dao.GenericDao#buildSpellIndex()
 *///from  ww  w.  j  a  v a 2 s  .c om
@Override
public void buildSpellIndex() throws IOException {
    FullTextSession searchSession = Search.getFullTextSession(sessionFactory.getCurrentSession());
    SearchFactory searchFactory = searchSession.getSearchFactory();
    IndexReader reader = searchFactory.getIndexReaderAccessor().open(getPersistentClass());
    try {
        FSDirectory spellDir = FSDirectory
                .open(new File(configuration.getIndexDir() + "/spell_" + getPersistentClass().getName()));
        SpellChecker spellChecker = new SpellChecker(spellDir);
        Dictionary dictionary = new LuceneDictionary(reader, "name");
        IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_35,
                searchFactory.getAnalyzer(getPersistentClass()));
        spellChecker.indexDictionary(dictionary, config, true);
    } catch (Exception e) {
        log.error("Error building spell index", e);
    } finally {
        searchFactory.getIndexReaderAccessor().close(reader);
    }

}

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

License:Open Source License

/**
 * @see fr.mael.jmusic.dao.GenericDao#getSuggestions(java.lang.String)
 *///  w  ww .  j  a  va 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:lucee.runtime.search.lucene2.LuceneSearchCollection.java

License:Open Source License

private void indexSpellCheck(String id) throws SearchException {
    if (!spellcheck)
        return;/*from   w  w  w.  j  av a 2 s  . c  o m*/

    IndexReader reader = null;
    FSDirectory spellDir = null;

    Resource dir = _createSpellDirectory(id);
    try {
        File spellFile = FileWrapper.toFile(dir);
        spellDir = FSDirectory.getDirectory(spellFile);
        reader = _getReader(id, false);
        Dictionary dictionary = new LuceneDictionary(reader, "contents");

        SpellChecker spellChecker = new SpellChecker(spellDir);
        spellChecker.indexDictionary(dictionary);

    } catch (IOException ioe) {
        throw new SearchException(ioe);
    } finally {
        flushEL(reader);
        closeEL(reader);
    }
}

From source file:lucee.runtime.search.lucene2.LuceneSearchCollection.java

License:Open Source License

private SpellChecker getSpellChecker(String id) throws IOException {
    FSDirectory siDir = FSDirectory.getDirectory(FileWrapper.toFile(_getSpellDirectory(id)));
    SpellChecker spellChecker = new SpellChecker(siDir);
    return spellChecker;
}

From source file:newseman.lucene.whisperer.SearchFiles.java

License:Apache License

public static void doSpellSearch(FSDirectory dir, BufferedReader in, Searcher searcher, Query query,
        int hitsPerPage, boolean raw, boolean interactive) throws IOException {

    int s_limit = 8;

    if (spelldict == null) {
        spelldict = new SpellChecker(dir);
    }/*from   w w w.  j av  a  2s  . c om*/
    ArrayList<String> suggestions = new ArrayList<String>();

    // first try the whole string
    String first[] = spelldict.suggestSimilar(query.toString("key"), s_limit);
    if (first.length > 0) {
        for (int i = 0; i < first.length; i++) {
            suggestions.add(first[i]);
        }
    }

    // now with each term
    if (suggestions.size() < 1) {
        HashSet<Term> terms = new HashSet<Term>();
        query.extractTerms(terms);
        Iterator<Term> iterator = terms.iterator();

        while (iterator.hasNext()) {
            Term term = iterator.next();
            String t[] = spelldict.suggestSimilar(term.text(), s_limit);
            for (int i = 0; i < t.length; i++) {
                suggestions.add(t[i]);
            }

        }
    }

    if (suggestions.size() > 0) {
        for (int i = 0; i < suggestions.size(); i++) {
            System.out.println((i + 1) + ". " + suggestions.get(i));
        }
    } else {
        System.out.println("No similar key for this query: " + query.toString());
    }

}

From source file:org.apache.solr.handler.SpellCheckerRequestHandler.java

License:Apache License

public void inform(SolrCore core) {
    termSourceField = args.get(SOURCE_FIELD, args.get("termSourceField"));
    try {//from w w  w  .j  av a  2s.  com
        String dir = args.get(INDEX_DIR, args.get("spellcheckerIndexDir"));
        if (null != dir) {
            File f = new File(dir);
            if (!f.isAbsolute()) {
                f = new File(core.getDataDir(), dir);
            }
            dirDescription = f.getAbsolutePath();
            log.info("using spell directory: " + dirDescription);
            spellcheckerIndexDir = FSDirectory.open(f);
        } else {
            log.info("using RAM based spell directory");
        }
        spellChecker = new SpellChecker(spellcheckerIndexDir);
    } catch (IOException e) {
        throw new RuntimeException("Cannot open SpellChecker index", e);
    }
}

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  www .  j av  a 2 s  .c o 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;
}

From source file:org.codesearch.searcher.server.util.STAlternativeSuggestor.java

License:Open Source License

/**
 * Returns a list of autocomplete suggestions.
 * @param queryString/* w w w.jav a 2  s  .  com*/
 * @return String[] of completion suggestions
 * @throws IOException
 */
public List<String> suggest(String queryString) throws IOException {
    SpellChecker spellChecker = new SpellChecker(spellIndexDirectory);
    String[] spresult = spellChecker.suggestSimilar(queryString, 10);
    if (spresult.length == 0) {
        return null;
    }
    LinkedList<String> suggestions = new LinkedList<String>();
    suggestions.addAll(Arrays.asList(spresult));

    return suggestions;
}

From source file:org.codesearch.searcher.server.util.STAlternativeSuggestor.java

License:Open Source License

/**
 * Creates the spell index for the SpellChecker
 * @param field//from   ww w  .j av a2 s . c  o  m
 * @param originalIndexDirectory
 * @param spellIndexDirectory
 * @throws IOException
 */
public void createSpellIndex(String field, Directory originalIndexDirectory, Directory spellIndexDirectory)
        throws IOException {
    IndexReader indexReader = null;
    try {
        indexReader = IndexReader.open(originalIndexDirectory);
        Dictionary dictionary = new LuceneDictionary(indexReader, field);
        SpellChecker spellChecker = new SpellChecker(spellIndexDirectory);
        spellChecker.indexDictionary(dictionary);
    } finally {
        if (indexReader != null) {
            indexReader.close();
        }
    }
}

From source file:org.olat.search.service.spell.SearchSpellChecker.java

License:Apache License

/**
 * Check for valid similar search terms//from www  .j  a v a2s.c  om
 * 
 * @param query
 * @return Returns list of String with similar search-words. Returns null when spell-checker is disabled or has an exception.
 */
public Set<String> check(final String query) {
    try {
        if (spellChecker == null) { // lazy initialization
            try {
                synchronized (spellDictionaryPath) {// o_clusterOK by:pb if service is only configured on one vm, which is recommended way
                    final File spellDictionaryFile = new File(spellDictionaryPath);
                    final Directory spellIndexDirectory = FSDirectory.open(spellDictionaryFile);
                    if (spellChecker == null && IndexReader.indexExists(spellIndexDirectory)
                            && isSpellCheckEnabled) {
                        spellChecker = new SpellChecker(spellIndexDirectory);
                        spellChecker.setAccuracy(0.7f);
                    }
                }
            } catch (final IOException e) {
                log.warn("Can not initialze SpellChecker", e);
            }
        }
        if (spellChecker != null) {
            final String[] words = spellChecker.suggestSimilar(query, 5);
            // Remove dublicate
            final Set<String> filteredList = new TreeSet<String>();
            for (final String word : words) {
                filteredList.add(word);
            }
            return filteredList;
        }
    } catch (final IOException e) {
        log.warn("Can not spell check", e);
        return null;
    }
    return null;
}