/// <summary> /// Reconstruct the corrected text from the spell check response and the original text /// </summary> /// <param name="response">spell check response</param> /// <param name="text">original text</param> /// <returns>the corrected text</returns> public static string ConstructCorrectedTextFromResponse(SpellCheckResult response, string text) { if (response.SpellingErrors != null && response.SpellingErrors.Length > 0) { Array.Sort(response.SpellingErrors, (token1, token2) => ((int)token1.Offset - (int)token2.Offset)); StringBuilder ret = new StringBuilder(); int prevStart = 0; int offset = -1; for (var i = 0; i < response.SpellingErrors.Length; i++) { SpellingError token = response.SpellingErrors[i]; if (offset == (int)token.Offset) { // duplicate flags. This shouldn't happen, but also doesn't hurt, so silently ignore it continue; } else { offset = (int)token.Offset; } if (offset > prevStart) { ret.Append(text.Substring(prevStart, offset - prevStart)); } string expectedToken = text.Substring(offset, token.Token.Length); if (String.Compare(expectedToken, token.Token, StringComparison.Ordinal) != 0) { throw new Exception("Token mismatch. Text:" + text + "\tToken does not match, expected: " + expectedToken + " but was: " + token.Token); } if (token.Type == "RepeatedToken") { // If there is a space before the repeated word, remove it. if (ret.Length > 0 && ret[ret.Length - 1] == ' ') { ret.Remove(ret.Length - 1, 1); } } else { string topSuggestion = null; if (token.Suggestions.Length == 0) { // No suggestion. Use original text topSuggestion = token.Token; } else { topSuggestion = token.Suggestions[0].Token; } ret.Append(topSuggestion); } prevStart = offset + token.Token.Length; } if (prevStart < text.Length) { ret.Append(text.Substring(prevStart)); } return(ret.ToString()); } else { // No flagged token, return the original query. return(text); } }
/// <summary> /// Reconstruct the corrected text from the spell check response and the original text /// </summary> /// <param name="response">spell check response</param> /// <param name="text">original text</param> /// <returns>the corrected text</returns> public static string ConstructCorrectedTextFromResponse(SpellCheckResult response, string text) { if (response.SpellingErrors != null && response.SpellingErrors.Length > 0) { Array.Sort(response.SpellingErrors, (token1, token2) => ((int)token1.Offset - (int)token2.Offset)); StringBuilder ret = new StringBuilder(); int prevStart = 0; int offset = -1; for (var i = 0; i < response.SpellingErrors.Length; i++) { SpellingError token = response.SpellingErrors[i]; if (offset == (int)token.Offset) { // duplicate flags. This shouldn't happen, but also doesn't hurt, so silently ignore it continue; } else { offset = (int)token.Offset; } if (offset > prevStart) { ret.Append(text.Substring(prevStart, offset - prevStart)); } string expectedToken = text.Substring(offset, token.Token.Length); if (String.Compare(expectedToken, token.Token, StringComparison.Ordinal) != 0) { throw new Exception("Token mismatch. Text:" + text + "\tToken does not match, expected: " + expectedToken + " but was: " + token.Token); } if (token.Type == "RepeatedToken") { // If there is a space before the repeated word, remove it. if (ret.Length > 0 && ret[ret.Length - 1] == ' ') { ret.Remove(ret.Length - 1, 1); } } else { string topSuggestion = null; if (token.Suggestions.Length == 0) { // No suggestion. Use original text topSuggestion = token.Token; } else { topSuggestion = token.Suggestions[0].Token; } ret.Append(topSuggestion); } prevStart = offset + token.Token.Length; } if (prevStart < text.Length) { ret.Append(text.Substring(prevStart)); } return ret.ToString(); } else { // No flagged token, return the original query. return text; } }