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

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

Introduction

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

Prototype

public void clearIndex() throws IOException 

Source Link

Document

Removes all terms from the spell check index.

Usage

From source file:com.ikon.module.db.stuff.IndexHelper.java

License:Open Source License

protected void buildSpellCheckerIndex(SearchFactory searchFactory) {
    IndexReader reader = null;//from  w w w .  j  a  v  a 2 s  .  c  om
    Directory dir = null;
    long _entr = System.currentTimeMillis();
    File spellCheckIndexDir = new File("lucene_index/spellcheck");
    log.info("Building SpellChecker index in {0}", spellCheckIndexDir.getAbsolutePath());
    ReaderProvider readerProvider = searchFactory.getReaderProvider();

    try {
        reader = readerProvider.openReader(searchFactory.getDirectoryProviders(NodeDocumentVersion.class)[0]);
        dir = FSDirectory.open(spellCheckIndexDir);
        SpellChecker spell = new SpellChecker(dir);
        spell.clearIndex();
        spell.indexDictionary(new LuceneDictionary(reader, NodeDocument.TEXT_FIELD));
        spell.close();
        dir.close();
        dir = null;
        long _exit = System.currentTimeMillis();
        log.info("Took {1} (ms) to build SpellChecker index in {0}", spellCheckIndexDir.getAbsolutePath(),
                String.valueOf((_exit - _entr)));
    } catch (Exception exc) {
        log.error("Failed to build spell checker index!", exc);
    } finally {
        if (dir != null) {
            try {
                dir.close();
            } catch (Exception zzz) {
            }
        }
        if (reader != null) {
            readerProvider.closeReader(reader);
        }
    }
}

From source file:es.pode.indexador.negocio.servicios.indexado.SrvIndexadorServiceImpl.java

License:Open Source License

/**
 * Reinicio del repositorio que nos muestra las palabras sugeridas
 * @param directorioIndiceSpell Objeto directorio con la informacin del directorio del repositorio de las palabras sugeridas
 * @throws IOException//  w w  w .  j a va 2 s. c  o  m
 * @throws Exception
 */
private void spellCheckerReset(Directory directorioIndiceSpell) throws IOException, Exception {

    if (logger.isDebugEnabled())
        logger.debug("Comprobamos el directorio del spellchecker [" + directorioIndiceSpell + "]");
    if (IndexReader.indexExists(directorioIndiceSpell)) {
        if (logger.isDebugEnabled())
            logger.debug("Creamos el spellchecher [" + directorioIndiceSpell + "]");
        SpellChecker spellChecker = new SpellChecker(directorioIndiceSpell);
        if (logger.isDebugEnabled())
            logger.debug("Reseteamos el contenido del spellchecher[" + directorioIndiceSpell + "]");
        spellChecker.clearIndex();
        directorioIndiceSpell.close();
    }
    //      si no existe el no es necesario reiniciarlo
    //      else
    //      {
    //         logger.error("No existe el spellchecher en el directorio["+directorioIndiceSpell+"]");
    //         throw new Exception("No existe el spellchecher en el directorio = " + directorioIndiceSpell);
    //      }
}

From source file:fastcampus.lucene.example.search.SpellCheckerExample.java

License:Apache License

public static void main(String[] args) throws Exception {
    Directory directory = FSDirectory.open(Paths.get("./index/spell/"));
    SpellChecker spellChecker = new SpellChecker(directory);

    //Analyzer analyzer = new StandardAnalyzer();                             // ? 
    Analyzer analyzer = new Analyzer() {
        @Override/*from  w  ww.  j  a  va  2 s .c  o  m*/
        protected TokenStreamComponents createComponents(String s) {
            Reader reader = new StringReader(s);
            Tokenizer tokenizer = new StandardTokenizer();
            tokenizer.setReader(reader);
            String name = "nfc_cf";
            Normalizer2 normalizer = Normalizer2.getInstance(null, name, Normalizer2.Mode.DECOMPOSE);
            TokenFilter filter = new ICUNormalizer2Filter(tokenizer, normalizer);
            return new TokenStreamComponents(tokenizer, filter);
        }
    };

    IndexWriterConfig indexWriterConfig = new IndexWriterConfig(analyzer); //?? Writer? ?  ?

    Path path = Paths.get("./data/spell/dic.txt");

    spellChecker.setSpellIndex(directory);
    spellChecker.clearIndex();
    spellChecker.indexDictionary(new PlainTextDictionary(path), indexWriterConfig, true);
    String wordForSuggestions = "?";
    //spellChecker.setStringDistance(new LevensteinDistance());  //#Levenstein  
    spellChecker.setStringDistance(new JaroWinklerDistance()); //Jaro-Winkler 

    int suggestionsNumber = 1;
    String[] suggestions = spellChecker.suggestSimilar(wordForSuggestions, suggestionsNumber);
    if (suggestions != null && suggestions.length > 0) {

        for (String word : suggestions) {

            System.out.println("Did you mean:" + word);

        }

    } else {

        System.out.println("No suggestions found for word:" + wordForSuggestions);

    }

}

From source file:org.sakaiproject.search.journal.impl.JournaledFSIndexStorage.java

License:Educational Community License

private void createSpellIndex(IndexReader indexReader) {
    if (!serverConfigurationService.getBoolean("search.experimental.didyoumean", false)) {
        return;//from   w w  w . java  2  s.  c  o m
    }

    log.info("create Spell Index");

    Long start = System.currentTimeMillis();
    try {

        log.info("main index is in: " + journalSettings.getSearchIndexDirectory());
        log.info("local base is: " + journalSettings.getLocalIndexBase());
        spellIndexDirectory = new NIOFSDirectory(new File(journalSettings.getLocalIndexBase() + "/spellindex"));
        if (indexReader == null) {
            log.info("unable to get index reader aborting spellindex creation");
            return;
        }
        Dictionary dictionary = new LuceneDictionary(indexReader, SearchService.FIELD_CONTENTS);
        SpellChecker spellChecker = new SpellChecker(spellIndexDirectory);
        spellChecker.clearIndex();
        spellChecker.indexDictionary(dictionary);
        log.info("New Spell dictionary constructed in " + (System.currentTimeMillis() - start));
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();

    }

    log.info("All done in " + (System.currentTimeMillis() - start));

}

From source file:org.silverpeas.search.indexEngine.model.DidYouMeanIndexer.java

License:Open Source License

/**
 * Clears all the entries from given spelling index
 * @param pathSpellChecker The SpellChecker's path to clear. The path must be a directory path.
 * @return true whether the index have been cleared otherwise false.
 *///from   w ww .j a v  a  2s. c  o  m
public static boolean clearSpellIndex(String pathSpellChecker) {
    boolean isCleared = false;
    try {
        // create a file object with given path
        File file = new File(pathSpellChecker);
        if (file != null && file.exists()) {
            // create a spellChecker with the file object
            SpellChecker spell = new SpellChecker(FSDirectory.open(file));
            // if index exists, clears his content
            if (spell != null) {
                spell.clearIndex();
                isCleared = true;
            }
        }
    } catch (IOException e) {
        SilverTrace.error("indexEngine", DidYouMeanIndexer.class.toString(), "root.EX_LOAD_IO_EXCEPTION", e);
    }
    return isCleared;
}

From source file:prman.model.SpellCheckManager.java

License:Open Source License

public void unindexDictionary(Locale locale) throws IOException {
    if (!isIndexed(locale))
        return;//from  w w  w.  j a va2  s . com
    SpellChecker sc = getSpellChecker(locale);
    if (sc != null) {
        sc.clearIndex();
        Directory dir = sc.getSpellIndex();
        String[] list = dir.list();
        for (int iCnt = 0; iCnt < list.length; iCnt++)
            dir.deleteFile(list[iCnt]);
        dir.close();
        spellCheckers.remove(locale);
    }
    File fdir = new File(DIR, locale.toString());
    // fdir.mkdirs();
    if (fdir.exists() && fdir.isDirectory()) {
        File[] contents = fdir.listFiles();
        for (int iCnt = 0; iCnt < contents.length; iCnt++) {
            contents[iCnt].delete();
        }
        fdir.delete();
    }
}