List of usage examples for org.apache.lucene.search.spell SpellChecker SpellChecker
public SpellChecker(Directory spellIndex) throws IOException
From source file:aos.lucene.tools.CreateSpellCheckerIndex.java
License:Apache License
public static void main(String[] args) throws IOException { if (args.length != 3) { LOGGER.info("Usage: java lia.tools.SpellCheckerTest SpellCheckerIndexDir IndexDir IndexField"); System.exit(1);/* www . j av a2 s. c o m*/ } String spellCheckDir = args[0]; String indexDir = args[1]; String indexField = args[2]; LOGGER.info("Now build SpellChecker index..."); Directory dir = FSDirectory.open(new File(spellCheckDir)); SpellChecker spell = new SpellChecker(dir); //#A long startTime = System.currentTimeMillis(); Directory dir2 = FSDirectory.open(new File(indexDir)); IndexReader r = DirectoryReader.open(dir2); //#B try { spell.indexDictionary(new LuceneDictionary(r, indexField)); //#C } finally { r.close(); } dir.close(); dir2.close(); long endTime = System.currentTimeMillis(); LOGGER.info(" took " + (endTime - startTime) + " milliseconds"); }
From source file:aos.lucene.tools.SpellCheckerExample.java
License:Apache License
public static void main(String[] args) throws IOException { if (args.length != 2) { LOGGER.info("Usage: java lia.tools.SpellCheckerTest SpellCheckerIndexDir wordToRespell"); System.exit(1);/*from www . j av a 2 s .c om*/ } String spellCheckDir = args[0]; String wordToRespell = args[1]; Directory dir = FSDirectory.open(new File(spellCheckDir)); if (!IndexReader.indexExists(dir)) { LOGGER.info("\nERROR: No spellchecker index at path \"" + spellCheckDir + "\"; please run CreateSpellCheckerIndex first\n"); System.exit(1); } SpellChecker spell = new SpellChecker(dir); // #A spell.setStringDistance(new LevensteinDistance()); // #B // spell.setStringDistance(new JaroWinklerDistance()); String[] suggestions = spell.suggestSimilar(wordToRespell, 5); // #C LOGGER.info(suggestions.length + " suggestions for '" + wordToRespell + "':"); for (String suggestion : suggestions) LOGGER.info(" " + suggestion); }
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;//from w w w . j av a2 s . com 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:com.bah.bahdit.main.search.utils.LevenshteinDistance.java
License:Apache License
/** * Given a context to place all of the spell check files in, this method * sets up the spellchecker object using the sample table * /*from w w w . j a va 2 s.c o m*/ * @param context - the context of the current servlet * @param sampleTable - the full text sample table * @return - a spellchecker object */ public static SpellChecker createSpellChecker(ServletContext context, HashMap<String, Integer> sampleTable) { SpellChecker spellChecker = null; // write terms from sample table to text file, to be basis of dictionary File f = new File("dictionary" + System.nanoTime() + ".txt"); try { f.createNewFile(); BufferedWriter out = new BufferedWriter(new FileWriter(f)); for (String entry : sampleTable.keySet()) { out.write(entry + "\n"); } } catch (IOException e) { e.printStackTrace(); } String dPath = System.getProperty("user.dir") + "/spellcheck" + System.nanoTime(); File dir = new File(dPath); Directory directory = null; try { directory = FSDirectory.open(dir); } catch (IOException e3) { e3.printStackTrace(); } try { spellChecker = new SpellChecker(directory); } catch (IOException e2) { e2.printStackTrace(); } StandardAnalyzer a = new StandardAnalyzer(Version.LUCENE_40); IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_40, a); boolean fullMerge = true; PlainTextDictionary dict = null; try { dict = new PlainTextDictionary(f); } catch (FileNotFoundException e1) { e1.printStackTrace(); } try { spellChecker.indexDictionary(dict, config, fullMerge); } catch (IOException e) { e.printStackTrace(); } return spellChecker; }
From source file:com.ikon.module.db.stuff.IndexHelper.java
License:Open Source License
public void updateSpellCheckerIndex(NodeDocumentVersion nDocVer) { log.info("Observed Wine added/updated event for {1} from Thread {0}", Thread.currentThread().getName(), String.valueOf(nDocVer)); String text = (nDocVer != null) ? nDocVer.getText() : null; if (text != null) { Dictionary dictionary = null; try {//from w w w . ja v a 2 s . c om FullTextEntityManager ftEm = (FullTextEntityManager) entityManager; SearchFactory searchFactory = ftEm.getSearchFactory(); dictionary = new SetDictionary(text, searchFactory.getAnalyzer("wine_en")); } catch (IOException ioExc) { log.error("Failed to analyze dictionary text {0} from Wine {1} to update spell checker due to: {2}" + text + nDocVer.getUuid() + ioExc.toString()); } if (dictionary != null) { Directory dir = null; // only allow one thread to update the index at a time ... // the Dictionary is pre-computed, so it should happen quickly // ... // this synchronized approach only works because this component // is application-scoped synchronized (this) { try { dir = FSDirectory.open(new File("lucene_index/spellcheck")); SpellChecker spell = new SpellChecker(dir); spell.indexDictionary(dictionary); spell.close(); log.info("Successfully updated the spell checker index after Document added/updated."); } catch (Exception exc) { log.error("Failed to update the spell checker index!", exc); } finally { if (dir != null) { try { dir.close(); } catch (Exception zzz) { } } } } } } }
From source file:com.ikon.module.db.stuff.IndexHelper.java
License:Open Source License
protected void buildSpellCheckerIndex(SearchFactory searchFactory) { IndexReader reader = null;// w w w . j a v a 2 s . co m 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:com.jaeksoft.searchlib.cache.SpellCheckerCache.java
License:Open Source License
public SpellChecker get(ReaderLocal reader, String field) throws IOException { rwl.w.lock();//from ww w. j a v a2 s . c o m try { FieldNameKey key = new FieldNameKey(field); SpellChecker spellChecker = getAndPromote(key); if (spellChecker != null) return spellChecker; LuceneDictionary dict = reader.getLuceneDirectionary(key.getFieldName()); SpellChecker spellchecker = new SpellChecker(new RAMDirectory()); spellchecker.indexDictionary(dict, new IndexWriterConfig(Version.LUCENE_36, null), true); put(key, spellchecker); return spellchecker; } finally { rwl.w.unlock(); } }
From source file:com.jaeksoft.searchlib.spellcheck.SpellCheckCacheItem.java
License:Open Source License
@Override protected void populate(Timer timer) throws Exception { LuceneDictionary dict = reader.getLuceneDirectionary(field); spellChecker = new SpellChecker(new RAMDirectory()); spellChecker.indexDictionary(dict, new IndexWriterConfig(Version.LUCENE_36, null), true); }
From source file:com.leavesfly.lia.tool.CreateSpellCheckerIndex.java
License:Apache License
public static void main(String[] args) throws IOException { if (args.length != 3) { System.out.println("Usage: java lia.tools.SpellCheckerTest SpellCheckerIndexDir IndexDir IndexField"); System.exit(1);/* ww w .ja v a 2 s. c o m*/ } String spellCheckDir = args[0]; String indexDir = args[1]; String indexField = args[2]; System.out.println("Now build SpellChecker index..."); Directory dir = FSDirectory.open(new File(spellCheckDir)); SpellChecker spell = new SpellChecker(dir); //#A long startTime = System.currentTimeMillis(); Directory dir2 = FSDirectory.open(new File(indexDir)); IndexReader r = IndexReader.open(dir2); //#B try { spell.indexDictionary(new LuceneDictionary(r, indexField)); //#C } finally { r.close(); } dir.close(); dir2.close(); long endTime = System.currentTimeMillis(); System.out.println(" took " + (endTime - startTime) + " milliseconds"); }
From source file:com.leavesfly.lia.tool.SpellCheckerExample.java
License:Apache License
public static void main(String[] args) throws IOException { if (args.length != 2) { System.out.println("Usage: java lia.tools.SpellCheckerTest SpellCheckerIndexDir wordToRespell"); System.exit(1);//from w w w .j a v a 2s .c o m } String spellCheckDir = args[0]; String wordToRespell = args[1]; Directory dir = FSDirectory.open(new File(spellCheckDir)); if (!IndexReader.indexExists(dir)) { System.out.println("\nERROR: No spellchecker index at path \"" + spellCheckDir + "\"; please run CreateSpellCheckerIndex first\n"); System.exit(1); } SpellChecker spell = new SpellChecker(dir); //#A spell.setStringDistance(new LevensteinDistance()); //#B //spell.setStringDistance(new JaroWinklerDistance()); String[] suggestions = spell.suggestSimilar(wordToRespell, 5); //#C System.out.println(suggestions.length + " suggestions for '" + wordToRespell + "':"); for (String suggestion : suggestions) System.out.println(" " + suggestion); }