Examples of SuggestWord


Examples of org.apache.lucene.search.spell.SuggestWord

            // find best suggestions
            for (int i = 0; i < suggestion.getNumFound(); i++) {
              String alternative = suggestion.getAlternatives().get(i);
              suggested.add(alternative);
              SuggestWord sug = suggestedVsWord.get(alternative);
              if (sug == null)  {
                sug = new SuggestWord();
                suggestedVsWord.put(alternative, sug);
              }
              sug.string = alternative;
              // alternative frequency is present only for extendedResults=true
              if (suggestion.getAlternativeFrequencies() != null && suggestion.getAlternativeFrequencies().size() > 0) {
                Integer freq = suggestion.getAlternativeFrequencies().get(i);
                if (freq != null) sug.freq += freq;
              }
            }
          }
          NamedList suggestions = (NamedList) nl.get("suggestions");
          if(suggestions != null) {
            List<Object> collationList = suggestions.getAll("collation");
            List<Object> collationRankList = suggestions.getAll("collationInternalRank");
            int i=0;
            if(collationList != null) {
              for(Object o : collationList)
              {
                if(o instanceof String)
                {
                  SpellCheckCollation coll = new SpellCheckCollation();
                  coll.setCollationQuery((String) o);
                  if(collationRankList!= null && collationRankList.size()>0)
                  {
                    coll.setInternalRank((Integer) collationRankList.get(i));
                    i++;
                  }
                  SpellCheckCollation priorColl = collations.get(coll.getCollationQuery());
                  if(priorColl != null)
                  {
                    coll.setInternalRank(Math.max(coll.getInternalRank(),priorColl.getInternalRank()));
                  }
                  collations.put(coll.getCollationQuery(), coll);
                } else
                {
                  NamedList expandedCollation = (NamedList) o;                 
                  SpellCheckCollation coll = new SpellCheckCollation();
                  coll.setCollationQuery((String) expandedCollation.get("collationQuery"));
                  coll.setHits((Integer) expandedCollation.get("hits"));
                  if(maxCollationTries>0)
                  {
                    coll.setInternalRank((Integer) expandedCollation.get("collationInternalRank"));
                  }
                  coll.setMisspellingsAndCorrections((NamedList) expandedCollation.get("misspellingsAndCorrections"));
                  SpellCheckCollation priorColl = collations.get(coll.getCollationQuery());
                  if(priorColl != null)
                  {
                    coll.setHits(coll.getHits() + priorColl.getHits());
                    coll.setInternalRank(Math.max(coll.getInternalRank(),priorColl.getInternalRank()));
                  }
                  collations.put(coll.getCollationQuery(), coll);
                }
              }
            }
          }
        }
      }
    }

    // all shard responses have been collected
    // create token and get top suggestions
    SpellingResult result = new SpellingResult(tokens); //todo: investigate, why does it need tokens beforehand?
    for (Map.Entry<String, HashSet<String>> entry : origVsSuggested.entrySet()) {
      String original = entry.getKey();
     
      //Only use this suggestion if all shards reported it as misspelled.
      Integer numShards = origVsShards.get(original);
      if(numShards<totalNumberShardResponses) {
        continue;
      }
     
      HashSet<String> suggested = entry.getValue();
      SuggestWordQueue sugQueue = new SuggestWordQueue(numSug);
      for (String suggestion : suggested) {
        SuggestWord sug = 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
View Full Code Here

Examples of org.apache.lucene.search.spell.SuggestWord

            // find best suggestions
            for (int i = 0; i < suggestion.getNumFound(); i++) {
              String alternative = suggestion.getAlternatives().get(i);
              suggested.add(alternative);
              SuggestWord sug = suggestedVsWord.get(alternative);
              if (sug == null)  {
                sug = new SuggestWord();
                suggestedVsWord.put(alternative, sug);
              }
              sug.string = alternative;
              // alternative frequency is present only for extendedResults=true
              if (suggestion.getAlternativeFrequencies() != null && suggestion.getAlternativeFrequencies().size() > 0) {
                Integer freq = suggestion.getAlternativeFrequencies().get(i);
                if (freq != null) sug.freq += freq;
              }
            }
          }
          NamedList suggestions = (NamedList) nl.get("suggestions");
          if(suggestions != null) {
            List<Object> collationList = suggestions.getAll("collation");
            List<Object> collationRankList = suggestions.getAll("collationInternalRank");
            int i=0;
            if(collationList != null) {
              for(Object o : collationList)
              {
                if(o instanceof String)
                {
                  SpellCheckCollation coll = new SpellCheckCollation();
                  coll.setCollationQuery((String) o);
                  if(collationRankList!= null && collationRankList.size()>0)
                  {
                    coll.setInternalRank((Integer) collationRankList.get(i));
                    i++;
                  }
                  SpellCheckCollation priorColl = collations.get(coll.getCollationQuery());
                  if(priorColl != null)
                  {
                    coll.setInternalRank(Math.max(coll.getInternalRank(),priorColl.getInternalRank()));
                  }
                  collations.put(coll.getCollationQuery(), coll);
                } else
                {
                  NamedList expandedCollation = (NamedList) o;                 
                  SpellCheckCollation coll = new SpellCheckCollation();
                  coll.setCollationQuery((String) expandedCollation.get("collationQuery"));
                  coll.setHits((Integer) expandedCollation.get("hits"));
                  if(maxCollationTries>0)
                  {
                    coll.setInternalRank((Integer) expandedCollation.get("collationInternalRank"));
                  }
                  coll.setMisspellingsAndCorrections((NamedList) expandedCollation.get("misspellingsAndCorrections"));
                  SpellCheckCollation priorColl = collations.get(coll.getCollationQuery());
                  if(priorColl != null)
                  {
                    coll.setHits(coll.getHits() + priorColl.getHits());
                    coll.setInternalRank(Math.max(coll.getInternalRank(),priorColl.getInternalRank()));
                  }
                  collations.put(coll.getCollationQuery(), coll);
                }
              }
            }
          }
        }
      }
    }

    // all shard responses have been collected
    // create token and get top suggestions
    SpellingResult result = new SpellingResult(tokens); //todo: investigate, why does it need tokens beforehand?
    for (Map.Entry<String, HashSet<String>> entry : origVsSuggested.entrySet()) {
      String original = entry.getKey();
     
      //Only use this suggestion if all shards reported it as misspelled.
      Integer numShards = origVsShards.get(original);
      if(numShards<totalNumberShardResponses) {
        continue;
      }
     
      HashSet<String> suggested = entry.getValue();
      SuggestWordQueue sugQueue = new SuggestWordQueue(numSug);
      for (String suggestion : suggested) {
        SuggestWord sug = 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
View Full Code Here

Examples of org.apache.lucene.search.spell.SuggestWord

        // System.out.println("HITS: " + hits.length());
        SuggestWordQueue sugQueue = new SuggestWordQueue(numSug);

        // go thru more than 'maxr' matches in case the distance filter triggers
        int stop = Math.min(hits.length(), 10 * numSug);
        SuggestWord sugWord = new SuggestWord();
        for (int i = 0; i < stop; i++) {

            sugWord.string = hits.doc(i).get(F_WORD + (language != null ? "-" + language : "")); // get
            // orig
            // word

            // don't suggest a word for itself, that would be silly
            if (sugWord.string == null || word.equals(sugWord.string)) {
                continue;
            }

            // edit distance
            sugWord.score = getStringDistance().getDistance(word, sugWord.string);
            if (sugWord.score < min) {
                continue;
            }

            if (ir != null && field != null) { // use the user index
                sugWord.freq = ir.docFreq(new Term(field, sugWord.string)); // freq
                // in
                // the
                // index
                // don't suggest a word that is not present in the field
                if ((morePopular && goalFreq > sugWord.freq) || sugWord.freq < 1) {
                    continue;
                }
            }
            sugQueue.insert(sugWord);
            if (sugQueue.size() == numSug) {
                // if queue full, maintain the minScore score
                min = ((SuggestWord) sugQueue.top()).score;
            }
            sugWord = new SuggestWord();
        }

        // convert to array string
        String[] list = new String[sugQueue.size()];
        for (int i = sugQueue.size() - 1; i >= 0; i--) {
View Full Code Here

Examples of org.apache.lucene.search.spell.SuggestWord

        final long frequency = original.frequency;
        spellchecker.setThresholdFrequency(this.suggestMode == SuggestMode.SUGGEST_ALWAYS ? 0 : thresholdFrequency(frequency, dictSize));
        SuggestWord[] suggestSimilar = spellchecker.suggestSimilar(new Term(field, term), numCandidates, reader, this.suggestMode);
        List<Candidate> candidates = new ArrayList<>(suggestSimilar.length);
        for (int i = 0; i < suggestSimilar.length; i++) {
            SuggestWord suggestWord = suggestSimilar[i];
            BytesRef candidate = new BytesRef(suggestWord.string);
            postFilter(new Candidate(candidate, internalFrequency(candidate), suggestWord.score, score(suggestWord.freq, suggestWord.score, dictSize), false), spare, byteSpare, candidates);
        }
        set.addCandidates(candidates);
        return set;
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.