Java 类org.apache.lucene.analysis.tokenattributes.CharTermAttributeImpl 实例源码

项目:webdsl    文件:SearchSuggester.java   
@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>();
}
项目:webdsl    文件:SearchSuggester.java   
@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>();
}