Exemplo n.º 1
0
                 Dictionary <string, IReadOnlyList <IReadOnlyList <string> > > Phrases) getHighlightElements(
            string queryStr)
        {
            var tokenizer = new MtgTolerantTokenizer(queryStr);

            tokenizer.Parse();

            Dictionary <Token, IReadOnlyList <string> > analyzedTokens;

            lock (_syncQueryParser)
            {
                analyzedTokens = tokenizer.Tokens.ToDictionary(
                    _ => _,
                    _ => getAnalyzedTokens(_, queryStr));
            }

            var highlightTerms = tokenizer.Tokens
                                 .Where(_ => analyzedTokens[_]?.Count == 1)
                                 .GroupBy(getDisplayField, Str.Comparer)
                                 .ToDictionary(
                gr => gr.Key,
                gr => gr.ToReadOnlyList());

            var highlightPhrases = tokenizer.Tokens
                                   .Where(_ => analyzedTokens[_]?.Count > 1)
                                   .GroupBy(getDisplayField, Str.Comparer)
                                   .ToDictionary(
                gr => gr.Key,
                gr => gr
                .Select(_ => analyzedTokens[_])
                .ToReadOnlyList());

            return(highlightTerms, highlightPhrases);
        }
Exemplo n.º 2
0
        public IntellisenseSuggest CycleValue(TextInputState input, bool backward, string language)
        {
            string query = input.Text;
            int    caret = input.Caret;

            var token = new MtgTolerantTokenizer(query).GetEditedToken(caret);

            if (token == null || token.Type.IsAny(TokenType.ModifierValue))
            {
                return(null);
            }

            string userField = token.ParentField ?? string.Empty;

            bool isFieldInvalid = !Adapter.IsAnyField(userField) && !Adapter.IsUserField(userField);

            if (isFieldInvalid)
            {
                return(null);
            }

            string currentValue;

            if (!Adapter.IsSuggestAnalyzedIn(userField, language))
            {
                token        = token.PhraseStart ?? token;
                currentValue = StringEscaper.Unescape(token.GetPhraseText(query));
            }
            else
            {
                currentValue = StringEscaper.Unescape(token.Value);
            }

            var snapshot  = State;
            var allValues = snapshot.GetValuesCache(userField, language);

            if (allValues.Count == 0)
            {
                return(null);
            }

            var currentIndex =
                allValues.BinarySearchLastIndexOf(str => Str.Comparer.Compare(str, currentValue) <= 0);

            int increment = backward ? -1 : 1;
            var nextIndex = currentIndex + increment;

            if (nextIndex == allValues.Count)
            {
                nextIndex = 0;
            }
            else if (nextIndex == -1)
            {
                nextIndex = allValues.Count - 1;
            }

            var nextValue = allValues[nextIndex];

            return(new IntellisenseSuggest(token, ReadOnlyList.From(nextValue), _allTokensAreValues));
        }
Exemplo n.º 3
0
        public IntellisenseSuggest Suggest(string language, TextInputState input)
        {
            string query = input.Text;
            int    caret = input.Caret;

            var token = new MtgTolerantTokenizer(query).GetEditedToken(caret);

            if (token == null || token.Type.IsAny(TokenType.ModifierValue))
            {
                return(LuceneSpellcheckerConstants.EmptySuggest);
            }

            string userField = token.ParentField ?? string.Empty;

            bool isFieldInvalid = !_adapter.IsAnyField(userField) && !_adapter.IsUserField(userField);

            if (!_adapter.IsSuggestAnalyzedIn(userField, language))
            {
                token = token.PhraseStart ?? token;
            }

            string valuePart =
                StringEscaper.Unescape(query.Substring(token.Position, caret - token.Position));

            if (token.Type.IsAny(TokenType.FieldValue | TokenType.Wildcard))
            {
                IReadOnlyList <string> valueSuggest;

                if (isFieldInvalid || string.IsNullOrEmpty(userField) && string.IsNullOrEmpty(valuePart))
                {
                    valueSuggest = ReadOnlyList.Empty <string>();
                }
                else if (_adapter.IsAnyField(userField))
                {
                    valueSuggest = suggestAllFieldValues(valuePart, language);
                }
                else
                {
                    valueSuggest = suggestValues(userField, language, valuePart);
                }

                if (!string.IsNullOrEmpty(userField))
                {
                    return(new IntellisenseSuggest(token, valueSuggest, _allTokensAreValues));
                }

                var fieldSuggest = suggestFields(fieldPart: valuePart);

                var values = fieldSuggest.Concat(valueSuggest).ToReadOnlyList();

                var types = fieldSuggest.Select(_ => TokenType.Field)
                            .Concat(valueSuggest.Select(_ => TokenType.FieldValue))
                            .ToReadOnlyList();

                return(new IntellisenseSuggest(token, values, types));
            }

            if (token.Type.IsAny(TokenType.Field))
            {
                return(new IntellisenseSuggest(token, suggestFields(fieldPart: valuePart), _allTokensAreField));
            }

            if (token.Type.IsAny(TokenType.Boolean))
            {
                return(new IntellisenseSuggest(token, LuceneSpellcheckerConstants.BooleanOperators, LuceneSpellcheckerConstants.AllTokensAreBoolean));
            }

            return(LuceneSpellcheckerConstants.EmptySuggest);
        }