Exemplo n.º 1
0
        public IList <SpellCheckCollation> GetSpellingSuggestions(string queryString, IndexReader reader, int maxSpellings, int maxCollations, bool onlyMorePopular)
        {
            var tokens = _queryConverter.convert(queryString);

            if (tokens == null || tokens.isEmpty())
            {
                return(new SpellCheckCollation[0]);
            }

            var options        = new SpellingOptions(tokens, reader, maxSpellings, onlyMorePopular, true, java.lang.Float.MIN_VALUE);
            var spellingResult = _spellChecker.GetSuggestions(options);

            if (spellingResult == null)
            {
                return(new SpellCheckCollation[0]);
            }

            var collations = SpellCheckCollator.Collate(spellingResult, queryString, maxCollations);

            collations.Sort();

            return(collations);
        }
Exemplo n.º 2
0
        public SpellingResult GetSuggestions(SpellingOptions options)
        {
            SpellingResult result          = new SpellingResult(options.tokens);
            bool           haveSuggestions = false;

            IndexReader reader = options.reader;
            Term        term   = field != null ? new Term(field, "") : null;
            //float theAccuracy = (options.accuracy == java.lang.Float.MIN_VALUE) ? spellChecker.getAccuracy() : options.accuracy;

            int count = java.lang.Math.max(options.count, DEFAULT_SUGGESTION_COUNT);

            for (var iter = options.tokens.iterator(); iter.hasNext();)
            {
                Token    token       = (Token)iter.next();
                string   tokenText   = new string(token.termBuffer(), 0, token.termLength());
                string[] suggestions = spellChecker.suggestSimilar(tokenText,
                                                                   count,
                                                                   field != null ? reader : null,
                                                                   field,
                                                                   options.onlyMorePopular /*, theAccuracy*/);
                if (suggestions.Length == 1 && suggestions[0].Equals(tokenText))
                {
                    //These are spelled the same, continue on
                    continue;
                }

                if (options.extendedResults && reader != null && field != null)
                {
                    term = term.createTerm(tokenText);
                    var termFreq = reader.docFreq(term);
                    result.add(token, termFreq);

                    // AC: add the original term to suggestions if it's frequent enough
                    // TODO: make the treshold configurable
                    if (termFreq > 100)
                    {
                        result.add(token, tokenText, termFreq);
                    }

                    int countLimit = java.lang.Math.min(options.count, suggestions.Length);
                    if (countLimit > 0)
                    {
                        for (int i = 0; i < countLimit; i++)
                        {
                            term = term.createTerm(suggestions[i]);
                            result.add(token, suggestions[i], reader.docFreq(term));
                            haveSuggestions = true;
                        }
                    }
                }
                else
                {
                    if (suggestions.Length > 0)
                    {
                        List /*<String>*/ suggList = Arrays.asList(suggestions);
                        if (suggestions.Length > options.count)
                        {
                            suggList = suggList.subList(0, options.count);
                        }
                        result.add(token, suggList);
                        haveSuggestions = true;
                    }
                }
            }
            return(haveSuggestions? result : null);
        }