internal IPredictionSuggestionCollection GetPhraseSuggestion(string[] prefix) { var suggestion = phraseSuggester.GetPhraseSuggestion(prefix); var collection = new LuceneWordSuggestionCollection(environment, null, 0, 0, suggestion); return(collection); }
public IPredictionSuggestionCollection GetSuggestions(SuggestionType type) { IPredictionSuggestionCollection suggestions; switch (type) { case SuggestionType.Character: suggestions = new CharacterProbabilisticSuggestionCollection(text, selectionStart); break; case SuggestionType.Word: suggestions = LuceneWordSuggestionCollection.Create(environment, wordSuggester, text, selectionStart, selectionLength, isAutoSpace); break; default: suggestions = EmptySuggestions.Instance; break; } return(suggestions); }
internal static IPredictionSuggestionCollection Create(IPredictionEnvironment environment, IWordSuggester wordSuggester, string text, int selectionStart, int selectionLength, bool isAutoSpace) { var prefixLength = isAutoSpace ? 0 : text.ReverseWordLength(selectionStart); var wordStart = selectionStart - prefixLength; var prefix = text.Substring(wordStart, prefixLength); var punctuationLength = text.ReversePunctuationLength(wordStart); var punctuationStart = wordStart - punctuationLength; var punctuation = text.Substring(punctuationStart, punctuationLength); var previousWords = new List <string>(); if (punctuationStart != 0 && (punctuationLength == 0 || !punctuation.IsSentenceEnding())) { do { var wordLength = text.ReverseWordLength(punctuationStart); Debug.Assert(wordLength != 0); var nextWordStart = punctuationStart - wordLength; var word = text.Substring(nextWordStart, wordLength); punctuationLength = text.ReversePunctuationLength(nextWordStart); punctuationStart = nextWordStart - punctuationLength; punctuation = text.Substring(punctuationStart, punctuationLength); previousWords.Add(word); }while (punctuationStart != 0 && !punctuation.IsSentenceEnding()); } var previousWordsArray = previousWords.ToArray(); var rawSuggestions = wordSuggester.GetSuggestions(previousWordsArray, prefix); // Count the number of lowercase and uppercase letters in the prefix we're working from. var lowerCount = 0; var upperCount = 0; for (var i = 0; i < prefix.Length && lowerCount < 1; i++) { var ch = prefix[i]; if (char.IsLower(ch)) { lowerCount++; } else { upperCount++; } } Debug.Assert(lowerCount + upperCount <= prefix.Length, "Sanity"); Debug.Assert(lowerCount == 0 || lowerCount == 1, "We shuold stop when we find the first lowercase letter"); if (upperCount == 1 || (upperCount != 0 && lowerCount != 0)) { // Just one uppercase suggests we're producing a capitalised word. rawSuggestions = rawSuggestions.Select((s) => s.Substring(0, 1).ToUpperInvariant() + s.Substring(1)); } else if (lowerCount == 0 && upperCount != 0) { Debug.Assert(2 <= upperCount, "Should have seen two or more letters to pick ALL CAPS"); rawSuggestions = rawSuggestions.Select((s) => s.ToUpperInvariant()); } var collection = new LuceneWordSuggestionCollection(environment, previousWordsArray, wordStart, selectionStart - wordStart + selectionLength, rawSuggestions); return(collection); }