private HSpellCorrections BuildCandidates(HSpellCorrections ret, Dictionary <string, HSpellWord> candidates) { var suggestions = candidates.OrderByDescending(c => c.Value.RelativeMatch); int x = 0; ret.Suggestions.Clear(); foreach (var suggest in suggestions) { x++; ret.Suggestions.Add(suggest.Value.Word); // only suggest the first X words. if (x >= MaxSuggestionResponses) { break; } } return(ret); }
public HSpellCorrections CorrectFrom(string txt, int idx) { if (idx >= txt.Length) { return(null); } // Find the next incorrect word. string substr = txt.Substring(idx); int idx2 = idx; List <string> str = substr.Split(StringExtensions.WordDelimiters).ToList(); foreach (string word in str) { string tmpWord = word; if (string.IsNullOrEmpty(word)) { idx2++; continue; } // If we have possessive version of things, strip the 's off before testing // the word. THis will solve issues like "My [mother's] favorite ring." if (tmpWord.EndsWith("'s")) { tmpWord = word.Substring(0, tmpWord.Length - 2); } // Skip things like ***, #HashTagsThatMakeNoSense and 1,2345.67 if (!tmpWord.IsWord()) { idx2 += word.Length + 1; continue; } HSpellCorrections cor = Correct(tmpWord); if (cor.SpelledCorrectly) { idx2 += word.Length + 1; } else { cor.Index = idx2; return(cor); } } return(null); }
public HSpellCorrections Correct(string word) { HSpellCorrections ret = new HSpellCorrections(); ret.Word = word; if (_dictionary.ContainsKey(word.ToLower())) { string testWord = word; string dictWord = _dictionary[word.ToLower()]; if (!dictWord.CaseSensitive()) { testWord = testWord.ToLower(); dictWord = dictWord.ToLower(); } if (testWord == dictWord) { ret.SpelledCorrectly = true; return(ret); } } // At this point, we know the word is assumed to be spelled incorrectly. // Go get word candidates. ret.SpelledCorrectly = false; Dictionary <string, HSpellWord> candidates = new Dictionary <string, HSpellWord>(); List <string> edits = Edits(word); GetCandidates(candidates, edits); if (candidates.Count > 0) { return(BuildCandidates(ret, candidates)); } // If we didn't find any candidates by the main word, look for second-level candidates based on the original edits. foreach (string item in edits) { List <string> round2Edits = Edits(item); GetCandidates(candidates, round2Edits); } if (candidates.Count > 0) { return(BuildCandidates(ret, candidates)); } return(ret); }