private static StringDistance resolveDistance(String distanceVal) { distanceVal = distanceVal.toLowerCase(Locale.US); if ("internal".equals(distanceVal)) { return DirectSpellChecker.INTERNAL_LEVENSHTEIN; } else if ("damerau_levenshtein".equals(distanceVal) || "damerauLevenshtein".equals(distanceVal)) { return new LuceneLevenshteinDistance(); } else if ("levenstein".equals(distanceVal)) { return new LevensteinDistance(); // TODO Jaro and Winkler are 2 people - so apply same naming logic // as damerau_levenshtein } else if ("jarowinkler".equals(distanceVal)) { return new JaroWinklerDistance(); } else if ("ngram".equals(distanceVal)) { return new NGramDistance(); } else { throw new IllegalArgumentException("Illegal distance option " + distanceVal); } }
@PostConstruct public void reset() { String indexDir = appConfig.getAllSpellCheckerDir(); try { Directory spellcheckDir = FSDirectory.open(new File(indexDir)); if (!IndexReader.indexExists(spellcheckDir)) { logger.info("Please reset index firstly!"); return; } SpellChecker newSpellChecker = new SpellChecker(spellcheckDir); newSpellChecker.setStringDistance(new JaroWinklerDistance()); newSpellChecker.setAccuracy(0.7f); if (spellChecker == null) { spellChecker = newSpellChecker; } else { final Closeable preSpellChecker = spellChecker; spellChecker = newSpellChecker; IOUtils.closeQuietly(preSpellChecker); } } catch (Exception e) { logger.error("Exception", e); } }
/** * 拼写错误的提示修正 * * @param keyword * @return */ @Override public List<String> spellChecker(Integer typeId, String sword) { try { List<String> strList = new ArrayList<String>(); SpellChecker sp = new SpellChecker(FSDirectory.getDirectory(typeId == appConfig.getGameTypeId() ? appConfig .getGameSpellIndexDir() : appConfig.getSoftSpellIndexDir()), new JaroWinklerDistance()); String[] suggestions = sp.suggestSimilar(sword, appConfig.getSuggestNum()); for (int i = 0; i < suggestions.length; i++) strList.add(suggestions[i]); return strList; } catch (IOException e) { logger.error("Exception", e); return null; } }
@Test public void testAlternateDistance() throws Exception { TestSpellChecker checker = new TestSpellChecker(); NamedList spellchecker = new NamedList(); spellchecker.add("classname", IndexBasedSpellChecker.class.getName()); File indexDir = createTempDir(); spellchecker.add(AbstractLuceneSpellChecker.INDEX_DIR, indexDir.getAbsolutePath()); spellchecker.add(AbstractLuceneSpellChecker.FIELD, "title"); spellchecker.add(AbstractLuceneSpellChecker.SPELLCHECKER_ARG_NAME, spellchecker); spellchecker.add(AbstractLuceneSpellChecker.STRING_DISTANCE, JaroWinklerDistance.class.getName()); SolrCore core = h.getCore(); String dictName = checker.init(spellchecker, core); assertTrue(dictName + " is not equal to " + SolrSpellChecker.DEFAULT_DICTIONARY_NAME, dictName.equals(SolrSpellChecker.DEFAULT_DICTIONARY_NAME) == true); RefCounted<SolrIndexSearcher> holder = core.getSearcher(); SolrIndexSearcher searcher = holder.get(); try { checker.build(core, searcher); SpellChecker sc = checker.getSpellChecker(); assertTrue("sc is null and it shouldn't be", sc != null); StringDistance sd = sc.getStringDistance(); assertTrue("sd is null and it shouldn't be", sd != null); assertTrue("sd is not an instance of " + JaroWinklerDistance.class.getName(), sd instanceof JaroWinklerDistance); } finally { holder.decref(); } }
@Test public void testAlternateDistance() throws Exception { TestSpellChecker checker = new TestSpellChecker(); NamedList spellchecker = new NamedList(); spellchecker.add("classname", IndexBasedSpellChecker.class.getName()); File indexDir = new File(TEMP_DIR, "spellingIdx" + new Date().getTime()); indexDir.mkdirs(); spellchecker.add(AbstractLuceneSpellChecker.INDEX_DIR, indexDir.getAbsolutePath()); spellchecker.add(AbstractLuceneSpellChecker.FIELD, "title"); spellchecker.add(AbstractLuceneSpellChecker.SPELLCHECKER_ARG_NAME, spellchecker); spellchecker.add(AbstractLuceneSpellChecker.STRING_DISTANCE, JaroWinklerDistance.class.getName()); SolrCore core = h.getCore(); String dictName = checker.init(spellchecker, core); assertTrue(dictName + " is not equal to " + SolrSpellChecker.DEFAULT_DICTIONARY_NAME, dictName.equals(SolrSpellChecker.DEFAULT_DICTIONARY_NAME) == true); RefCounted<SolrIndexSearcher> holder = core.getSearcher(); SolrIndexSearcher searcher = holder.get(); try { checker.build(core, searcher); SpellChecker sc = checker.getSpellChecker(); assertTrue("sc is null and it shouldn't be", sc != null); StringDistance sd = sc.getStringDistance(); assertTrue("sd is null and it shouldn't be", sd != null); assertTrue("sd is not an instance of " + JaroWinklerDistance.class.getName(), sd instanceof JaroWinklerDistance); } finally { holder.decref(); } }
private static void check() { try { Class.forName(JaroWinklerDistance.class.getName()); Class.forName(LevensteinDistance.class.getName()); Class.forName(StringDistance.class.getName()); } catch (ClassNotFoundException e) { throw new RuntimeException(e); } }
@Override public StringDistance toLucene() { return new JaroWinklerDistance(); }