本文整理汇总了Java中org.apache.lucene.search.spell.SpellChecker.indexDictionary方法的典型用法代码示例。如果您正苦于以下问题:Java SpellChecker.indexDictionary方法的具体用法?Java SpellChecker.indexDictionary怎么用?Java SpellChecker.indexDictionary使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.lucene.search.spell.SpellChecker
的用法示例。
在下文中一共展示了SpellChecker.indexDictionary方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: 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;
}
示例2: 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;
}
}
示例3: 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();
}
示例4: 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;
}
示例5: 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);
}
}
}
示例6: 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.");
}
}
示例7: 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);
}
}
示例8: SpellImpl
import org.apache.lucene.search.spell.SpellChecker; //导入方法依赖的package包/类
/**
* Constructor. Initializes directory for indexing and dictionary
*
* @param props
* properties file Object
* @throws IOException
* when dictionary or directory path is/are invalid or
* inaccessible
*/
public SpellImpl(Properties props) throws IOException {
directory = FSDirectory.open(new File(props.getProperty(
"index_directory", "res/index")));
dictionary = new PlainTextDictionary(new File(props.getProperty(
"dictionary_path", "res/dict_en.txt")));
spellChecker = new SpellChecker(directory);
StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_36);
IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_36,
analyzer);
spellChecker.clearIndex();
spellChecker.indexDictionary(dictionary, config, true);
spellChecker.setAccuracy(Float.parseFloat(props.getProperty(
"default_accuracy", "0.75")));
setMaxSuggestions(Integer.parseInt(props.getProperty("max_suggestions",
"5")));
}
示例9: populate
import org.apache.lucene.search.spell.SpellChecker; //导入方法依赖的package包/类
@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);
}
示例10: prepare
import org.apache.lucene.search.spell.SpellChecker; //导入方法依赖的package包/类
public void prepare(Map conf, TridentOperationContext context){
super.prepare(conf, context);
File dir = new File(System.getProperty("user.home") + "/dictionaries");
Directory directory;
try {
directory = FSDirectory.open(dir);
spellchecker = new SpellChecker(directory);
StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_36);
IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_36, analyzer);
URL dictionaryFile = TermFilter.class.getResource("/dictionaries/fulldictionary00.txt");
spellchecker.indexDictionary(new PlainTextDictionary(new File(dictionaryFile.toURI())), config, true);
} catch (Exception e) {
LOG.error(e.toString());
}
}
示例11: main
import org.apache.lucene.search.spell.SpellChecker; //导入方法依赖的package包/类
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);
}
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");
}
示例12: updateSpellCheckerIndex
import org.apache.lucene.search.spell.SpellChecker; //导入方法依赖的package包/类
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 {
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) {
}
}
}
}
}
}
}
示例13: createSpellIndex
import org.apache.lucene.search.spell.SpellChecker; //导入方法依赖的package包/类
/**
* Creates a new spell-check index based on search-index
*/
public void createSpellIndex() {
if (isSpellCheckEnabled) {
IndexReader indexReader = null;
try {
log.info("Start generating Spell-Index...");
long startSpellIndexTime = 0;
if (log.isDebugEnabled()) {
startSpellIndexTime = System.currentTimeMillis();
}
final Directory indexDir = FSDirectory.open(new File(indexPath));
indexReader = IndexReader.open(indexDir);
// 1. Create content spellIndex
final File spellDictionaryFile = new File(spellDictionaryPath);
final Directory contentSpellIndexDirectory = FSDirectory.open(new File(spellDictionaryPath + CONTENT_PATH));// true
final SpellChecker contentSpellChecker = new SpellChecker(contentSpellIndexDirectory);
final Dictionary contentDictionary = new LuceneDictionary(indexReader, AbstractOlatDocument.CONTENT_FIELD_NAME);
contentSpellChecker.indexDictionary(contentDictionary);
// 2. Create title spellIndex
final Directory titleSpellIndexDirectory = FSDirectory.open(new File(spellDictionaryPath + TITLE_PATH));// true
final SpellChecker titleSpellChecker = new SpellChecker(titleSpellIndexDirectory);
final Dictionary titleDictionary = new LuceneDictionary(indexReader, AbstractOlatDocument.TITLE_FIELD_NAME);
titleSpellChecker.indexDictionary(titleDictionary);
// 3. Create description spellIndex
final Directory descriptionSpellIndexDirectory = FSDirectory.open(new File(spellDictionaryPath + DESCRIPTION_PATH));// true
final SpellChecker descriptionSpellChecker = new SpellChecker(descriptionSpellIndexDirectory);
final Dictionary descriptionDictionary = new LuceneDictionary(indexReader, AbstractOlatDocument.DESCRIPTION_FIELD_NAME);
descriptionSpellChecker.indexDictionary(descriptionDictionary);
// 4. Create author spellIndex
final Directory authorSpellIndexDirectory = FSDirectory.open(new File(spellDictionaryPath + AUTHOR_PATH));// true
final SpellChecker authorSpellChecker = new SpellChecker(authorSpellIndexDirectory);
final Dictionary authorDictionary = new LuceneDictionary(indexReader, AbstractOlatDocument.AUTHOR_FIELD_NAME);
authorSpellChecker.indexDictionary(authorDictionary);
// Merge all part spell indexes (content,title etc.) to one common spell index
final Directory spellIndexDirectory = FSDirectory.open(spellDictionaryFile);// true
final IndexWriter merger = new IndexWriter(spellIndexDirectory, new StandardAnalyzer(Version.LUCENE_CURRENT), true, IndexWriter.MaxFieldLength.UNLIMITED);
final Directory[] directories = { contentSpellIndexDirectory, titleSpellIndexDirectory, descriptionSpellIndexDirectory, authorSpellIndexDirectory };
merger.addIndexesNoOptimize(directories);
merger.optimize();
merger.close();
spellChecker = new SpellChecker(spellIndexDirectory);
spellChecker.setAccuracy(0.7f);
if (log.isDebugEnabled()) {
log.debug("SpellIndex created in " + (System.currentTimeMillis() - startSpellIndexTime) + "ms");
}
log.info("New generated Spell-Index ready to use.");
} catch (final IOException ioEx) {
log.warn("Can not create SpellIndex", ioEx);
} finally {
if (indexReader != null) {
try {
indexReader.close();
} catch (final IOException e) {
log.warn("Can not close indexReader properly", e);
}
}
}
}
}