/// <summary> /// Gets a list of suggested words from the active word storage, based on what was typed by the user. /// </summary> /// <param name="searchQuery">The typed search query.</param> /// <returns>A list of suggested words.</returns> public List <SuggestedWord> GetSuggestedWords(string searchQuery) { if (string.IsNullOrEmpty(searchQuery) || _activeWordStorage == null || !_activeWordStorage.IsReady) { return(new List <SuggestedWord>()); } string[] words = searchQuery.Split(SpaceCharArray, StringSplitOptions.RemoveEmptyEntries); List <SuggestedWord> suggestedWords = new List <SuggestedWord>(); // query only had spaces if (words.Length == 0) { return(suggestedWords); } string lastWord = words[words.Length - 1]; if (searchQuery[searchQuery.Length - 1] != ' ') { suggestedWords.AddRange( _activeWordStorage.GetWordsWithPrefix(lastWord) .Select(word => new SuggestedWord(word, SuggestedWord.SuggestionType.Prefixed))); } if (suggestedWords.Count >= SuggestedWordsCountThreshold) { return(suggestedWords); } suggestedWords.AddRange( _activeWordStorage.GetFollowUpWords(lastWord) .Where(word => !suggestedWords.Any(suggested => suggested.Word == word)) .Select(word => new SuggestedWord(word, SuggestedWord.SuggestionType.FollowUp))); // if we still don't reach the threshold of suggestions, assume there's a/some typo(s) => use fuzzy string matching if (suggestedWords.Count >= SuggestedWordsCountThreshold) { return(suggestedWords); } int tolerance = Math.Min(2, Convert.ToInt32(lastWord.Length * 0.7f)); suggestedWords.AddRange( _activeWordStorage.GetFuzzyMatchedWords(lastWord, tolerance) .Where(word => !suggestedWords.Any(suggested => suggested.Word == word)) .Select(word => new SuggestedWord(word, SuggestedWord.SuggestionType.FuzzyMatch))); if (suggestedWords.Count >= SuggestedWordsCountThreshold) { return(suggestedWords); } suggestedWords.AddRange( _activeWordStorage.GetFuzzyMatchedWordsAlternate(lastWord) .Where(word => !suggestedWords.Any(suggested => suggested.Word == word)) .Select(word => new SuggestedWord(word, SuggestedWord.SuggestionType.FuzzyMatch))); return(suggestedWords); }
/// <summary> /// Gets a list of suggested words from the active word storage, based on what was typed by the user. /// </summary> /// <param name="searchQuery">The typed search query.</param> /// <returns>A list of suggested words.</returns> public List <string> GetSuggestedWords(string searchQuery) { if (string.IsNullOrEmpty(searchQuery) || _activeWordStorage == null || !_activeWordStorage.IsReady) { return(new List <string>()); } string[] words = searchQuery.Split(new char[] { ' ' }, System.StringSplitOptions.RemoveEmptyEntries); List <string> suggestedWords = new List <string>(); // query only had spaces if (words.Length == 0) { return(suggestedWords); } string lastWord = words[words.Length - 1]; if (searchQuery[searchQuery.Length - 1] != ' ') { suggestedWords.AddRange(_activeWordStorage.GetWordsWithPrefix(lastWord)); } suggestedWords.AddRange(_activeWordStorage.GetFollowUpWords(lastWord)); return(suggestedWords); }