Example usage for org.apache.solr.spelling SpellingResult addFrequency

List of usage examples for org.apache.solr.spelling SpellingResult addFrequency

Introduction

In this page you can find the example usage for org.apache.solr.spelling SpellingResult addFrequency.

Prototype

public void addFrequency(Token token, int docFreq) 

Source Link

Document

Adds an original token with its document frequency

Usage

From source file:org.dice.solrenhancements.spellchecker.DiceDirectSolrSpellChecker.java

License:Apache License

@Override
public SpellingResult getSuggestions(SpellingOptions options) throws IOException {

    LOG.debug("getSuggestions: " + options.tokens);
    // load the typos file if not loaded

    SpellingResult result = new SpellingResult();
    float accuracy = (options.accuracy == Float.MIN_VALUE) ? checker.getAccuracy() : options.accuracy;

    for (Token token : options.tokens) {
        String tokenText = token.toString();
        Term term = new Term(field, tokenText);
        int freq = options.reader.docFreq(term);
        int count = (options.alternativeTermCount != null && freq > 0) ? options.alternativeTermCount
                : options.count;/*w  ww  . ja va  2  s  .com*/
        SuggestWord[] suggestions = checker.suggestSimilar(term, count, options.reader, options.suggestMode,
                accuracy);
        result.addFrequency(token, freq);

        // Dice functionality: Allow also loading of a list of spelling corrections to apply in addition
        // to the standard functionality. This allows us to configure common typos to correct that may exceed the
        // max edit distance used by solr
        if (this.typosLoaded) {
            String normTokenText = normalize(tokenText);
            String match = this.mapTypos.get(normTokenText);
            if (match != null) {
                int matchFreq = options.reader.docFreq(new Term(field, match));
                // only ever suggest values that are in the index and more frequent
                // than the original word
                if (matchFreq > 0 && matchFreq > freq) {
                    result.add(token, match, matchFreq);
                }
            }
        }

        // If considering alternatives to "correctly-spelled" terms, then add the
        // original as a viable suggestion.
        if (options.alternativeTermCount != null && freq > 0) {
            boolean foundOriginal = false;
            SuggestWord[] suggestionsWithOrig = new SuggestWord[suggestions.length + 1];
            for (int i = 0; i < suggestions.length; i++) {
                if (suggestions[i].string.equals(tokenText)) {
                    foundOriginal = true;
                    break;
                }
                suggestionsWithOrig[i + 1] = suggestions[i];
            }
            if (!foundOriginal) {
                SuggestWord orig = new SuggestWord();
                orig.freq = freq;
                orig.string = tokenText;
                suggestionsWithOrig[0] = orig;
                suggestions = suggestionsWithOrig;
            }
        }
        if (suggestions.length == 0 && freq == 0) {
            List<String> empty = Collections.emptyList();
            result.add(token, empty);
        } else {
            for (SuggestWord suggestion : suggestions) {
                result.add(token, suggestion.string, suggestion.freq);
            }
        }
    }
    return result;
}