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 => (IReadOnlyList <Token>)gr.ToList()); var highlightPhrases = tokenizer.Tokens .Where(_ => analyzedTokens[_]?.Count > 1) .GroupBy(getDisplayField, Str.Comparer) .ToDictionary( gr => gr.Key, gr => (IReadOnlyList <IReadOnlyList <string> >)gr.Select(_ => analyzedTokens[_]).ToList()); return(highlightTerms, highlightPhrases); }
public IntellisenseSuggest Suggest(TextInputState input, 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(LuceneSpellcheckerConstants.EmptySuggest); } string userField = Adapter.GetActualField(token.ParentField); 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 = Empty <string> .Array; } else { if (Adapter.IsAnyField(userField)) { valueSuggest = State.SuggestAllFieldValues(valuePart, language); } else { valueSuggest = State.SuggestValues(userField, language, valuePart); } } if (!string.IsNullOrEmpty(userField)) { return(new IntellisenseSuggest(token, valueSuggest, _allTokensAreValues)); } var fieldSuggest = suggestFields(fieldPart: valuePart); var values = fieldSuggest.Concat(valueSuggest).ToList(); var types = fieldSuggest.Select(_ => TokenType.Field) .Concat(valueSuggest.Select(_ => TokenType.FieldValue)) .ToList(); 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); }
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 = Adapter.GetActualField(token.ParentField); 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); } int currentIndex; if (Adapter.IsFloatField(userField)) { float currentParsed = float.Parse(currentValue, NumberStyles.Float, Str.Culture); currentIndex = allValues.BinarySearchLastIndex(str => float.Parse(str, NumberStyles.Float, Str.Culture) - currentParsed <= 0); } else if (Adapter.IsIntField(userField)) { float currentParsed = int.Parse(currentValue, NumberStyles.Integer, Str.Culture); currentIndex = allValues.BinarySearchLastIndex(str => int.Parse(str, NumberStyles.Integer, Str.Culture) - currentParsed <= 0); } else { currentIndex = allValues.BinarySearchLastIndex(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, new[] { nextValue }, _allTokensAreValues)); }