本文整理汇总了Java中org.apache.lucene.search.spell.SpellChecker类的典型用法代码示例。如果您正苦于以下问题:Java SpellChecker类的具体用法?Java SpellChecker怎么用?Java SpellChecker使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
SpellChecker类属于org.apache.lucene.search.spell包,在下文中一共展示了SpellChecker类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: reset
import org.apache.lucene.search.spell.SpellChecker; //导入依赖的package包/类
@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);
}
}
示例2: suggestEndpointOptions
import org.apache.lucene.search.spell.SpellChecker; //导入依赖的package包/类
@Override
public String[] suggestEndpointOptions(Set<String> names, String unknownOption) {
// each option must be on a separate line in a String
StringBuilder sb = new StringBuilder();
for (String name : names) {
sb.append(name);
sb.append("\n");
}
StringReader reader = new StringReader(sb.toString());
try {
PlainTextDictionary words = new PlainTextDictionary(reader);
// use in-memory lucene spell checker to make the suggestions
RAMDirectory dir = new RAMDirectory();
SpellChecker checker = new SpellChecker(dir);
checker.indexDictionary(words, new IndexWriterConfig(new KeywordAnalyzer()), false);
return checker.suggestSimilar(unknownOption, maxSuggestions);
} catch (Exception e) {
// ignore
}
return null;
}
示例3: findMisspelledWords
import org.apache.lucene.search.spell.SpellChecker; //导入依赖的package包/类
protected List<String> findMisspelledWords(Iterator<String> checkedWordsIterator,
String lang) throws SpellCheckException {
List<String> misspelledWordsList = new ArrayList<String>();
SpellChecker checker = (SpellChecker) getChecker(lang);
try {
while (checkedWordsIterator.hasNext()) {
String word = checkedWordsIterator.next();
if (!word.equals("") && !checker.exist(word)) {
misspelledWordsList.add(word);
}
}
} catch (IOException e) {
logger.log(Level.SEVERE, "Failed to find misspelled words", e);
throw new SpellCheckException("Failed to find misspelled words", e);
}
return misspelledWordsList;
}
示例4: VocabularyNeo4jImpl
import org.apache.lucene.search.spell.SpellChecker; //导入依赖的package包/类
@Inject
public VocabularyNeo4jImpl(GraphDatabaseService graph,
@Nullable @IndicatesNeo4jGraphLocation String neo4jLocation, CurieUtil curieUtil,
NodeTransformer transformer) throws IOException {
this.graph = graph;
this.curieUtil = curieUtil;
this.transformer = transformer;
if (null != neo4jLocation) {
Directory indexDirectory =
FSDirectory.open((new File(new File(neo4jLocation), "index/lucene/node/node_auto_index"))
.toPath());
Directory spellDirectory =
FSDirectory.open((new File(new File(neo4jLocation), "index/lucene/spellchecker"))
.toPath());
spellChecker = new SpellChecker(spellDirectory);
try (IndexReader reader = DirectoryReader.open(indexDirectory)) {
IndexWriterConfig config = new IndexWriterConfig(new KeywordAnalyzer());
spellChecker.indexDictionary(new LuceneDictionary(reader, NodeProperties.LABEL
+ LuceneUtils.EXACT_SUFFIX), config, true);
}
} else {
spellChecker = null;
}
}
示例5: main
import org.apache.lucene.search.spell.SpellChecker; //导入依赖的package包/类
public static void main(String[] args) throws IOException {
if (args.length != 2) {
LOGGER.info("Usage: java lia.tools.SpellCheckerTest SpellCheckerIndexDir wordToRespell");
System.exit(1);
}
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);
}
示例6: spellChecker
import org.apache.lucene.search.spell.SpellChecker; //导入依赖的package包/类
/**
* 拼写错误的提示修正
*
* @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;
}
}
示例7: initSpellIndex
import org.apache.lucene.search.spell.SpellChecker; //导入依赖的package包/类
/**
* 初始化SpellIndex
*
* @throws IOException
*/
public void initSpellIndex(boolean isGame) throws IOException {
FileOPHelper.del(isGame ? appConfig.getGameSpellIndexDir() : appConfig.getSoftSpellIndexDir());
Directory spellIndexDir = FSDirectory.getDirectory(new File(isGame ? appConfig.getGameSpellIndexDir()
: appConfig.getSoftSpellIndexDir()));
SpellChecker spellChecker = new SpellChecker(spellIndexDir);
spellChecker.indexDictionary(new PlainTextDictionary(new File(isGame ? appConfig.getGameSuggestDict()
: appConfig.getSoftSuggestDict())));
spellIndexDir.close();
}
示例8: createIndexSpellchecker
import org.apache.lucene.search.spell.SpellChecker; //导入依赖的package包/类
@NotNull
private static SpellChecker createIndexSpellchecker(@NotNull final Directory index) throws IOException {
final Directory spellCheckerDirectory = new RAMDirectory();
final IndexReader indexReader = DirectoryReader.open(index);
final Analyzer analyzer = new SimpleAnalyzer();
final IndexWriterConfig config = new IndexWriterConfig(analyzer);
final Dictionary dictionary = new HighFrequencyDictionary(indexReader, DRUG_TERMS_FIELD, 0.0f);
final SpellChecker spellChecker = new SpellChecker(spellCheckerDirectory);
spellChecker.indexDictionary(dictionary, config, false);
spellChecker.setAccuracy(SPELLCHECK_ACCURACY);
return spellChecker;
}
示例9: spellcheckAnalyzer
import org.apache.lucene.search.spell.SpellChecker; //导入依赖的package包/类
@NotNull
private static Analyzer spellcheckAnalyzer(@NotNull final SpellChecker spellChecker) {
return new Analyzer() {
@Override
protected TokenStreamComponents createComponents(@NotNull final String field) {
final Tokenizer source = new WhitespaceTokenizer();
source.setReader(new StringReader(field));
final SpellCheckerTokenFilter spellCheckFilter = new SpellCheckerTokenFilter(defaultTokenFilter(source), spellChecker);
final TokenFilter concatenatingFilter = new ConcatenatingFilter(spellCheckFilter, ' ');
return new TokenStreamComponents(source, concatenatingFilter);
}
};
}
示例10: buildSpellCheckerIndex
import org.apache.lucene.search.spell.SpellChecker; //导入依赖的package包/类
protected void buildSpellCheckerIndex(SearchFactory searchFactory) {
IndexReader reader = null;
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);
}
}
}
示例11: setProperties
import org.apache.lucene.search.spell.SpellChecker; //导入依赖的package包/类
@Override
public void setProperties(Properties p) throws Exception {
// Initialize acronyms map
if (p.containsKey("acronymsFile")) {
final BufferedReader in = new BufferedReader(
new FileReader(new File(p.getProperty("acronymsFile"))));
String line;
while ((line = in.readLine()) != null) {
String[] tokens = FieldedStringTokenizer.split(line, "\t");
if (!acronymExpansionMap.containsKey(tokens[0])) {
acronymExpansionMap.put(tokens[0], new HashSet<String>(2));
}
acronymExpansionMap.get(tokens[0]).add(tokens[1]);
}
in.close();
} else {
throw new Exception("Required property acronymsFile not present.");
}
// Initialize spell checker
if (p.containsKey("spellingFile") && p.containsKey("spellingIndex")) {
// expect properties to have "spellingFile" and "spellingIndex"
final File dir = new File(p.getProperty("spellingIndex"));
final Directory directory = FSDirectory.open(dir);
spellChecker =
new SpellChecker(directory, new LuceneLevenshteinDistance());
final IndexWriterConfig indexWriterConfig =
new IndexWriterConfig(Version.LATEST, new WhitespaceAnalyzer());
spellChecker.indexDictionary(
new PlainTextDictionary(new File(p.getProperty("spellingFile"))),
indexWriterConfig, false);
} else {
throw new Exception(
"Required property spellingFile or spellingIndex not present.");
}
}
示例12: findSuggestions
import org.apache.lucene.search.spell.SpellChecker; //导入依赖的package包/类
protected List<String> findSuggestions(String word, String lang, int maxSuggestions) throws SpellCheckException {
SpellChecker checker = (SpellChecker) getChecker(lang);
try {
String[] suggestions = checker.suggestSimilar(word, maxSuggestions);
return Arrays.asList(suggestions);
} catch (IOException e) {
logger.log(Level.SEVERE, "Failed to find suggestions", e);
throw new SpellCheckException("Failed to find suggestions", e);
}
}
示例13: testAlternateDistance
import org.apache.lucene.search.spell.SpellChecker; //导入依赖的package包/类
@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();
}
}
示例14: forceSpellCheckerRenewal
import org.apache.lucene.search.spell.SpellChecker; //导入依赖的package包/类
public static synchronized void forceSpellCheckerRenewal(String indexPath){
SpellChecker sp = spellCheckMap.get(indexPath);
if(sp!=null) {
try {
sp.close();
} catch (IOException e) {
org.webdsl.logging.Logger.error("EXCEPTION",e);
}
}
spellCheckMap.remove(indexPath);
}
示例15: indexSpellCheck
import org.apache.lucene.search.spell.SpellChecker; //导入依赖的package包/类
private void indexSpellCheck(String id) throws SearchException {
if(!spellcheck) return;
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);
}
}