Example usage for org.apache.lucene.search.spell SuggestWordQueue SuggestWordQueue

List of usage examples for org.apache.lucene.search.spell SuggestWordQueue SuggestWordQueue

Introduction

In this page you can find the example usage for org.apache.lucene.search.spell SuggestWordQueue SuggestWordQueue.

Prototype

public SuggestWordQueue(int size) 

Source Link

Document

Use the #DEFAULT_COMPARATOR

Usage

From source file:org.apache.solr.spelling.SolrSpellChecker.java

License:Apache License

/**
 * Integrate spelling suggestions from the various shards in a distributed environment.
 *//*  w  w  w.j a v  a2  s. c  o m*/
public SpellingResult mergeSuggestions(SpellCheckMergeData mergeData, int numSug, int count,
        boolean extendedResults) {
    float min = 0.5f;
    try {
        min = getAccuracy();
    } catch (UnsupportedOperationException uoe) {
        //just use .5 as a default
    }

    StringDistance sd = null;
    try {
        sd = getStringDistance() == null ? new LevensteinDistance() : getStringDistance();
    } catch (UnsupportedOperationException uoe) {
        sd = new LevensteinDistance();
    }

    SpellingResult result = new SpellingResult();
    for (Map.Entry<String, HashSet<String>> entry : mergeData.origVsSuggested.entrySet()) {
        String original = entry.getKey();

        //Only use this suggestion if all shards reported it as misspelled.
        Integer numShards = mergeData.origVsShards.get(original);
        if (numShards < mergeData.totalNumberShardResponses) {
            continue;
        }

        HashSet<String> suggested = entry.getValue();
        SuggestWordQueue sugQueue = new SuggestWordQueue(numSug);
        for (String suggestion : suggested) {
            SuggestWord sug = mergeData.suggestedVsWord.get(suggestion);
            sug.score = sd.getDistance(original, sug.string);
            if (sug.score < min)
                continue;
            sugQueue.insertWithOverflow(sug);
            if (sugQueue.size() == numSug) {
                // if queue full, maintain the minScore score
                min = sugQueue.top().score;
            }
        }

        // create token
        SpellCheckResponse.Suggestion suggestion = mergeData.origVsSuggestion.get(original);
        Token token = new Token(original, suggestion.getStartOffset(), suggestion.getEndOffset());

        // get top 'count' suggestions out of 'sugQueue.size()' candidates
        SuggestWord[] suggestions = new SuggestWord[Math.min(count, sugQueue.size())];
        // skip the first sugQueue.size() - count elements
        for (int k = 0; k < sugQueue.size() - count; k++)
            sugQueue.pop();
        // now collect the top 'count' responses
        for (int k = Math.min(count, sugQueue.size()) - 1; k >= 0; k--) {
            suggestions[k] = sugQueue.pop();
        }

        if (extendedResults) {
            Integer o = mergeData.origVsFreq.get(original);
            if (o != null)
                result.addFrequency(token, o);
            for (SuggestWord word : suggestions)
                result.add(token, word.string, word.freq);
        } else {
            List<String> words = new ArrayList<String>(sugQueue.size());
            for (SuggestWord word : suggestions)
                words.add(word.string);
            result.add(token, words);
        }
    }
    return result;
}