本文整理汇总了Java中org.apache.lucene.analysis.tokenattributes.CharTermAttributeImpl类的典型用法代码示例。如果您正苦于以下问题:Java CharTermAttributeImpl类的具体用法?Java CharTermAttributeImpl怎么用?Java CharTermAttributeImpl使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
CharTermAttributeImpl类属于org.apache.lucene.analysis.tokenattributes包,在下文中一共展示了CharTermAttributeImpl类的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: findSpellSuggestionsForField
import org.apache.lucene.analysis.tokenattributes.CharTermAttributeImpl; //导入依赖的package包/类
@SuppressWarnings("deprecation")
public static ArrayList<String> findSpellSuggestionsForField(Class<?> entityClass, String baseDir,
String suggestedField, int maxSuggestionCount, float accuracy, boolean morePopular,
Analyzer analyzer, String toSuggestOn) {
if (toSuggestOn == null || toSuggestOn.isEmpty())
return new ArrayList<String>();
SpellChecker spellChecker = null;
IndexReader fieldIR = null;
boolean hasSuggestions = false;
String indexPath = baseDir+suggestedField;
try {
spellChecker = getSpellChecker(indexPath);
spellChecker.setAccuracy(accuracy);
TokenStream tokenStream = analyzer.tokenStream(suggestedField, new StringReader(
toSuggestOn));
CharTermAttributeImpl ta = (CharTermAttributeImpl) tokenStream
.addAttribute(CharTermAttribute.class);
ArrayList<String[]> allSuggestions = new ArrayList<String[]>();
String word;
String[] suggestions;
while (tokenStream.incrementToken()) {
word = ta.term();
suggestions = null;
if (!morePopular) {
suggestions = spellChecker.suggestSimilar(word, maxSuggestionCount);
} else {
if (fieldIR == null)
fieldIR = getIndexReader(entityClass);
suggestions = spellChecker.suggestSimilar(word, maxSuggestionCount, fieldIR,
suggestedField, true);
}
if (suggestions == null || suggestions.length == 0)
suggestions = new String[] { word };
else
hasSuggestions = true;
allSuggestions.add(suggestions);
}
if (!hasSuggestions)
// if no suggestions were found, return empty list
return new ArrayList<String>();
else
return formSuggestions(maxSuggestionCount, allSuggestions);
} catch (Exception e) {
org.webdsl.logging.Logger.error("EXCEPTION",e);
//if something goes wrong, close and remove current SpellChecker instance, so it gets renewed
try {
spellChecker.close();
} catch (IOException e2) {
org.webdsl.logging.Logger.error("EXCEPTION",e2);
}
spellCheckMap.remove(indexPath);
}
finally {
searchfactory.getReaderProvider().closeReader(fieldIR);
}
return new ArrayList<String>();
}
示例2: findAutoCompletionsForField
import org.apache.lucene.analysis.tokenattributes.CharTermAttributeImpl; //导入依赖的package包/类
@SuppressWarnings("deprecation")
public static ArrayList<String> findAutoCompletionsForField(Class<?> entityClass, String baseDir,
String suggestedField, int maxSuggestionCount, Analyzer analyzer, String toSuggestOn) {
if (toSuggestOn == null || toSuggestOn.isEmpty())
return new ArrayList<String>();
AutoCompleter autoCompleter = null;
String indexPath = baseDir + suggestedField;
try {
autoCompleter = getAutoCompleter(indexPath);
TokenStream tokenStream = analyzer.tokenStream(suggestedField, new StringReader(
toSuggestOn));
CharTermAttributeImpl ta = (CharTermAttributeImpl) tokenStream
.addAttribute(CharTermAttribute.class);
boolean dontstop = tokenStream.incrementToken();
StringBuilder prefixSb = new StringBuilder( toSuggestOn.length() + 16 );
String word = "";
while (dontstop){ //eat up all tokens
word = ta.term();
dontstop = tokenStream.incrementToken();
if(dontstop)
prefixSb.append(word ).append( " ");
}
String prefix = prefixSb.toString();
String[] suggestions = autoCompleter.suggestSimilar(word, maxSuggestionCount);
ArrayList<String> allSuggestions = new ArrayList<String>();
if (suggestions == null || suggestions.length == 0){
return allSuggestions;
}
for(int i = 0; i < suggestions.length; i++){
allSuggestions.add(prefix + suggestions[i]);
}
return allSuggestions;
} catch (Exception e) {
org.webdsl.logging.Logger.error("EXCEPTION",e);
//if something goes wrong, close and remove current AutoCompleter instance, so it gets renewed
try {
autoCompleter.close();
} catch (IOException e2) {
org.webdsl.logging.Logger.error("EXCEPTION",e2);
}
autoCompleterMap.remove(indexPath);
}
return new ArrayList<String>();
}