org.apache.lucene.analysis.tokenattributes.CharTermAttributeImpl Java Examples

The following examples show how to use org.apache.lucene.analysis.tokenattributes.CharTermAttributeImpl. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SearchSuggester.java    From webdsl with Apache License 2.0 4 votes vote down vote up
@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>();
}
 
Example #2
Source File: SearchSuggester.java    From webdsl with Apache License 2.0 4 votes vote down vote up
@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>();
}